<?php
// ── Bootstrap (no HTML yet) ──────────────────────────────────
require_once __DIR__ . '/includes/admin_bootstrap.php';

$msg    = '';
$action = $_GET['action'] ?? 'list';
$editId = (int)($_GET['id'] ?? 0);

// DELETE — redirect before any HTML
if ($action === 'delete' && $editId) {
    $pdo->prepare("DELETE FROM menus WHERE id=?")->execute([$editId]);
    redirect(BASE_URL . '/admin/menus.php?deleted=1');
}

// SAVE
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_menu'])) {
    $name     = trim($_POST['name']     ?? '');
    $link     = trim($_POST['link']     ?? '');
    $position = (int)($_POST['position'] ?? 0);
    $mid      = (int)($_POST['menu_id'] ?? 0);

    if (!$name || !$link) {
        $msg = '<div class="alert alert-error"><i class="fas fa-exclamation-circle"></i> নাম ও লিংক আবশ্যক।</div>';
    } else {
        if ($mid) {
            $pdo->prepare("UPDATE menus SET name=?,link=?,position=? WHERE id=?")->execute([$name,$link,$position,$mid]);
        } else {
            $pdo->prepare("INSERT INTO menus (name,link,position) VALUES (?,?,?)")->execute([$name,$link,$position]);
        }
        redirect(BASE_URL . '/admin/menus.php?saved=1');
    }
}

// Load data
$menus = $pdo->query("SELECT * FROM menus ORDER BY position ASC")->fetchAll();
$editing = null;
if ($editId && $action === 'edit') {
    $es = $pdo->prepare("SELECT * FROM menus WHERE id=?");
    $es->execute([$editId]);
    $editing = $es->fetch();
}

// ── HTML starts here ─────────────────────────────────────────

// ── HTML output starts ──
include __DIR__ . '/includes/admin_header.php';

?>

<h2>মেনু ব্যবস্থাপনা</h2>

<?= $msg ?>
<?php if(isset($_GET['deleted'])): ?><div class="alert alert-success"><i class="fas fa-check-circle"></i> মেনু মুছে ফেলা হয়েছে।</div><?php endif; ?>
<?php if(isset($_GET['saved'])): ?><div class="alert alert-success"><i class="fas fa-check-circle"></i> মেনু সংরক্ষণ সফল।</div><?php endif; ?>

<div style="display:grid;grid-template-columns:1fr 360px;gap:24px;align-items:start;flex-wrap:wrap;">
    <div class="card">
        <div class="card-header"><h3>হেডার মেনু আইটেম</h3></div>
        <div class="table-wrap">
            <table>
                <thead><tr><th>অবস্থান</th><th>নাম</th><th>লিংক</th><th>অ্যাকশন</th></tr></thead>
                <tbody>
                <?php foreach ($menus as $menu): ?>
                <tr>
                    <td><?= $menu['position'] ?></td>
                    <td><strong><?= e($menu['name']) ?></strong></td>
                    <td style="font-size:.82rem;color:var(--g600);"><?= e($menu['link']) ?></td>
                    <td>
                        <div class="actions">
                            <a href="?action=edit&id=<?=$menu['id']?>" class="topbar-btn btn-navy"><i class="fas fa-edit"></i></a>
                            <a href="?action=delete&id=<?=$menu['id']?>" class="topbar-btn btn-red"
                               data-confirm="এই মেনু মুছে ফেলবেন?"><i class="fas fa-trash"></i></a>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <h3><?= $editing ? 'মেনু সম্পাদনা' : 'নতুন মেনু' ?></h3>
            <?php if($editing): ?><a href="?" class="topbar-btn btn-gray">বাতিল</a><?php endif; ?>
        </div>
        <div class="card-body">
            <form method="POST">
                <input type="hidden" name="menu_id" value="<?= $editing['id'] ?? 0 ?>">
                <div class="form-group">
                    <label class="form-label">মেনুর নাম *</label>
                    <input type="text" class="form-control" name="name"
                           value="<?= e($editing['name'] ?? '') ?>"
                           placeholder="যেমন: হোম, পণ্য, যোগাযোগ" required>
                </div>
                <div class="form-group">
                    <label class="form-label">লিংক *</label>
                    <input type="text" class="form-control" name="link"
                           value="<?= e($editing['link'] ?? '') ?>"
                           placeholder="index.php বা https://..." required>
                    <div class="form-hint">সরাসরি ফাইলের নাম (যেমন: index.php) অথবা পূর্ণ URL</div>
                </div>
                <div class="form-group">
                    <label class="form-label">অবস্থান (ক্রম)</label>
                    <input type="number" class="form-control" name="position"
                           value="<?= $editing['position'] ?? count($menus)+1 ?>" min="0">
                </div>
                <button type="submit" name="save_menu" class="topbar-btn btn-gold">
                    <i class="fas fa-save"></i> সংরক্ষণ করুন
                </button>
            </form>
        </div>
    </div>
</div>

<?php include __DIR__ . '/includes/admin_footer.php'; ?>
