"use strict";
layui.extend({
    common    :   'js/common',
    xmSelect    :   'layui/dist/xm-select',
}).use(['jquery','form','common','xmSelect'],function(){
    var $ = layui.jquery;

    //  引入下拉菜单
    var xmSelect = layui.xmSelect;
    var common = layui.common;


    //  页面元素
    var $node = {
        //  取消按钮
        resetBtn        :   $('button[type="reset"]')
    };

    var pageDate = {
        parentId : $('#parentId').val(),
        flag : $('#flag').val()
    };

    //  绑定表单提交事件
    layui.form.on("submit(entity_submit_event)",function(data){
        common.timer($('button[lay-filter="entity_submit_event"]'));
        $.ajax({
            url     :   '/device/path/add',
            data    :   data.field,
            type    :   'POST',
            success :   function () {
                $node.resetBtn.click();
                parent.location.reload();
            }
        });
    });

    //  点击取消事件
    $node.resetBtn.on('click',function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });

    //  绑定所属机构绑定事件
    layui.form.on('select(residential_event)', function(data){
        $('#residential_name').val($('#residential_id option[value="'+ data.value +'"]').text());
    });

    $.ajax({
        type: 'GET',
        url: '/permissions/dict/getByCode/pathType',
        success: function (result) {
            pathTypeDict.update({data:result.list})
        },
    });
    var pathTypeDict = xmSelect.render({
        el: '#path_type',
        name: 'type',
        layVerify: 'required',
        radio: true,
        clickClose: true,
        prop: {
            name: 'dictValue',
            value: 'dictKey',
        },
        on: function (data) {},
        model: {
            label: {
                type: 'text',
                text: {
                    left: '',
                    right: '',
                    separator: ', ',
                },
            }
        },
    });
    $.ajax({
        type: 'GET',
        url: '/permissions/dict/getByCode/pathUse',
        success: function (result) {
            pathUseDict.update({data:result.list})
        },
    });
    var pathUseDict = xmSelect.render({
        el: '#path_use',
        name: 'pathUse',
        layVerify: 'required',
        radio: true,
        clickClose: true,
        prop: {
            name: 'dictValue',
            value: 'dictKey',
        },
        on: function (data) {},
        model: {
            label: {
                type: 'text',
                text: {
                    left: '',
                    right: '',
                    separator: ', ',
                },
            }
        },
    });
    /**
     * 初始化所属小区下拉框（列表）
     */
    var residentialSelect = xmSelect.render({
        el: '#residential_id',
        name: 'xqId',
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/estate/agency/findResidentialListByCondition',
                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) {
                $('#residential_name').val(change[0].name);
            }
            if (!data.isAdd) {
                $('#residential_name').val('');
            }
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

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

    (function() {
        if (pageDate.parentId != null){
            switch (pageDate.flag) {
                case '2':
                    $.ajax({
                        type: 'GET',
                        url: '/estate/residential/findResidentialById/'+pageDate.parentId,
                        success: function (result) {
                            residentialSelect.update({disabled:true,data:[result.data]}).setValue([pageDate.parentId]);
                        }
                    });
                    break;
                default:
                    break;
            }
        }
    })()
});