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

    //  页面元素
    var $node = {
        //  取消按钮
        resetBtn : $('button[type="reset"]'),
    };
    $.ajax({
        type: 'GET',
        url: '/permissions/dict/getByCode/deviceFactory',
        success: function (result) {
            deviceFactoryDict.update({data:result.list}).setValue([$('#deviceFactoryValue').val()])
        },
    });
    var deviceFactoryDict = xmSelect.render({
        el: '#deviceFactory',
        name: 'deviceFactory',
        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/playModel',
        success: function (result) {
            playModelDict.update({data:result.list}).setValue([$('#playModelValue').val()])
        },
    });
    var playModelDict = xmSelect.render({
        el: '#playModel',
        name: 'playModel',
        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/isShowUsername',
        success: function (result) {
            isShowUsernameDict.update({data:result.list}).setValue([$('#isShowUsernameValue').val()])
        },
    });
    var isShowUsernameDict = xmSelect.render({
        el: '#isShowUsername',
        name: 'isShowUsername',
        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/discernBoxPosition',
        success: function (result) {
            discernBoxPositionDict.update({data:result.list}).setValue([$('#discernBoxPositionValue').val()])
        },
    });
    var discernBoxPositionDict = xmSelect.render({
        el: '#discernBoxPosition',
        name: 'discernBoxPosition',
        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(obj){
        let data = obj.field;
        if (data.haveAdminPwd == '2'){
            data.adminPwd = '';
        }
        $.ajax({
            url     :   '/deviceTemplate/manage/updateDeviceTemplate',
            data    :   data,
            type    :   'POST',
            success :   function (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("修改失败");
                }
            }
        });
    });

    //  表单校验
    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个字符'
            }
        },
        size : function (value) {
            if (value != ''){
                if (value < 0 || value > 100){
                    return '请输入0-100以内的整数'
                }
            }
        },
        //  密码校验
        password   : [
            /^[0-9]{4,6}$/
            ,'密码由4到6个数字组成'
        ],
        //  密码校验
        adminPassword   : [
            /^[0-9]{4,8}$/
            ,'管理密码由4到8个数字组成'
        ],
    });

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

    //监听是否支持视频通话功能
    layui.form.on("radio(isHaveCall)",function (data) {
        switch (data.value) {
            case "1" :
                $('input[name="callVolume"]').show();
                $('#callVolumeLabel').show();
                break;
            case "2" :
                $('input[name="callVolume"]').hide().val('80');
                $('#callVolumeLabel').hide();
                break;
            default :
                break;
        }
    });

    //监听是否支持人脸识别功能
    layui.form.on("radio(isHaveFace)",function (data) {
        switch (data.value) {
            case "1" :
                $('#face_options').get(0).style.display = "block";
                break;
            case "2" :
                $('#face_options').get(0).style.display = "none";
                $('input[name="faceResolution"]').val('75');
                break;
            default :
                break;
        }
    });

    //监听是否拥有管理密码
    layui.form.on("radio(haveAdminPwd)",function (data) {
        switch (data.value) {
            case "1" :
                $('#adminPwdInput').show();
                $('#adminPwdLabel').show();
                break;
            case "2" :
                $('#adminPwdInput').hide();
                $('#adminPwdLabel').hide();
                $('input[name="adminPwd"]').val('123456');
                break;
            default :
                break;
        }
    });
});
