{
if (this.otpCooldown <= 1) {
this.otpCooldown = 0;
clearInterval(this.cooldownTimer);
this.cooldownTimer = null;
} else {
this.otpCooldown -= 1;
}
}, 1000);
},
async sendOtp() {
if (!this.phone) { alert('شماره همراه را وارد کنید.'); return; }
if (!this.captchaCode || !this.captchaId) { alert('لطفاً کد امنیتی را وارد کنید.'); return; }
if (this.otpBusy || this.otpCooldown > 0) return;
this.otpBusy = true;
this.otpMsg = '';
const fd = new FormData();
fd.append('action', 'send_login_otp');
fd.append('phone', this.phone);
fd.append('captcha_id', this.captchaId);
fd.append('captcha_code', this.captchaCode);
fd.append('website', '');
try {
const r = await fetch(this.captchaApi, { method: 'POST', body: fd, credentials: 'same-origin' });
const d = await r.json();
if (d.success) {
this.otpSent = true;
this.otpMsg = d.message || 'کد ارسال شد.';
if (d.data && d.data.hint) this.otpMsg += ' — ' + d.data.hint;
this.startCooldown((d.data && d.data.cooldown) || 60);
this.refreshCaptcha();
} else {
alert(d.message || 'ارسال کد ناموفق بود.');
this.refreshCaptcha();
if (d.data && d.data.cooldown) this.startCooldown(d.data.cooldown);
}
} catch (e) {
alert('خطا در ارتباط با سرور.');
this.refreshCaptcha();
}
this.otpBusy = false;
},
async submitLogin() {
const fd = new FormData();
if (this.mode === 'password') {
if (!this.captchaCode || !this.captchaId) {
alert('لطفاً کد امنیتی را وارد کنید.');
return;
}
fd.append('action', 'login');
fd.append('phone', this.phone);
fd.append('password', this.password);
fd.append('captcha_id', this.captchaId);
fd.append('captcha_code', this.captchaCode);
fd.append('website', '');
} else {
if (!this.otpSent) {
alert('ابتدا کد پیامکی را دریافت کنید.');
return;
}
if (!this.otpCode) {
alert('کد پیامکی را وارد کنید.');
return;
}
fd.append('action', 'login_otp');
fd.append('phone', this.phone);
fd.append('otp', this.otpCode);
}
if (this.nextPath) fd.append('next', this.nextPath);
try {
const r = await fetch(this.captchaApi, { method: 'POST', body: fd, credentials: 'same-origin' });
const d = await r.json();
if (d.success) {
alert(d.message);
window.location.href = (d.data && d.data.redirect) || 'https://extruck.co/dashboard';
} else {
alert(d.message || 'ورود ناموفق بود.');
if (this.mode === 'password') this.refreshCaptcha();
}
} catch (e) {
alert('خطا در ارتباط با سرور.');
if (this.mode === 'password') this.refreshCaptcha();
}
}
}">