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

    //  页面节点
    var $node = {
        //  确认密码字段
        'confirmPwdInput'   :   $('input[name="confirmLoginPwd"]'),
        //  取消按钮
        'resetBtn'      :   $('button[type="reset"]')
    };

    //  初始化事件
    (function(){
        //  提交表单
        layui.form.on("submit(entity_insert_filter)",function(data){
            common.timer($('button[lay-filter="entity_insert_filter"]'));
            $.ajax({
                url     :   '/permissions/account/modifyPassword',
                data    :   JSON.stringify(data.field),
                dataType: "json",
                contentType : 'application/json',
                type    :   'POST',
                success :   function(res){
                    if (res.code == 200){
                        window.location.href = '/permissions/authentication/login';
                    }else {
                        window.location.reload();
                    }
                },
            });

            // common.sendAsyncRequest("/permissions/account/modifyPassword", "POST", data.field, function (res) {
            //     debugger
            // });
        });

        //  表单校验
        layui.form.verify({
            //  密码校验
            password   : [
                /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,12}$/
                ,'密码必须是8-12位的数字和字母,且不能出现空格'
            ],
            //  确认密码
            passwordRe  :   function(value){
                var newPassword = $('#new_password').val();
                var oldPassword = $('#old_password').val();
                if (newPassword == oldPassword){
                    return '新旧密码不能相同';
                }
                if (newPassword !== value){
                    return '与前面输入的密码不一致';
                }
            },
        });

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

    })();

});