apiready = function() {
    var ctrl = {
        init: {},
        bind: {},
        password: null, //密码
        repeatPassword: null, //确认密码
        toast: {}, //弹框提示

    };
    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('#check-in'), "touchend", function() {

            var password = $api.trim($api.val($api.dom('#password')));
            var repeatPassword = $api.trim($api.val($api.dom('#confirm-password')));
            //密码为空禁止点击
            if (!password || password == null) {
                //$api.attr($api.dom('#check-in'), 'disabled', true);
                $api.text($api.dom('#password-tip'), '请输入密码');
            } else {
                //$api.removeAttr($api.dom('#check-in'), 'disabled');
                $api.text($api.dom('#password-tip'), '');
            }
            if (!repeatPassword) {
                //$api.attr($api.dom('#check-in'), 'disabled', true);
                $api.text($api.dom('#confirm-tip'), '请输入密码');
            } else {
                //$api.removeAttr($api.dom('#check-in'), 'disabled');
                $api.text($api.dom('#confirm-tip'), '');
            }
            if (password && repeatPassword && password == repeatPassword) {
                $api.removeAttr($api.dom('#check-in'), 'disabled');
                var url = "/admin/rest/user/forget/password";
                var data = {
                    "password": password,
                    "userId": api.pageParam.id //userId
                }
                $api.post(url, data, function(res, err) {
                    if (err) {
                        ctrl.toast("网络请求失败");
                    } else {
                        //密码重置成功，跳转到登陆页
                        if (res.code == 200) {
                            api.alert({
                                title: '',
                                msg: '重置密码成功，请重新登陆',
                            }, function(ret, err) {
                                api.openWin({
                                    name: '跳转到登陆页',
                                    url: '../../html/user/login.html'
                                });
                                window.setTimeout(function() {
                                    api.closeWin();
                                }, 500)

                            });
                        } else {
                            //密码重置失败
                            ctrl.toast("重置密码失败");
                        }
                    }
                });
            }
        });
        //确认密码选框失去焦点时如果两次密码不一致给出提示并且阻止点击确认修改按钮
        $api.addEvt($api.dom('#confirm-password'), 'blur', function() {
            ctrl.password = $api.trim($api.val($api.dom('#password')));
            ctrl.repeatPassword = $api.trim($api.val($api.dom('#confirm-password')));
            var password = ctrl.password;
            var repeatPassword = ctrl.repeatPassword;
            if (password && repeatPassword) {
                if (ctrl.password != ctrl.repeatPassword) {
                    $api.attr($api.dom('#check-in'), 'disabled', true);
                    $api.text($api.dom('#confirm-tip'), '两次密码不一致');
                    $api.css($api.dom('#check-in'), 'background:#d9d9d9');
                } else {
                    $api.text($api.dom('#confirm-tip'), '');
                    $api.css($api.dom('#check-in'), 'background:#E9BE2B');
                    $api.removeAttr($api.dom('#check-in'), 'disabled')
                }
            }

        })
        $api.addEvt($api.dom('#confirm-password'), 'keyup', function() {
            var password = $api.trim($api.val($api.dom('#password')));
            var repeatPassword = $api.trim($api.val($api.dom('#confirm-password')));
            if (password && repeatPassword) {
                if (password != repeatPassword) {
                    $api.removeAttr($api.dom('#check-in'), 'disabled')
                    $api.css($api.dom('#check-in'), 'background:#d9d9d9');
                } else {
                    $api.css($api.dom('#check-in'), 'background:#E9BE2B');
                }
            }
        })

    }
    ctrl.init();
}
