<?php
// تأكد من استدعاء السيسشن لو لم تكن مستدعاة داخل config.php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
include 'config.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
    $stmt->execute([$username]);
    $user = $stmt->fetch();

    if ($user && password_verify($password, $user['password'])) {
        $_SESSION['user_id'] = $user['id'];
        $_SESSION['username'] = $user['username'];
        $_SESSION['full_name'] = $user['full_name'];
        $_SESSION['branch_id'] = $user['branch_id'];
        header("Location: dashboard.php");
        exit();
    } else {
        $error = "بيانات الدخول غير صحيحة.. أعد المحاولة يا حارس البوابة";
    }
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>بوابة الدخول الفرعونية - <?= $sys_settings['system_name'] ?? 'النظام الملكي' ?></title>
    <!-- Bootstrap RTL -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
    <!-- Font Awesome للأيقونات -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <!-- خط Cairo المميز -->
    <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;700;900&display=swap" rel="stylesheet">
    
    <style>
        :root {
            --pharaoh-gold: #D4AF37;
            --pharaoh-dark-gold: #AA7C11;
            --pharaoh-black: #111111;
            --pharaoh-slate: #1e1e1e;
            --mint-green: #2ecc71; 
            --text-light: #ffffff;
        }
        
        body {
            font-family: 'Cairo', sans-serif;
            background-color: #121212;
            background-image: 
                linear-gradient(rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0.85)),
                radial-gradient(circle at 50% 50%, transparent 60%, #000 100%),
                linear-gradient(30deg, #1c1a17 12%, transparent 12.5%, transparent 87%, #1c1a17 87.5%, #1c1a17),
                linear-gradient(150deg, #1c1a17 12%, transparent 12.5%, transparent 87%, #1c1a17 87.5%, #1c1a17),
                linear-gradient(30deg, #1c1a17 61%, transparent 61.5%, transparent 85%, #1c1a17 85.5%, #1c1a17),
                linear-gradient(150deg, #1c1a17 61%, transparent 61.5%, transparent 85%, #1c1a17 85.5%, #1c1a17),
                linear-gradient(90deg, #181614 20%, transparent 20%, transparent 80%, #181614 80%, #181614),
                linear-gradient(0deg, #181614 20%, transparent 20%, transparent 80%, #181614 80%, #181614);
            background-size: 100% 100%, 100% 100%, 80px 140px, 80px 140px, 80px 140px, 80px 140px, 80px 140px, 80px 140px;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
            overflow: hidden;
            color: var(--text-light);
        }

        /* --- تأثير الشعلات الفرعونية الجانبية --- */
        .pharaoh-torch {
            position: fixed;
            top: 50%;
            transform: translateY(-50%);
            width: 60px;
            height: 180px;
            z-index: 2;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .torch-left { left: 5%; }
        .torch-right { right: 5%; }

        /* حركات تذبذب وتوهج اللهب */
        .fire-glow {
            position: relative;
            width: 35px;
            height: 55px;
            background: linear-gradient(to top, rgba(255, 60, 0, 0) 0%, rgba(255, 60, 0, 0.8) 20%, rgba(255, 150, 0, 0.9) 60%, rgba(255, 230, 100, 1) 100%);
            border-radius: 50% 50% 35% 35% / 60% 60% 40% 40%;
            filter: blur(1px);
            animation: flicker 1.5s ease-in-out infinite alternate;
            box-shadow: 0 -10px 20px rgba(255, 60, 0, 0.5), 0 -20px 40px rgba(255, 150, 0, 0.3), 0 10px 20px rgba(214, 175, 55, 0.2);
        }
        
        .fire-glow::before {
            content: '';
            position: absolute;
            bottom: 0; left: 15%; width: 70%; height: 75%;
            background: linear-gradient(to top, rgba(255, 100, 0, 0) 0%, rgba(255, 200, 0, 0.9) 50%, #ffffff 100%);
            border-radius: 50% 50% 35% 35% / 60% 60% 40% 40%;
            animation: flicker 1.1s ease-in-out infinite alternate-reverse;
        }

        /* حامل الشعلة الكأس وبدن الحامل */
        .torch-cup {
            width: 40px;
            height: 18px;
            background: linear-gradient(180deg, var(--pharaoh-gold), #44320a);
            border-radius: 4px 4px 50% 50%;
            border-top: 3px solid var(--pharaoh-black);
            box-shadow: 0 4px 8px rgba(0,0,0,0.6);
            z-index: 3;
        }

        .torch-holder {
            width: 12px;
            height: 70px;
            background: linear-gradient(90deg, var(--pharaoh-dark-gold), var(--pharaoh-gold), var(--pharaoh-dark-gold));
            border-radius: 2px;
            position: relative;
            box-shadow: 0 5px 15px rgba(0,0,0,0.8);
        }

        .torch-mount {
            width: 26px;
            height: 26px;
            background: #222;
            border: 2px solid var(--pharaoh-gold);
            border-radius: 50%;
            position: absolute;
            bottom: 10px;
            left: 50%;
            transform: translateX(-50%);
            z-index: -1;
        }

        @keyframes flicker {
            0% { transform: scale(0.95) rotate(-1deg); opacity: 0.9; }
            40% { transform: scale(1.03) rotate(1deg); }
            70% { transform: scale(0.98) rotate(-0.5deg); opacity: 1; }
            100% { transform: scale(1.05) rotate(0.5deg); }
        }

        /* إخفاء الشعلات في الشاشات الصغيرة للمحافظة على المظهر المريح للمستخدم */
        @media (max-width: 850px) {
            .pharaoh-torch { display: none; }
        }

        /* كارت تسجيل الدخول الفرعوني المطور */
        .login-card {
            background: rgba(15, 15, 15, 0.92);
            backdrop-filter: blur(20px);
            border-radius: 20px;
            border: 2px solid var(--pharaoh-gold);
            width: 100%;
            max-width: 440px;
            padding: 40px;
            box-shadow: 0 0 35px rgba(212, 175, 55, 0.2), inset 0 0 15px rgba(212, 175, 55, 0.05);
            transform: translateY(0);
            transition: all 0.4s ease;
            position: relative;
            z-index: 5;
        }

        .login-card:hover { 
            transform: translateY(-5px); 
            box-shadow: 0 0 45px rgba(212, 175, 55, 0.35);
        }

        .form-label {
            color: var(--pharaoh-gold) !important;
            font-size: 0.95rem;
            text-shadow: 1px 1px 2px rgba(0,0,0,0.8);
        }

        .form-control-custom {
            background-color: var(--pharaoh-slate) !important;
            border: 1px solid #444 !important;
            color: #ffffff !important; 
            border-radius: 10px;
            padding: 14px;
            transition: 0.3s;
        }
        
        .form-control-custom::placeholder {
            color: #b0b0b0 !important;
            opacity: 1; 
        }

        .form-control-custom:focus {
            border-color: var(--mint-green) !important;
            box-shadow: 0 0 12px rgba(46, 204, 113, 0.4) !important;
            background-color: #252525 !important;
        }

        .input-group-text-custom {
            background-color: #2a2a2a !important;
            border: 1px solid #444 !important;
            color: var(--pharaoh-gold) !important; 
        }

        .btn-login {
            background: linear-gradient(135deg, var(--pharaoh-gold) 0%, var(--pharaoh-dark-gold) 100%);
            color: #000000 !important;
            border: none;
            padding: 14px;
            border-radius: 12px;
            font-weight: 900;
            font-size: 1.1rem;
            transition: 0.3s ease;
        }

        .btn-login:hover {
            background: linear-gradient(135deg, #ffffff 0%, var(--pharaoh-gold) 100%);
            box-shadow: 0 0 25px rgba(212, 175, 55, 0.7);
            transform: scale(1.02);
        }

        /* --- شاشة اللودر الفرعونية المحدثة --- */
        #pharaoh-loader {
            display: none;
            position: fixed; top: 0; left: 0; width: 100%; height: 100%;
            background: rgba(10, 10, 10, 0.97);
            z-index: 10000;
            flex-direction: column;
            justify-content: center; align-items: center;
        }

        .statue-wrapper {
            position: relative;
            width: 160px;
            height: 200px;
            display: flex;
            justify-content: center;
        }

        .statue-half {
            position: absolute;
            width: 80px;
            height: 180px;
            background: var(--pharaoh-gold);
            transition: all 0.8s cubic-bezier(0.19, 1, 0.22, 1);
            box-shadow: inset 0 0 20px rgba(0,0,0,0.6), 0 0 30px rgba(212,175,55,0.3);
        }

        .left-s {
            left: 0;
            border-radius: 80px 0 0 120px;
            background: linear-gradient(90deg, #ffd700, var(--pharaoh-dark-gold));
            border-left: 8px solid #111;
            background-image: repeating-linear-gradient(45deg, transparent, transparent 10px, #111 10px, #111 20px);
            transform: translateX(-200px) rotate(-45deg);
            opacity: 0;
        }

        .right-s {
            right: 0;
            border-radius: 0 80px 120px 0;
            background: linear-gradient(270deg, #ffd700, var(--pharaoh-dark-gold));
            border-right: 8px solid #111;
            background-image: repeating-linear-gradient(-45deg, transparent, transparent 10px, #111 10px, #111 20px);
            transform: translateX(200px) rotate(45deg);
            opacity: 0;
        }

        .merging .left-s { 
            transform: translateX(0px) rotate(0deg); 
            opacity: 1;
        }
        .merging .right-s { 
            transform: translateX(0px) rotate(0deg); 
            opacity: 1;
        }

        .statue-wrapper::after {
            content: '';
            position: absolute;
            bottom: -15px;
            width: 24px;
            height: 45px;
            background: repeating-linear-gradient(0deg, #111, #111 5px, var(--pharaoh-gold) 5px, var(--pharaoh-gold) 10px);
            border-radius: 0 0 5px 5px;
            opacity: 0;
            transition: 0.5s ease 0.6s;
            box-shadow: 0 5px 10px rgba(0,0,0,0.5);
        }
        .merging::after { opacity: 1; }

        .loading-msg {
            margin-top: 50px;
            font-weight: 700;
            font-size: 1.3rem;
            color: var(--pharaoh-gold);
            text-shadow: 0 0 10px rgba(212, 175, 55, 0.5);
            animation: pulse-gold 1.5s infinite;
        }

        @keyframes pulse-gold {
            0% { opacity: 0.5; transform: scale(0.98); }
            50% { opacity: 1; transform: scale(1); color: var(--mint-green); }
            100% { opacity: 0.5; transform: scale(0.98); }
        }
    </style>
</head>
<body>

    <!-- الشعلة الجانبية اليسرى -->
    <div class="pharaoh-torch torch-left">
        <div class="fire-glow"></div>
        <div class="torch-cup"></div>
        <div class="torch-holder">
            <div class="torch-mount"></div>
        </div>
    </div>

    <!-- الشعلة الجانبية اليمنى -->
    <div class="pharaoh-torch torch-right">
        <div class="fire-glow"></div>
        <div class="torch-cup"></div>
        <div class="torch-holder">
            <div class="torch-mount"></div>
        </div>
    </div>

    <!-- لودر المعبد الفرعوني والتمثال الملكي المضمون -->
    <div id="pharaoh-loader">
        <div class="statue-wrapper" id="statueObj">
            <div class="statue-half left-s"></div>
            <div class="statue-half right-s"></div>
        </div>
        <div class="loading-msg"><i class="fa-solid fa-ankh me-2"></i> جاري فتح الحجرات الملكية للنظام...</div>
    </div>

    <!-- كارت تسجيل الدخول الفرعوني المحسن -->
    <div class="login-card">
        <div class="text-center mb-4">
            <div class="mb-2" style="font-size: 3rem; color: var(--pharaoh-gold);">
                <i class="fa-solid fa-pyramid"></i>
            </div>
            <h2 style="color: var(--pharaoh-gold); font-weight: 900; letter-spacing: 1px;">لوحة الإدارة والمبيعات</h2>
            <p style="color: #b0b0b0 !important;" class="small">بوابة الوصول الآمنة للمحراب الرقمي</p>
        </div>
        
        <?php if(isset($error)): ?> 
            <div class="alert alert-danger py-2 small border-0 text-center bg-danger bg-opacity-25 text-white">
                <i class="fa-solid fa-triangle-exclamation me-1"></i> <?= $error ?>
            </div> 
        <?php endif; ?>

        <form id="posForm" method="POST">
            <div class="mb-3">
                <label class="form-label fw-bold">اسم المستخدم</label>
                <div class="input-group">
                    <span class="input-group-text input-group-text-custom"><i class="fa-solid fa-user-shield"></i></span>
                    <input type="text" name="username" class="form-control form-control-lg form-control-custom" placeholder="أدخل اسم المستخدم الحقيقي" required style="font-size: 1rem;">
                </div>
            </div>
            <div class="mb-4">
                <label class="form-label fw-bold">كلمة المرور الفرعونية</label>
                <div class="input-group">
                    <span class="input-group-text input-group-text-custom"><i class="fa-solid fa-key"></i></span>
                    <input type="password" name="password" class="form-control form-control-lg form-control-custom" placeholder="••••••••" required style="font-size: 1rem;">
                </div>
            </div>
            <button type="submit" class="btn btn-login w-100 shadow-sm">
                <i class="fa-solid fa-dungeon me-1"></i> دخول المحراب الملكي
            </button>
        </form>
    </div>

    <script>
        document.getElementById('posForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const loader = document.getElementById('pharaoh-loader');
            const statueObj = document.getElementById('statueObj');
            
            loader.style.display = 'flex';
            
            // تفعيل أنيميشن التقاء نصفي قناع توت عنخ آمون
            setTimeout(() => {
                statueObj.classList.add('merging');
            }, 150);

            // تحويل البيانات بعد انتهاء الأنميشن بـ 2.2 ثانية
            setTimeout(() => {
                this.submit();
            }, 2200);
        });
    </script>
</body>
</html>