"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 $node = {
        //  取消按钮
        idInput:$('input[name="id"]'),
        resetBtn        :   $('button[type="reset"]'),
        name        :   $('input[name="name"]'),
        type : $('select[name="type"]'),
        residential : $("#residentialId"),
        residentialName : $('#residential_name'),
        pathUse : $('#path_use')
    };

    $.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})
    //  初始化
    $.ajax({
        url     :   '/device/path/get/' + $node.idInput.val(),
        success :   function(result){
            var data = result.data;
            //  数据渲染
            if(data){
                $node.name.val(data.name);
                         /*$node.type.val(data.type);
                        $node.pathUse.val(data.pathUse);*/
                        $node.residential.val(data.xqId);
                        pathTypeDict.setValue([data.type]);
                        pathUseDict.setValue([data.pathUse]);
                        layui.form.render('select');
                    }
                }
            });
        },
    });
    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: ', ',
                },
            }
        },
    });

    //  绑定表单提交事件
    layui.form.on("submit(entity_submit_event)",function(data){
        var $residentialCheck=$('#residential_id :selected');
        data.field.xqId=$residentialCheck.val();
        data.field.xqName=$residentialCheck.text();
        $.ajax({
            url     :   '/device/path/editEntity',
            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){
        $node.residentialName.val($('#residential_id option[value="'+ data.value +'"]').text());
    });

    /**
     * 初始化所属小区下拉框（列表）
     */
    var initResidentialFn = function(){
        $.ajax({
            url     :   '/estate/agency/findResidentialListByCondition',
            type    :   'GET',
            success :   function (result) {
                var node = $('#residential_id');
                var currentResidentialId=$("#residentialId").val();
                //  清空元素
                node.html('');
                result.list.forEach(function(item){
                    if(currentResidentialId==item.id){
                        node.append('<option value="' + item.id + '" selected="selected" >' + item.name + '</option>');
                    }else{
                        node.append('<option value="' + item.id + '" >' + item.name + '</option>');
                    }

                });
                //重新渲染
                layui.form.render('select');
                if(!currentResidentialId){
                    $('#residential_name').val( node.next().find('dl dd').eq(0).text());
                }
            }
        });
    };

    //  表单校验
    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 () {
        //  加载所属小区
        initResidentialFn();
    })();
});
