<!DOCTYPE html>
<html lang="ar" dir="rtl" data-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>تسجيل الدخول | مُصادَق</title>
    <link rel="icon" type="image/jpeg" href="assets/img/logo.jpg">
    
    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+Arabic:wght@300;400;500;600;700&family=IBM+Plex+Mono:wght@400;500&display=swap" rel="stylesheet">
    
    <!-- Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    
    <!-- Alpine.js -->
    <script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>

    <style>
        :root {
            --emerald: #10b981;
            --bg-base: #080c14;
            --bg-surface: #0d1424;
            --border-default: rgba(255,255,255,0.10);
            --text-primary: #f0f6fc;
            --text-secondary: #8b949e;
        }

        body {
            font-family: 'IBM Plex Sans Arabic', sans-serif;
            background-color: var(--bg-base);
            color: var(--text-primary);
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
            overflow: hidden;
        }

        .login-card {
            background-color: var(--bg-surface);
            border: 1px solid var(--border-default);
            padding: 3rem;
            width: 100%;
            max-width: 450px;
            border-radius: 8px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
        }

        .input-field {
            background-color: #05080f;
            border: 1px solid var(--border-default);
            color: white;
            width: 100%;
            padding: 0.75rem 1rem;
            border-radius: 4px;
            outline: none;
            transition: border-color 0.2s;
        }

        .input-field:focus {
            border-color: var(--emerald);
        }

        .btn-primary {
            background-color: var(--emerald);
            color: #000;
            width: 100%;
            padding: 0.75rem;
            border-radius: 4px;
            font-weight: 700;
            transition: opacity 0.2s;
        }

        .btn-primary:hover {
            opacity: 0.9;
        }

        .glow {
            position: absolute;
            width: 500px;
            height: 500px;
            background: radial-gradient(circle, rgba(16, 185, 129, 0.05) 0%, rgba(0, 0, 0, 0) 70%);
            z-index: -1;
        }
    </style>
</head>
<body x-data="loginForm">
    <div class="glow"></div>
    
    <div class="login-card">
        <div class="text-center mb-10">
            <div class="w-20 h-20 bg-emerald-500 rounded-2xl flex items-center justify-center overflow-hidden mx-auto mb-4 shadow-lg shadow-emerald-500/20">
                <img src="assets/img/logo.jpg" alt="Musadaq Logo" class="w-full h-full object-cover">
            </div>
            <h1 class="text-2xl font-bold mb-2">مرحباً بك في مُصادَق</h1>
            <p class="text-sm text-gray-500">نظام أتمتة الفواتير الضريبية الذكي</p>
        </div>

        <form @submit.prevent="submitLogin">
            <div class="mb-6">
                <label class="block text-xs font-bold uppercase tracking-widest text-gray-500 mb-2">البريد الإلكتروني</label>
                <input type="email" x-model="email" class="input-field" placeholder="name@company.com" required>
            </div>

            <div class="mb-8">
                <label class="block text-xs font-bold uppercase tracking-widest text-gray-500 mb-2">كلمة المرور</label>
                <input type="password" x-model="password" class="input-field" placeholder="••••••••" required>
            </div>

            <template x-if="error">
                <div class="mb-6 p-3 bg-red-950/50 border border-red-500/50 text-red-200 text-sm rounded" x-text="error"></div>
            </template>

            <button type="submit" class="btn-primary flex items-center justify-center gap-2" :disabled="loading">
                <span x-show="loading" class="animate-spin text-lg">↻</span>
                <span x-text="loading ? 'جاري التحقق...' : 'دخول النظام ↵'"></span>
            </button>
        </form>

        <div class="mt-8 pt-8 border-t border-gray-800 text-center">
            <p class="text-sm text-gray-500">ليس لديك حساب؟ <a href="#" class="text-emerald-500 hover:underline">ابدأ التجربة المجانية</a></p>
        </div>
    </div>

    <script>
        document.addEventListener('alpine:init', () => {
            Alpine.data('loginForm', () => ({
                email: '',
                password: '',
                error: '',
                loading: false,

                async submitLogin() {
                    this.loading = true;
                    this.error = '';
                    
                    try {
                        const response = await fetch('/api/v1/auth/login', {
                            method: 'POST',
                            headers: { 'Content-Type': 'application/json' },
                            body: JSON.stringify({
                                email: this.email,
                                password: this.password
                            })
                        });

                        const result = await response.json();

                        if (result.success) {
                            localStorage.setItem('access_token', result.data.access_token);
                            localStorage.setItem('user', JSON.stringify(result.data.user));
                            window.location.href = '/';
                        } else {
                            this.error = result.message || 'خطأ في تسجيل الدخول';
                        }
                    } catch (e) {
                        this.error = 'حدث خطأ في الاتصال بالخادم';
                    } finally {
                        this.loading = false;
                    }
                }
            }));
        });
    </script>
</body>
</html>
