<?php
require_once __DIR__ . '/../config.php';
requireAdmin();
$db = getDB();
$msg = '';
$action = $_GET['action'] ?? 'list';
$id     = (int)($_GET['id'] ?? 0);

if ($action === 'delete' && $id) {
    $db->prepare("DELETE FROM testimonials WHERE id=?")->execute([$id]);
    redirect(SITE_URL . '/admin/testimonials.php?msg=deleted');
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_testi'])) {
    $name    = trim($_POST['name'] ?? '');
    $company = trim($_POST['company'] ?? '');
    $city    = trim($_POST['city'] ?? '');
    $rating  = (int)($_POST['rating'] ?? 5);
    $message = trim($_POST['message'] ?? '');
    $status  = $_POST['status'] ?? 'active';
    if ($id) {
        $db->prepare("UPDATE testimonials SET name=?,company=?,city=?,rating=?,message=?,status=? WHERE id=?")->execute([$name,$company,$city,$rating,$message,$status,$id]);
        $msg = 'Updated.';
    } else {
        $db->prepare("INSERT INTO testimonials (name,company,city,rating,message,status) VALUES (?,?,?,?,?,?)")->execute([$name,$company,$city,$rating,$message,$status]);
        $msg = 'Added.'; $action = 'list';
    }
}
$testi = $id ? $db->query("SELECT * FROM testimonials WHERE id=$id")->fetch() : [];
$list  = $db->query("SELECT * FROM testimonials ORDER BY id DESC")->fetchAll();
$page_title = 'Testimonials';
include __DIR__ . '/header.php';
?>
<?php if ($msg || isset($_GET['msg'])): ?>
<div class="alert alert-success"><?= $msg ?: 'Testimonial deleted.' ?></div>
<?php endif; ?>
<div style="display:grid;grid-template-columns:1fr 1.5fr;gap:24px;">
  <div class="card">
    <div class="card-header"><h2><?= $id ? 'Edit' : 'Add' ?> Testimonial</h2></div>
    <div class="card-body">
      <form method="POST">
        <input type="hidden" name="save_testi" value="1">
        <div class="form-group"><label>Customer Name *</label><input type="text" name="name" value="<?= h($testi['name'] ?? '') ?>" required></div>
        <div class="form-group"><label>Company</label><input type="text" name="company" value="<?= h($testi['company'] ?? '') ?>"></div>
        <div class="form-group"><label>City</label><input type="text" name="city" value="<?= h($testi['city'] ?? '') ?>"></div>
        <div class="form-group"><label>Rating (1-5)</label><input type="number" name="rating" min="1" max="5" value="<?= h($testi['rating'] ?? 5) ?>"></div>
        <div class="form-group"><label>Testimonial *</label><textarea name="message" required rows="4"><?= h($testi['message'] ?? '') ?></textarea></div>
        <div class="form-group"><label>Status</label><select name="status"><option value="active" <?= ($testi['status'] ?? 'active')==='active'?'selected':'' ?>>Active</option><option value="inactive" <?= ($testi['status'] ?? '')==='inactive'?'selected':'' ?>>Inactive</option></select></div>
        <div style="display:flex;gap:10px;"><button type="submit" class="btn-primary">Save</button><?php if ($id): ?><a href="testimonials.php" class="btn-secondary">Cancel</a><?php endif; ?></div>
      </form>
    </div>
  </div>
  <div class="card">
    <div class="card-header"><h2>All Testimonials</h2></div>
    <div class="table-wrap">
      <table>
        <thead><tr><th>Name</th><th>Company</th><th>Rating</th><th>Status</th><th>Actions</th></tr></thead>
        <tbody>
          <?php foreach ($list as $t): ?>
          <tr>
            <td><strong><?= h($t['name']) ?></strong></td>
            <td><?= h($t['company']) ?>, <?= h($t['city']) ?></td>
            <td><?= str_repeat('⭐', (int)$t['rating']) ?></td>
            <td><span class="badge badge-<?= $t['status'] ?>"><?= ucfirst($t['status']) ?></span></td>
            <td class="action-btns">
              <a href="?action=edit&id=<?= $t['id'] ?>" class="btn-action btn-edit">Edit</a>
              <a href="?action=delete&id=<?= $t['id'] ?>" class="btn-action btn-delete" onclick="return confirm('Delete?')">Delete</a>
            </td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>
<?php include __DIR__ . '/footer.php'; ?>
