@extends('layout')

@section('title', __('store.auth_verify_phone_title'))

@section('content')
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-6 col-lg-5">
            <div class="card shadow">
                <div class="card-body p-4 text-center">
                    <div class="mb-4">
                        <i class="fas fa-mobile-alt fa-4x text-success mb-3"></i>
                        <h2>{{ __('store.auth_verify_phone_title') }}</h2>
                    </div>

                    @if(session('success'))
                        <div class="alert alert-success">
                            {{ session('success') }}
                        </div>
                    @endif

                    @if($errors->any())
                        <div class="alert alert-danger">
                            <ul class="mb-0">
                                @foreach($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <div class="mb-4">
                        <p class="text-muted">
                            {{ __('store.auth_verify_phone_description') }}
                            @if(Auth::guard('client')->user()->phone)
                                <strong>{{ Auth::guard('client')->user()->phone }}</strong>
                            @endif
                        </p>
                    </div>

                    
                    @if(!session('success') || !str_contains(session('success'), __('store.auth_verification_code_sent')))
                    <form method="POST" action="{{ route('store.auth.send-phone-verification') }}" class="mb-4">
                        @csrf
                        <div class="d-grid">
                            <button type="submit" class="btn btn-outline-primary btn-lg">
                                <i class="fas fa-paper-plane me-2"></i>
                                {{ __('store.auth_send_verification_code') }}
                            </button>
                        </div>
                    </form>
                    @endif

                    
                    <form method="POST" action="{{ route('store.auth.verify-phone') }}">
                        @csrf
                        <div class="mb-3">
                            <label for="code" class="form-label">{{ __('store.auth_verification_code') }}</label>
                            <input type="text" class="form-control text-center @error('code') is-invalid @enderror"
                                   id="code" name="code" placeholder="123456" maxlength="6" style="font-size: 1.5rem; letter-spacing: 0.5rem;" required>
                            @error('code')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                            <div class="form-text">{{ __('store.auth_enter_6_digit_code') }}</div>
                        </div>

                        <div class="d-grid mb-3">
                            <button type="submit" class="btn btn-success btn-lg">
                                <i class="fas fa-check-circle me-2"></i>
                                {{ __('store.auth_verify_phone') }}
                            </button>
                        </div>
                    </form>

                    <div class="text-center">
                        <p class="mb-2 text-muted">{{ __('store.auth_didnt_receive_code') }}</p>
                        <form method="POST" action="{{ route('store.auth.send-phone-verification') }}" class="d-inline">
                            @csrf
                            <button type="submit" class="btn btn-outline-secondary">
                                <i class="fas fa-redo me-1"></i>
                                {{ __('store.auth_resend_code') }}
                            </button>
                        </form>
                    </div>

                    <div class="mt-4">
                        <form method="POST" action="{{ route('store.auth.logout') }}" class="d-inline">
                            @csrf
                            <button type="submit" class="btn btn-link p-0 text-muted text-decoration-none">
                                <i class="fas fa-sign-out-alt me-1"></i>
                                {{ __('store.auth_logout') }}
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Auto-focus verification code input
    const codeInput = document.getElementById('code');
    if (codeInput) {
        codeInput.focus();

        // Auto-format input (only numbers)
        codeInput.addEventListener('input', function(e) {
            let value = e.target.value.replace(/\D/g, '').substring(0, 6);
            e.target.value = value;
        });
    }
});
</script>
@endpush
@endsection