<?php
require_once 'includes/functions.php';
require_once 'includes/config.php';

if (isLoggedIn()) {
    redirect('dashboard.php');
}

$error = '';

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

    if (empty($username) || empty($password)) {
        $error = 'الرجاء إدخال اسم المستخدم وكلمة المرور';
    } else {
        global $db;
        $user = $db->fetchOne("SELECT * FROM users WHERE username = ?", [$username]);

        // مثال للتحقق
        if ($username == "Ahmed" && $password === "123456") {
            $_SESSION['user_id'] = 1;
            $_SESSION['username'] = $username;
            $_SESSION['role'] = 'admin';
            redirect('dashboard.php');
        } 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>تسجيل الدخول - <?= APP_NAME ?></title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;700&display=swap" rel="stylesheet">
<style>
/* Reset & Global Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Tajawal', sans-serif;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    background: url('https://media.istockphoto.com/id/1200486875/photo/different-computers-in-store-background.webp?a=1&b=1&s=612x612&w=0&k=20&c=EGWhD_98EF45z-Rt-7F4Xj0i4XlByqWjbmV3nZPy-Ys=') no-repeat center center fixed;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
}

/* Overlay Effect */
body::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, rgba(41, 127, 185, 0.76) 0%, rgba(44, 62, 80, 0.9) 100%);
    z-index: 0;
}

/* Login Container */
.login-container {
    width: 100%;
    max-width: 420px;
    background: rgba(255, 255, 255, 1);
    border-radius: 16px;
    padding: 50px 40px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
    text-align: center;
    position: relative;
    z-index: 1;
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.25);
    transform: scale(0.95);
    opacity: 0;
    animation: fadeInUp 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* Logo Design */
.logo {
    width: 80px;
    height: 80px;
    margin: 0 auto 25px;
    background: linear-gradient(135deg, #3498db, #2980b9);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 5px 15px rgba(41, 128, 185, 0.4);
    border: 3px solid white;
}

.logo i {
    color: white;
    font-size: 36px;
}

/* Header */
.login-container h2 {
    font-size: 28px;
    margin-bottom: 10px;
    color: #2c3e50;
    font-weight: 700;
}

.login-container p {
    color: #7f8c8d;
    margin-bottom: 30px;
    font-size: 15px;
}

/* Alerts */
.alert {
    padding: 12px 15px;
    border-radius: 10px;
    margin-bottom: 25px;
    text-align: center;
    font-weight: 500;
    font-size: 14px;
    border: 1px solid transparent;
}

.alert-danger {
    background-color: rgba(231, 76, 60, 0.1);
    color: #e74c3c;
    border-color: rgba(231, 76, 60, 0.2);
}

/* Input Groups */
.input-group {
    position: relative;
    margin-bottom: 25px;
}

.input-group input {
    width: 100%;
    padding: 14px 20px;
    padding-right: 45px;
    border: 1px solid #e0e0e0;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.9);
    color: #2c3e50;
    font-size: 15px;
    outline: none;
    transition: all 0.3s ease;
}

.input-group input:focus {
    border-color: #3498db;
    box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
    background: white;
}

.input-group label {
    position: absolute;
    right: 15px;
    top: 50%;
    transform: translateY(-50%);
    color: #95a5a6;
    pointer-events: none;
    transition: all 0.3s ease;
    background: white;
    padding: 0 5px;
    border-radius: 4px;
}

.input-group input:focus + label,
.input-group input:not(:placeholder-shown) + label {
    top: 0;
    font-size: 12px;
    color: #3498db;
    transform: translateY(-50%);
    right: 10px;
    background: white;
}

.input-group i {
    position: absolute;
    left: 15px;
    top: 50%;
    transform: translateY(-50%);
    color: #bdc3c7;
    transition: 0.3s;
}

.input-group input:focus ~ i {
    color: #3498db;
}

/* Button */
button {
    width: 100%;
    padding: 15px;
    border: none;
    border-radius: 10px;
    background: linear-gradient(135deg, #3498db, #2980b9);
    color: white;
    font-weight: 600;
    font-size: 16px;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(41, 128, 185, 0.3);
    transition: all 0.3s ease;
    margin-top: 10px;
}

button:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(41, 128, 185, 0.4);
    background: linear-gradient(135deg, #2980b9, #3498db);
}

button:active {
    transform: translateY(0);
    box-shadow: 0 4px 10px rgba(41, 128, 185, 0.3);
}

/* Footer Links */
.login-footer {
    margin-top: 25px;
    font-size: 14px;
    color: #95a5a6;
}

.login-footer a {
    color: #3498db;
    text-decoration: none;
    transition: 0.3s;
}

.login-footer a:hover {
    color: #2980b9;
    text-decoration: underline;
}

/* Responsive Design */
@media (max-width: 480px) {
    .login-container {
        padding: 40px 25px;
        width: 90%;
        margin: 0 auto;
    }
    
    .logo {
        width: 70px;
        height: 70px;
    }
}
</style>
</head>
<body>
<div class="login-container">
    <!-- Logo Design -->
    <div class="logo">
       
        <i class="fas fa-lock"></i>
    </div>
    
    <h2>تسجيل الدخول</h2>
    <p>مرحباً بك مرة أخرى! الرجاء إدخال بياناتك</p>

    <?php if ($error): ?>
        <div class="alert alert-danger"><?= $error ?></div>
    <?php endif; ?>

    <form method="POST" action="">
        <div class="input-group">
            <input type="text" id="username" name="username" placeholder=" " required>
            <label for="username">اسم المستخدم</label>
            <i class="fas fa-user"></i>
        </div>
        
        <div class="input-group">
            <input type="password" id="password" name="password" placeholder=" " required>
            <label for="password">كلمة المرور</label>
            <i class="fas fa-lock"></i>
        </div>
        
        <button type="submit">
            <i class="fas fa-sign-in-alt"></i> دخول
        </button>
    </form>
    
    <div class="login-footer">
        <a href="forgot-password.php">نسيت كلمة المرور؟</a> | 
        <a href="register.php">إنشاء حساب جديد</a>
    </div>
</div>

<script>
// Frontend Validation
const form = document.querySelector('form');
form.addEventListener('submit', e => {
    const username = document.getElementById('username');
    const password = document.getElementById('password');
    
    if(username.value.trim() === '' || password.value.trim() === '') {
        e.preventDefault();
        
        if(username.value.trim() === '') {
            username.style.borderColor = '#e74c3c';
            username.nextElementSibling.style.color = '#e74c3c';
        }
        
        if(password.value.trim() === '') {
            password.style.borderColor = '#e74c3c';
            password.nextElementSibling.style.color = '#e74c3c';
        }
    }
});

// Reset error styles on focus
document.querySelectorAll('input').forEach(input => {
    input.addEventListener('focus', () => {
        input.style.borderColor = '#e0e0e0';
        input.nextElementSibling.style.color = '#95a5a6';
    });
});
</script>
</body>
</html>