/*
 * @require "captcha.scss"
 * */
define([
    'utils',
    'config',
    'dialog'
], function (utils, config, dialog) {
    var apiUrl = config.apiUrl;
    var confirmCallback = $.noop;
    //create captcha dialog
    var captcha_dialog;

    $(document)
        .on("click", "#sms_send_btn", function (e) {
            e.preventDefault();
            utils._jsonP({
                    url: apiUrl.sms_send,
                    data: {
                        phone: $('#phoneNo').val(),
                        captcha: $('#captchaCode').val()
                    }
                }, function (res) {
                    if (+res.result === 1) {
                        captcha_dialog.hide();
                        var sms_input_dialog = dialog({
                            title: "验证码",
                            content: SQ.T(__inline('sms_input.html')),
                            buttons: {
                                "点击确认": function (text, dialog) {
                                    // text 是文字，比如这里是 "确定"
                                    // dialog 就是该弹窗的实例，可以调用上面的方法，比如
                                    confirmCallback && confirmCallback($("#verifyCode").val(), sms_input_dialog);
                                }
                            }
                        });
                    } else {
                        $(".captcha-img").prop("src", apiUrl.captcha + "&t=" + Math.random());
                        $('#captchaCode').val('');

                        var alert = dialog({
                            title: '温馨提示',
                            content: res.msg,
                            width: 350
                        });
                        alert.bind('show', function () {
                            $('.sq-dialog-client', alert.el).css({
                                "border-color": "#f19300",
                                "border-width": "1px",
                                "border-style": "solid"
                            })
                        });
                        alert.show();
                    }
                }
            );
        })
        .on("click", ".captcha-img", function (e) {
            e.preventDefault();
            $(".captcha-img").prop("src", apiUrl.captcha + "&t=" + Math.random());
        });

    var obj = {
        show: function (callback) {
            var captcha_dialog = dialog({
                title: "验证码",
                content: SQ.T(__inline("captcha.html"), {url: apiUrl.captcha + "&t=" + Math.random()}),
                height:100,
                buttons: {
                    "确认": function (text, dialog) {
                        // text 是文字，比如这里是 "确定"
                        // dialog 就是该弹窗的实例，可以调用上面的方法，比如
                        confirmCallback && confirmCallback($('#captchaCode').val(),captcha_dialog);
                    }
                }
            });

            $("#captchaCode").keyup(function(e){
                if(e.keyCode === 13&&$('#captchaCode').val()){
                    confirmCallback && confirmCallback($('#captchaCode').val(),captcha_dialog);
                }
            });

            captcha_dialog.show();
            confirmCallback = callback;
        },
        error: function () {
            $(".captcha-img").click();
            $("#captcha").val("");
            $(".captcha-tips").removeClass("hide");
        },
        hide: function () {
            that.dialog.hide();
        },
        withSms: {
            show: function (callback, hideCallback) {
                captcha_dialog = dialog({
                    title: "验证码",
                    content: SQ.T(__inline('captchaWithSms.html'), {url: apiUrl.captcha + "&t=" + Math.random()})
                });
                captcha_dialog.show();
                confirmCallback = callback;
            },
            error: function () {
                $(".captcha-img").click();
                $("#captcha").val("");
                $(".captcha-tips").removeClass("hide");
            },
            hide: function () {
                that.dialog.hide();
            },
            /**
             * get captcha text
             * @returns {*|jQuery}
             */
            getCode: function () {
                var value = $("#captcha").val();
                return value;
            },
            getFlag: function () {
                return $(".captcha-box").data("flag");
            }
        }
    };

    return obj;
});