
<?php
include 'config.php';
checkAdminAuth();

// تحميل ملف التكوين


// إنشاء جدول customer_notes إذا لم يكن موجوداً
$create_table_sql = "
CREATE TABLE IF NOT EXISTS customer_notes (
    id INT PRIMARY KEY AUTO_INCREMENT,
    customer_id INT,
    customer_name VARCHAR(255),
    subject VARCHAR(255),
    message TEXT,
    status ENUM('new', 'read', 'in_progress', 'resolved') DEFAULT 'new',
    admin_reply TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";

if (!$conn->query($create_table_sql)) {
    die("❌ فشل في إنشاء/التحقق من جدول الملاحظات: " . $conn->error);
}

// معالجة الإجراءات
$message = '';
$message_type = '';

// تحديث حالة الملاحظة
if (isset($_GET['mark_as_read']) && is_numeric($_GET['mark_as_read'])) {
    $note_id = intval($_GET['mark_as_read']);
    $conn->query("UPDATE customer_notes SET status = 'read' WHERE id = $note_id");
    $message = "تم تحديث حالة الملاحظة كمقروءة";
    $message_type = "success";
}

// حذف الملاحظة
if (isset($_GET['delete']) && is_numeric($_GET['delete'])) {
    $note_id = intval($_GET['delete']);
    if ($conn->query("DELETE FROM customer_notes WHERE id = $note_id")) {
        $message = "تم حذف الملاحظة بنجاح";
        $message_type = "success";
    } else {
        $message = "خطأ في حذف الملاحظة: " . $conn->error;
        $message_type = "danger";
    }
}

// الرد على الملاحظة
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['reply'])) {
    $note_id = intval($_POST['note_id']);
    $admin_reply = trim($conn->real_escape_string($_POST['admin_reply']));
    $status = $conn->real_escape_string($_POST['status']);
    
    if (!empty($admin_reply)) {
        $stmt = $conn->prepare("UPDATE customer_notes SET admin_reply = ?, status = ?, updated_at = NOW() WHERE id = ?");
        $stmt->bind_param("ssi", $admin_reply, $status, $note_id);
        
        if ($stmt->execute()) {
            $message = "تم إرسال الرد بنجاح";
            $message_type = "success";
        } else {
            $message = "خطأ في إرسال الرد: " . $stmt->error;
            $message_type = "danger";
        }
        $stmt->close();
    }
}

// تحديث حالة الملاحظة مباشرة
if (isset($_GET['update_status']) && isset($_GET['id'])) {
    $note_id = intval($_GET['id']);
    $status = $conn->real_escape_string($_GET['update_status']);
    
    $allowed_statuses = ['new', 'read', 'in_progress', 'resolved'];
    if (in_array($status, $allowed_statuses)) {
        if ($conn->query("UPDATE customer_notes SET status = '$status' WHERE id = $note_id")) {
            $message = "تم تحديث حالة الملاحظة";
            $message_type = "success";
        }
    }
}

// جلب جميع الملاحظات مع معلومات العميل من جدول customers
$query = "SELECT cn.*, c.phone, c.address 
          FROM customer_notes cn 
          LEFT JOIN customers c ON cn.customer_id = c.id 
          ORDER BY 
            CASE cn.status 
                WHEN 'new' THEN 1
                WHEN 'in_progress' THEN 2
                WHEN 'read' THEN 3
                WHEN 'resolved' THEN 4
            END,
            cn.created_at DESC";
          
$notes_result = $conn->query($query);

if (!$notes_result) {
    die("❌ خطأ في جلب الملاحظات: " . $conn->error);
}

$notes = $notes_result;

// عد الملاحظات الجديدة
$new_count_query = $conn->query("SELECT COUNT(*) as count FROM customer_notes WHERE status = 'new'");
$new_count_row = $new_count_query->fetch_assoc();
$new_notes_count = $new_count_row['count'] ?? 0;

