<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/flash.php';
require_admin();
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrf_verify($_POST['csrf'] ?? null)) {
    if (isset($_POST['delete_id'])) {
        $did = (int)($_POST['delete_id'] ?? 0);
        if ($did > 0) {
            db()->prepare('DELETE FROM pricing_plans WHERE id = ?')->execute([$did]);
            admin_flash_set('success', 'Pricing plan removed.');
        }
        header('Location: pricing.php');
        exit;
    }
    if (isset($_POST['save_pricing_header'])) {
        $sub = trim((string)($_POST['home_pricing_subtitle'] ?? ''));
        $ttl = trim((string)($_POST['home_pricing_title'] ?? ''));
        $ins = db()->prepare(
            'INSERT INTO site_settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)'
        );
        $ins->execute(['home_pricing_subtitle', $sub]);
        $ins->execute(['home_pricing_title', $ttl]);
        admin_flash_set('success', 'Pricing section heading saved.');
        header('Location: pricing.php');
        exit;
    }
}
$st = db()->query('SELECT * FROM pricing_plans ORDER BY sort_order ASC, id ASC');
$rows = $st->fetchAll();

$page = 'pricing';
$pageTitle = 'Home pricing';
$pageSubtitle = 'Cards and feature lists in the “Pricing plan” block on the home page.';
$breadcrumbs = [['label' => 'Dashboard', 'href' => 'index.php'], ['label' => 'Home pricing', 'href' => null]];
$headerActions = '<a href="pricing-edit.php" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i>Add plan</a>';
require __DIR__ . '/includes/layout-header.php';
$headSub = setting('home_pricing_subtitle', 'Pricing Plan');
$headTtl = setting('home_pricing_title', 'Our Pricing Plan For you');
?>
<div class="admin-card rounded mb-4">
  <div class="card-header">Section heading (home page)</div>
  <div class="card-body">
    <form method="post" class="row g-3 align-items-end">
      <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
      <div class="col-md-3">
        <label class="form-label small text-muted">Eyebrow / subtitle</label>
        <input type="text" name="home_pricing_subtitle" class="form-control" value="<?= h($headSub) ?>">
      </div>
      <div class="col-md-6">
        <label class="form-label small text-muted">Main title</label>
        <input type="text" name="home_pricing_title" class="form-control" value="<?= h($headTtl) ?>">
      </div>
      <div class="col-md-3">
        <button type="submit" name="save_pricing_header" value="1" class="btn btn-primary w-100">Save heading</button>
      </div>
    </form>
  </div>
</div>
<div class="admin-card rounded">
  <div class="card-header py-3">Plans (<?= count($rows) ?>)</div>
  <div class="table-responsive">
    <table class="table table-hover admin-table mb-0">
      <thead>
        <tr>
          <th>ID</th>
          <th>Plan name</th>
          <th>Price</th>
          <th>Order</th>
          <th>Status</th>
          <th class="text-end">Actions</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($rows as $r): ?>
          <tr>
            <td class="text-muted"><?= (int)$r['id'] ?></td>
            <td class="fw-semibold"><?= h((string)$r['plan_name']) ?></td>
            <td><?= h((string)$r['price_display']) ?></td>
            <td><?= (int)$r['sort_order'] ?></td>
            <td><?= !empty($r['is_active']) ? '<span class="badge bg-success">Active</span>' : '<span class="badge bg-secondary">Off</span>' ?></td>
            <td class="text-end text-nowrap">
              <a href="pricing-edit.php?id=<?= (int)$r['id'] ?>" class="btn btn-sm btn-outline-primary">Edit</a>
              <form method="post" class="d-inline" onsubmit="return confirm('Delete this plan and all its feature lines?');">
                <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
                <input type="hidden" name="delete_id" value="<?= (int)$r['id'] ?>">
                <button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
              </form>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
  <?php if (!$rows): ?><div class="card-body text-center text-muted py-4">No plans yet. <a href="pricing-edit.php">Add a plan</a> or run the database migration.</div><?php endif; ?>
</div>
<?php require __DIR__ . '/includes/layout-footer.php'; ?>
