<?php
require __DIR__ . '/../Login/config.php';
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION['user_id'])) {
  header("Location: ../Login/login.php");
  exit;
}

$user_id = $_SESSION['user_id'];
$book_id = intval($_GET['id'] ?? 0);

if ($book_id <= 0) {
  die("ID buku tidak valid.");
}

// Cek apakah user memang sudah mengembalikan buku ini
$stmt = $pdo->prepare("
  SELECT COUNT(*) 
  FROM borrowings br
  JOIN book_items bi ON br.book_item_id = bi.id
  WHERE br.user_id = ? AND bi.book_id = ? AND br.status = 'dikembalikan'
");
$stmt->execute([$user_id, $book_id]);
if ($stmt->fetchColumn() == 0) {
  die("Kamu belum bisa menulis ulasan untuk buku ini.");
}

// Ambil data buku
$stmt = $pdo->prepare("SELECT judul, gambar FROM books WHERE id = ?");
$stmt->execute([$book_id]);
$book = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$book) die("Buku tidak ditemukan.");
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tulis Ulasan | <?= htmlspecialchars($book['judul']) ?></title>
<style>
body {
  font-family: "Poppins", sans-serif;
  background: #f5f9f7;
  margin: 0;
  padding: 40px;
}
.container {
  max-width: 600px;
  margin: auto;
  background: #fff;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 3px 10px rgba(0,0,0,0.1);
}
h2 {
  color: #069797;
  text-align: center;
  margin-bottom: 25px;
}
.book-info {
  display: flex;
  gap: 20px;
  align-items: center;
  margin-bottom: 20px;
}
.book-info img {
  width: 100px;
  height: 140px;
  object-fit: cover;
  border-radius: 6px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
label { font-weight: 500; display: block; margin: 10px 0 5px; }
select, textarea {
  width: 100%;
  padding: 8px;
  border-radius: 6px;
  border: 1px solid #ccc;
}
button {
  background: #069797;
  color: white;
  padding: 10px 15px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  width: 100%;
  margin-top: 15px;
  font-size: 15px;
}
button:hover { background: #057c7c; }
</style>
</head>
<body>
<div class="container">
  <h2>Tulis Ulasan</h2>
  <div class="book-info">
    <img src="<?= htmlspecialchars('../' . $book['gambar']) ?>" alt="<?= htmlspecialchars($book['judul']) ?>">
    <div>
      <h3><?= htmlspecialchars($book['judul']) ?></h3>
      <p>Beri rating dan komentar untuk buku ini</p>
    </div>
  </div>
  
  <form action="submit_review.php" method="POST">
    <input type="hidden" name="book_id" value="<?= $book_id ?>">
    
    <label for="rating">Rating:</label>
    <select name="rating" id="rating" required>
      <option value="">-- Pilih --</option>
      <option value="5">★★★★★ (5)</option>
      <option value="4">★★★★☆ (4)</option>
      <option value="3">★★★☆☆ (3)</option>
      <option value="2">★★☆☆☆ (2)</option>
      <option value="1">★☆☆☆☆ (1)</option>
    </select>
    
    <label for="komentar">Komentar:</label>
    <textarea name="komentar" id="komentar" rows="4" placeholder="Tulis pendapatmu tentang buku ini..." required></textarea>
    
    <button type="submit">Kirim Ulasan</button>
  </form>
</div>
</body>
</html>
