<!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>
    
    <!-- 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;
        }

        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;
            min-height: 100vh;
            margin: 0;
            padding: 2rem 0;
        }

        .login-card {
            background-color: var(--bg-surface);
            border: 1px solid var(--border-default);
            padding: 2.5rem;
            width: 100%;
            max-width: 500px;
            border-radius: 8px;
        }

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

        .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;
        }
    </style>
</head>
<body x-data="registerForm">
    <div class="login-card">
        <div class="text-center mb-8">
            <h1 class="text-2xl font-bold mb-2">ابدأ مع مُصادَق</h1>
            <p class="text-sm text-gray-500">سجل شركتك الآن وابدأ أتمتة فواتيرك</p>
        </div>

        <form @submit.prevent="submitRegister">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-4">
                <div>
                    <label class="block text-xs font-bold text-gray-500 mb-2 uppercase">اسم الشركة</label>
                    <input type="text" x-model="tenant_name" class="input-field" placeholder="شركة المثال" required>
                </div>
                <div>
                    <label class="block text-xs font-bold text-gray-500 mb-2 uppercase">اسم المسؤول</label>
                    <input type="text" x-model="user_name" class="input-field" placeholder="حمزة" required>
                </div>
            </div>

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

            <div class="mb-6">
                <label class="block text-xs font-bold text-gray-500 mb-2 uppercase">كلمة المرور</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" :disabled="loading">
                <span x-text="loading ? 'جاري إنشاء الحساب...' : 'إنشاء الحساب والبدء ↵'"></span>
            </button>
        </form>

        <div class="mt-8 pt-8 border-t border-gray-800 text-center text-sm">
            <p class="text-gray-500">لديك حساب بالفعل؟ <a href="/login.php" class="text-emerald-500 hover:underline">تسجيل الدخول</a></p>
        </div>
    </div>

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

                async submitRegister() {
                    this.loading = true;
                    this.error = '';
                    
                    try {
                        const response = await fetch('/api/v1/auth/register', {
                            method: 'POST',
                            headers: { 'Content-Type': 'application/json' },
                            body: JSON.stringify({
                                tenant_name: this.tenant_name,
                                user_name: this.user_name,
                                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>
