<?php
session_start();

// إذا كان مسجل دخول بالفعل، وجهه للوحة التحكم
if (isset($_SESSION['user_id']) && isset($_SESSION['role']) && $_SESSION['role'] === 'admin') {
    header('Location: index.php');
    exit;
}

require_once '../config/database.php';
require_once '../models/user.php';

$database = new Database();
$db = $database->getConnection();

$error = '';
$success = '';

// معالجة تسجيل الدخول
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $identifier = $_POST['identifier'] ?? ''; // يمكن أن يكون بريد أو رقم هاتف
    $password = $_POST['password'] ?? '';
    
    if (empty($identifier) || empty($password)) {
        $error = 'يرجى إدخال البريد الإلكتروني/رقم الهاتف وكلمة المرور';
    } else {
        $user_model = new User($db);
        
        // التحقق إذا كان المدخل رقم هاتف أو بريد إلكتروني
        $is_phone = preg_match('/^[0-9+\-\s()]+$/', $identifier);
        
        if ($is_phone) {
            // تنظيف رقم الهاتف
            $phone = preg_replace('/[^0-9+]/', '', $identifier);
            
            // البحث بالهاتف
            $query = "SELECT * FROM users WHERE phone = :phone AND role = 'admin' LIMIT 1";
            $stmt = $db->prepare($query);
            $stmt->bindParam(':phone', $phone);
            $stmt->execute();
            $user = $stmt->fetch(PDO::FETCH_ASSOC);
            
            if ($user && password_verify($password, $user['password'])) {
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['user_name'] = $user['name'];
                $_SESSION['user_email'] = $user['email'];
                $_SESSION['user_phone'] = $user['phone'];
                $_SESSION['role'] = $user['role'];
                
                header('Location: index.php');
                exit;
            } else {
                $error = 'بيانات الدخول غير صحيحة أو ليس لديك صلاحيات الأدمن';
            }
        } else {
            // تسجيل دخول بالبريد الإلكتروني (الطريقة القديمة)
            $user = $user_model->login($identifier, $password);
            
            if ($user && $user['role'] === 'admin') {
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['user_name'] = $user['name'];
                $_SESSION['user_email'] = $user['email'];
                $_SESSION['user_phone'] = $user['phone'] ?? '';
                $_SESSION['role'] = $user['role'];
                
                header('Location: index.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>تسجيل الدخول - لوحة التحكم</title>
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Tajawal', sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        
        .login-container {
            background: #1e293b;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.5);
            padding: 40px;
            width: 100%;
            max-width: 450px;
            border: 1px solid rgba(255,255,255,0.1);
        }
        
        .logo {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .logo h1 {
            font-size: 32px;
            color: #60a5fa;
            margin-bottom: 10px;
        }
        
        .logo p {
            color: #94a3b8;
            font-size: 16px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #e2e8f0;
            font-weight: 500;
        }
        
        .form-group input {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #334155;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s;
            background: #0f172a;
            color: #e2e8f0;
        }
        
        .form-group input::placeholder {
            color: #64748b;
        }
        
        .form-group input:focus {
            outline: none;
            border-color: #3b82f6;
            box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
        }
        
        .btn-login {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.2s;
        }
        
        .btn-login:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }
        
        .btn-login:active {
            transform: translateY(0);
        }
        
        .alert {
            padding: 12px 16px;
            border-radius: 10px;
            margin-bottom: 20px;
            font-size: 14px;
        }
        
        .alert-error {
            background: rgba(239, 68, 68, 0.1);
            color: #fca5a5;
            border: 1px solid rgba(239, 68, 68, 0.3);
        }
        
        .alert-success {
            background: rgba(16, 185, 129, 0.1);
            color: #6ee7b7;
            border: 1px solid rgba(16, 185, 129, 0.3);
        }
        
        .back-link {
            text-align: center;
            margin-top: 20px;
        }
        
        .back-link a {
            color: #60a5fa;
            text-decoration: none;
            font-weight: 500;
        }
        
        .back-link a:hover {
            text-decoration: underline;
        }
        
        .demo-credentials {
            background: #0f172a;
            border-radius: 10px;
            padding: 15px;
            margin-top: 20px;
            font-size: 14px;
            border: 1px solid #334155;
        }
        
        .demo-credentials h3 {
            color: #60a5fa;
            margin-bottom: 10px;
            font-size: 16px;
        }
        
        .demo-credentials p {
            color: #94a3b8;
            margin: 5px 0;
        }
        
        .demo-credentials code {
            background: #1e293b;
            padding: 2px 8px;
            border-radius: 4px;
            color: #e2e8f0;
            font-family: monospace;
            border: 1px solid #334155;
        }
        
        .input-icon {
            position: relative;
        }
        
        .input-icon i {
            position: absolute;
            right: 16px;
            top: 50%;
            transform: translateY(-50%);
            color: #64748b;
        }
        
        .input-icon input {
            padding-right: 45px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="logo">
            <h1>🌸 Roz Skin</h1>
            <p>لوحة التحكم</p>
        </div>
        
        <?php if ($error): ?>
            <div class="alert alert-error">
                ⚠️ <?php echo htmlspecialchars($error); ?>
            </div>
        <?php endif; ?>
        
        <?php if ($success): ?>
            <div class="alert alert-success">
                ✓ <?php echo htmlspecialchars($success); ?>
            </div>
        <?php endif; ?>
        
        <form method="POST" action="">
            <div class="form-group">
                <label for="identifier">📧 البريد الإلكتروني أو 📱 رقم الهاتف</label>
                <div class="input-icon">
                    <i class="fas fa-user"></i>
                    <input type="text" 
                           id="identifier" 
                           name="identifier" 
                           placeholder="admin@example.com أو 01234567890"
                           required
                           value="<?php echo isset($_POST['identifier']) ? htmlspecialchars($_POST['identifier']) : ''; ?>">
                </div>
            </div>
            
            <div class="form-group">
                <label for="password">🔒 كلمة المرور</label>
                <div class="input-icon">
                    <i class="fas fa-lock"></i>
                    <input type="password" 
                           id="password" 
                           name="password" 
                           placeholder="••••••••"
                           required>
                </div>
            </div>
            
            <button type="submit" class="btn-login">
                🚀 تسجيل الدخول
            </button>
        </form>
        
        <div class="demo-credentials">
            <h3>🔑 بيانات الدخول التجريبية</h3>
            <p><strong>البريد:</strong> <code>admin@example.com</code></p>
            <p><strong>الهاتف:</strong> <code>01234567890</code></p>
            <p><strong>كلمة المرور:</strong> <code>admin123</code></p>
        </div>
        
        <div class="back-link">
            <a href="../public/">← العودة للموقع</a>
        </div>
    </div>
</body>
</html>
