apiready = function() {
    var ctrl = {
        init: {},
        bind: {},
        verifyPwd: {},
        checkIn: {},
        toast: {},
        forbidChangePwd: {} //客户体验禁止修改密码
    }
    ctrl.init = function() {
        // 适配安卓状态栏
        CommonModel.fitInStatusBar();
        ctrl.bind();
    }
    ctrl.toast = function(msg) {
        api.toast({
            msg: msg,
            duration: 3000,
            locaiton: 'top'
        });
    }
    ctrl.bind = function() {
        //点击返回按钮
        $api.addEvt($api.dom("#back"), "touchend", function() {
            api.closeWin();
        })
        $api.addEvt($api.dom("#confirm-pwd"), "keyup", function() {
            var newPwd = $api.trim($api.val($api.dom("#new-pwd")));
            var confirmPwd = $api.trim($api.val($api.dom("#confirm-pwd")));
            var originPwd = $api.trim($api.val($api.dom("#origin-pwd")));
            if (confirmPwd && newPwd) {
                if (newPwd != confirmPwd) {
                    $api.text($api.dom('#confirm-pwd-tip'), '两次密码不一致')
                } else {
                    $api.text($api.dom('#confirm-pwd-tip'), '')
                }
            } else {
                $api.text($api.dom('#confirm-pwd-tip'), '')
            }

            if (originPwd && newPwd && confirmPwd) {
                if (newPwd == confirmPwd && originPwd != newPwd && confirmPwd != originPwd) {
                    $api.css($api.dom('#confirm'), 'background:#E9BE2B');
                } else {
                    $api.css($api.dom('#confirm'), 'background:#d9d9d9');
                }

            } else {
                $api.css($api.dom('#confirm'), 'background:#d9d9d9');
            }
        })
        $api.addEvt($api.dom("#new-pwd"), "keyup", function() {
            var newPwd = $api.trim($api.val($api.dom("#new-pwd")));
            var confirmPwd = $api.trim($api.val($api.dom("#confirm-pwd")));
            if (confirmPwd != '' && newPwd != '') {
                if (newPwd != confirmPwd) {
                    $api.text($api.dom('#confirm-pwd-tip'), '两次密码不一致')
                } else {
                    $api.text($api.dom('#confirm-pwd-tip'), '')
                }
            } else {
                $api.text($api.dom('#confirm-pwd-tip'), '')
            }
        });
        //如果新密码和原密码相同给出提示
        $api.addEvt($api.dom("#new-pwd"), "keyup", function() {
            var newPwd = $api.trim($api.val($api.dom("#new-pwd")));
            var originPwd = $api.trim($api.val($api.dom("#origin-pwd")));
            if (newPwd != '' && originPwd != '') {
                if (newPwd == originPwd) {
                    $api.text($api.dom('#new-pwd-tip'), '不能跟原密码相同')
                } else {
                    $api.text($api.dom('#new-pwd-tip'), '')
                }
            } else {
                $api.text($api.dom('#new-pwd-tip'), '')

            }

        });

        $api.addEvt($api.dom('#confirm'), 'touchend', function() {
            var originPwd = $api.trim($api.val($api.dom("#origin-pwd")));
            var newPwd = $api.trim($api.val($api.dom("#new-pwd")));
            var confirmPwd = $api.trim($api.val($api.dom("#confirm-pwd")));
            if (originPwd != '' && newPwd != '' && confirmPwd != '') {
                if (newPwd == confirmPwd && originPwd != newPwd && confirmPwd != originPwd) {
                    $api.text($api.dom('#origin-pwd-tip'), '');
                    $api.text($api.dom('#new-pwd-tip'), '');
                    $api.text($api.dom('#confirm-pwd-tip'), '');
                    ctrl.checkIn(originPwd, newPwd);
                }

            } else {
                if (!originPwd) {
                    $api.text($api.dom('#origin-pwd-tip'), '请填写原密码');
                    return;
                } else {
                    $api.text($api.dom('#origin-pwd-tip'), '');
                }

                if (!newPwd) {
                    $api.text($api.dom('#new-pwd-tip'), '请填写新密码');
                    return;
                } else {
                    $api.text($api.dom('#new-pwd-tip'), '');
                }
                if (!confirmPwd) {
                    $api.text($api.dom('#confirm-pwd-tip'), '请确认新密码');
                } else {
                    $api.text($api.dom('#confirm-pwd-tip'), '');
                }
                if (newPwd && originPwd && newPwd == originPwd) {
                    $api.text($api.dom('#new-pwd-tip'), '不能跟原密码相同');
                } else {
                    $api.text($api.dom('#new-pwd-tip'), '');
                }

            }

        });

    }

    ctrl.checkIn = function(password, newPassword) {
        var url = '/admin/rest/user/edit/password';
        var username = $api.getStorage('username');
        var data = {
            "userName": username,
            "oldPassword": password,
            "newPassword": newPassword
        }
        $api.post(url, data, function(res, err) {
            if (err) {
                ctrl.toast("网络请求失败");
            } else {
                if (!res.code || res.code != 200) {
                    $api.text($api.dom('#origin-pwd-tip'), '原密码错误');
                } else {
                    $api.text($api.dom('#origin-pwd-tip'), '');
                    api.alert({
                        title: '',
                        msg: '修改密码成功请重新登陆',
                    }, function(ret, err) {
                        api.openWin({
                            name: '跳转到登陆页',
                            url: '../../html/user/login.html'
                        });
                        window.setTimeout(function() {
                            api.closeWin();
                        }, 500)
                    });

                }

            }

        });
    }

    ctrl.init();
}
