apiready = function() {
    var ctrl = {
        init: {},
        bind: {},
        phoneEmpty: {}, //验证手机号是否为空以及格式是否正确
        getCode: {}, //获取验证码
        refreshCode: {}, //验证码倒计时
        toast: {}, //弹出框提示
        checkIn: {}, //提交信息重置密码
        isCountdowning: false //是否正在进行倒计时
    };
    ctrl.init = function() {
        // 适配安卓状态栏
        CommonModel.fitInStatusBar();
        ctrl.bind();
    }
    ctrl.toast = function(msg) {
        api.toast({
            msg: msg,
            duration: 3000,
            locaiton: 'top'
        });
    }
    ctrl.phoneEmpty = function() {
        ctrl.phoneNumber = $api.trim($api.val($api.dom('#phone')));
        //var code        = $api.trim($api.val($api.dom('#confirm-code')));
        if (ctrl.phoneNumber == '') {
            $api.text($api.dom('#phone-tip'), '请输入手机号');
        } else {
            var reg = /^1[0-9]{10}$/;
            if (!reg.test(ctrl.phoneNumber)) {
                $api.text($api.dom('#phone-tip'), '手机号格式不正确');
            } else {
                $api.text($api.dom('#phone-tip'), '');
                return true;
            }
        }
    }

    ctrl.bind = function() {
            $api.addEvt($api.dom("#back"), "touchend", function() {
                    api.closeWin();
                })
                //     //点击确认按钮 验证手机号及验证码是否正确
            $api.addEvt($api.dom('#submit'), 'touchend', function() {
                    ctrl.checkIn();
                })
                //点击获取验证码按钮获取验证码
            $api.addEvt($api.dom('#get-code'), 'touchend', function() {
                    $api.attr($api.dom('#get-code'), "disabled", true);
                    if (ctrl.phoneEmpty()) {
                        //手机号正确并且正在进行倒计时
                        if (ctrl.isCountdowning) {
                            return;
                        } else {
                            //手机号正确并且倒计时结束
                            $api.removeAttr($api.dom('#get-code'), "disabled");
                            ctrl.getCode();
                        }

                    }
                })
                //手机号选框失去焦点时
            $api.addEvt($api.dom('#phone'), 'blur', function() {
                if (ctrl.phoneEmpty()) {
                    //手机号正确并且此时没有进行验证码倒计时
                    if (!ctrl.isCountdowning) {
                        $api.removeAttr($api.dom('#get-code'), "disabled");
                    }
                } else {
                    $api.attr($api.dom('#get-code'), "disabled", true);
                }
            })


            $api.addEvt($api.dom('#code'), 'keyup', function() {
                var phoneNumber = $api.trim($api.val($api.dom('#phone')));;
                var code = $api.trim($api.val($api.dom('#code')));
                if (phoneNumber != '' && code != '' && phoneNumber != null && code != null) {
                    $api.css($api.dom('#submit'), 'background:#E9BE2B');
                } else {
                    $api.css($api.dom('#submit'), 'background:#d9d9d9');
                }
            })
        }
        //获取验证码
    ctrl.getCode = function() {
            var url = "/admin/rest/user/get/sms";
            var data = {
                "userName": ctrl.phoneNumber
            };
            $api.post(url, data, function(res, err) {
                if (err) {
                    ctrl.toast("网络请求失败");
                } else {
                    if (res.code == 200) {
                        //验证手机号码正确进行倒计时
                        ctrl.refreshCode();

                    } else {
                        //$api.attr($api.dom('#get-code'), "disabled", true);
                        $api.text($api.dom('#phone-tip'), '手机号不正确');
                    }
                }
            });
        }
        //提交信息进行重置密码
    ctrl.checkIn = function() {
            var phoneNumber = $api.trim($api.val($api.dom('#phone')));;
            var code = $api.trim($api.val($api.dom('#code')));
            if (phoneNumber == '' || phoneNumber == null) {
                $api.text($api.dom('#phone-tip'), '请输入手机号');
            } else {
                $api.text($api.dom('#phone-tip'), '');
            }
            if (code == '' || code == null) {
                $api.text($api.dom('#code-tip'), '请输入验证码');
            } else {
                $api.text($api.dom('#code-tip'), '');
            }

            if (phoneNumber != '' && code != '' && phoneNumber != null && code != null) {
                $api.css($api.dom('#submit'), 'background:#E9BE2B');
                var url = "/admin/rest/user/sms/validate";
                var data = {
                    "userName": phoneNumber,
                    "code": code
                }
                $api.post(url, data, function(res, err) {
                    if (err) {
                        ctrl.toast("网络请求失败");
                    } else {
                        if (res.code != 200) {
                            //验证码错误
                            $api.text($api.dom('#code-tip'), '验证码不正确');
                        } else {
                            //验证码正确跳转到重置密码页
                            $api.text($api.dom('#code-tip'), '');
                            api.openWin({
                                name: 'resetPassword',
                                url: '../../html/user/resetPassword.html',
                                pageParam: {
                                    id: res.body //userId
                                }
                            });
                            // api.closeWin({});
                            window.setTimeout(function() {
                                api.closeWin();
                            }, 500)
                        }
                    }
                });
            }

        }
        //验证码倒计时
    ctrl.refreshCode = function() {
        //如果正在倒计时则不执行
        if (ctrl.isCountdowning) {
            return;
        }
        ctrl.isCountdowning = true;
        $api.val($api.dom('#get-code'), '短信已发送');
        var i = 60;
        $api.attr($api.dom('#get-code'), "disabled", true);
        if (ctrl.timer) {
            window.clearInterval(ctrl.timer);
        }
        ctrl.timer = window.setInterval(function() {
            i--;
            $api.val($api.dom('#get-code'), i + '秒之后重试');
            //倒计时为0时可以重新获取验证码
            if (i == 0) {
                ctrl.isCountdowning = false;
                window.clearInterval(ctrl.timer);
                $api.val($api.dom('#get-code'), '获取验证码');
                $api.removeAttr($api.dom('#get-code'), "disabled");
            }
        }, 1000)

    }
    ctrl.init();
}
