/**
 * 企业修改密码
 * created by liufang 2018/8/17
 */
$(function(){
    var ERROR_OK = 200;
    var contextPath=$("#contextPath").val();
    var account,openId,organizationId,userStatus,register,developId,userId;
    var accountPsd = {
        init: function(){
            this.render();
            this.bindEvent();
        },
        render: function(){

        },
        bindEvent: function(){
            var _this = this;
            account = this.GetQueryString("virAccount");
            openId = this.GetQueryString("openId");
            organizationId = this.GetQueryString("organizationId");
            // if(organizationId==""||organizationId==null){
            // organizationId=sessionStorage.getItem("orgId");
            // }
            userStatus = this.GetQueryString("userStatus");
            register = this.GetQueryString("register");
            developId = this.GetQueryString("developId");
            userId = this.GetQueryString("userId");
            //集中焦点
            $(".account-text").on("click",function(){
                $(".error-tips-wp").hide();
            })
            //确认修改
            $(".login-btn").on("click",function(){
                _this.changePsd();
            })

        },
        /*修改密码*/
        changePsd: function(){
            var $errorTip = $(".error-tips-wp");
            //原密码
            var oldPassword = $(".account-old-psd").val();
            if(!oldPassword){
                $errorTip.show().html("请输入原密码！");
                return false;
            }
            //新密码
            var newPassword = $(".account-new-psd").val();
            if(!newPassword){
                $errorTip.show().html("请输入新密码！");
                return false;
            }
            //确认密码
            var surePassword = $(".account-sure-psd").val();
            if(!surePassword){
                $errorTip.show().html("请输入确认密码！");
                return false;
            }
            if(newPassword !=surePassword){
                $errorTip.show().html("两次密码输入不一致！");
                return false;
            }
            this.ajaxChangePsd(oldPassword,newPassword)
        },
        /**
         * 修改企业账号的密码
         * @param oldPassword 旧密码
         * @param newPassword 新密码
         */
        ajaxChangePsd: function(oldPassword,newPassword){
            $.ajax({
                url :contextPath+"/wechat/appUser/updatePassword",
                type : "POST",
                dataType:"json",
                data: {
                    oldPassword : oldPassword ,
                    newPassword: newPassword,
                    openId: openId,
                    account: account,
                    orgId: organizationId
                },
                success : function(data) {
                    if(data.code==ERROR_OK){
                        var result = data.data;
                        window.location.href=$("#contextPath").val()+"/wechat/appUser/virAccount?openId="+openId+"&developId="+developId+"&userStatus="+userStatus+"&userId="+userId+"&register="+register+"&organizationId="+organizationId;
                    }else{
                        layer.open({
                            content: data.msg
                            ,skin: 'msg'
                            ,time: 2 //2秒后自动关闭
                        });
                    }
                },
                error : function(data) {
                    // alert("系统繁忙,菜单加载失败..");
                }
            });
        },
        /**
         * 获取url的参数
         * @param name 参数名
         */
        GetQueryString: function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        },
    }
    accountPsd.init();
})