"use client" import { useEffect, useState } from 'react' import { useRouter } from 'next/navigation' import { api } from '@/lib/api' import { Smartphone, ArrowLeft } from 'lucide-react' import Loading from '@/components/Loading' import { MobileInput } from '@/components/forms' import { ErrorMessage } from '@/components/common' import { getCurrentPersianYear, getCurrentPersianMonth, getCurrentPersianDay } from '@/lib/date/persian-date' export default function Home() { const router = useRouter() const [mobile, setMobile] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { // Check if user is logged in const token = localStorage.getItem('authToken') const userStr = localStorage.getItem('user') if (token && userStr) { try { const user = JSON.parse(userStr) // Try to use the saved device from localStorage first const savedDeviceId = localStorage.getItem('selectedDeviceId') if (savedDeviceId) { const today = `${getCurrentPersianYear()}/${String(getCurrentPersianMonth()).padStart(2, '0')}/${String(getCurrentPersianDay()).padStart(2, '0')}` router.push(`/daily-report?deviceId=${savedDeviceId}&date=${encodeURIComponent(today)}`) return } // No saved device — fetch user devices and auto-select the first one api.getUserDevices(user.id) .then((devices) => { if (devices.length > 0) { const firstDevice = devices[0] // Save to localStorage localStorage.setItem('selectedDeviceId', String(firstDevice.id)) localStorage.setItem('selectedDeviceName', firstDevice.deviceName) const today = `${getCurrentPersianYear()}/${String(getCurrentPersianMonth()).padStart(2, '0')}/${String(getCurrentPersianDay()).padStart(2, '0')}` router.push(`/daily-report?deviceId=${firstDevice.id}&date=${encodeURIComponent(today)}`) } else { // No devices — go to devices page to show the error router.push('/devices') } }) .catch(() => { // On error, fall back to devices page router.push('/devices') }) } catch { // Invalid user data, go to login } } }, [router]) const normalizeMobile = (mobile: string): string => { const digitsOnly = mobile.replace(/\D/g, '') if (digitsOnly.startsWith('9') && digitsOnly.length === 10) { return '0' + digitsOnly } if (digitsOnly.startsWith('0098') && digitsOnly.length === 13) { return '0' + digitsOnly.substring(3) } if (digitsOnly.startsWith('98') && digitsOnly.length === 12) { return '0' + digitsOnly.substring(2) } return digitsOnly } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError(null) const normalizedMobile = normalizeMobile(mobile) if (!normalizedMobile || normalizedMobile.length !== 11 || !normalizedMobile.startsWith('09')) { setError('لطفاً شماره موبایل معتبر وارد کنید (مثال: 09123456789)') return } setLoading(true) try { const result = await api.sendCode({ mobile: normalizedMobile }) if (result.success) { // Redirect to verify page with mobile number router.push(`/verify-code?mobile=${encodeURIComponent(normalizedMobile)}`) return // Don't set loading to false — component will unmount } else { setError(result.message || 'خطا در ارسال کد') } } catch (error: unknown) { console.error('Error sending code:', error) setError(error instanceof Error ? error.message : 'خطا در ارتباط با سرور') } finally { setLoading(false) } } const canSubmit = mobile.length >= 10 if (loading) { return } return (

ورود با شماره موبایل

شماره موبایل خود را وارد کنید تا کد فعال‌سازی برای شما ارسال شود

{ setMobile(value) setError(null) }} disabled={loading} />
{error && }
) }