layui.extend({
    common    :   'js/common',
}).use(['jquery','layer','common','form','laydate','table','layer'],function () {
    var $ = layui.jquery;
    var common = layui.common;
    /* -----------弹出层*/
    let layer  = layui.layer;

    /* ----------日期选择器*/
    let laydate = layui.laydate;
    laydate.render({
        elem: '#dateSelector' ,
        type: 'month'
    });

   /* -----------表格数据*/
    let table = layui.table;
    common.layuiTable.render(table,{
        elem: '#billsTable'
        ,url: '/bills/manage/page'
        ,id:'billsTable'
        // ,toolbar: '#topBar'
        ,method:'get'
        ,cols: [[
            // {field: 'id', title: 'ID', width:80, sort: true},
            {field: 'billsDate', title: '账单日期'}
            // ,{field: 'agencyName', title: '社区名称'}
            ,{field: 'residentialName', title: '小区名称'}
            ,{field: 'buildingName', title: '楼栋名称'}
            ,{field: 'unitName', title: '单元名称'}
            // ,{field: 'floorName', title: '楼层名称'}
            ,{field: 'roomName', title: '房间名称'}
            ,{field: 'totalFee',title: '缴费金额'}
            ,{field: 'status', title: '支付状态',templet: (row)=>{
                return row.status==1? "未支付":"已支付";
                }}
            ,{ title: '操作', toolbar:  '#rightBar',width: 150 ,fixed: 'right'}
        ]]
        ,page: true
        ,response: {
            statusCode: 200
        }
        ,parseData: (res)=>{
            console.log(res,222);
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.data.total,
                "data": res.data.records
            }
        }
    });
    table.on('tool(billsTable)', (obj)=> {
        var data = obj.data; //获得当前行数据
        switch (obj.event) {
            case "detail":
                layer.open({
                type: 2,
                area: ['1000px', '600px'],
                maxmin: true,
                title: "账单详情",
                content: "/bills/manage/detailPage?id="+obj.data.id
            });
            break;
            case "delete":
                // console.log(data,'删除');
                // layer.confirm('是否确认删除？',{icon: 7, title:'提示'}, function(index){
                //     common.sendSyncRequest("/bills/manage/delete",'POST',
                //          data.id,
                //         function (result){
                //         if(result.data){
                //             //  刷新表格
                //             queryTableFn();
                //             layer.msg("删除成功");
                //             layer.close(index);
                //         }else{
                //             layer.msg("删除失败");
                //         }
                //     });
                // });
                layer.confirm('是否确定删除？',{btn:['确定','取消'],title:'提示'},function (index) {
                    $.ajax({
                        url:"/bills/manage/delete",
                        type:'POST',
                        async:false,
                        contentType:'application/json;charset=UTF-8',
                        data:JSON.stringify(data),
                        success:function (result,index) {
                            if(result.data){
                                //刷新表格
                                queryTableFn();
                                layer.msg('删除成功！',{icon: 1});
                                // layer.close(index);
                            }else {
                                layer.msg('删除失败！',{icon: 2});
                            }
                        },
                    });
                });
            break;
            case "edit":
                layer.open({
                type: 2,
                area: ['1000px', '600px'],
                maxmin: true,
                title: "编辑账单",
                content: "/bills/manage/editPage?id="+obj.data.id
            });
            break;
            default:break;
        }
    });

    //回调函数
    function callbackFn(result) {
        if(result.data){
            layer.msg("删除成功",{
                time: 500
                ,shade : [0.1 , '#000' , true]
            },function () {
                //获取当前弹窗出层的名字
                var index = parent.layer.getFrameIndex(window.name);
                //关闭弹窗层
                parent.layer.close(index);
                // 父页面刷新
                parent.location.reload();
            });
        }else {//添加失败
            layer.msg("删除失败");
        }
    };


    var queryBeginDate,queryEndDate;
    var queryTableFn=function(){
        //执行重载
        table.reload('billsTable', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    }
    // table.on('toolbar(billsTable)', (obj)=>{
    //     switch(obj.event){
    //         case 'add':
    //             layer.open({
    //                 title: "添加账单",
    //                 content: "/bills/manage/addPage"
    //             });
    //             break;
    //         case 'delete':
    //             break;
    //     };
    // });

    /* ----------查询表单*/
    let form = layui.form;
    form.on('submit(query)', (data)=>{
        table.reload('billsTable', {
            where: {
                status: data.field.status,
                billsDate: data.field.billsDate,
                agencyName:  data.field.agencyName,
                residentialName:data.field.residentialName,
                buildingName:  data.field.buildingName,
                floorName:  data.field.floorName,
                unitName:  data.field.unitName,
                roomName:  data.field.roomName,
            }
        });
        console.log(data);
        return false;
    });

    //监听添加
    form.on('submit(add)', function(){
        // common.openDefaultIframe({
        //     title   :   '添加账单',
        //     content :   '/bills/manage/addPage' //  打开内容
        // });
        layer.open({
            type: 2,
            area: ['1000px', '600px'],
            maxmin: true,
            title: "添加账单",
            content: '/bills/manage/addPage'
        });
    });
});