<x-guest-layout title="ভেরিফিকেশন কোড">
    <div class="max-w-md mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <div class="bg-white rounded-xl shadow-md p-8">
            <div class="text-center mb-8">
                <div class="mx-auto w-12 h-12 bg-blue-100 rounded-full flex items-center justify-center mb-4">
                    <svg class="w-6 h-6 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 18h.01M8 21h8a2 2 0 002-2V5a2 2 0 00-2-2H8a2 2 0 00-2 2v14a2 2 0 002 2z"></path>
                    </svg>
                </div>
                <h1 class="text-2xl font-bold text-gray-900 mb-2">ভেরিফিকেশন কোড</h1>
                <p class="text-sm text-gray-600">
                    আমরা আপনার মোবাইল নম্বরে একটি <strong>৪-সংখ্যার কোড</strong> পাঠিয়েছি। নিচে কোডটি লিখুন অথবা SMS থেকে অটো-ফিল করুন।
                </p>
            </div>

            @if(session('success'))
                <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-6">
                    {{ session('success') }}
                </div>
            @endif

            @if(session('error'))
                <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-6">
                    {{ session('error') }}
                </div>
            @endif

            <form id="otpVerifyForm" class="space-y-6">
                @csrf
                <input type="hidden" id="otpPhone" name="phone" value="{{ $phone }}">
                <input type="hidden" id="otpType" name="type" value="{{ $type }}">

                <div>
                    <label for="otpCode" class="block text-sm font-medium text-gray-700 mb-2">যাচাইকরণ কোড</label>
                    <input type="text" id="otpCode" name="otp"
                           inputmode="numeric" autocomplete="one-time-code"
                           maxlength="4" required
                           class="w-full px-4 py-4 border border-gray-300 rounded-lg text-center text-2xl tracking-widest focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                           placeholder="0000">
                    <p id="otpError" class="text-red-500 text-sm mt-2 hidden"></p>
                </div>

                <button type="submit" id="otpVerifyBtn"
                        class="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-3 px-4 rounded-lg transition-colors flex items-center justify-center">
                    <span id="otpVerifyText">যাচাই করুন</span>
                    <span id="otpVerifyLoading" class="hidden ml-2">
                        <svg class="animate-spin h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
                            <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                            <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                        </svg>
                    </span>
                </button>
            </form>

            <div class="mt-6 text-center">
                <button type="button" id="resendOtpBtn"
                        class="text-blue-600 hover:text-blue-500 text-sm font-medium">
                    কোড পুনরায় পাঠান
                </button>
                <p id="resendTimer" class="text-sm text-gray-500 mt-1 hidden">
                    <span id="timerCount">60</span> সেকেন্ডের মধ্যে আবার পাঠানো যাবে
                </p>
            </div>
        </div>
    </div>

    <script>
    (function () {
        const form = document.getElementById('otpVerifyForm');
        const phoneInput = document.getElementById('otpPhone');
        const otpCodeInput = document.getElementById('otpCode');
        const verifyBtn = document.getElementById('otpVerifyBtn');
        const verifyText = document.getElementById('otpVerifyText');
        const verifyLoading = document.getElementById('otpVerifyLoading');
        const otpError = document.getElementById('otpError');
        const resendBtn = document.getElementById('resendOtpBtn');
        const resendTimer = document.getElementById('resendTimer');
        const timerCount = document.getElementById('timerCount');

        otpCodeInput.addEventListener('input', function (e) {
            e.target.value = e.target.value.replace(/[^0-9]/g, '');
        });

        form.addEventListener('submit', function (e) {
            e.preventDefault();
            
            const otp = otpCodeInput.value.trim();
            const phone = phoneInput.value;

            if (!otp || otp.length !== 4) {
                otpError.textContent = '৪-সংখ্যার সঠিক কোড লিখুন।';
                otpError.classList.remove('hidden');
                return;
            }

            verifyBtn.disabled = true;
            verifyText.textContent = 'যাচাই হচ্ছে...';
            verifyLoading.classList.remove('hidden');
            otpError.classList.add('hidden');

            fetch('{{ route("login.verify.otp") }}', {
                method: 'POST',
                headers: {
                    'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ phone: phone, otp: otp })
            })
            .then(r => r.json())
            .then(data => {
                if (data.success) {
                    window.location.href = data.redirect;
                } else {
                    otpError.textContent = data.message || 'সঠিক কোড নেই।';
                    otpError.classList.remove('hidden');
                }
            })
            .catch(() => {
                otpError.textContent = 'একটি সমস্যা হয়েছে। আবার চেষ্টা করুন।';
                otpError.classList.remove('hidden');
            })
            .finally(() => {
                verifyBtn.disabled = false;
                verifyText.textContent = 'যাচাই করুন';
                verifyLoading.classList.add('hidden');
            });
        });

        let timerInterval;
        function startResendTimer() {
            let timeLeft = 60;
            resendBtn.style.display = 'none';
            resendTimer.classList.remove('hidden');
            timerCount.textContent = timeLeft;
            clearInterval(timerInterval);
            timerInterval = setInterval(function () {
                timeLeft--;
                timerCount.textContent = timeLeft;
                if (timeLeft <= 0) {
                    clearInterval(timerInterval);
                    resendBtn.style.display = 'inline';
                    resendTimer.classList.add('hidden');
                }
            }, 1000);
        }

        resendBtn.addEventListener('click', function () {
            const phone = phoneInput.value;
            if (!phone) return;
            
            fetch('{{ route("login.resend.otp") }}', {
                method: 'POST',
                headers: {
                    'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ phone: phone })
            })
            .then(r => r.json())
            .then(data => {
                if (data.success) {
                    alert('নতুন কোড পাঠানো হয়েছে।');
                    startResendTimer();
                } else {
                    alert(data.message || 'পুনরায় পাঠাতে সমস্যা হয়েছে।');
                }
            })
            .catch(() => {
                alert('একটি সমস্যা হয়েছে। অনুগ্রহ করে আবার চেষ্টা করুন।');
            });
        });

        startResendTimer();
        setTimeout(function () { otpCodeInput.focus(); }, 100);
    })();
    </script>
</x-guest-layout>