// الحصول على عدد الملاحظات بكل حالة
$stats_query = $conn->query("SELECT status, COUNT(*) as count FROM customer_notes GROUP BY status");
$status_counts = ['new' => 0, 'read' => 0, 'in_progress' => 0, 'resolved' => 0];
while ($stat = $stats_query->fetch_assoc()) {
    $status_counts[$stat['status']] = $stat['count'];
}
?>

<!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://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        :root {
            --primary-color: #F57C00;
            --secondary-color: #4CAF50;
            --warning-color: #FF9800;
            --info-color: #2196F3;
            --danger-color: #F44336;
        }
        
        body {
            font-family: 'Tajawal', sans-serif;
            background-color: #f8f9fa;
            overflow-x: hidden;
        }
        
        .sidebar {
            background: linear-gradient(135deg, #2c3e50, #34495e);
            color: white;
            height: 100vh;
            position: fixed;
            right: 0;
            top: 0;
            width: 250px;
            transition: all 0.3s ease;
            z-index: 1000;
            box-shadow: -2px 0 15px rgba(0,0,0,0.1);
        }
        
        .sidebar.collapsed {
            right: -250px;
        }
        
        .sidebar .nav-link {
            color: rgba(255,255,255,0.8);
            padding: 15px 20px;
            border-bottom: 1px solid rgba(255,255,255,0.05);
            display: flex;
            align-items: center;
            gap: 10px;
            transition: all 0.3s;
            position: relative;
        }
        
        .sidebar .nav-link:hover, 
        .sidebar .nav-link.active {
            background: rgba(255,255,255,0.1);
            padding-right: 25px;
            color: white;
        }
        
        .sidebar .nav-link.active::before {
            content: '';
            position: absolute;
            right: 0;
            top: 0;
            height: 100%;
            width: 4px;
            background: #F57C00;
        }
        
        .main-content {
            margin-right: 250px;
            transition: all 0.3s ease;
            min-height: 100vh;
            background: #f8f9fa;
        }
        
        .main-content.expanded {
            margin-right: 0;
        }
        
        .navbar {
            background: white;
            box-shadow: 0 2px 15px rgba(0,0,0,0.1);
            position: sticky;
            top: 0;
            z-index: 999;
        }
        
        .sidebar-toggle {
            display: none;
            background: none;
            border: none;
            font-size: 1.5rem;
            color: #F57C00;
        }
        
        .overlay {
            position: fixed;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            background: rgba(0,0,0,0.5);
            z-index: 999;
            display: none;
            transition: all 0.3s ease;
        }
        
        .overlay.show {
            display: block;
        }
        
        .status-badge {
            font-size: 0.8rem;
            padding: 5px 12px;
            border-radius: 20px;
        }
        
        .status-new { background: var(--danger-color); color: white; }
        .status-read { background: var(--warning-color); color: black; }
        .status-in_progress { background: var(--info-color); color: white; }
        .status-resolved { background: var(--secondary-color); color: white; }
        
        .note-card {
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            border: none;
            transition: all 0.3s ease;
        }
        
        .note-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 20px rgba(0,0,0,0.15);
        }
        
        .note-card.new {
            border-right: 5px solid var(--danger-color);
        }
        
        .note-content {
            position: relative;
        }
        
        .note-meta {
            font-size: 0.85rem;
            color: #6c757d;
        }
        
        .reply-section {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 15px;
            margin-top: 15px;
            border-right: 3px solid var(--info-color);
        }
        
        .reply-form {
            background: #e9ecef;
            border-radius: 10px;
            padding: 15px;
            margin-top: 10px;
        }
        
        .stats-card {
            border-radius: 10px;
            padding: 20px;
            text-align: center;
            margin-bottom: 20px;
            color: white;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .stats-number {
            font-size: 2.5rem;
            font-weight: bold;
            margin-bottom: 10px;
        }
        
        .action-buttons {
            display: flex;
            gap: 5px;
            flex-wrap: wrap;
        }
        
        .btn-xs {
            padding: 3px 8px;
            font-size: 0.75rem;
        }
        
        .customer-info {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 10px;
            margin-top: 10px;
            font-size: 0.9rem;
        }
        
        @media (max-width: 768px) {
            .sidebar {
                right: -250px;
                width: 280px;
            }
            .sidebar.show {
                right: 0;
            }
            .main-content {
                margin-right: 0;
            }
            .sidebar-toggle {
                display: block;
            }
        }
    </style>
</head>
<body>
    <!-- Overlay for mobile -->
    <div class="overlay" id="overlay"></div>

    <!-- Sidebar -->
    <div class="sidebar" id="sidebar">
        <div class="p-3 text-center border-bottom border-white-10">
            <h4 class="mb-0"><i class="bi bi-shop"></i> Max Admin</h4>
            <small class="text-white-50">نظام إدارة المطعم</small>
        </div>
        <nav class="nav flex-column">
            <a class="nav-link" href="dashboard.php">
                <i class="bi bi-speedometer2"></i>لوحة التحكم
            </a>
            <a class="nav-link" href="meals.php">
                <i class="bi bi-egg-fried"></i>إدارة الوجبات
            </a>
            <a class="nav-link" href="categories.php">
                <i class="bi bi-tags"></i>التصنيفات
            </a>
            <a class="nav-link" href="captains.php">
                <i class="bi bi-person-badge"></i>الكباتن
            </a>
            <a class="nav-link" href="orders.php">
                <i class="bi bi-cart"></i>الطلبات
            </a>
            <a class="nav-link" href="sliders.php">
                <i class="bi bi-images"></i>العروض
            </a>
            <a class="nav-link active" href="notes.php">
                <i class="bi bi-sticky"></i>ملاحظات العملاء
            </a>
            <a class="nav-link" href="reports.php">
                <i class="bi bi-graph-up"></i>التقارير
            </a>
            <a class="nav-link" href="users.php">
                <i class="bi bi-people"></i>المستخدمين
            </a>
            <a class="nav-link" href="settings.php">
                <i class="bi bi-gear"></i>الإعدادات
            </a>
            <a class="nav-link" href="logout.php">
                <i class="bi bi-box-arrow-left"></i>تسجيل الخروج
            </a>
        </nav>
    </div>

    <div class="main-content" id="mainContent">
        <nav class="navbar navbar-expand-lg">
            <div class="container-fluid">
                <button class="sidebar-toggle" type="button" id="sidebarToggle">
                    <i class="bi bi-list"></i>
                </button>
                <div class="collapse navbar-collapse" id="navbarNav">
                    <ul class="navbar-nav me-auto">
                        <li class="nav-item">
                            <span class="navbar-text">
                                <i class="bi bi-person-circle me-1"></i>مرحباً، <?php echo $_SESSION['admin_name']; ?>
                            </span>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>

        <div class="container-fluid mt-4">
            <?php if ($message): ?>
                <div class="alert alert-<?php echo $message_type; ?> alert-dismissible fade show" role="alert">
                    <?php echo $message; ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            <?php endif; ?>

            <!-- إحصائيات -->
            <div class="row mb-4">
                <div class="col-md-3">
                    <div class="stats-card bg-danger">
                        <div class="stats-number"><?php echo $status_counts['new']; ?></div>
                        <div class="stats-label">ملاحظات جديدة</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stats-card bg-warning">
                        <div class="stats-number"><?php echo $status_counts['read']; ?></div>
                        <div class="stats-label">ملاحظات مقروءة</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stats-card bg-info">
                        <div class="stats-number"><?php echo $status_counts['in_progress']; ?></div>
                        <div class="stats-label">قيد المعالجة</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stats-card bg-success">
                        <div class="stats-number"><?php echo $status_counts['resolved']; ?></div>
                        <div class="stats-label">تم حلها</div>
                    </div>
                </div>
            </div>

            <!-- أزرار التحكم -->
            <div class="row mb-3">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body d-flex justify-content-between align-items-center">
                            <h5 class="card-title mb-0">
                                <i class="bi bi-chat-text me-2"></i>جميع ملاحظات العملاء
                                <?php if ($new_notes_count > 0): ?>
                                    <span class="badge bg-danger"><?php echo $new_notes_count; ?> جديدة</span>
                                <?php endif; ?>
                            </h5>
                            <div class="d-flex gap-2">
                                <button class="btn btn-primary btn-sm" onclick="location.reload()">
                                    <i class="bi bi-arrow-clockwise"></i> تحديث
                                </button>
                                <div class="dropdown">
                                    <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown">
                                        <i class="bi bi-filter"></i> تصفية حسب الحالة
                                    </button>
                                    <ul class="dropdown-menu">
                                        <li><a class="dropdown-item" href="notes.php">الكل</a></li>
                                        <li><hr class="dropdown-divider"></li>
                                        <li><a class="dropdown-item" href="notes.php?status=new">الجديدة فقط</a></li>
                                        <li><a class="dropdown-item" href="notes.php?status=read">المقروءة</a></li>
                                        <li><a class="dropdown-item" href="notes.php?status=in_progress">قيد المعالجة</a></li>
                                        <li><a class="dropdown-item" href="notes.php?status=resolved">تم الحل</a></li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- قائمة الملاحظات -->
            <div class="row">
                <?php if ($notes->num_rows > 0): 
                    while($note = $notes->fetch_assoc()): ?>
                        <div class="col-lg-6 mb-3">
                            <div class="card note-card <?php echo $note['status'] == 'new' ? 'new' : ''; ?>">
                                <div class="card-header d-flex justify-content-between align-items-center">
                                    <div>
                                        <h6 class="mb-0 fw-bold text-truncate" style="max-width: 250px;">
                                            <?php echo htmlspecialchars($note['subject']); ?>
                                        </h6>
                                        <small class="text-muted note-meta">
                                            <i class="bi bi-person me-1"></i>
                                            <?php echo htmlspecialchars($note['customer_name']); ?>
                                        </small>
                                    </div>
                                    <div class="d-flex gap-2 align-items-center">
                                        <span class="status-badge status-<?php echo $note['status']; ?>">
                                            <?php 
                                            switch($note['status']) {
                                                case 'new': echo 'جديدة'; break;
                                                case 'read': echo 'مقروءة'; break;
                                                case 'in_progress': echo 'قيد المعالجة'; break;
                                                case 'resolved': echo 'تم الحل'; break;
                                            }
                                            ?>
                                        </span>
                                    </div>
                                </div>
                                
                                <div class="card-body">
                                    <!-- معلومات العميل -->
                                    <div class="customer-info">
                                        <div class="row">
                                            <?php if (!empty($note['phone'])): ?>
                                                <div class="col-md-6">
                                                    <i class="bi bi-phone me-1"></i>
                                                    <strong>الهاتف:</strong> <?php echo htmlspecialchars($note['phone']); ?>
                                                </div>
                                            <?php endif; ?>
                                            <?php if (!empty($note['address'])): ?>
                                                <div class="col-md-6">
                                                    <i class="bi bi-geo-alt me-1"></i>
                                                    <strong>العنوان:</strong> <?php echo htmlspecialchars($note['address']); ?>
                                                </div>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                    
                                    <!-- محتوى الملاحظة -->
                                    <div class="note-content mt-3">
                                        <p class="card-text"><?php echo nl2br(htmlspecialchars($note['message'])); ?></p>
                                        
                                        <div class="note-meta d-flex justify-content-between align-items-center mt-3">
                                            <small>
                                                <i class="bi bi-clock me-1"></i>
                                                <?php echo date('Y/m/d H:i', strtotime($note['created_at'])); ?>
                                            </small>
                                            
                                            <!-- أزرار الإجراءات -->
                                            <div class="action-buttons">
                                                <?php if($note['status'] == 'new'): ?>
                                                    <a href="notes.php?mark_as_read=<?php echo $note['id']; ?>" 
                                                       class="btn btn-xs btn-success" title="تحديد كمقروء">
                                                        <i class="bi bi-check"></i> قراءة
                                                    </a>
                                                <?php endif; ?>
                                                
                                                <button class="btn btn-xs btn-primary" 
                                                        type="button" 
                                                        data-bs-toggle="collapse" 
                                                        data-bs-target="#replyForm<?php echo $note['id']; ?>"
                                                        title="الرد">
                                                    <i class="bi bi-reply"></i> رد
                                                </button>
                                                
                                                <?php if($note['status'] != 'in_progress'): ?>
                                                    <a href="notes.php?update_status=in_progress&id=<?php echo $note['id']; ?>" 
                                                       class="btn btn-xs btn-info" title="تحويل لقيد المعالجة">
                                                        <i class="bi bi-gear"></i> معالجة
                                                    </a>
                                                <?php endif; ?>
                                                
                                                <?php if($note['status'] != 'resolved'): ?>
                                                    <a href="notes.php?update_status=resolved&id=<?php echo $note['id']; ?>" 
                                                       class="btn btn-xs btn-success" title="تم الحل">
                                                        <i class="bi bi-check-circle"></i> حل
                                                    </a>
                                                <?php endif; ?>
                                                
                                                <a href="notes.php?delete=<?php echo $note['id']; ?>" 
                                                   class="btn btn-xs btn-danger" 
                                                   onclick="return confirm('هل أنت متأكد من حذف هذه الملاحظة؟')"
                                                   title="حذف">
                                                    <i class="bi bi-trash"></i> حذف
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <!-- رد الإدارة -->
                                    <?php if(!empty($note['admin_reply'])): ?>
                                        <div class="reply-section mt-3">
                                            <h6 class="fw-bold text-primary">
                                                <i class="bi bi-person-badge me-1"></i>رد الإدارة:
                                            </h6>
                                            <p><?php echo nl2br(htmlspecialchars($note['admin_reply'])); ?></p>
                                            <small class="text-muted">
                                                <i class="bi bi-clock me-1"></i>
                                                آخر تحديث: <?php echo date('Y/m/d H:i', strtotime($note['updated_at'])); ?>
                                            </small>
                                        </div>
                                    <?php endif; ?>
                                    
                                    <!-- نموذج الرد -->
                                    <div class="collapse" id="replyForm<?php echo $note['id']; ?>">
                                        <div class="reply-form mt-3">
                                            <form method="POST">
                                                <input type="hidden" name="note_id" value="<?php echo $note['id']; ?>">
                                                <div class="mb-2">
                                                    <label class="form-label fw-bold">رد الإدارة:</label>
                                                    <textarea class="form-control form-control-sm" 
                                                              name="admin_reply" 
                                                              rows="3" 
                                                              placeholder="اكتب رد الإدارة هنا..."
                                                              required><?php echo htmlspecialchars($note['admin_reply'] ?? ''); ?></textarea>
                                                </div>
                                                <div class="mb-2">
                                                    <label class="form-label fw-bold">تحديث الحالة:</label>
                                                    <select class="form-select form-select-sm" name="status" required>
                                                        <option value="new" <?php echo $note['status'] == 'new' ? 'selected' : ''; ?>>جديدة</option>
                                                        <option value="read" <?php echo $note['status'] == 'read' ? 'selected' : ''; ?>>مقروءة</option>
                                                        <option value="in_progress" <?php echo $note['status'] == 'in_progress' ? 'selected' : ''; ?>>قيد المعالجة</option>
                                                        <option value="resolved" <?php echo $note['status'] == 'resolved' ? 'selected' : ''; ?>>تم الحل</option>
                                                    </select>
                                                </div>
                                                <div class="d-flex gap-2">
                                                    <button type="submit" name="reply" class="btn btn-sm btn-primary">
                                                        <i class="bi bi-send me-1"></i> إرسال الرد
                                                    </button>
                                                    <button type="button" class="btn btn-sm btn-secondary" 
                                                            data-bs-toggle="collapse" 
                                                            data-bs-target="#replyForm<?php echo $note['id']; ?>">
                                                        إلغاء
                                                    </button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endwhile; ?>
                <?php else: ?>
                    <div class="col-12">
                        <div class="text-center py-5">
                            <i class="bi bi-chat-text display-1 text-muted mb-3"></i>
                            <h4 class="text-muted">لا توجد ملاحظات</h4>
                            <p class="text-muted">لم يتم إرسال أي ملاحظات من العملاء حتى الآن</p>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
            
            <!-- إحصائيات إضافية -->
            <div class="row mt-4">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-header">
                            <h5 class="card-title mb-0"><i class="bi bi-graph-up me-2"></i>إحصائيات الملاحظات</h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <h6>التوزيع الشهري</h6>
                                    <?php
                                    $monthly_stats = $conn->query("
                                        SELECT DATE_FORMAT(created_at, '%Y-%m') as month, 
                                               COUNT(*) as count
                                        FROM customer_notes 
                                        WHERE created_at >= DATE_SUB(NOW(), INTERVAL 6 MONTH)
                                        GROUP BY DATE_FORMAT(created_at, '%Y-%m')
                                        ORDER BY month DESC
                                    ");
                                    
                                    if ($monthly_stats && $monthly_stats->num_rows > 0) {
                                        echo '<ul class="list-group">';
                                        while ($stat = $monthly_stats->fetch_assoc()) {
                                            $month_name = date('F Y', strtotime($stat['month'] . '-01'));
                                            echo '<li class="list-group-item d-flex justify-content-between align-items-center">';
                                            echo $month_name;
                                            echo '<span class="badge bg-primary rounded-pill">' . $stat['count'] . '</span>';
                                            echo '</li>';
                                        }
                                        echo '</ul>';
                                    } else {
                                        echo '<p class="text-muted">لا توجد بيانات للإظهار</p>';
                                    }
                                    ?>
                                </div>
                                <div class="col-md-6">
                                    <h6>أكثر العملاء تفاعلاً</h6>
                                    <?php
                                    $top_customers = $conn->query("
                                        SELECT customer_name, COUNT(*) as notes_count
                                        FROM customer_notes 
                                        GROUP BY customer_id, customer_name
                                        ORDER BY notes_count DESC
                                        LIMIT 5
                                    ");
                                    
                                    if ($top_customers && $top_customers->num_rows > 0) {
                                        echo '<ul class="list-group">';
                                        while ($customer = $top_customers->fetch_assoc()) {
                                            echo '<li class="list-group-item d-flex justify-content-between align-items-center">';
                                            echo htmlspecialchars($customer['customer_name']);
                                            echo '<span class="badge bg-success rounded-pill">' . $customer['notes_count'] . '</span>';
                                            echo '</li>';
                                        }
                                        echo '</ul>';
                                    } else {
                                        echo '<p class="text-muted">لا توجد بيانات للإظهار</p>';
                                    }
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Sidebar toggle functionality
        const sidebar = document.getElementById('sidebar');
        const mainContent = document.getElementById('mainContent');
        const sidebarToggle = document.getElementById('sidebarToggle');
        const overlay = document.getElementById('overlay');

        function toggleSidebar() {
            sidebar.classList.toggle('show');
            overlay.classList.toggle('show');
        }

        sidebarToggle.addEventListener('click', toggleSidebar);
        overlay.addEventListener('click', toggleSidebar);

        // Close sidebar when clicking on a link (mobile)
        if (window.innerWidth <= 768) {
            document.querySelectorAll('.sidebar .nav-link').forEach(link => {
                link.addEventListener('click', toggleSidebar);
            });
        }

        // Adjust sidebar on window resize
        window.addEventListener('resize', function() {
            if (window.innerWidth > 768) {
                sidebar.classList.remove('show');
                overlay.classList.remove('show');
            }
        });

        // تأكيد الحذف
        document.querySelectorAll('a[href*="delete="]').forEach(link => {
            link.addEventListener('click', function(e) {
                if (!confirm('هل أنت متأكد من حذف هذه الملاحظة؟ لا يمكن التراجع عن هذا الإجراء.')) {
                    e.preventDefault();
                }
            });
        });

        // تحديث تلقائي كل 60 ثانية
        setInterval(() => {
            const newCountElement = document.querySelector('.badge.bg-danger');
            if (newCountElement) {
                // يمكن إضافة AJAX للتحقق من الملاحظات الجديدة
                console.log('جاري التحقق من تحديثات الملاحظات...');
            }
        }, 60000);
    </script>
</body>
</html>

<?php
$conn->close();
?>
