<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Vote - <%= row.photo_name %></title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
</head>
<body class="bg-light">

<div class="container py-4" style="max-width:720px;">
  <a href="/voting-dresscode/" class="btn btn-outline-secondary btn-sm mb-3">← Kembali</a>

  <div class="card shadow-sm">
    <img src="<%= row.photo_url %>" style="width:100%; height:360px; object-fit:cover;">
    <div class="card-body">
      <h5 class="mb-1"><%= row.photo_name %></h5>
      <div class="text-muted small mb-3">⭐ Total: <b><%= agg.star_sum %></b> • 🗳 Votes: <b><%= agg.vote_count %></b></div>

      <div class="mb-2 fw-bold">Pilih rating:</div>
      <div class="d-flex gap-2 flex-wrap">
        <% for(let s=1;s<=5;s++){ %>
          <button class="btn btn-warning" onclick="vote(<%= row.id %>, <%= s %>)"><%= s %> ⭐</button>
        <% } %>
      </div>
    </div>
  </div>
</div>

<script>
async function vote(candidate_id, stars){
  try{
    const r = await fetch("/voting-dresscode/api/vote", {
      method:"POST",
      headers:{ "Content-Type":"application/json", "Accept":"application/json" },
      body: JSON.stringify({ candidate_id, stars })
    });
    const j = await r.json().catch(()=>({}));

    if(!r.ok || j.error){
      return Swal.fire("Gagal", j.error || "Vote gagal", "error");
    }

    Swal.fire({ title:"Terima kasih!", text:"Vote kamu sudah masuk 🙌", icon:"success", timer:1200, showConfirmButton:false })
      .then(()=> location.reload());
  }catch(err){
    console.error(err);
    Swal.fire("Error", "Server error", "error");
  }
}
</script>

</body>
</html>
