"use strict";
layui.extend({
    common    :   'js/common'
}).use(['jquery','common','form','laytpl','element'],function(){
    var $ = layui.jquery;
    //  引入表单插件
    var form = layui.form;

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

    var pageData = {
        id : $('#id').val(),
        optionsId : $('#optionsId').val(),
        serialNumber : $('#serialNumber').val(),
        deviceModelNumber : $('#deviceModelNumber').val(),
        deviceFactory : $('#deviceFactory').val(),
        all :   $('input[name="all"]').val(),
        faceOpenDoor    :   $('input[name="faceOpenDoor"]').val(),
        cardOpenDoor    :   $('input[name="cardOpenDoor"]').val(),
        remoteOpenDoor  :   $('input[name="remoteOpenDoor"]').val(),
        pwdOpenDoor :   $('input[name="pwdOpenDoor"]').val(),
        keyboard    :   $('input[name="keyboard"]').val(),
        adPlay  :   $('input[name="adPlay"]').val()
    };

    //  点击取消事件
    $node.resetBtn.on('click',function(){
        //  关闭当前页面弹窗
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });

    //  绑定表单提交事件
    layui.form.on("submit(entity_submit_event)",function(obj){
        let data = obj;
        data.field.serialNumber = pageData.serialNumber;
        data.field.deviceModelNumber = pageData.deviceModelNumber;
        data.field.deviceFactory = pageData.deviceFactory;
        switch (data.elem.value) {
            case "device" :
                data.field.id = pageData.id;
                $.ajax({
                    url     :   '/device/manage/edit',
                    data    :   data.field,
                    type    :   'POST',
                    success :   function(){
                        $node.resetBtn.click();
                        parent.location.reload();
                        layer.msg('修改成功')
                    }
                });
                break;
            case "options" :
                data.field.id = pageData.optionsId;
                data.field.deviceId = pageData.id;
                $.ajax({
                    url     :   '/device/options/edit',
                    data    :   data.field,
                    type    :   'POST',
                    success :   function(){
                        $node.resetBtn.click();
                        parent.location.reload();
                        layer.msg('修改成功')
                    }
                });
                break;
                case "addOptions" :
                data.field.deviceId = pageData.id;
                $.ajax({
                    url     :   '/device/options/add',
                    data    :   data.field,
                    type    :   'POST',
                    success :   function(){
                        $node.resetBtn.click();
                        parent.location.reload();
                        layer.msg('添加成功')
                    }
                });
                break;
        }
    });

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

    //监听是否支持人脸识别功能
    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(all)",function (data) {
        switch (data.value) {
            case "1" :
                $("input[name=faceOpenDoor][value='1']").prop("checked","true");
                $("input[name=cardOpenDoor][value='1']").prop("checked","true");
                $("input[name=remoteOpenDoor][value='1']").prop("checked","true");
                $("input[name=pwdOpenDoor][value='1']").prop("checked","true");
                $("input[name=keyboard][value='1']").prop("checked","true");
                $("input[name=adPlay][value='1']").prop("checked","true");
                break;
            case "2" :
                $("input[name=faceOpenDoor][value='2']").prop("checked","true");
                $("input[name=cardOpenDoor][value='2']").prop("checked","true");
                $("input[name=remoteOpenDoor][value='2']").prop("checked","true");
                $("input[name=pwdOpenDoor][value='2']").prop("checked","true");
                $("input[name=keyboard][value='2']").prop("checked","true");
                $("input[name=adPlay][value='2']").prop("checked","true");
                break;
            default :
                break;
        }
        form.render();
    });
});
