<?php
$page_title = 'Manage Testimonials';
require_once 'header.php';

$success = '';
$error = '';

// Delete
if (isset($_GET['delete']) && $pdo) {
    $pdo->prepare("DELETE FROM testimonials WHERE id=?")->execute([(int)$_GET['delete']]);
    $success = 'Testimonial deleted.';
}

// Save
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save'])) {
    $id = (int)($_POST['id'] ?? 0);
    $name = sanitize($_POST['name'] ?? '');
    $role = sanitize($_POST['role'] ?? '');
    $content = sanitize($_POST['content'] ?? '');
    $rating = min(5, max(1, (int)($_POST['rating'] ?? 5)));
    $is_active = isset($_POST['is_active']) ? 1 : 0;

    if ($name && $content) {
        if ($id) {
            $pdo->prepare("UPDATE testimonials SET name=?, role=?, content=?, rating=?, is_active=? WHERE id=?")->execute([$name, $role, $content, $rating, $is_active, $id]);
            $success = 'Testimonial updated.';
        } else {
            $pdo->prepare("INSERT INTO testimonials (name, role, content, rating, is_active) VALUES (?, ?, ?, ?, ?)")->execute([$name, $role, $content, $rating, $is_active]);
            $success = 'Testimonial created.';
        }
    } else {
        $error = 'Name and content are required.';
    }
}

$testimonials = [];
if ($pdo) {
    $testimonials = $pdo->query("SELECT * FROM testimonials ORDER BY created_at DESC")->fetchAll();
}

$editItem = null;
if (isset($_GET['edit']) && $pdo) {
    $stmt = $pdo->prepare("SELECT * FROM testimonials WHERE id=?");
    $stmt->execute([(int)$_GET['edit']]);
    $editItem = $stmt->fetch();
}
?>

<h1>Manage Testimonials</h1>
<p class="subtitle">Manage parent and student testimonials</p>

<?php if ($success): ?><div class="alert alert-success"><?php echo $success; ?></div><?php endif; ?>
<?php if ($error): ?><div class="alert alert-danger"><?php echo $error; ?></div><?php endif; ?>

<div class="card-admin mb-4">
  <h5 class="fw-bold mb-3"><?php echo $editItem ? 'Edit Testimonial' : 'Add New Testimonial'; ?></h5>
  <form method="post">
    <input type="hidden" name="id" value="<?php echo $editItem['id'] ?? 0; ?>">
    <div class="row g-3">
      <div class="col-md-6">
        <label class="form-label fw-semibold">Name</label>
        <input type="text" name="name" class="form-control" value="<?php echo $editItem ? htmlspecialchars($editItem['name']) : ''; ?>" required>
      </div>
      <div class="col-md-3">
        <label class="form-label fw-semibold">Role</label>
        <input type="text" name="role" class="form-control" value="<?php echo $editItem ? htmlspecialchars($editItem['role']) : ''; ?>" placeholder="e.g. Parent of Year 6">
      </div>
      <div class="col-md-3">
        <label class="form-label fw-semibold">Rating</label>
        <select name="rating" class="form-select">
          <?php for ($r = 5; $r >= 1; $r--): ?>
          <option value="<?php echo $r; ?>" <?php echo ($editItem && (int)$editItem['rating'] === $r) ? 'selected' : ''; ?>><?php echo $r; ?> Stars</option>
          <?php endfor; ?>
        </select>
      </div>
      <div class="col-12">
        <label class="form-label fw-semibold">Content</label>
        <textarea name="content" class="form-control" rows="4" required><?php echo $editItem ? htmlspecialchars($editItem['content']) : ''; ?></textarea>
      </div>
      <div class="col-12">
        <div class="form-check">
          <input type="checkbox" name="is_active" class="form-check-input" id="isActive" value="1" <?php echo (!$editItem || $editItem['is_active']) ? 'checked' : ''; ?>>
          <label class="form-check-label" for="isActive">Active (visible on site)</label>
        </div>
      </div>
      <div class="col-12">
        <button type="submit" name="save" class="btn btn-primary"><?php echo $editItem ? 'Update' : 'Save'; ?> Testimonial</button>
        <?php if ($editItem): ?><a href="testimonials.php" class="btn btn-outline-secondary">Cancel</a><?php endif; ?>
      </div>
    </div>
  </form>
</div>

<div class="card-admin">
  <div class="table-responsive">
    <table class="table table-admin mb-0">
      <thead>
        <tr>
          <th>Name</th>
          <th>Role</th>
          <th>Rating</th>
          <th>Active</th>
          <th>Date</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($testimonials as $t): ?>
        <tr>
          <td><?php echo htmlspecialchars($t['name']); ?></td>
          <td><?php echo htmlspecialchars($t['role']); ?></td>
          <td><?php echo str_repeat('&#9733;', (int)$t['rating']); ?></td>
          <td><?php echo $t['is_active'] ? '<span class="badge bg-success">Yes</span>' : '<span class="badge bg-secondary">No</span>'; ?></td>
          <td><?php echo date('j M Y', strtotime($t['created_at'])); ?></td>
          <td>
            <a href="?edit=<?php echo $t['id']; ?>" class="btn btn-sm btn-outline-primary btn-admin">Edit</a>
            <a href="?delete=<?php echo $t['id']; ?>" class="btn btn-sm btn-outline-danger btn-admin" onclick="return confirm('Delete?')">Delete</a>
          </td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>

  </main>
</div>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>
