<?php
// hasil_test_qc.php

// Konfigurasi koneksi 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);
}

// Ambil semua data hasil tes
$sql = "SELECT * FROM test_qc ORDER BY id DESC";
$result = $conn->query($sql);

// Fungsi untuk menghitung jumlah peserta tes
function getTotalParticipants($conn) {
    $sql = "SELECT COUNT(DISTINCT nama) as total FROM test_qc";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();
    return $row['total'];
}

$total_participants = getTotalParticipants($conn);
?>

<!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 QC</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 20px;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            max-width: 1200px;
            margin: 0 auto;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 30px;
        }
        .stats {
            background: #e8f4fc;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 30px;
            display: flex;
            justify-content: space-around;
            flex-wrap: wrap;
        }
        .stat-box {
            background: white;
            padding: 15px 20px;
            border-radius: 6px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            margin: 10px;
            text-align: center;
            min-width: 200px;
        }
        .stat-box h3 {
            margin-top: 0;
            color: #3498db;
        }
        .stat-box p {
            font-size: 24px;
            font-weight: bold;
            margin: 10px 0 0 0;
            color: #2c3e50;
        }
        .search-box {
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
        }
        .search-box input {
            padding: 10px 15px;
            border: 1px solid #ddd;
            border-radius: 6px;
            width: 300px;
            font-size: 16px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #3498db;
            color: white;
            position: sticky;
            top: 0;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .view-btn {
            background-color: #28a745;
            color: white;
            border: none;
            padding: 6px 12px;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            font-size: 14px;
        }
        .view-btn:hover {
            background-color: #218838;
        }
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0,0,0,0.4);
        }
        .modal-content {
            background-color: #fefefe;
            margin: 5% auto;
            padding: 30px;
            border-radius: 10px;
            width: 70%;
            max-width: 800px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.3);
        }
        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
        }
        .close:hover {
            color: black;
        }
        .answer-section {
            margin-top: 20px;
        }
        .answer-section h3 {
            color: #2c3e50;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }
        .answer-section p {
            background: #f9f9f9;
            padding: 15px;
            border-radius: 6px;
            white-space: pre-wrap;
        }
        @media (max-width: 768px) {
            .container {
                padding: 15px;
            }
            .stat-box {
                min-width: 100%;
                margin: 5px 0;
            }
            .search-box input {
                width: 100%;
                margin-bottom: 10px;
            }
            table {
                font-size: 14px;
            }
            th, td {
                padding: 8px 10px;
            }
            .modal-content {
                width: 95%;
                margin: 10% auto;
                padding: 15px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Hasil Tes Supervisor Quality Control</h1>
        
        <div class="stats">
            <div class="stat-box">
                <h3>Total Peserta</h3>
                <p><?php echo $total_participants; ?></p>
            </div>
            <div class="stat-box">
                <h3>Total Tes</h3>
                <p><?php echo $result->num_rows; ?></p>
            </div>
            <div class="stat-box">
                <h3>Terakhir Diupdate</h3>
                <p><?php echo date('d M Y H:i'); ?></p>
            </div>
        </div>
        
        <div class="search-box">
            <input type="text" id="searchInput" placeholder="Cari berdasarkan nama..." onkeyup="searchTable()">
        </div>
        
        <table id="resultsTable">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Nama Kandidat</th>
                    <th>Soal</th>
                    <th>Tanggal Tes</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if ($result->num_rows > 0) {
                    $no = 1;
                    while($row = $result->fetch_assoc()) {
                        echo "<tr>";
                        echo "<td>".$no++."</td>";
                        echo "<td>".htmlspecialchars($row['nama'])."</td>";
                        echo "<td>".substr(htmlspecialchars($row['soal']), 0, 50)."...</td>";
                        echo "<td>".date('d M Y H:i', strtotime($row['timestamp'] ?? 'now'))."</td>";
                        echo "<td><button class='view-btn' onclick='openModal(\"".addslashes($row['nama'])."\", \"".addslashes($row['soal'])."\", \"".addslashes($row['jawaban'])."\")'>Lihat Jawaban</button></td>";
                        echo "</tr>";
                    }
                } else {
                    echo "<tr><td colspan='5' style='text-align:center;'>Tidak ada data hasil tes</td></tr>";
                }
                ?>
            </tbody>
        </table>
    </div>
    
    <!-- Modal untuk menampilkan jawaban lengkap -->
    <div id="answerModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal()">&times;</span>
            <h2 id="modalTitle"></h2>
            <div class="answer-section">
                <h3>Soal:</h3>
                <p id="modalQuestion"></p>
            </div>
            <div class="answer-section">
                <h3>Jawaban:</h3>
                <p id="modalAnswer"></p>
            </div>
        </div>
    </div>
    
    <script>
        // Fungsi untuk membuka modal
        function openModal(nama, soal, jawaban) {
            document.getElementById('modalTitle').innerText = "Jawaban dari " + nama;
            document.getElementById('modalQuestion').innerText = soal;
            document.getElementById('modalAnswer').innerText = jawaban;
            document.getElementById('answerModal').style.display = "block";
        }
        
        // Fungsi untuk menutup modal
        function closeModal() {
            document.getElementById('answerModal').style.display = "none";
        }
        
        // Tutup modal ketika klik di luar area modal
        window.onclick = function(event) {
            var modal = document.getElementById('answerModal');
            if (event.target == modal) {
                closeModal();
            }
        }
        
        // Fungsi untuk pencarian
        function searchTable() {
            var input, filter, table, tr, td, i, txtValue;
            input = document.getElementById("searchInput");
            filter = input.value.toUpperCase();
            table = document.getElementById("resultsTable");
            tr = table.getElementsByTagName("tr");
            
            for (i = 0; i < tr.length; i++) {
                td = tr[i].getElementsByTagName("td")[1]; // Kolom nama
                if (td) {
                    txtValue = td.textContent || td.innerText;
                    if (txtValue.toUpperCase().indexOf(filter) > -1) {
                        tr[i].style.display = "";
                    } else {
                        tr[i].style.display = "none";
                    }
                }       
            }
        }
    </script>
</body>
</html>