<?php
session_start();
if(!isset($_SESSION['admin_id'])){
  header("Location: index.php");
  exit;
}

require_once "../db.php";

/* ===== CURRENT FILTER ===== */
$status = $_REQUEST['status'] ?? 0;

/* ===== UPDATE STATUS ===== */
if(isset($_POST['update_status'])){
  $pdo->prepare(
    "UPDATE reviews SET status=? WHERE id=?"
  )->execute([
    (int)$_POST['new_status'],
    (int)$_POST['review_id']
  ]);
}

/* ===== FETCH REVIEWS ===== */
$stmt = $pdo->prepare(
  "SELECT * FROM reviews WHERE status=? ORDER BY created_at DESC"
);
$stmt->execute([$status]);
$reviews = $stmt->fetchAll();

function statusText($s){
  return $s==0?'Pending':($s==1?'Approved':'Rejected');
}
?>
<style>
/* CONTENT-ONLY STYLES */

.review-header{
  display:flex;
  justify-content:space-between;
  align-items:center;
  margin-bottom:14px;
  flex-wrap:wrap;
  gap:10px;
}
.review-header h2{margin:0}

.review-header select{
  padding:8px 12px;
  border-radius:10px;
  border:1px solid #cbd5e1;
}

.review-card{
  background:#fff;
  border-radius:16px;
  padding:14px;
  margin-bottom:14px;
  box-shadow:0 6px 16px rgba(0,0,0,.08);
}

.review-top{
  display:flex;
  justify-content:space-between;
  gap:10px;
}
.review-name{font-weight:600}
.review-rating{
  background:#16a34a;
  color:#fff;
  padding:4px 8px;
  border-radius:8px;
  font-size:13px;
}

.review-message{
  margin:10px 0;
  font-size:14px;
  color:#334155;
  line-height:1.5;
}

.badge{
  display:inline-block;
  font-size:12px;
  padding:4px 10px;
  border-radius:8px;
  margin-bottom:6px;
}
.pending{background:#fde68a}
.approved{background:#bbf7d0}
.rejected{background:#fecaca}

.review-actions{
  display:flex;
  gap:8px;
  flex-wrap:wrap;
  margin-top:8px;
}
.review-actions select,
.review-actions button{
  padding:6px 12px;
  border-radius:8px;
  border:1px solid #cbd5e1;
}
.review-actions button{
  background:#2563eb;
  color:#fff;
  border:none;
}

.review-date{
  font-size:12px;
  color:#64748b;
  margin-top:6px;
}

@media(max-width:600px){
  .review-top{flex-direction:column;align-items:flex-start}
}
</style>

<div class="review-header">
  <h2>⭐ Manage Reviews</h2>

  <form method="get">
    <input type="hidden" name="page" value="manage_reviews">
    <select name="status" onchange="this.form.submit()">
      <option value="0" <?= $status==0?'selected':'' ?>>Pending</option>
      <option value="1" <?= $status==1?'selected':'' ?>>Approved</option>
      <option value="2" <?= $status==2?'selected':'' ?>>Rejected</option>
    </select>
  </form>
</div>

<?php if(!$reviews): ?>
  <p>No reviews found.</p>
<?php endif; ?>

<?php foreach($reviews as $r): ?>
<div class="review-card">

  <div class="review-top">
    <div class="review-name"><?= htmlspecialchars($r['name']) ?></div>
    <div class="review-rating">⭐ <?= $r['rating'] ?>/5</div>
  </div>

  <div class="review-message">
    <?= nl2br(htmlspecialchars($r['message'])) ?>
  </div>

  <span class="badge <?= $r['status']==0?'pending':($r['status']==1?'approved':'rejected') ?>">
    <?= statusText($r['status']) ?>
  </span>

  <form method="post" class="review-actions">
    <input type="hidden" name="page" value="manage_reviews">
    <input type="hidden" name="status" value="<?= $status ?>">
    <input type="hidden" name="review_id" value="<?= $r['id'] ?>">

    <select name="new_status">
      <option value="0" <?= $r['status']==0?'selected':'' ?>>Pending</option>
      <option value="1" <?= $r['status']==1?'selected':'' ?>>Approve</option>
      <option value="2" <?= $r['status']==2?'selected':'' ?>>Reject</option>
    </select>

    <button name="update_status">Update</button>
  </form>

  <div class="review-date">
    <?= date("d M Y, h:i A", strtotime($r['created_at'])) ?>
  </div>
</div>
<?php endforeach; ?>
