<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require "../config.php";

$id = $_GET['id'] ?? 0;
$q = $conn->query("SELECT * FROM survei_kepuasan WHERE id='$id'");
$d = $q->fetch_assoc();

if(!$d){
    die("Data tidak ditemukan");
}

/* ================= LABEL NILAI ================= */
function labelNilai($n){
    switch($n){
        case 4: return "<span class='badge bg-success'>Sangat Baik</span>";
        case 3: return "<span class='badge bg-primary'>Baik</span>";
        case 2: return "<span class='badge bg-warning text-dark'>Cukup</span>";
        case 1: return "<span class='badge bg-danger'>Kurang</span>";
        default: return "-";
    }
}

/* ================= PERTANYAAN ================= */
$pertanyaan = [
    1 => "Persyaratan pelayanan",
    2 => "Prosedur pelayanan",
    3 => "Waktu pelayanan",
    4 => "Biaya/tarif",
    5 => "Produk pelayanan",
    6 => "Kompetensi petugas",
    7 => "Perilaku petugas",
    8 => "Penanganan pengaduan",
    9 => "Sarana dan prasarana",
    10 => "Kualitas sistem pelayanan"
];

/* ================= HITUNG RATA2 ================= */
$total = 0;
$count = 0;

for($i=1;$i<=10;$i++){
    if(isset($d["p$i"])){
        $total += (int)$d["p$i"];
        $count++;
    }
}

$rata = $count ? round($total/$count,2) : 0;
?>

<?php include 'layout.php'; ?>

<h3 class="mb-3">📋 Detail Survei Kepuasan</h3>

<div class="card shadow p-4">

<!-- ================= BIODATA ================= -->
<h5 class="mb-3">👤 Data Responden</h5>

<table class="table table-bordered">
<tr><td>Nama</td><td><?= $d['nama'] ?></td></tr>
<tr><td>Email</td><td><?= $d['email'] ?></td></tr>
<tr><td>Jenis Kelamin</td><td><?= $d['jenis_kelamin'] ?></td></tr>
<tr><td>Pekerjaan</td><td><?= $d['pekerjaan'] ?></td></tr>
<tr><td>Instansi</td><td><?= $d['instansi'] ?></td></tr>
<tr><td>Pendidikan</td><td><?= $d['pendidikan'] ?></td></tr>
<tr><td>Tanggal</td><td><?= $d['created_at'] ?></td></tr>
</table>

<!-- ================= NILAI ================= -->
<h5 class="mt-4">📊 Hasil Penilaian</h5>

<div class="alert alert-success text-center">
<b>Nilai Rata-rata: <?= $rata ?></b>
</div>

<table class="table table-bordered">
<thead class="table-dark text-center">
<tr>
<th>No</th>
<th>Pertanyaan</th>
<th>Jawaban</th>
</tr>
</thead>

<tbody>

<?php foreach($pertanyaan as $i => $tanya){ ?>
<tr class="<?= ($d["p$i"] >= 3) ? 'table-success' : 'table-warning' ?>">
<td class="text-center"><?= $i ?></td>
<td><?= $tanya ?></td>
<td class="text-center"><?= labelNilai($d["p$i"]) ?></td>
</tr>
<?php } ?>

</tbody>
</table>

<!-- ================= SARAN ================= -->
<h5 class="mt-4">💬 Saran</h5>

<div class="alert alert-info">
<?= $d['saran'] ?: 'Tidak ada saran' ?>
</div>

<!-- ================= AKSI ================= -->
<div class="text-center mt-3">
<a href="survei.php" class="btn btn-secondary">Kembali</a>
</div>

</div>

<?php include 'layout_footer.php'; ?>