<?php
session_start();
if (!isset($_SESSION["admin"]) || $_SESSION["admin"] !== "Digital2026") {
  header("Location: index.php?error=Anda tidak dibenarkan akses modul admin");
  exit;
}

include 'dbconn.php';

$sql = "SELECT candIC, candName, candCourse, COALESCE(voteCount, 0) AS voteCount FROM tblcandidate ORDER BY COALESCE(voteCount, 0) DESC, candName ASC";
$result = mysqli_query($conn, $sql);

$rows = array();
$totalVotes = 0;
$highestVotes = 0;

if ($result) {
  while ($row = mysqli_fetch_assoc($result)) {
    $row['voteCount'] = (int)$row['voteCount'];
    $totalVotes += $row['voteCount'];
    if ($row['voteCount'] > $highestVotes) {
      $highestVotes = $row['voteCount'];
    }
    $rows[] = $row;
  }
}

$jumlahCalon = count($rows);
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">
  <title>Keputusan Pemilihan - Admin</title>
  <link href="assets/img/kvs.png" rel="icon">
  <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  <style>
    body { background: #f4f7fb; font-family: "Poppins", sans-serif; }
    .panel { background: #fff; border-radius: 14px; box-shadow: 0 10px 25px rgba(0,0,0,.08); }
    .summary-box { border-radius: 12px; background: #eef5ff; }
    .winner-row { background: #e9fff2 !important; font-weight: 600; }
    @media print {
      .no-print { display: none !important; }
      body { background: #fff; }
      .panel { box-shadow: none; }
    }
  </style>
</head>
<body>
  <div class="container py-4 py-md-5">
    <div class="d-flex justify-content-between align-items-center mb-3 no-print">
      <h3 class="mb-0">Keputusan Pemilihan MPP <?php echo date("Y"); ?></h3>
      <div>
        <a href="admin.php" class="btn btn-outline-secondary mr-2">Kembali Admin</a>
        <button class="btn btn-primary" onclick="window.print()">Cetak</button>
      </div>
    </div>

    <div class="row mb-3">
      <div class="col-md-4 mb-2">
        <div class="summary-box p-3">
          <small class="text-muted d-block">Jumlah Calon</small>
          <strong><?php echo $jumlahCalon; ?></strong>
        </div>
      </div>
      <div class="col-md-4 mb-2">
        <div class="summary-box p-3">
          <small class="text-muted d-block">Jumlah Undian Keseluruhan</small>
          <strong><?php echo $totalVotes; ?></strong>
        </div>
      </div>
      <div class="col-md-4 mb-2">
        <div class="summary-box p-3">
          <small class="text-muted d-block">Undian Tertinggi</small>
          <strong><?php echo $highestVotes; ?></strong>
        </div>
      </div>
    </div>

    <div class="panel p-3 p-md-4">
      <h5 class="mb-3">Senarai Nama Mengikut Undian Tertinggi</h5>
      <div class="table-responsive mb-4">
        <table class="table table-bordered mb-0">
          <thead>
            <tr>
              <th>Kedudukan</th>
              <th>Nama Calon</th>
              <th>Jumlah Undian</th>
            </tr>
          </thead>
          <tbody>
            <?php if ($jumlahCalon === 0) { ?>
              <tr>
                <td colspan="3" class="text-center text-muted">Tiada data calon dijumpai.</td>
              </tr>
            <?php } else { ?>
              <?php foreach ($rows as $index => $row) { ?>
                <tr class="<?php echo ($row['voteCount'] === $highestVotes && $highestVotes > 0) ? 'winner-row' : ''; ?>">
                  <td><?php echo $index + 1; ?></td>
                  <td><?php echo htmlspecialchars($row["candName"]); ?></td>
                  <td><?php echo $row["voteCount"]; ?></td>
                </tr>
              <?php } ?>
            <?php } ?>
          </tbody>
        </table>
      </div>

      <div class="table-responsive">
        <table class="table table-striped table-hover mb-0">
          <thead class="thead-dark">
            <tr>
              <th>Kedudukan</th>
              <th>ID Calon</th>
              <th>Nama Calon</th>
              <th>Kelas</th>
              <th>Bilangan Undian</th>
            </tr>
          </thead>
          <tbody>
            <?php if ($jumlahCalon === 0) { ?>
              <tr>
                <td colspan="5" class="text-center text-muted">Tiada data calon dijumpai.</td>
              </tr>
            <?php } else { ?>
              <?php foreach ($rows as $index => $row) { ?>
                <tr class="<?php echo ($row['voteCount'] === $highestVotes && $highestVotes > 0) ? 'winner-row' : ''; ?>">
                  <td><?php echo $index + 1; ?></td>
                  <td><?php echo htmlspecialchars($row["candIC"]); ?></td>
                  <td><?php echo htmlspecialchars($row["candName"]); ?></td>
                  <td><?php echo htmlspecialchars($row["candCourse"]); ?></td>
                  <td><?php echo $row["voteCount"]; ?></td>
                </tr>
              <?php } ?>
            <?php } ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</body>
</html>