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

requireAdminLogin();

// Menü öğelerini tablo formatında göster (sadece bu sayfada kullanılan yerel fonksiyon)
function displayMenuItemsTable($parentId = null, $level = 0) {
    $items = getMenuItems($parentId);
    foreach ($items as $item) {
        $indent = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level);
        echo '<tr>';
        echo '<td>' . $item['id'] . '</td>';
        echo '<td>' . $indent . ($level > 0 ? '└─ ' : '') . e($item['title']) . '</td>';
        echo '<td><code>' . e($item['url']) . '</code></td>';
        echo '<td>';
        if ($item['icon']) {
            echo '<i class="' . e($item['icon']) . '"></i> ';
        }
        echo e($item['target']);
        echo '</td>';
        echo '<td>' . $item['sort_order'] . '</td>';
        echo '<td>';
        echo '<span class="badge bg-' . ($item['status'] === 'active' ? 'success' : 'secondary') . '">';
        echo $item['status'] === 'active' ? 'Aktif' : 'Pasif';
        echo '</span>';
        echo '</td>';
        echo '<td>';
        echo '<a href="edit.php?id=' . $item['id'] . '" class="btn btn-sm btn-warning">';
        echo '<i class="fas fa-edit"></i>';
        echo '</a> ';
        echo '<a href="delete.php?id=' . $item['id'] . '" class="btn btn-sm btn-danger" onclick="return confirm(\'Bu menü öğesini silmek istediğinize emin misiniz?\')">';
        echo '<i class="fas fa-trash"></i>';
        echo '</a>';
        echo '</td>';
        echo '</tr>';
        // Alt menüleri göster
        displayMenuItemsTable($item['id'], $level + 1);
    }
}

$page_title = 'Menü Yönetimi';
include '../includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3">Menü Yönetimi</h1>
    <a href="create.php" class="btn btn-primary">
        <i class="fas fa-plus me-2"></i>Yeni Menü Öğesi Ekle
    </a>
</div>

<div class="card">
    <div class="card-body">
        <?php
        $allItems = getMenuItems();
        if (empty($allItems)):
        ?>
            <p class="text-muted text-center py-5">Henüz menü öğesi eklenmemiş.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Başlık</th>
                            <th>URL</th>
                            <th>İkon / Hedef</th>
                            <th>Sıra</th>
                            <th>Durum</th>
                            <th>İşlemler</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php displayMenuItemsTable(); ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php include '../includes/footer.php'; ?>

