<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header("Location: login.php");
    exit;
}

$jsonFile = 'admin-sidebar-links.json';
$adminLinks = file_exists($jsonFile) ? json_decode(file_get_contents($jsonFile), true) : [];

// ✅ Process Save
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $updatedLinks = [];
    $total = count($_POST['row_id']);

    for ($i = 0; $i < $total; $i++) {
        $label = trim($_POST['label'][$i] ?? '');
        if ($label === '' || isset($_POST['delete'][$i])) continue;

        $updatedLinks[] = [
            'label' => $label,
            'url' => trim($_POST['url'][$i] ?? ''),
            'icon' => trim($_POST['icon'][$i] ?? 'fa-link'),
            'new_tab' => isset($_POST['new_tab'][$i]),
            'divider_before' => isset($_POST['divider_before'][$i]),
            'enabled' => isset($_POST['enabled'][$i]),
            'fixed' => isset($_POST['fixed'][$i]),
            'order' => (int) ($_POST['order'][$i] ?? 0)
        ];
    }

    // ✅ Remove Logout if present
    $updatedLinks = array_filter($updatedLinks, fn($l) => strtolower($l['label']) !== 'logout');

    file_put_contents($jsonFile, json_encode(array_values($updatedLinks), JSON_PRETTY_PRINT));
    $adminLinks = array_values($updatedLinks);
    $message = "✅ Saved successfully.";
}
?>

<!DOCTYPE html>
<html>
<head>
  <title>Edit Admin Sidebar</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
  <style>
    body { padding: 40px; font-family: 'Segoe UI', sans-serif; }
    .form-check-input:checked { background-color: #0d6efd; }
  </style>
</head>
<body style="margin:0;">
<div style="display:flex; min-height:100vh;">

<!-- Sidebar -->
  <div class="admin-sidebar" style="width:220px; background:#343a40; padding:20px;">
    <?php include 'google-translate-widget-front-end.php'; ?>
    <?php include 'admin-links.php'; ?>
  </div>
<div class="container">



  <h2>⚙️ Edit Admin Sidebar Links</h2>
  <?php if (!empty($message)): ?><div class="alert alert-success"><?= $message ?></div><?php endif; ?>

  <form method="POST">
    <table class="table table-bordered">
      <thead class="table-light">
        <tr>
          <th>Label</th><th>URL</th><th>Icon</th>
          <th>New Tab</th><th>Divider</th><th>Enabled</th><th>Fixed</th><th>Order</th><th>Delete</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($adminLinks as $i => $link): ?>
        <tr>
          <input type="hidden" name="row_id[]" value="<?= $i ?>">
          <td><input type="text" name="label[]" value="<?= htmlspecialchars($link['label']) ?>" class="form-control"></td>
          <td><input type="text" name="url[]" value="<?= htmlspecialchars($link['url']) ?>" class="form-control"></td>
          <td><input type="text" name="icon[]" value="<?= htmlspecialchars($link['icon']) ?>" class="form-control"></td>
          <td class="text-center"><input type="checkbox" class="form-check-input" name="new_tab[<?= $i ?>]" <?= !empty($link['new_tab']) ? 'checked' : '' ?>></td>
          <td class="text-center"><input type="checkbox" class="form-check-input" name="divider_before[<?= $i ?>]" <?= !empty($link['divider_before']) ? 'checked' : '' ?>></td>
          <td class="text-center"><input type="checkbox" class="form-check-input" name="enabled[<?= $i ?>]" <?= !empty($link['enabled']) ? 'checked' : '' ?>></td>
          <td class="text-center"><input type="checkbox" class="form-check-input" name="fixed[<?= $i ?>]" <?= !empty($link['fixed']) ? 'checked' : '' ?>></td>
          <td><input type="number" name="order[]" value="<?= $link['order'] ?? 0 ?>" class="form-control"></td>
          <td class="text-center"><input type="checkbox" class="form-check-input" name="delete[<?= $i ?>]"></td>
        </tr>
        <?php endforeach; ?>

        <tr class="table-success">
          <td colspan="9"><strong>Add New Link</strong></td>
        </tr>
        <tr>
          <input type="hidden" name="row_id[]" value="<?= count($adminLinks) ?>">
          <td><input type="text" name="label[]" class="form-control"></td>
          <td><input type="text" name="url[]" class="form-control"></td>
          <td><input type="text" name="icon[]" value="fa-link" class="form-control"></td>
          <td class="text-center"><input type="checkbox" class="form-check-input" name="new_tab[<?= count($adminLinks) ?>]"></td>
          <td class="text-center"><input type="checkbox" class="form-check-input" name="divider_before[<?= count($adminLinks) ?>]"></td>
          <td class="text-center"><input type="checkbox" class="form-check-input" name="enabled[<?= count($adminLinks) ?>]" checked></td>
          <td class="text-center"><input type="checkbox" class="form-check-input" name="fixed[<?= count($adminLinks) ?>]"></td>
          <td><input type="number" name="order[]" value="0" class="form-control"></td>
          <td></td>
        </tr>
      </tbody>
    </table>
    <button type="submit" class="btn btn-primary">💾 Save</button>
  </form>
  <?php include 'admin-footer.php'; ?>
</div>
</body>
</html>
