<?php
require_once 'config.php';

if (isset($_SESSION['user_id'])) {
    header('Location: dashboard.php');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (function_exists('login_attempts_check')) {
        $rl = login_attempts_check();
        if (!$rl['allowed']) {
            $error = $rl['message'];
        }
    }
    if (empty($error)) {
        $username = trim($_POST['username'] ?? '');
        $password = $_POST['password'] ?? '';
        
        if (empty($username) || empty($password)) {
            $error = 'يرجى إدخال اسم المستخدم وكلمة المرور';
        } else {
            $stmt = $conn->prepare("SELECT id, password_hash, is_admin, can_edit, can_delete, can_export FROM users WHERE username = ?");
            $stmt->bind_param("s", $username);
            $stmt->execute();
            $result = $stmt->get_result();
            
            if ($row = $result->fetch_assoc()) {
                if (password_verify($password, $row['password_hash'])) {
                    if (function_exists('login_attempt_success')) login_attempt_success();
                    session_regenerate_id(true);
                    $_SESSION['user_id'] = $row['id'];
                    $_SESSION['username'] = $username;
                    if (function_exists('logActivity')) logActivity('login', 'auth', null, 'تسجيل دخول', $row['id']);
                    $_SESSION['is_admin'] = !empty($row['is_admin']);
                    $_SESSION['can_edit'] = !empty($row['can_edit']);
                    $_SESSION['can_delete'] = !empty($row['can_delete']);
                    $_SESSION['can_export'] = !empty($row['can_export']);
                    header('Location: dashboard.php');
                    exit;
                }
            }
            if (function_exists('login_attempt_fail')) login_attempt_fail();
            $error = 'اسم المستخدم أو كلمة المرور غير صحيحة';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <?php include 'includes/favicon.php'; ?>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>تسجيل الدخول - نظام صيانة الأجهزة</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+Arabic:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        * { font-family: 'IBM Plex Sans Arabic', sans-serif; }
        body {
            min-height: 100vh;
            background: linear-gradient(135deg, #0f172a 0%, #1e293b 40%, #334155 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 2rem;
            position: relative;
            overflow: hidden;
        }
        body::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -30%;
            width: 80%;
            height: 150%;
            background: radial-gradient(ellipse, rgba(59, 130, 246, 0.15) 0%, transparent 60%);
        }
        .login-wrapper {
            position: relative;
            z-index: 1;
            width: 100%;
            max-width: 440px;
            animation: fadeInUp 0.5s ease;
        }
        @keyframes fadeInUp {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .login-card {
            background: #fff;
            border-radius: 24px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
            overflow: hidden;
            border: 1px solid rgba(255,255,255,0.1);
        }
        .login-header {
            background: linear-gradient(135deg, #1e40af 0%, #2563eb 100%);
            color: white;
            padding: 2.5rem 2rem;
            text-align: center;
        }
        .login-header .icon-wrap {
            width: 70px;
            height: 70px;
            background: rgba(255,255,255,0.2);
            border-radius: 18px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 1rem;
            font-size: 2rem;
        }
        .login-header h1 { font-size: 1.5rem; font-weight: 700; margin-bottom: 0.25rem; }
        .login-header p { opacity: 0.9; font-size: 0.85rem; }
        .login-body { padding: 2rem 2rem 2.5rem; }
        .login-body .form-control {
            border-radius: 12px;
            padding: 0.85rem 1rem;
            border: 1px solid #e2e8f0;
        }
        .login-body .form-control:focus {
            border-color: #3b82f6;
            box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
        }
        .btn-login {
            background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
            border: none;
            padding: 0.9rem;
            border-radius: 12px;
            font-weight: 600;
            font-size: 1rem;
            transition: all 0.2s;
        }
        .btn-login:hover {
            transform: translateY(-1px);
            box-shadow: 0 10px 25px rgba(37, 99, 235, 0.35);
        }
        .login-footer {
            text-align: center;
            margin-top: 1.5rem;
            color: rgba(255,255,255,0.6);
            font-size: 0.8rem;
        }
    </style>
</head>
<body>
    <div class="login-wrapper">
        <div class="login-card">
            <div class="login-header">
                <div class="icon-wrap"><i class="bi bi-gear-fill"></i></div>
                <h1>نظام صيانة الأجهزة</h1>
                <p>Maintenance Job Sheet</p>
            </div>
            <div class="login-body">
                <?php if ($error): ?>
                <div class="alert alert-danger d-flex align-items-center">
                    <i class="bi bi-exclamation-circle me-2"></i>
                    <?= htmlspecialchars($error) ?>
                </div>
                <?php endif; ?>
                <form method="POST" action="">
                    <?php if (function_exists('csrf_field')) echo csrf_field(); ?>
                    <div class="mb-3">
                        <label class="form-label fw-600">اسم المستخدم</label>
                        <input type="text" name="username" class="form-control" required autofocus 
                            placeholder="أدخل اسم المستخدم" value="<?= htmlspecialchars($_POST['username'] ?? '') ?>">
                    </div>
                    <div class="mb-4">
                        <label class="form-label fw-600">كلمة المرور</label>
                        <input type="password" name="password" class="form-control" required placeholder="أدخل كلمة المرور">
                    </div>
                    <button type="submit" class="btn btn-primary btn-login w-100">
                        <i class="bi bi-box-arrow-in-left me-1"></i> تسجيل الدخول
                    </button>
                </form>
            </div>
        </div>
        <p class="login-footer">النظام محمي بكلمة مرور</p>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
