layui.extend({
    common: 'js/common',
    xmSelect: 'layui/dist/xm-select',
}).use(['jquery', 'table', 'layer', 'form', 'common', 'upload', 'xmSelect'], function () {
    var $ = layui.jquery;
    //  引入表单插件
    var form = layui.form;
    var common = layui.common;
    //  弹窗插件
    var layer = layui.layer;
    //  引入表格插件
    var table = layui.table;
    //  图片上传
    var upload = layui.upload;
    //定义加载层id
    var loadId;
    //图片服务器地址
    var fastDfsServer = $('#fastDfsServer').val();


    //监听提交
    form.on('submit(addConfirm)', function (data) {
        common.timer($('button[lay-filter="addConfirm"]'));
        var adsenseName =$("#adsenseNamePre").val()+$("#adsenseNameSuf").val();
        // console.log("广告位名称："+adsenseName)
        data.field.adsenseName = $("#adsenseNamePre").val()+$("#adsenseNameSuf").val();
        common.sendAsyncRequest("/ad/adsense/addAdAdsense", 'POST', data.field, callbackFn);
    });

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

    //取消按钮
    $('button[type="reset"]').on('click', function () {
        if (parent) {
            //  关闭当前页面弹窗
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        } else {
            //  关闭页面
            window.close();
        }
    });

    // layui.form.verify({
    //     specialCharacters: function (value) {
    //         if (!/^[a-zA-Z0-9_\u4e00-\u9fa5\s·]+$/.test(value)) {
    //             return '名称不能有特殊字符';
    //         }
    //         if (/(^\_)|(\__)|(\_+$)/.test(value)) {
    //             return '名称首尾不能出现下划线\'_\'';
    //         }
    //     },
    //     length: function (value) {
    //         if (value.length < 2 || value.length > 20) {
    //             return '名称只能输入2-20个字符!'
    //         }
    //     },
    // });

    //省查询下拉列表
    var regionProvinceSelect = xmSelect.render({
        el: '#regionProvince',
        name: 'regionProvince',
        layVerify: 'required',
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/common/area/getSubList/100000',
                data: {
                    name: val,
                    isSelect: true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                regionCitySelect.update({data: initCityFn({parentCode: change[0].code}), disabled: false});
                regionAreaSelect.update({data: []});
                residentialSelect.update({data: []});
                deviceSelect.update({data: []});
            }
            if (!data.isAdd) {
                regionCitySelect.update({disabled: true});
                regionAreaSelect.update({disabled: true});
                residentialSelect.update({disabled: true});
                deviceSelect.update({disabled: true});
            }
            regionCitySelect.setValue([]);
            regionAreaSelect.setValue([]);
            residentialSelect.setValue([]);
            deviceSelect.setValue([]);
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    //市查询下拉列表
    var regionCitySelect = xmSelect.render({
        el: '#regionCity',
        name: 'regionCity',
        layVerify: 'required',
        disabled: true,
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/common/area/getSubList/' + regionProvinceSelect.getValue('valueStr') != null ? -1 : regionProvinceSelect.getValue('valueStr'),
                data: {
                    name: val,
                    isSelect: true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                regionAreaSelect.update({data: initAreaFn({parentCode: change[0].code}), disabled: false});
                residentialSelect.update({data: []});
                deviceSelect.update({data: []});
            }
            if (!data.isAdd) {
                regionAreaSelect.update({disabled: true});
                residentialSelect.update({disabled: true});
                deviceSelect.update({disabled: true});
            }
            regionAreaSelect.setValue([]);
            residentialSelect.setValue([]);
            deviceSelect.setValue([]);
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    //区查询下拉列表
    var regionAreaSelect = xmSelect.render({
        el: '#regionArea',
        name: 'residentialArea',
        layVerify: 'required',
        disabled: true,
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/common/area/getSubList/' + regionCitySelect.getValue('valueStr') != null ? -1 : regionCitySelect.getValue('valueStr'),
                data: {
                    name: val,
                    isSelect: true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                residentialSelect.update({data: initResidentialFn({regionArea: change[0].code}), disabled: false});
                deviceSelect.update({data: []});
            }
            if (!data.isAdd) {
                residentialSelect.update({disabled: true});
                deviceSelect.update({disabled: true});
            }
            residentialSelect.setValue([]);
            deviceSelect.setValue([]);

        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    //小区查询下拉列表
    var residentialSelect = xmSelect.render({
        el: '#residential',
        name: 'residentialId',
        layVerify: 'required',
        disabled: true,
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/estate/residential/getListByLikeName/' + regionAreaSelect.getValue('valueStr') != null ? -1 : regionAreaSelect.getValue('valueStr'),
                data: {
                    name: val,

                    isSelect: true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                deviceSelect.update({data: initDeviceFn({xqId: change[0].id}), disabled: false});
                $("#adsenseNamePre").val(change[0].name)
            }
            if (!data.isAdd) {
                deviceSelect.update({disabled: true});
            }
            deviceSelect.setValue([]);
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });
    //设备查询下拉列表
    var deviceSelect = xmSelect.render({
        el: '#diviceId',
        name: 'deviceId',
        // layVerify: 'required',
        disabled: true,
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/device/manage/pagingQuery/xqId=' + residentialSelect.getValue('valueStr') != null ? -1 : residentialSelect.getValue('valueStr') + "&size=100000",
                data: {
                    name: val,
                    isSelect: true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
            }
            if (!data.isAdd) {
            }
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    function initCityFn(data) {
        var cityList = null;
        common.sendSyncRequest("/common/area/getSubList/" + data.parentCode, 'get', {}, function (result) {
            cityList = result.list;
        });
        return cityList;
    }

    function initAreaFn(data) {
        var areaList = null;
        common.sendSyncRequest("/common/area/getSubList/" + data.parentCode, 'get', {}, function (result) {
            areaList = result.list;
        });
        return areaList;
    }

    function initResidentialFn(data) {
        var residentialList = null;
        common.sendSyncRequest("/estate/residential/getListByLikeName/", 'get', {regionArea: data.regionArea}, function (result) {
            residentialList = result.list;
        });
        return residentialList;
    }

    function initDeviceFn(data) {
        var deviceList = null;
        common.sendSyncRequest("/device/manage/pagingQuery/", 'get', {
            xqId: data.xqId,
            size: 100000
        }, function (result) {
            deviceList = result.data.records;
        });
        return deviceList;
    }

    //监听广告位类型下拉选，如果是设备则显示设备下拉选，如果是门体灯箱/围栏灯箱则显示门体编号
    form.on('select(adsenseType)', function (data) {
        var adsenseType = $("select[name=adsenseType]").val();
        console.log(adsenseType)
        if (adsenseType == "1") {
            $("#deviceIdDiv").show();
            $("#doorNumberDiv").hide();
        } else {
            $("#deviceIdDiv").hide();
            $("#doorNumberDiv").show();
        }

    });


});


