<?php
/**
 * employees.php - إدارة الموظفين (نسخة كابتن)
 */

include 'config.php';
checkAdminAuth();

// ============================================
// تحسين الأداء - استعلام واحد لكل شيء
// ============================================

// إحصائيات سريعة
$stats = $conn->query("
    SELECT 
        COUNT(*) as total,
        SUM(is_active) as active,
        AVG(salary) as avg_salary,
        SUM(salary) as total_salary
    FROM employees
")->fetch_assoc();

// معالجة POST
$message = '';
$type = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    // إضافة موظف
    if (isset($_POST['add'])) {
        $name = $conn->real_escape_string($_POST['name']);
        $email = $conn->real_escape_string($_POST['email']);
        $phone = $conn->real_escape_string($_POST['phone']);
        $position = $conn->real_escape_string($_POST['position']);
        $salary = floatval($_POST['salary']);
        $active = isset($_POST['active']) ? 1 : 0;
        
        $conn->query("INSERT INTO employees (name, email, phone, position, salary, is_active) 
                     VALUES ('$name', '$email', '$phone', '$position', $salary, $active)");
        
        $message = "✅ تمت الإضافة بنجاح";
        $type = "success";
        header("Refresh:0");
    }
    
    // تحديث موظف
    if (isset($_POST['update'])) {
        $id = intval($_POST['id']);
        $name = $conn->real_escape_string($_POST['name']);
        $email = $conn->real_escape_string($_POST['email']);
        $phone = $conn->real_escape_string($_POST['phone']);
        $position = $conn->real_escape_string($_POST['position']);
        $salary = floatval($_POST['salary']);
        $active = isset($_POST['active']) ? 1 : 0;
        
        $conn->query("UPDATE employees SET 
                     name='$name', email='$email', phone='$phone', 
                     position='$position', salary=$salary, is_active=$active
                     WHERE id=$id");
        
        $message = "✅ تم التحديث بنجاح";
        $type = "success";
        header("Refresh:0");
    }
    
    // حذف موظف
    if (isset($_POST['delete'])) {
        $id = intval($_POST['id']);
        $conn->query("DELETE FROM employees WHERE id = $id");
        $message = "✅ تم الحذف بنجاح";
        $type = "success";
        header("Refresh:0");
    }
    
    // تغيير الحالة
    if (isset($_POST['toggle'])) {
        $id = intval($_POST['id']);
        $conn->query("UPDATE employees SET is_active = NOT is_active WHERE id = $id");
        $message = "✅ تم تغيير الحالة";
        $type = "success";
        header("Refresh:0");
    }
}

// جلب الموظفين
$employees = $conn->query("SELECT * FROM employees ORDER BY id DESC");
?>
<!DOCTYPE html>
<html dir="rtl" lang="ar">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes">
    <title>الموظفين - Max</title>
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            -webkit-tap-highlight-color: transparent;
        }
        
        body {
            font-family: 'Tajawal', sans-serif;
            background: #f5f7fa;
            padding: 80px 16px 20px;
        }
        
        /* ===== الهيدر ===== */
        .header {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 60px;
            background: #F57C00;
            color: white;
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 16px;
            z-index: 1000;
            box-shadow: 0 2px 10px rgba(245,124,0,0.3);
        }
        
        .header h1 {
            font-size: 1.3rem;
            font-weight: 800;
        }
        
        .header i {
            font-size: 1.5rem;
            width: 44px;
            height: 44px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: rgba(255,255,255,0.2);
            border-radius: 50%;
            cursor: pointer;
        }
        
        /* ===== القائمة الجانبية ===== */
        .sidebar-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.5);
            z-index: 2000;
            display: none;
            backdrop-filter: blur(3px);
        }
        
        .sidebar-overlay.active {
            display: block;
        }
        
        .sidebar {
            position: fixed;
            top: 0;
            right: -300px;
            width: 280px;
            height: 100vh;
            background: white;
            z-index: 2001;
            transition: right 0.3s ease;
            display: flex;
            flex-direction: column;
            box-shadow: -5px 0 20px rgba(0,0,0,0.2);
        }
        
        .sidebar.active {
            right: 0;
        }
        
        .sidebar-header {
            background: linear-gradient(135deg, #F57C00, #FF9800);
            color: white;
            padding: 30px 20px;
            position: relative;
        }
        
        .sidebar-close {
            position: absolute;
            top: 10px;
            left: 10px;
            background: rgba(255,255,255,0.2);
            border: none;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            color: white;
            font-size: 1.3rem;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
        }
        
        .sidebar-user {
            text-align: center;
        }
        
        .sidebar-avatar {
            width: 70px;
            height: 70px;
            background: rgba(255,255,255,0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2.5rem;
            margin: 0 auto 10px;
            border: 3px solid rgba(255,255,255,0.3);
        }
        
        .sidebar-name {
            font-weight: 700;
            font-size: 1.2rem;
            color: white;
        }
        
        .sidebar-email {
            font-size: 0.85rem;
            opacity: 0.9;
            color: white;
        }
        
        .sidebar-menu {
            flex: 1;
            overflow-y: auto;
        }
        
        .menu-item {
            display: flex;
            align-items: center;
            padding: 15px 20px;
            color: #2C3E50;
            text-decoration: none;
            border-bottom: 1px solid #f0f0f0;
            transition: all 0.2s ease;
        }
        
        .menu-item:hover {
            background: #FFF3E0;
        }
        
        .menu-item.active {
            background: #FFF3E0;
            border-right: 4px solid #F57C00;
        }
        
        .menu-item i {
            width: 30px;
            color: #F57C00;
            font-size: 1.2rem;
        }
        
        .menu-item span {
            flex: 1;
        }
        
        .menu-badge {
            background: #F44336;
            color: white;
            padding: 3px 8px;
            border-radius: 20px;
            font-size: 11px;
        }
        
        .sidebar-footer {
            padding: 20px;
            text-align: center;
            border-top: 1px solid #f0f0f0;
            color: #6c757d;
            font-size: 0.85rem;
        }
        
        /* ===== إحصائيات ===== */
        .stats {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 12px;
            margin-bottom: 24px;
        }
        
        @media (min-width: 500px) {
            .stats {
                grid-template-columns: repeat(4, 1fr);
            }
        }
        
        .stat-card {
            background: white;
            padding: 20px;
            border-radius: 16px;
            text-align: center;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        }
        
        .stat-value {
            font-size: 1.8rem;
            font-weight: 800;
            color: #F57C00;
            line-height: 1.2;
        }
        
        .stat-label {
            font-size: 0.85rem;
            color: #6c757d;
        }
        
        /* ===== زر الإضافة ===== */
        .btn-add {
            background: #F57C00;
            color: white;
            border: none;
            padding: 14px 28px;
            border-radius: 30px;
            font-weight: 700;
            font-size: 1rem;
            display: flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
            margin-bottom: 20px;
            box-shadow: 0 4px 12px rgba(245,124,0,0.3);
            transition: all 0.2s ease;
        }
        
        .btn-add:active {
            transform: scale(0.95);
        }
        
        /* ===== رسالة ===== */
        .alert {
            background: white;
            padding: 16px;
            border-radius: 12px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
            border-right: 4px solid;
        }
        
        .alert.success {
            background: #d4edda;
            color: #155724;
            border-color: #4CAF50;
        }
        
        .alert.danger {
            background: #f8d7da;
            color: #721c24;
            border-color: #F44336;
        }
        
        /* ===== بحث ===== */
        .search {
            position: relative;
            margin-bottom: 20px;
        }
        
        .search i {
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #F57C00;
        }
        
        .search input {
            width: 100%;
            padding: 14px 45px;
            border: 2px solid #e0e0e0;
            border-radius: 30px;
            font-family: 'Tajawal', sans-serif;
            font-size: 1rem;
        }
        
        .search input:focus {
            outline: none;
            border-color: #F57C00;
        }
        
        /* ===== بطاقات الموظفين ===== */
        .employees-grid {
            display: grid;
            gap: 12px;
        }
        
        .emp-card {
            background: white;
            border-radius: 20px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
            border: 1px solid #eee;
            display: flex;
            flex-direction: row;
            min-height: 140px;
        }
        
        @media (max-width: 380px) {
            .emp-card {
                flex-direction: column;
            }
            .emp-avatar {
                width: 100% !important;
                height: 120px !important;
            }
        }
        
        .emp-avatar {
            width: 120px;
            height: 140px;
            background: #f8f9fa;
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 3rem;
            color: #F57C00;
        }
        
        .status-badge {
            position: absolute;
            top: 8px;
            right: 8px;
            padding: 4px 8px;
            border-radius: 20px;
            font-size: 0.7rem;
            font-weight: 700;
            background: white;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .status-badge.active {
            background: #4CAF50;
            color: white;
        }
        
        .status-badge.inactive {
            background: #6c757d;
            color: white;
        }
        
        .emp-content {
            flex: 1;
            padding: 12px;
        }
        
        .emp-name {
            font-weight: 800;
            font-size: 1.1rem;
            margin-bottom: 4px;
            color: #2C3E50;
        }
        
        .emp-detail {
            font-size: 0.85rem;
            color: #6c757d;
            margin-bottom: 4px;
            display: flex;
            align-items: center;
            gap: 6px;
        }
        
        .emp-detail i {
            color: #F57C00;
            width: 20px;
        }
        
        .emp-salary {
            font-weight: 800;
            color: #F57C00;
            font-size: 1.1rem;
            margin: 8px 0;
        }
        
        .emp-actions {
            display: flex;
            gap: 8px;
            margin-top: 8px;
        }
        
        .action-btn {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            border: none;
            background: #f8f9fa;
            color: #6c757d;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        
        .action-btn.toggle:active { background: #4CAF50; color: white; }
        .action-btn.edit:active { background: #FFC107; color: white; }
        .action-btn.delete:active { background: #F44336; color: white; }
        
        /* ===== زر الإضافة العائم ===== */
        .fab {
            position: fixed;
            bottom: 24px;
            right: 24px;
            width: 56px;
            height: 56px;
            border-radius: 50%;
            background: #F57C00;
            color: white;
            border: none;
            box-shadow: 0 4px 12px rgba(245,124,0,0.4);
            font-size: 1.5rem;
            cursor: pointer;
            z-index: 1000;
            transition: all 0.2s ease;
        }
        
        .fab:active {
            transform: scale(0.9);
        }
        
        /* ===== مودال ===== */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.5);
            z-index: 2002;
            align-items: flex-end;
            justify-content: center;
        }
        
        .modal.show {
            display: flex;
        }
        
        .modal-content {
            background: white;
            width: 100%;
            max-width: 500px;
            border-radius: 30px 30px 0 0;
            padding: 24px;
            max-height: 85vh;
            overflow-y: auto;
        }
        
        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        
        .modal-header h3 {
            font-size: 1.3rem;
            font-weight: 800;
            color: #F57C00;
        }
        
        .close {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            border: none;
            background: #f8f9fa;
            font-size: 1.2rem;
            cursor: pointer;
        }
        
        .form-group {
            margin-bottom: 16px;
        }
        
        .form-group label {
            display: block;
            font-weight: 700;
            margin-bottom: 6px;
            color: #2C3E50;
        }
        
        .form-control {
            width: 100%;
            padding: 14px;
            border: 2px solid #e0e0e0;
            border-radius: 12px;
            font-family: 'Tajawal', sans-serif;
            font-size: 1rem;
        }
        
        .form-control:focus {
            outline: none;
            border-color: #F57C00;
        }
        
        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 12px;
        }
        
        .switch {
            position: relative;
            display: inline-block;
            width: 60px;
            height: 34px;
        }
        
        .switch input {
            opacity: 0;
            width: 0;
            height: 0;
        }
        
        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #ccc;
            transition: .4s;
            border-radius: 34px;
        }
        
        .slider:before {
            position: absolute;
            content: "";
            height: 26px;
            width: 26px;
            left: 4px;
            bottom: 4px;
            background-color: white;
            transition: .4s;
            border-radius: 50%;
        }
        
        input:checked + .slider {
            background-color: #F57C00;
        }
        
        input:checked + .slider:before {
            transform: translateX(26px);
        }
        
        .btn {
            width: 100%;
            padding: 16px;
            border: none;
            border-radius: 12px;
            font-weight: 700;
            font-size: 1rem;
            cursor: pointer;
            margin-top: 8px;
        }
        
        .btn-primary {
            background: #F57C00;
            color: white;
        }
        
        .btn-secondary {
            background: #f0f0f0;
            color: #6c757d;
        }
        
        .empty-state {
            text-align: center;
            padding: 60px 20px;
            background: white;
            border-radius: 16px;
        }
        
        .empty-state i {
            font-size: 4rem;
            color: #FFB74D;
            margin-bottom: 16px;
        }
    </style>
</head>
<body>

<!-- الهيدر -->
<div class="header">
    <i class="bi bi-list" onclick="openSidebar()"></i>
    <h1><i class="bi bi-people"></i> الموظفين</h1>
    <i class="bi bi-bell"></i>
</div>

<!-- القائمة الجانبية -->
<?php include 'sidebar.php'; ?>

<!-- المحتوى الرئيسي -->
<div class="container">
    <!-- إحصائيات -->
    <div class="stats">
        <div class="stat-card">
            <div class="stat-value"><?php echo $stats['total'] ?? 0; ?></div>
            <div class="stat-label">إجمالي</div>
        </div>
        <div class="stat-card">
            <div class="stat-value"><?php echo $stats['active'] ?? 0; ?></div>
            <div class="stat-label">نشط</div>
        </div>
        <div class="stat-card">
            <div class="stat-value"><?php echo number_format($stats['avg_salary'] ?? 0, 0); ?></div>
            <div class="stat-label">متوسط الراتب</div>
        </div>
        <div class="stat-card">
            <div class="stat-value"><?php echo number_format($stats['total_salary'] ?? 0, 0); ?></div>
            <div class="stat-label">إجمالي الرواتب</div>
        </div>
    </div>
    
    <!-- رسالة -->
    <?php if ($message): ?>
    <div class="alert <?php echo $type; ?>">
        <i class="bi bi-<?php echo $type == 'success' ? 'check-circle' : 'exclamation-triangle'; ?>"></i>
        <?php echo $message; ?>
    </div>
    <?php endif; ?>
    
    <!-- بحث -->
    <div class="search">
        <i class="bi bi-search"></i>
        <input type="text" id="searchInput" placeholder="ابحث عن موظف...">
    </div>
    
    <!-- قائمة الموظفين -->
    <div class="employees-grid" id="employeesGrid">
        <?php if ($employees->num_rows > 0): ?>
            <?php while($emp = $employees->fetch_assoc()): ?>
            <div class="emp-card" data-name="<?php echo strtolower($emp['name']); ?>" data-email="<?php echo strtolower($emp['email']); ?>">
                <div class="emp-avatar">
                    <i class="bi bi-person-badge"></i>
                    <div class="status-badge <?php echo $emp['is_active'] ? 'active' : 'inactive'; ?>">
                        <?php echo $emp['is_active'] ? 'نشط' : 'غير نشط'; ?>
                    </div>
                </div>
                <div class="emp-content">
                    <div class="emp-name"><?php echo htmlspecialchars($emp['name']); ?></div>
                    <div class="emp-detail"><i class="bi bi-envelope"></i> <?php echo $emp['email']; ?></div>
                    <div class="emp-detail"><i class="bi bi-telephone"></i> <?php echo $emp['phone']; ?></div>
                    <div class="emp-detail"><i class="bi bi-briefcase"></i> <?php echo $emp['position']; ?></div>
                    <div class="emp-salary"><?php echo number_format($emp['salary']); ?> <small>ريال</small></div>
                    
                    <div class="emp-actions">
                        <form method="POST" style="display:inline;">
                            <input type="hidden" name="id" value="<?php echo $emp['id']; ?>">
                            <button type="submit" name="toggle" class="action-btn toggle" title="تغيير الحالة">
                                <i class="bi bi-<?php echo $emp['is_active'] ? 'pause' : 'play'; ?>"></i>
                            </button>
                        </form>
                        
                        <button class="action-btn edit" onclick='editEmployee(<?php echo json_encode($emp); ?>)' title="تعديل">
                            <i class="bi bi-pencil"></i>
                        </button>
                        
                        <form method="POST" style="display:inline;" onsubmit="return confirm('تأكيد حذف الموظف؟')">
                            <input type="hidden" name="id" value="<?php echo $emp['id']; ?>">
                            <button type="submit" name="delete" class="action-btn delete" title="حذف">
                                <i class="bi bi-trash"></i>
                            </button>
                        </form>
                    </div>
                </div>
            </div>
            <?php endwhile; ?>
        <?php else: ?>
            <div class="empty-state">
                <i class="bi bi-people"></i>
                <h3>لا يوجد موظفين</h3>
                <p>أضف أول موظف الآن</p>
            </div>
        <?php endif; ?>
    </div>
</div>

<!-- زر الإضافة العائم -->
<button class="fab" onclick="openAddModal()">
    <i class="bi bi-plus"></i>
</button>

<!-- مودال الإضافة -->
<div class="modal" id="addModal">
    <div class="modal-content">
        <div class="modal-header">
            <h3><i class="bi bi-person-plus"></i> إضافة موظف جديد</h3>
            <button class="close" onclick="closeModal('addModal')"><i class="bi bi-x"></i></button>
        </div>
        <form method="POST">
            <div class="form-group">
                <label>الاسم الكامل</label>
                <input type="text" class="form-control" name="name" required>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label>البريد الإلكتروني</label>
                    <input type="email" class="form-control" name="email" required>
                </div>
                <div class="form-group">
                    <label>رقم الهاتف</label>
                    <input type="text" class="form-control" name="phone" required>
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label>الوظيفة</label>
                    <select class="form-control" name="position" required>
                        <option value="">اختر</option>
                        <option value="مدير">مدير</option>
                        <option value="كاشير">كاشير</option>
                        <option value="طباخ">طباخ</option>
                        <option value="نادل">نادل</option>
                        <option value="مشرف">مشرف</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>الراتب</label>
                    <input type="number" step="0.01" class="form-control" name="salary" required>
                </div>
            </div>
            
            <div class="form-group">
                <label class="switch">
                    <input type="checkbox" name="active" checked>
                    <span class="slider"></span>
                </label>
                <span style="margin-right: 10px;">موظف نشط</span>
            </div>
            
            <button type="submit" name="add" class="btn btn-primary">إضافة الموظف</button>
        </form>
        <button class="btn btn-secondary" onclick="closeModal('addModal')">إلغاء</button>
    </div>
</div>

<!-- مودال التعديل -->
<div class="modal" id="editModal">
    <div class="modal-content">
        <div class="modal-header">
            <h3><i class="bi bi-pencil-square"></i> تعديل بيانات موظف</h3>
            <button class="close" onclick="closeModal('editModal')"><i class="bi bi-x"></i></button>
        </div>
        <form method="POST" id="editForm">
            <input type="hidden" name="id" id="edit_id">
            
            <div class="form-group">
                <label>الاسم الكامل</label>
                <input type="text" class="form-control" name="name" id="edit_name" required>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label>البريد الإلكتروني</label>
                    <input type="email" class="form-control" name="email" id="edit_email" required>
                </div>
                <div class="form-group">
                    <label>رقم الهاتف</label>
                    <input type="text" class="form-control" name="phone" id="edit_phone" required>
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label>الوظيفة</label>
                    <select class="form-control" name="position" id="edit_position" required>
                        <option value="مدير">مدير</option>
                        <option value="كاشير">كاشير</option>
                        <option value="طباخ">طباخ</option>
                        <option value="نادل">نادل</option>
                        <option value="مشرف">مشرف</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>الراتب</label>
                    <input type="number" step="0.01" class="form-control" name="salary" id="edit_salary" required>
                </div>
            </div>
            
            <div class="form-group">
                <label class="switch">
                    <input type="checkbox" name="active" id="edit_active">
                    <span class="slider"></span>
                </label>
                <span style="margin-right: 10px;">موظف نشط</span>
            </div>
            
            <button type="submit" name="update" class="btn btn-primary">تحديث البيانات</button>
        </form>
        <button class="btn btn-secondary" onclick="closeModal('editModal')">إلغاء</button>
    </div>
</div>

<script>
// دالة فتح القائمة الجانبية
function openSidebar() {
    document.getElementById('sidebar').classList.add('active');
    document.getElementById('sidebarOverlay').classList.add('active');
    document.body.style.overflow = 'hidden';
}

// دوال المودال
function openAddModal() {
    document.getElementById('addModal').classList.add('show');
}

function closeModal(id) {
    document.getElementById(id).classList.remove('show');
    document.body.style.overflow = '';
}

// تعديل موظف
function editEmployee(emp) {
    document.getElementById('edit_id').value = emp.id;
    document.getElementById('edit_name').value = emp.name;
    document.getElementById('edit_email').value = emp.email;
    document.getElementById('edit_phone').value = emp.phone;
    document.getElementById('edit_position').value = emp.position;
    document.getElementById('edit_salary').value = emp.salary;
    document.getElementById('edit_active').checked = emp.is_active == 1;
    
    document.getElementById('editModal').classList.add('show');
}

// بحث فوري
let searchTimeout;
document.getElementById('searchInput')?.addEventListener('input', function(e) {
    clearTimeout(searchTimeout);
    const term = e.target.value.toLowerCase();
    
    searchTimeout = setTimeout(() => {
        document.querySelectorAll('.emp-card').forEach(card => {
            const name = card.dataset.name;
            const email = card.dataset.email;
            if (name.includes(term) || email.includes(term)) {
                card.style.display = 'flex';
            } else {
                card.style.display = 'none';
            }
        });
    }, 300);
});

// إغلاق المودال بالنقر خارج المحتوى
document.querySelectorAll('.modal').forEach(modal => {
    modal.addEventListener('click', function(e) {
        if (e.target === this) {
            this.classList.remove('show');
            document.body.style.overflow = '';
        }
    });
});

// إغلاق بالـ ESC
document.addEventListener('keydown', function(e) {
    if (e.key === 'Escape') {
        closeSidebar();
        document.querySelectorAll('.modal.show').forEach(m => {
            m.classList.remove('show');
        });
        document.body.style.overflow = '';
    }
});

// إغلاق القائمة عند النقر على الروابط
document.querySelectorAll('.menu-item').forEach(item => {
    item.addEventListener('click', function() {
        closeSidebar();
    });
});
</script>

</body>
</html>