<?php
// hasil_rest_spv.php

require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

// Koneksi ke database
define('DB_HOST', 'localhost');
define('DB_USER', 'wiselie_nanda');
define('DB_PASS', 'Clemira123');
define('DB_NAME', 'wiselie_inventory');

$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($conn->connect_error) {
    die("Koneksi gagal: " . $conn->connect_error);
}

if (isset($_GET['export']) && $_GET['export'] == 'excel') {
    $sql = "SELECT nama, soal, jawaban FROM test_spv_warehouse ORDER BY nama, soal";
    $result = $conn->query($sql);

    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();

    $sheet->setCellValue('A1', 'Nama');
    $sheet->setCellValue('B1', 'Soal');
    $sheet->setCellValue('C1', 'Jawaban');

    $row = 2;
    while ($data = $result->fetch_assoc()) {
        $sheet->setCellValue('A' . $row, $data['nama']);
        $sheet->setCellValue('B' . $row, $data['soal']);
        $sheet->setCellValue('C' . $row, $data['jawaban']);
        $row++;
    }

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="hasil_test_spv_warehouse.xlsx"');
    header('Cache-Control: max-age=0');

    $writer = new Xlsx($spreadsheet);
    $writer->save('php://output');
    exit;
}

$sql = "SELECT nama, soal, jawaban FROM test_spv_warehouse ORDER BY nama, soal";
$result = $conn->query($sql);

$jawaban_per_kandidat = [];

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $jawaban_per_kandidat[$row['nama']][] = [
            'soal' => $row['soal'],
            'jawaban' => $row['jawaban']
        ];
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hasil Tes Supervisor Warehouse</title>
    <style>
        body { font-family: Arial, sans-serif; background-color: #f4f6f8; padding: 20px; }
        .container { max-width: 1000px; margin: auto; background: #fff; padding: 30px; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
        h1 { text-align: center; color: #333; margin-bottom: 40px; }
        h2 { margin-top: 40px; color: #007bff; border-bottom: 2px solid #eee; padding-bottom: 10px; }
        .soal { font-weight: bold; margin-top: 20px; }
        .jawaban { background: #f9f9f9; padding: 10px; border-left: 4px solid #007bff; margin-top: 5px; white-space: pre-wrap; border-radius: 5px; }
        .export-btn { text-align: center; margin-bottom: 30px; }
        .export-btn a { background-color: #28a745; color: #fff; padding: 10px 20px; text-decoration: none; border-radius: 6px; font-weight: bold; }
        .export-btn a:hover { background-color: #218838; }
    </style>
</head>
<body>
    <div class="container">
        <h1>Hasil Tes Supervisor Warehouse</h1>
        <div class="export-btn">
            <a href="?export=excel">Export ke Excel</a>
        </div>
        <?php if (!empty($jawaban_per_kandidat)): ?>
            <?php foreach ($jawaban_per_kandidat as $nama => $jawabans): ?>
                <h2><?= htmlspecialchars($nama) ?></h2>
                <?php foreach ($jawabans as $item): ?>
                    <div class="soal"><?= htmlspecialchars($item['soal']) ?></div>
                    <div class="jawaban"><?= nl2br(htmlspecialchars($item['jawaban'])) ?></div>
                <?php endforeach; ?>
            <?php endforeach; ?>
        <?php else: ?>
            <p>Belum ada jawaban yang tersedia.</p>
        <?php endif; ?>
    </div>
</body>
</html>
