layui.extend({
    common: 'js/common',
    region: 'layui/lay/mymodules/region',
    xmSelect: 'layui/dist/xm-select',
}).use(['table', 'jquery', 'layer', 'common', 'region', 'form', 'xmSelect', 'laydate'], function () {
    //  弹窗插件
    var layer = layui.layer;
    //  Jquery插件
    var $ = layui.jquery;
    //  引入公共模块
    var common = layui.common;
    //  引入表格
    var table = layui.table;
    //  引入表单
    var form = layui.form;
    //  引入下拉列表组件
    var xmSelect = layui.xmSelect;

    var laydate = layui.laydate;

    var layuiTable = null;

    // 查询框表单
    var $node = {
        //订单号
        bizOrderNoInput: $('input[name = "bizOrderNo"]'),
        //订单类型
        orderType: $('#order_type'),
        //是否收费
        isCharge: $('#is_charge'),
        //付款状态
        payStatus: $('#pay_status'),
        //付款方式
        payType: $('#pay_type'),
        //小区搜索
        residentialId: $('#residential_id'),
        //单元搜索
        unitId: $('#unit_id'),
        //房间搜索
        roomId: $('#room_id'),
        //住户姓名
        nameInput: $('input[name="name"]'),
        //联系方式
        telInput: $('input[name="tel"]'),
        //创建时间
        createDate: $('#create_date'),
        beginDate: $('#begin_date'),
        endDate: $('#end_date')
    };

    //  页面数据
    var pageData = {};

    //tableId集合
    var table_data = [];

    //  重新加载表格
    var reloadTable = function (obj) {
        //重新加载表格
        layuiTable.reload({
            where: {
                bizOrderNo: $node.bizOrderNoInput.val(),
                orderType: $('#order_type option:selected').val(),
                isCharge: $('#is_charge option:selected').val(),
                payStatus: $('#pay_status option:selected').val(),
                payType: $('#pay_type option:selected').val(),
                residentialId: $node.residentialId.val(),
                unitId: $node.unitId.val(),
                roomId: $node.roomId.val(),
                name: $node.nameInput.val(),
                tel: $node.telInput.val(),
                startDateCondition: $node.beginDate.val(),
                endDateCondition: $node.endDate.val(),
                //排序
                sortField   :   obj?obj.field:'',
                sortOrder   :   obj?obj.type:'',
            }
            //重新从第1页开始
            , page: {
                curr: 1
            },
            initSort:obj
        });
    };

    //  初始化
    (function () {
        //  初始化表格
        layuiTable = common.layuiTable.render(layui.table, {
            elem: '#table_id'
            , url: '/estate/userOrder/showUserOrderPage'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，
            , cols: [[
                {field: 'bizOrderNo', title: '订单号', align: 'center'}
                , {field: 'tel', title: '手机号', align: 'center'}
                , {field: 'residentialName', title: '小区名称', align: 'center'}
                , {field: 'unitName', title: '单元名称', align: 'center'}
                , {field: 'roomName', title: '房间名称', align: 'center'}
                , {field: 'name', title: '姓名', align: 'center'}
                , {field: 'refund', title: '退款状态', align: 'center',sort: true,templet:'#refundStatus'}
                , {field: 'refundFee', title: '退款金额', align: 'center',sort: true}
                , {field: 'refundDate', title: '退款时间', align: 'center',sort: true}
                , {
                    field: 'orderType', title: '订单类型', align: 'center',sort:true, templet: function (d) {
                        if (d.orderType == "FACE_ENABLE") {
                            return "人脸激活"
                        } else {
                            return "未知"
                        }
                    }
                }
                , {field: 'isCharge', title: '是否收费', align: 'center',sort:true, templet: '#isCharge'}
                , {field: 'shouldPayAmount', title: '应付款', align: 'center'}
                , {field: 'actualPayAmount', title: '实际付款', align: 'center'}
                , {field: 'payStatus', title: '付款状态', align: 'center',sort:true, templet: '#payStatus'}
                , {
                    field: 'payType', title: '付款方式', align: 'center',sort:true, templet: function (d) {
                        if (d.payType == "wxPay") {
                            return "微信支付"
                        } else if (d.payType == "aliPay") {
                            return "支付宝支付"
                        } else {
                            return "未知"
                        }
                    }
                }
                , {field: 'payTime', title: '支付时间', align: 'center',sort:true}
                , {field: 'createDate', title: '创建时间', align: 'center',sort:true}
                , {field: 'imageUri', title: '支付人脸', align: 'center',sort:true, templet: '#imageUri'}
                // ,{ title: '操作',align: 'center', toolbar: '#table-useradmin-webuser',width:180 }
            ]]
            //  开启分页功能
            , page: true
            , done: function (res, curr, count) {
                layer.photos({
                    photos: '#layPhotosTbImg',
                    shade: .1,
                    closeBtn: true
                });
            }
        });
    })();

    //排序
    layui.table.on('sort(table_event)',function (obj) {
        reloadTable(obj);
    });

    //日期时间范围
    laydate.render({
        elem: '#create_date'
        , type: 'date'
        , range: true
        , min: '1990-1-1 00:00:00'
        , max: 0 //最大日期为当天
        , done: function (value, date, endDate) {
            var dateArray = value.split(" - ");
            $("#begin_date").val(dateArray[0] + ' 00:00:00');
            $("#end_date").val(dateArray[1] + ' 23:59:59');
        }
    });

    //监听行工具事件 lay-filter="table_event"
    table.on('tool(table_event)', function (obj) {
    });

    //监听搜索
    form.on('submit(query)', function () {
        reloadTable();
    });

    //监听重置查询
    form.on('submit(reset)', function () {
        residentialSelect.setValue([]);
        unitestateRepair({data:[],disabled:true});
        roomSelect.update({data:[],disabled:true});
        $node.residentialId.val('');
        $node.unitId.val('');
        $node.roomId.val('');
        $node.bizOrderNoInput.val('');
        $('#order_type').val('');
        $('#is_charge').val('');
        $('#pay_status').val('');
        $('#pay_type').val('');
        $node.nameInput.val('');
        $node.telInput.val('');
        $node.beginDate.val('');
        $node.endDate.val('');
        $node.createDate.val('');
        form.render('select');
        reloadTable();
    });

    //小区查询下拉列表
    var residentialSelect = xmSelect.render({
        el: '#residential_name',
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        toolbar: {
            show: true,
            list: [{
                name: '清空',
                method() {
                    residentialSelect.setValue([]);
                    $node.residentialId.val('');
                    unitSelect.update({disabled: true}).setValue([]);
                    $node.unitId.val('');
                    roomSelect.update({disabled: true}).setValue([]);
                    $node.roomId.val('');
                }
            }]
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/estate/residential/getListByLikeName',
                data: {
                    name: val,
                    // selectTreeId: selectTreeId,
                    regionProvince: $('input[name="regionProvince"]').val(),
                    regionCity: $('input[name="regionCity"]').val(),
                    regionArea: $('input[name="regionArea"]').val(),
                    isSelect: true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                $node.residentialId.val(change[0].id);
                unitSelect.update({data: initUnitFn({residentialId: change[0].id}), disabled: false});
                roomSelect.update({data: []});
            }
            if (!data.isAdd) {
                $node.residentialId.val('');
                unitSelect.update({disabled: true});
                roomSelect.update({disabled: true});
            }
            $node.unitId.val('');
            $node.roomId.val('');
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    //单元查询下拉列表
    var unitSelect = xmSelect.render({
        el: '#unit_name',
        disabled: true,
        tips: '请先选择小区',
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        toolbar: {
            show: true,
            list: [{
                name: '清空',
                method() {
                    unitSelect.setValue([]);
                    $node.unitId.val('');
                    roomSelect.update({disabled: true}).setValue([]);
                    $node.roomId.val('');
                }
            }]
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/estate/unit/getListByLikeName',
                data: {
                    name: val,
                    residentialId: $node.residentialId.val(),
                    isSelect: true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                $node.unitId.val(change[0].id);
                roomSelect.update({
                    data: initRoomFn({residentialId: $node.residentialId.val(), unitId: change[0].id}),
                    disabled: false
                });
            }
            if (!data.isAdd) {
                $node.unitId.val('');
                roomSelect.update({data: [], disabled: true});
            }
            $node.roomId.val('');
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    //房间查询下拉列表
    var roomSelect = xmSelect.render({
        el: '#room_name',
        disabled: true,
        tips: '请先选择小区和单元',
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        toolbar: {
            show: true,
            list: [{
                name: '清空',
                method() {
                    roomSelect.setValue([]);
                    $node.roomId.val('');
                }
            }]
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/estate/room/getListByLikeName',
                data: {
                    name: val,
                    residentialId: $node.residentialId.val(),
                    unitId: $node.unitId.val(),
                    isSelect: true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                $node.roomId.val(change[0].id)
            }
            if (!data.isAdd) {
                $node.roomId.val('');
            }
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    function initResidentialFn() {
        var residentialList;
        common.sendSyncRequest("/estate/residential/getListByLikeName", 'get', {
            // selectTreeId: selectTreeId,
            regionProvince: $('input[name="regionProvince"]').val(),
            regionCity: $('input[name="regionCity"]').val(),
            regionArea: $('input[name="regionArea"]').val(),
            isSelect: true
        }, function (result) {
            residentialList = result.list;
        });
        return residentialList;
    }

    function initUnitFn(data) {
        var unitList;
        common.sendSyncRequest("/estate/unit/getListByLikeName", 'get', {
            residentialId: data.residentialId,
            id: data.id,
            isSelect: true
        }, function (result) {
            unitList = result.list;
        });
        return unitList;
    }

    function initRoomFn(data) {
        var roomList;
        common.sendSyncRequest("/estate/room/getListByLikeName", 'get', {
            residentialId: data.residentialId,
            unitId: data.unitId,
            id: data.id,
            isSelect: true
        }, function (result) {
            roomList = result.list;
        });
        return roomList;
    }
});
