<?php 
include 'header.php'; 
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') { exit; }

// إضافة رابط جديد للمنيو
if (isset($_POST['add_menu'])) {
    $stmt = $conn->prepare("INSERT INTO menus (title, url, order_no, location) VALUES (?, ?, ?, ?)");
    $stmt->execute([$_POST['title'], $_POST['url'], $_POST['order_no'], $_POST['location']]);
    echo "<script>alert('تم إضافة الرابط بنجاح'); window.location='manage_menu.php';</script>";
}

// حذف رابط
if (isset($_GET['delete'])) {
    $stmt = $conn->prepare("DELETE FROM menus WHERE id = ?");
    $stmt->execute([$_GET['delete']]);
    header("Location: manage_menu.php");
}

$menus = $conn->query("SELECT * FROM menus ORDER BY order_no ASC")->fetchAll();
?>

<div class="container mt-4 text-end">
    <div class="row">
        <!-- فورم الإضافة -->
        <div class="col-md-4">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-primary text-white fw-bold">إضافة رابط جديد للمنيو</div>
                <div class="card-body">
                    <form method="POST">
                        <div class="mb-3 text-start">
                            <label class="small fw-bold d-block text-end">نص الرابط (مثلاً: اتصل بنا)</label>
                            <input type="text" name="title" class="form-control" required>
                        </div>
                        <div class="mb-3 text-start">
                            <label class="small fw-bold d-block text-end">الرابط (URL)</label>
                            <input type="text" name="url" class="form-control" placeholder="contact.php" required>
                        </div>
                        <div class="row">
                            <div class="col-6 mb-3 text-start">
                                <label class="small fw-bold d-block text-end">الترتيب</label>
                                <input type="number" name="order_no" class="form-control" value="0">
                            </div>
                            <div class="col-6 mb-3 text-start">
                                <label class="small fw-bold d-block text-end">المكان</label>
                                <select name="location" class="form-select">
                                    <option value="header">الهيدر</option>
                                    <option value="footer">الفوتر</option>
                                </select>
                            </div>
                        </div>
                        <button type="submit" name="add_menu" class="btn btn-primary w-100 fw-bold">إضافة للمنيو</button>
                    </form>
                </div>
            </div>
        </div>

        <!-- عرض المنيو الحالي -->
        <div class="col-md-8">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-white fw-bold">الروابط الحالية في المنيو</div>
                <div class="card-body p-0">
                    <ul class="list-group list-group-flush">
                        <?php foreach($menus as $m): ?>
                        <li class="list-group-item d-flex justify-content-between align-items-center p-3">
                            <div>
                                <span class="badge bg-light text-dark me-2">#<?php echo $m['order_no']; ?></span>
                                <strong><?php echo $m['title']; ?></strong>
                                <small class="text-muted ms-3"><?php echo $m['url']; ?></small>
                            </div>
                            <a href="manage_menu.php?delete=<?php echo $m['id']; ?>" class="text-danger" onclick="return confirm('حذف هذا الرابط؟')"><i class="fas fa-trash"></i></a>
                        </li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include 'footer.php'; ?>