var $=require("jquery"),Form=require("./form.js"),SmsVerify=function(e,r){r=r||null;var t=$.extend({generateServer:"",selectorPhone:"",selectorGenerate:"",selectorCountdown:"",selectorRegenerate:"",selectorCaptcha:"",interval:60,tipError:function(e){r?r.tipError(e):alert(e)},sendError:function(e){r?r.tipError(e):alert(e)}},e),o=0,n=function(){var e=$(t.selectorCountdown);e.is("input")?e.val(o+" s"):e.html(o+" s"),o>0?(o--,setTimeout(n,1e3),$(t.selectorCountdown).show(),$(t.selectorRegenerate).hide()):(e.hide(),$(t.selectorCountdown).hide(),$(t.selectorRegenerate).show())},a=!1,l=function(){var e=$(t.selectorPhone).val();if(!e)return t.tipError("手机号为空"),!1;if(!/^1[0-9]{10}$/.test(e))return t.tipError("手机号格式不正确"),!1;var l=null;return t.selectorCaptcha&&!(l=$(t.selectorCaptcha).val())?(t.tipError("验证码为空"),!1):!a&&(a=!0,r&&r.loadingOn(),$.post(t.generateServer,{phone:e,captcha:l},function(e){r&&r.loadingOff(),a=!1,Form.defaultCallback(e,{success:function(e){e.data&&alert(e.data),$(t.selectorGenerate).hide(),o=t.interval,n(o)},error:function(e){e.data&&alert(e.data),t.sendError(e.msg)}},r)}),!1)};$(t.selectorGenerate).on("click",l),$(t.selectorRegenerate).on("click",l)};module.exports=SmsVerify;