<?php
require_once 'auth.php';
include 'db.php';
requireLogin($conn);

// Semak status & role
$user_email = $_SESSION['email'] ?? '';
$check_status = mysqli_prepare($conn, "SELECT status, role FROM users WHERE email = ?");
mysqli_stmt_bind_param($check_status, "s", $user_email);
mysqli_stmt_execute($check_status);
$status_result = mysqli_stmt_get_result($check_status);
$user_data = mysqli_fetch_assoc($status_result);

if (!$user_data || $user_data['status'] !== 'approved') {
    destroyActiveSession();
    header("Location: login.php?error=account_blocked");
    exit();
}

if (!isset($_SESSION['role'])) {
    $_SESSION['role'] = $user_data['role'] ?? 'user';
}

$role = $_SESSION['role'];
$user_id = (int) ($_SESSION['user_id'] ?? 0);

$success_message = '';
$error_message = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['submit_uat'])) {
        // Save user info
        $department = mysqli_real_escape_string($conn, $_POST['department'] ?? '');
        $position = mysqli_real_escape_string($conn, $_POST['position'] ?? '');
        $phone = mysqli_real_escape_string($conn, $_POST['phone'] ?? '');
        $notes = mysqli_real_escape_string($conn, $_POST['additional_notes'] ?? '');
        
        $check_user_info = mysqli_prepare($conn, "SELECT id FROM uat_user_info WHERE user_id = ?");
        mysqli_stmt_bind_param($check_user_info, "i", $user_id);
        mysqli_stmt_execute($check_user_info);
        $existing_user_info = mysqli_stmt_get_result($check_user_info);
        
        if (mysqli_num_rows($existing_user_info) > 0) {
            $update_user_info = mysqli_prepare($conn, "UPDATE uat_user_info SET department = ?, position = ?, phone = ?, additional_notes = ?, completed_at = CURRENT_TIMESTAMP WHERE user_id = ?");
            mysqli_stmt_bind_param($update_user_info, "ssssi", $department, $position, $phone, $notes, $user_id);
            mysqli_stmt_execute($update_user_info);
        } else {
            $insert_user_info = mysqli_prepare($conn, "INSERT INTO uat_user_info (user_id, department, position, phone, additional_notes) VALUES (?, ?, ?, ?, ?)");
            mysqli_stmt_bind_param($insert_user_info, "issss", $user_id, $department, $position, $phone, $notes);
            mysqli_stmt_execute($insert_user_info);
        }
        
        // Save responses
        if (isset($_POST['responses']) && is_array($_POST['responses'])) {
            foreach ($_POST['responses'] as $qkey => $response) {
                $status = mysqli_real_escape_string($conn, $response['status'] ?? 'belum_test');
                $notes = mysqli_real_escape_string($conn, $response['notes'] ?? '');
                
                // Use a hash of the key as a numeric question_id
                $qid = crc32($qkey);
                
                $check_response = mysqli_prepare($conn, "SELECT id FROM uat_responses WHERE user_id = ? AND question_id = ?");
                mysqli_stmt_bind_param($check_response, "ii", $user_id, $qid);
                mysqli_stmt_execute($check_response);
                $existing = mysqli_stmt_get_result($check_response);
                
                if (mysqli_num_rows($existing) > 0) {
                    $row = mysqli_fetch_assoc($existing);
                    $update_response = mysqli_prepare($conn, "UPDATE uat_responses SET status = ?, notes = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?");
                    mysqli_stmt_bind_param($update_response, "ssi", $status, $notes, $row['id']);
                    mysqli_stmt_execute($update_response);
                } else {
                    $insert_response = mysqli_prepare($conn, "INSERT INTO uat_responses (user_id, question_id, status, notes) VALUES (?, ?, ?, ?)");
                    mysqli_stmt_bind_param($insert_response, "iiss", $user_id, $qid, $status, $notes);
                    mysqli_stmt_execute($insert_response);
                }
            }
        }
        
        $success_message = 'UAT anda telah berjaya disimpan!';
    }
    
    // Handle Pass Module action
    if (isset($_POST['pass_module']) && isset($_POST['module_name'])) {
        $module_name = mysqli_real_escape_string($conn, $_POST['module_name']);
        
        $get_questions = mysqli_prepare($conn, "SELECT id FROM uat_questions WHERE module = ? AND is_active = 1");
        mysqli_stmt_bind_param($get_questions, "s", $module_name);
        mysqli_stmt_execute($get_questions);
        $questions_result = mysqli_stmt_get_result($get_questions);
        
        while ($question = mysqli_fetch_assoc($questions_result)) {
            $question_id = $question['id'];
            
            $check_response = mysqli_prepare($conn, "SELECT id FROM uat_responses WHERE user_id = ? AND question_id = ?");
            mysqli_stmt_bind_param($check_response, "ii", $user_id, $question_id);
            mysqli_stmt_execute($check_response);
            $existing_response = mysqli_stmt_get_result($check_response);
            
            if (mysqli_num_rows($existing_response) > 0) {
                $update_response = mysqli_prepare($conn, "UPDATE uat_responses SET status = 'pass', updated_at = CURRENT_TIMESTAMP WHERE user_id = ? AND question_id = ?");
                mysqli_stmt_bind_param($update_response, "ii", $user_id, $question_id);
                mysqli_stmt_execute($update_response);
            } else {
                $insert_response = mysqli_prepare($conn, "INSERT INTO uat_responses (user_id, question_id, status) VALUES (?, ?, 'pass')");
                mysqli_stmt_bind_param($insert_response, "ii", $user_id, $question_id);
                mysqli_stmt_execute($insert_response);
            }
        }
        
        $success_message = 'Modul ' . htmlspecialchars($module_name) . ' telah ditandakan sebagai Pass!';
    }
    
    // Handle Reset Module action
    if (isset($_POST['reset_module']) && isset($_POST['module_name'])) {
        $module_name = mysqli_real_escape_string($conn, $_POST['module_name']);
        
        $delete_responses = mysqli_prepare($conn, "DELETE r FROM uat_responses r 
                                                 JOIN uat_questions q ON r.question_id = q.id 
                                                 WHERE r.user_id = ? AND q.module = ?");
        mysqli_stmt_bind_param($delete_responses, "is", $user_id, $module_name);
        mysqli_stmt_execute($delete_responses);
        
        $success_message = 'Modul ' . htmlspecialchars($module_name) . ' telah direset!';
    }
    
    // Handle Pass All action
    if (isset($_POST['pass_all'])) {
        $get_all_questions = mysqli_prepare($conn, "SELECT id FROM uat_questions WHERE is_active = 1");
        mysqli_stmt_execute($get_all_questions);
        $questions_result = mysqli_stmt_get_result($get_all_questions);
        
        while ($question = mysqli_fetch_assoc($questions_result)) {
            $question_id = $question['id'];
            
            $check_response = mysqli_prepare($conn, "SELECT id FROM uat_responses WHERE user_id = ? AND question_id = ?");
            mysqli_stmt_bind_param($check_response, "ii", $user_id, $question_id);
            mysqli_stmt_execute($check_response);
            $existing_response = mysqli_stmt_get_result($check_response);
            
            if (mysqli_num_rows($existing_response) > 0) {
                $update_response = mysqli_prepare($conn, "UPDATE uat_responses SET status = 'pass', updated_at = CURRENT_TIMESTAMP WHERE user_id = ? AND question_id = ?");
                mysqli_stmt_bind_param($update_response, "ii", $user_id, $question_id);
                mysqli_stmt_execute($update_response);
            } else {
                $insert_response = mysqli_prepare($conn, "INSERT INTO uat_responses (user_id, question_id, status) VALUES (?, ?, 'pass')");
                mysqli_stmt_bind_param($insert_response, "ii", $user_id, $question_id);
                mysqli_stmt_execute($insert_response);
            }
        }
        
        $success_message = 'Semua item telah ditandakan sebagai Pass!';
    }
    
    // Handle Reset All action
    if (isset($_POST['reset_all'])) {
        $delete_all_responses = mysqli_prepare($conn, "DELETE FROM uat_responses WHERE user_id = ?");
        mysqli_stmt_bind_param($delete_all_responses, "i", $user_id);
        mysqli_stmt_execute($delete_all_responses);
        
        $success_message = 'Semua jawapan telah direset!';
    }
}

// Get user info
$user_info_stmt = mysqli_prepare($conn, "SELECT * FROM uat_user_info WHERE user_id = ?");
mysqli_stmt_bind_param($user_info_stmt, "i", $user_id);
mysqli_stmt_execute($user_info_stmt);
$user_info_result = mysqli_stmt_get_result($user_info_stmt);
$user_info = mysqli_fetch_assoc($user_info_result);

// Hardcoded modules from UAT text file
$checklist_modules = [
    'MODUL LOGIN' => [
        ['title' => 'Adakah pengguna boleh memasukkan username dan password untuk log masuk?', 'expected' => 'Pengguna berjaya masuk ke dalam sistem jika maklumat betul'],
        ['title' => 'Adakah sistem memaparkan mesej ralat jika username atau password salah?', 'expected' => 'Mesej ralat dipaparkan'],
        ['title' => 'Adakah sistem menghalang pengguna daripada mengakses sistem tanpa login?', 'expected' => 'Pengguna diarahkan ke halaman login'],
        ['title' => 'Adakah fungsi "Remember Me" berfungsi dengan baik?', 'expected' => 'Maklumat login pengguna disimpan mengikut tetapan'],
        ['title' => 'Adakah pengguna boleh logout daripada sistem?', 'expected' => 'Sesi pengguna berjaya ditamatkan.'],
    ],
    'MODUL DASHBOARD' => [
        ['title' => 'Adakah dashboard berjaya dipaparkan selepas pengguna login?', 'expected' => 'Dashboard dipaparkan dengan lengkap.'],
        ['title' => 'Adakah jumlah rekod fail dipaparkan dengan betul?', 'expected' => 'Jumlah rekod adalah sama dengan data sistem'],
        ['title' => 'Adakah paparan statistik atau carta menunjukkan maklumat yang tepat?', 'expected' => 'Data carta adalah berdasarkan rekod sebenar'],
        ['title' => 'Adakah menu navigasi sistem boleh digunakan?', 'expected' => 'Semua menu boleh diakses dan berfungsi dengan baik.'],
    ],
    'MODUL PENDAFTARAN FAIL / REKOD BAHARU' => [
        ['title' => 'Adakah pengguna boleh menambah rekod fail baharu?', 'expected' => 'Rekod fail berjaya disimpan ke dalam sistem.'],
        ['title' => 'Adakah sistem menyemak maklumat wajib sebelum penyimpanan dibuat?', 'expected' => 'Sistem meminta pengguna melengkapkan maklumat'],
        ['title' => 'Adakah maklumat fail yang dimasukkan disimpan dengan tepat?', 'expected' => 'Maklumat fail berjaya direkodkan dengan tepat.'],
        ['title' => 'Adakah sistem menjana nombor rujukan fail dengan betul?', 'expected' => 'Nombor rujukan fail dijana secara unik.'],
        ['title' => 'Adakah mesej berjaya dipaparkan selepas rekod disimpan?', 'expected' => 'Sistem memaparkan mesej berjaya selepas rekod disimpan.'],
    ],
    'MODUL PAPARAN SENARAI FAIL' => [
        ['title' => 'Adakah semua rekod fail dipaparkan dalam senarai?', 'expected' => 'Semua rekod fail dipaparkan.'],
        ['title' => 'Adakah maklumat fail yang dipaparkan adalah tepat?', 'expected' => 'Maklumat yang dipaparkan adalah tepat berdasarkan rekod sistem.'],
        ['title' => 'Adakah fungsi pagination berfungsi dengan baik?', 'expected' => 'Pengguna boleh melihat halaman lain'],
        ['title' => 'Adakah pengguna boleh melihat maklumat terperinci fail?', 'expected' => 'Maklumat lengkap fail berjaya dipaparkan.'],
    ],
    'MODUL KEMASKINI FAIL' => [
        ['title' => 'Adakah pengguna boleh mengubah maklumat fail?', 'expected' => 'Maklumat fail berjaya dikemaskini.'],
        ['title' => 'Adakah perubahan maklumat disimpan dengan betul?', 'expected' => 'Data terkini dipaparkan dalam sistem.'],
        ['title' => 'Adakah sistem melakukan semakan sebelum kemaskini?', 'expected' => 'Sistem melakukan validasi dan meminta pengguna melengkapkan maklumat.'],
    ],
    'MODUL PADAM FAIL' => [
        ['title' => 'Adakah pengguna boleh memadam rekod fail?', 'expected' => 'Rekod berjaya dipadam'],
        ['title' => 'Adakah sistem memaparkan pengesahan sebelum proses padam dilakukan?', 'expected' => 'Popup pengesahan dipaparkan'],
        ['title' => 'Adakah rekod yang telah dipadam tidak lagi dipaparkan?', 'expected' => 'Rekod dikeluarkan daripada sistem.'],
    ],
    'MODUL CARIAN FAIL' => [
        ['title' => 'Adakah pengguna boleh mencari fail menggunakan kata kunci?', 'expected' => 'Sistem memaparkan rekod yang berkaitan.'],
        ['title' => 'Adakah carian berdasarkan nombor fail berfungsi?', 'expected' => 'Fail yang sepadan berjaya dipaparkan.'],
        ['title' => 'Adakah sistem memaparkan mesej jika tiada rekod dijumpai?', 'expected' => 'Sistem memaparkan mesej "Tiada rekod dijumpai".'],
    ],
    'MODUL PROFIL PENGGUNA' => [
        ['title' => 'Adakah pengguna boleh melihat maklumat profil sendiri?', 'expected' => 'Maklumat profil pengguna berjaya dipaparkan.'],
        ['title' => 'Adakah pengguna boleh mengemaskini maklumat profil?', 'expected' => 'Maklumat profil berjaya dikemaskini.'],
        ['title' => 'Adakah pengguna boleh menukar kata laluan?', 'expected' => 'Kata laluan berjaya ditukar.'],
        ['title' => 'Adakah pengguna boleh mendaftarkan tandatangan pengguna?', 'expected' => 'Tandatangan pengguna berjaya disimpan dalam sistem.'],
        ['title' => 'Adakah pengguna boleh melihat tandatangan yang telah didaftarkan?', 'expected' => 'Tandatangan dipaparkan dengan betul.'],
        ['title' => 'Adakah pengguna boleh mengubah tandatangan sedia ada?', 'expected' => 'Tandatangan baharu berjaya dikemaskini.'],
        ['title' => 'Adakah sistem meminta pengesahan emel sebelum perubahan tandatangan?', 'expected' => 'Sistem meminta pengesahan emel sebelum perubahan dibuat.'],
        ['title' => 'Adakah OTP dihantar ke emel pengguna untuk pengesahan?', 'expected' => 'OTP berjaya diterima melalui emel.'],
        ['title' => 'Adakah sistem menolak OTP yang salah atau tamat tempoh?', 'expected' => 'Sistem memaparkan mesej ralat OTP tidak sah.'],
    ],
    'MODUL KESELAMATAN SISTEM' => [
        ['title' => 'Adakah sistem hanya membenarkan pengguna yang telah login mengakses sistem?', 'expected' => 'Akses tanpa login disekat'],
        ['title' => 'Adakah halaman dalaman dilindungi daripada akses terus melalui URL?', 'expected' => 'Sistem mengarahkan pengguna ke halaman login.'],
        ['title' => 'Adakah sistem menamatkan sesi selepas logout?', 'expected' => 'Sesi pengguna ditamatkan.'],
        ['title' => 'Adakah fungsi OTP melindungi perubahan maklumat sensitif?', 'expected' => 'Pengesahan tambahan diperlukan.'],
    ],
    'MODUL PENGURUSAN SURAT' => [
        ['title' => 'Adakah pengguna boleh membuka modul surat?', 'expected' => 'Modul surat berjaya dipaparkan.'],
        ['title' => 'Adakah pengguna boleh mencipta surat baharu?', 'expected' => 'Surat baharu berjaya dicipta.'],
        ['title' => 'Adakah pengguna boleh memasukkan maklumat surat seperti tajuk, penerima dan kandungan?', 'expected' => 'Maklumat surat berjaya disimpan.'],
        ['title' => 'Adakah editor surat membenarkan pengguna menaip dan mengubah kandungan surat?', 'expected' => 'Kandungan surat boleh diubah dengan baik.'],
        ['title' => 'Adakah format surat seperti font, saiz tulisan, jarak perenggan dan alignment berfungsi?', 'expected' => 'Format surat dipaparkan dengan betul.'],
        ['title' => 'Adakah pengguna boleh menyimpan surat sebagai draf?', 'expected' => 'Surat berjaya disimpan sebagai draf.'],
        ['title' => 'Adakah pengguna boleh mengemaskini surat yang telah disimpan?', 'expected' => 'Perubahan surat berjaya disimpan.'],
        ['title' => 'Adakah pengguna boleh menghantar surat untuk proses semakan/kelulusan?', 'expected' => 'Surat berjaya dihantar kepada pihak berkaitan.'],
        ['title' => 'Adakah status surat berubah mengikut proses tindakan?', 'expected' => 'Status dikemaskini dengan betul.'],
        ['title' => 'Adakah surat boleh dijana dalam format PDF/cetakan?', 'expected' => 'Surat berjaya dijana dalam format yang ditetapkan.'],
        ['title' => 'Adakah tandatangan pengguna dimasukkan ke dalam surat dengan betul?', 'expected' => 'Tandatangan pengguna dipaparkan pada kedudukan yang betul.'],
        ['title' => 'Adakah sistem menghalang kehilangan data ketika surat belum disimpan?', 'expected' => 'Amaran dipaparkan kepada pengguna'],
    ],
];

// Get user's existing responses keyed by module_name_index
$user_responses = [];
$responses_stmt = mysqli_prepare($conn, "SELECT question_id, status, notes FROM uat_responses WHERE user_id = ?");
mysqli_stmt_bind_param($responses_stmt, "i", $user_id);
mysqli_stmt_execute($responses_stmt);
$responses_result = mysqli_stmt_get_result($responses_stmt);
while ($row = mysqli_fetch_assoc($responses_result)) {
    $user_responses[$row['question_id']] = $row;
}

// Calculate module statistics from hardcoded modules
$module_stats = [];
foreach ($checklist_modules as $module => $questions) {
    $total = count($questions);
    $pass = 0;
    $fail = 0;
    $tidak_berkenaan = 0;
    $belum = 0;
    
    foreach ($questions as $qidx => $q) {
        $qkey = $module . '_' . $qidx;
        $qid_hash = crc32($qkey);
        $response = $user_responses[$qid_hash] ?? null;
        if ($response) {
            if ($response['status'] === 'pass') {
                $pass++;
            } elseif ($response['status'] === 'fail') {
                $fail++;
            } elseif ($response['status'] === 'tidak_berkenaan') {
                $tidak_berkenaan++;
            }
        } else {
            $belum++;
        }
    }
    
    $module_stats[$module] = [
        'total' => $total,
        'pass' => $pass,
        'fail' => $fail,
        'tidak_berkenaan' => $tidak_berkenaan,
        'belum' => $belum
    ];
}

// Get current user details for default values
$current_user_stmt = mysqli_prepare($conn, "SELECT fullname, department, position FROM users WHERE id = ?");
mysqli_stmt_bind_param($current_user_stmt, "i", $user_id);
mysqli_stmt_execute($current_user_stmt);
$current_user = mysqli_fetch_assoc(mysqli_stmt_get_result($current_user_stmt));
?>
<!DOCTYPE html>
<html lang="ms">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>UAT | Sistem Pengurusan Fail</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <?php renderAppShellAssets(); ?>
    <style>
        :root {
            --primary: #0077b6; --primary-dark: #005f99; --secondary: #00b4d8;
            --deep-blue: #023e5f;
            --superadmin: #6c5ce7; --superadmin-dark: #5b4cdb;
            --bg-light: rgba(245, 250, 253, 0.72); --bg-card: rgba(255, 255, 255, 0.68);
            --text-main: #163447; --text-muted: #2d5367; --border: rgba(255, 255, 255, 0.48);
            --sidebar-width: 280px; --success: #1a9955; --danger: #dc3545; --warning: #ffc107;
            --glass: rgba(255, 255, 255, 0.58); --glass-strong: rgba(255, 255, 255, 0.76);
            --shadow-soft: 0 24px 70px rgba(2, 62, 95, 0.16);
        }
        * { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Segoe UI', 'Inter', Tahoma, Geneva, Verdana, sans-serif; }
        body {
            min-height: 100vh;
            display: flex;
            color: var(--text-main);
            background:
                linear-gradient(135deg, rgba(245, 250, 253, 0.90), rgba(222, 246, 252, 0.76)),
                url('assets/auth-bg.png') center/cover no-repeat fixed;
            overflow-x: hidden;
        }

        body::before,
        body::after {
            content: "";
            position: fixed;
            pointer-events: none;
            z-index: 0;
        }

        body::before {
            width: 42vw;
            height: 42vw;
            right: -16vw;
            top: -18vw;
            border-radius: 50%;
            background: radial-gradient(circle, rgba(0, 180, 216, 0.24), transparent 62%);
            filter: blur(4px);
        }

        body::after {
            width: 48vw;
            height: 48vw;
            left: 120px;
            bottom: -30vw;
            border-radius: 46px;
            background:
                linear-gradient(155deg, rgba(255, 255, 255, 0.20), rgba(255, 255, 255, 0.03)),
                repeating-linear-gradient(0deg, transparent 0 34px, rgba(0, 119, 182, 0.08) 35px 36px);
            border: 1px solid rgba(255, 255, 255, 0.32);
            transform: rotate(-9deg);
        }

        .sidebar {
            width: var(--sidebar-width);
            background:
                linear-gradient(180deg, rgba(2, 62, 95, 0.94), rgba(0, 119, 182, 0.84)),
                radial-gradient(circle at 22% 8%, rgba(0, 180, 216, 0.28), transparent 32%);
            border-right: 1px solid rgba(255, 255, 255, 0.22);
            position: fixed;
            height: 100vh;
            padding: 20px 0;
            overflow-y: auto;
            overflow-x: hidden;
            z-index: 1000;
            box-shadow: 18px 0 44px rgba(2, 38, 60, 0.20);
            transition: width 0.3s ease;
            display: flex;
            flex-direction: column;
            backdrop-filter: blur(18px) saturate(1.25);
            -webkit-backdrop-filter: blur(18px) saturate(1.25);
        }
        .sidebar.collapsed { width: 70px; }
        .sidebar-header {
            text-align: center;
            padding: 0 20px 20px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.16);
            margin-bottom: 20px;
            white-space: nowrap;
            overflow: hidden;
            background: transparent !important;
        }
        .sidebar.collapsed .sidebar-header h3, .sidebar.collapsed .sidebar-header p, .sidebar.collapsed .menu-text { display: none; }
        .sidebar-header img { width: 54px; margin-bottom: 10px; transition: margin 0.3s ease; filter: drop-shadow(0 10px 18px rgba(0,0,0,0.25)); }
        .sidebar.collapsed .sidebar-header img { margin: 0 auto 10px; }
        .sidebar-header h3 { font-size: 1rem; font-weight: 800; color: #fff; margin-bottom: 4px; letter-spacing: 0.01em; }
        .sidebar-header p { font-size: 0.75rem; color: rgba(255,255,255,0.72); }
        .role-badge { display: inline-block; padding: 3px 10px; border-radius: 12px; font-size: 0.7rem; font-weight: 600; margin-left: 8px; }
        .role-badge.user { background: #e6f4ff; color: var(--primary); }
        .role-badge.admin { background: #d4edda; color: var(--success); }
        .role-badge.superadmin { background: linear-gradient(90deg, var(--superadmin), var(--superadmin-dark)); color: #fff; }
        .sidebar-menu { list-style: none; padding: 0 10px; display: flex; flex-direction: column; flex: 1; }
        .sidebar-section { padding: 10px 18px 6px; color: rgba(255,255,255,0.58); font-size: 0.72rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.06em; }
        .sidebar-spacer { margin-top: auto; }
        .sidebar-menu li a { display: flex; align-items: center; gap: 12px; padding: 12px 18px; color: rgba(255,255,255,0.88); text-decoration: none; border-radius: 10px; margin-bottom: 5px; transition: all 0.2s ease; font-size: 0.9rem; white-space: nowrap; }
        .sidebar-menu li a:hover, .sidebar-menu li a.active { background: rgba(255,255,255,0.18); color: #fff; box-shadow: inset 0 0 0 1px rgba(255,255,255,0.18); }
        .sidebar-menu li a i { width: 20px; text-align: center; font-size: 1.1rem; }
        .sidebar-menu .dropdown { position: relative; }
        .sidebar-menu .dropdown-toggle { display: flex; align-items: center; justify-content: space-between; width: 100%; padding: 12px 18px; color: rgba(255,255,255,0.88); background: none; border: none; border-radius: 10px; cursor: pointer; font-size: 0.9rem; text-align: left; transition: all 0.2s ease; }
        .sidebar-menu .dropdown-toggle:hover, .sidebar-menu .dropdown-toggle.active { background: rgba(255,255,255,0.18); color: #fff; box-shadow: inset 0 0 0 1px rgba(255,255,255,0.18); }
        .sidebar-menu .dropdown-toggle i.chevron { transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1); font-size: 0.8rem; }
        .sidebar-menu .dropdown.active .dropdown-toggle i.chevron { transform: rotate(180deg); }
        .sidebar-menu .dropdown-menu { display: flex; flex-direction: column; padding-left: 42px; margin-top: 4px; max-height: 0; opacity: 0; overflow: hidden; transition: max-height 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.25s ease; }
        .sidebar-menu .dropdown.active .dropdown-menu { max-height: 260px; opacity: 1; }
        .sidebar-menu .dropdown-menu a { padding: 8px 18px; color: rgba(255,255,255,0.78); text-decoration: none; border-radius: 8px; font-size: 0.85rem; display: flex; align-items: center; gap: 8px; opacity: 0; transform: translateY(-5px); transition: opacity 0.2s ease, transform 0.2s ease; }
        .sidebar-menu .dropdown.active .dropdown-menu a { opacity: 1; transform: translateY(0); }
        .sidebar-menu .dropdown-menu a:hover, .sidebar-menu .dropdown-menu a.active { background: rgba(255,255,255,0.16); color: #fff; }
        .sidebar.collapsed + .main { margin-left: 70px; }
        .sidebar-toggle { position: absolute; right: 10px; top: 20px; width: 28px; height: 28px; background: rgba(255,255,255,0.20); color: #fff; border: 1px solid rgba(255,255,255,0.28); border-radius: 50%; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 0.7rem; box-shadow: 0 10px 22px rgba(0,0,0,0.14); transition: transform 0.3s ease; z-index: 1001; }
        .sidebar.collapsed .sidebar-toggle { transform: rotate(180deg); }
        .main { position: relative; z-index: 1; margin-left: var(--sidebar-width); flex: 1; padding: 28px 32px; transition: margin-left 0.3s ease; }
        .top-bar {
            background: var(--glass);
            padding: 18px 24px;
            border-radius: 18px;
            box-shadow: var(--shadow-soft);
            margin-bottom: 18px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border: 1px solid var(--border);
            backdrop-filter: blur(22px) saturate(1.25);
            -webkit-backdrop-filter: blur(22px) saturate(1.25);
        }
        .top-bar h2 { color: var(--primary-dark); font-size: 1.35rem; font-weight: 850; display: flex; align-items: center; gap: 10px; }
        .top-bar h2 i { color: var(--secondary); }
        .uat-container {
            background: var(--glass);
            border-radius: 22px;
            padding: 30px;
            box-shadow: var(--shadow-soft);
            border: 1px solid var(--border);
            backdrop-filter: blur(24px) saturate(1.25);
        }
        .uat-layout {
            display: grid;
            grid-template-columns: 1fr 380px;
            gap: 25px;
        }
        .uat-layout.full-width {
            grid-template-columns: 1fr;
        }
        .uat-form-column {
            min-width: 0;
        }
        .uat-responses-column {
            background: rgba(255, 255, 255, 0.5);
            border-radius: 18px;
            padding: 20px;
            border: 1px solid rgba(0, 119, 182, 0.15);
            height: fit-content;
            position: sticky;
            top: 20px;
        }
        .uat-section {
            margin-bottom: 30px;
        }
        .glass-card {
            background: rgba(255, 255, 255, 0.6);
            border: 1px solid rgba(255, 255, 255, 0.4);
            border-radius: 18px;
            padding: 25px;
            box-shadow: 0 8px 24px rgba(2, 62, 95, 0.12);
            backdrop-filter: blur(20px) saturate(1.2);
            margin-bottom: 25px;
        }
        .uat-section-title {
            color: var(--primary-dark);
            font-size: 1.25rem;
            font-weight: 800;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid rgba(0, 119, 182, 0.2);
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .uat-section-title i { color: var(--secondary); }
        .form-group { margin-bottom: 20px; }
        .form-label {
            display: block;
            color: var(--text-main);
            font-weight: 600;
            margin-bottom: 8px;
            font-size: 0.95rem;
        }
        .form-control {
            width: 100%;
            padding: 12px 16px;
            border: 1px solid rgba(0, 119, 182, 0.3);
            border-radius: 10px;
            font-size: 0.95rem;
            background: rgba(255, 255, 255, 0.9);
            transition: all 0.2s ease;
        }
        .form-control:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(0, 119, 182, 0.1);
        }
        textarea.form-control { resize: vertical; min-height: 100px; }
        .question-card {
            background: rgba(255, 255, 255, 0.7);
            border: 1px solid rgba(0, 119, 182, 0.15);
            border-radius: 14px;
            padding: 20px;
            margin-bottom: 15px;
            transition: all 0.2s ease;
        }
        .question-card:hover {
            box-shadow: 0 8px 20px rgba(2, 62, 95, 0.1);
        }
        .module-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid rgba(0, 119, 182, 0.2);
        }
        .module-title {
            color: var(--primary-dark);
            font-size: 1.25rem;
            font-weight: 800;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .module-stats {
            display: flex;
            gap: 15px;
            align-items: center;
        }
        .stat-item {
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 0.9rem;
            font-weight: 600;
        }
        .stat-badge {
            display: inline-block;
            padding: 4px 10px;
            border-radius: 12px;
            font-size: 0.85rem;
            font-weight: 700;
        }
        .stat-badge.total {
            background: rgba(0, 119, 182, 0.15);
            color: var(--primary);
        }
        .stat-badge.pass {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
        }
        .stat-badge.isu {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
        }
        .stat-badge.fail {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
        }
        .stat-badge.tidak_berkenaan {
            background: rgba(108, 117, 125, 0.15);
            color: #6c757d;
        }
        .stat-badge.belum {
            background: rgba(108, 117, 125, 0.15);
            color: #6c757d;
        }
        .module-actions {
            display: flex;
            gap: 10px;
        }
        .btn-module {
            padding: 8px 16px;
            border: none;
            border-radius: 8px;
            font-size: 0.85rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        .btn-pass {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
            border: 1px solid rgba(26, 153, 85, 0.3);
        }
        .btn-pass:hover {
            background: rgba(26, 153, 85, 0.25);
        }
        .btn-reset {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
            border: 1px solid rgba(220, 53, 69, 0.3);
        }
        .btn-reset:hover {
            background: rgba(220, 53, 69, 0.25);
        }
        .test-item-title {
            color: var(--primary-dark);
            font-size: 1.1rem;
            font-weight: 700;
            margin-bottom: 12px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .status-badge-item {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 12px;
            font-size: 0.8rem;
            font-weight: 600;
        }
        .status-badge-item.belum-test {
            background: rgba(108, 117, 125, 0.15);
            color: #6c757d;
        }
        .status-badge-item.pass {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
        }
        .status-badge-item.isu {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
        }
        .status-badge-item.fail {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
        }
        .status-badge-item.tidak_berkenaan {
            background: rgba(108, 117, 125, 0.15);
            color: #6c757d;
        }
        .test-description {
            color: var(--text-main);
            font-size: 0.95rem;
            margin-bottom: 10px;
            line-height: 1.5;
        }
        .expected-result {
            color: var(--text-muted);
            font-size: 0.9rem;
            margin-bottom: 15px;
            font-style: italic;
            background: rgba(26, 153, 85, 0.08);
            padding: 10px 14px;
            border-radius: 8px;
            border-left: 3px solid var(--success);
        }
        .expected-result::before {
            content: "✓ ";
            font-weight: 600;
            color: var(--success);
            font-style: normal;
        }
        .test-result-section {
            display: flex;
            gap: 20px;
            align-items: flex-start;
            margin-top: 15px;
            padding-top: 15px;
            border-top: 1px solid rgba(0, 119, 182, 0.15);
        }
        .result-dropdown {
            flex: 0 0 auto;
        }
        .result-dropdown select {
            padding: 8px 12px;
            border: 1px solid rgba(0, 119, 182, 0.3);
            border-radius: 8px;
            font-size: 0.9rem;
            background: rgba(255, 255, 255, 0.9);
            cursor: pointer;
        }
        .result-notes {
            flex: 1;
        }
        .result-notes textarea {
            width: 100%;
            padding: 10px 14px;
            border: 1px solid rgba(0, 119, 182, 0.3);
            border-radius: 8px;
            font-size: 0.9rem;
            background: rgba(255, 255, 255, 0.9);
            resize: vertical;
            min-height: 80px;
        }
        .result-notes textarea:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(0, 119, 182, 0.1);
        }
        .responses-header {
            margin-bottom: 20px;
        }
        .responses-header h3 {
            color: var(--primary-dark);
            font-size: 1.1rem;
            font-weight: 800;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .search-filter-bar {
            display: flex;
            gap: 10px;
            margin-bottom: 15px;
        }
        .search-filter-bar input,
        .search-filter-bar select {
            flex: 1;
            padding: 10px 14px;
            border: 1px solid rgba(0, 119, 182, 0.3);
            border-radius: 8px;
            font-size: 0.9rem;
            background: rgba(255, 255, 255, 0.9);
        }
        .search-filter-bar input:focus,
        .search-filter-bar select:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(0, 119, 182, 0.1);
        }
        .tester-card {
            background: rgba(255, 255, 255, 0.7);
            border: 1px solid rgba(0, 119, 182, 0.15);
            border-radius: 12px;
            padding: 15px;
            margin-bottom: 12px;
            transition: all 0.2s ease;
        }
        .tester-card:hover {
            box-shadow: 0 4px 12px rgba(2, 62, 95, 0.1);
        }
        .tester-header {
            display: flex;
            justify-content: space-between;
            align-items: start;
            margin-bottom: 10px;
        }
        .tester-name {
            color: var(--primary-dark);
            font-weight: 700;
            font-size: 0.95rem;
        }
        .tester-time {
            color: var(--text-muted);
            font-size: 0.8rem;
        }
        .tester-status {
            display: inline-block;
            padding: 4px 10px;
            border-radius: 12px;
            font-size: 0.75rem;
            font-weight: 600;
            margin-bottom: 8px;
        }
        .tester-status.belum_lengkap {
            background: rgba(108, 117, 125, 0.15);
            color: #6c757d;
        }
        .tester-status.gagal {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
        }
        .tester-status.lengkap {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
        }
        .tester-stats {
            display: flex;
            gap: 12px;
            font-size: 0.85rem;
            color: var(--text-muted);
            margin-bottom: 10px;
        }
        .tester-stats span {
            font-weight: 600;
        }
        .btn-view-detail {
            width: 100%;
            padding: 8px 12px;
            background: rgba(0, 119, 182, 0.1);
            color: var(--primary);
            border: 1px solid rgba(0, 119, 182, 0.3);
            border-radius: 8px;
            font-size: 0.85rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        .btn-view-detail:hover {
            background: rgba(0, 119, 182, 0.2);
        }
        .global-actions {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid rgba(0, 119, 182, 0.15);
        }
        .btn-global {
            flex: 1;
            padding: 10px 16px;
            border: none;
            border-radius: 8px;
            font-size: 0.9rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        .btn-all-pass {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
            border: 1px solid rgba(26, 153, 85, 0.3);
        }
        .btn-all-pass:hover {
            background: rgba(26, 153, 85, 0.25);
        }
        .btn-reset-all {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
            border: 1px solid rgba(220, 53, 69, 0.3);
        }
        .btn-reset-all:hover {
            background: rgba(220, 53, 69, 0.25);
        }
        @media (max-width: 1200px) {
            .uat-layout {
                grid-template-columns: 1fr;
            }
            .uat-responses-column {
                position: static;
            }
        }
        @media (max-width: 992px) {
            .sidebar { width: 70px; }
            .sidebar .sidebar-header h3, .sidebar .sidebar-header p, .sidebar .menu-text, .sidebar .role-badge, .sidebar .dropdown-menu, .sidebar .sidebar-section { display: none; }
            .main { margin-left: 70px; }
            .sidebar-toggle { display: none; }
        }
        .sidebar {
            overflow: hidden;
        }
        .sidebar-header {
            flex: 0 0 auto;
        }
        .sidebar-menu {
            flex: 1 1 auto;
            min-height: 0;
            overflow-y: auto;
            overflow-x: hidden;
            padding-bottom: 0;
            overscroll-behavior: contain;
            scrollbar-width: thin;
            scrollbar-color: #8f8f8f transparent;
        }
        .sidebar-menu::-webkit-scrollbar {
            width: 10px;
        }
        .sidebar-menu::-webkit-scrollbar-track {
            background: transparent;
        }
        .sidebar-menu::-webkit-scrollbar-thumb {
            background: #8f8f8f;
            border-radius: 999px;
            border: 2px solid transparent;
            background-clip: content-box;
        }
        .sidebar-menu::-webkit-scrollbar-button {
            display: none;
            width: 0;
            height: 0;
        }
        .main .btn,
        .main .tab-btn,
        .main .action-btn,
        .main .btn-action,
        .main .filter-btn,
        .main .button,
        .main .generate-closing-btn,
        .main .attachment-remove-btn,
        .main .page-add-btn,
        .main .page-delete-btn,
        .main .delete-current-page-btn,
        .main .form-actions a,
        .main .form-actions button,
        .main .filter-actions a,
        .main .filter-actions button,
        .main .document-command a,
        .main .document-command button,
        .main table a[class*="btn"],
        .main table button[class*="btn"],
        .main input[type="submit"],
        .main input[type="button"] {
            transition: transform 0.2s ease, filter 0.2s ease, box-shadow 0.2s ease, background-color 0.2s ease, border-color 0.2s ease !important;
            will-change: transform;
        }
        .main .btn:hover:not(:disabled),
        .main .tab-btn:hover:not(:disabled),
        .main .action-btn:hover:not(:disabled),
        .main .btn-action:hover:not(:disabled),
        .main .filter-btn:hover:not(:disabled),
        .main .button:hover:not(:disabled),
        .main .generate-closing-btn:hover:not(:disabled),
        .main .attachment-remove-btn:hover:not(:disabled),
        .main .page-add-btn:hover:not(:disabled),
        .main .page-delete-btn:hover:not(:disabled),
        .main .delete-current-page-btn:hover:not(:disabled),
        .main .form-actions a:hover,
        .main .form-actions button:hover:not(:disabled),
        .main .filter-actions a:hover,
        .main .filter-actions button:hover:not(:disabled),
        .main .document-command a:hover,
        .main .document-command button:hover:not(:disabled),
        .main table a[class*="btn"]:hover,
        .main table button[class*="btn"]:hover:not(:disabled),
        .main input[type="submit"]:hover:not(:disabled),
        .main input[type="button"]:hover:not(:disabled) {
            transform: translateY(-1px);
            filter: brightness(1.04);
            box-shadow: 0 14px 28px rgba(0, 119, 182, 0.18);
        }
        .main .btn:active:not(:disabled),
        .main .tab-btn:active:not(:disabled),
        .main .action-btn:active:not(:disabled),
        .main .btn-action:active:not(:disabled),
        .main .filter-btn:active:not(:disabled),
        .main .button:active:not(:disabled),
        .main .generate-closing-btn:active:not(:disabled),
        .main .attachment-remove-btn:active:not(:disabled),
        .main .page-add-btn:active:not(:disabled),
        .main .page-delete-btn:active:not(:disabled),
        .main .delete-current-page-btn:active:not(:disabled),
        .main .form-actions a:active,
        .main .form-actions button:active:not(:disabled),
        .main .filter-actions a:active,
        .main .filter-actions button:active:not(:disabled),
        .main .document-command a:active,
        .main .document-command button:active:not(:disabled),
        .main table a[class*="btn"]:active,
        .main table button[class*="btn"]:active:not(:disabled),
        .main input[type="submit"]:active:not(:disabled),
        .main input[type="button"]:active:not(:disabled) {
            transform: translateY(0);
            filter: brightness(0.98);
        }
        .btn-submit {
            background: linear-gradient(90deg, var(--primary), var(--secondary));
            color: #fff;
            border: none;
            padding: 14px 32px;
            border-radius: 12px;
            font-size: 1rem;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.2s ease;
            box-shadow: 0 10px 20px rgba(0, 119, 182, 0.2);
        }
        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 14px 28px rgba(0, 119, 182, 0.3);
        }
        .alert {
            padding: 16px 20px;
            border-radius: 12px;
            margin-bottom: 20px;
            font-weight: 600;
        }
        .alert-success {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
            border: 1px solid rgba(26, 153, 85, 0.3);
        }
        .alert-error {
            background: rgba(220, 53, 69, 0.15);
            color: var(--danger);
            border: 1px solid rgba(220, 53, 69, 0.3);
        }
        .uat-users-table {
            width: 100%;
            border-collapse: collapse;
            background: rgba(255, 255, 255, 0.8);
            border-radius: 14px;
            overflow: hidden;
            margin-top: 20px;
        }
        .uat-users-table th {
            background: rgba(0, 119, 182, 0.1);
            color: var(--primary-dark);
            font-weight: 800;
            padding: 14px 16px;
            text-align: left;
        }
        .uat-users-table td {
            padding: 14px 16px;
            border-bottom: 1px solid rgba(0, 119, 182, 0.1);
        }
        .uat-users-table tr:hover td {
            background: rgba(231, 248, 252, 0.5);
        }
        .status-badge {
            display: inline-block;
            padding: 6px 12px;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 600;
        }
        .status-completed {
            background: rgba(26, 153, 85, 0.15);
            color: var(--success);
        }
        @media (max-width: 992px) {
            .sidebar { width: 70px; }
            .sidebar .sidebar-header h3, .sidebar .sidebar-header p, .sidebar .menu-text { display: none; }
            .main { margin-left: 70px; }
        }
    </style>
</head>
<body class="role-<?= htmlspecialchars($role) ?>">

    <aside class="sidebar" id="sidebar">
        <button class="sidebar-toggle" id="sidebarToggle" title="Tutup/Buka Menu">
            <i class="fas fa-chevron-left"></i>
        </button>
        <div class="sidebar-header">
            <img src="mainpp.png" alt="Logo MAINPP">
            <h3>SISTEM PENGURUSAN FAIL</h3>
            <p>Jabatan Teknologi Maklumat</p>
        </div>
        <ul class="sidebar-menu">
            <?php renderUnifiedSidebarMenu($conn, $role, 'UAT.php'); ?>
        </ul>
    </aside>

    <main class="main">
        <div class="top-bar">
            <h2><i class="fas fa-clipboard-check"></i> User Acceptance Testing (UAT)</h2>
            <?php renderTopBarUserMenu($conn, $role, 'UAT.php'); ?>
        </div>

        <?php
        renderBreadcrumb([
            ['label' => 'UAT', 'url' => 'UAT.php', 'current' => true]
        ]);
        ?>

        <div class="uat-container">
            <?php if ($success_message): ?>
                <div class="alert alert-success">
                    <i class="fas fa-check-circle"></i> <?= htmlspecialchars($success_message) ?>
                </div>
            <?php endif; ?>

<?php if ($error_message): ?>
                <div class="alert alert-error">
                    <i class="fas fa-exclamation-circle"></i> <?= htmlspecialchars($error_message) ?>
                </div>
            <?php endif; ?>

            <div class="uat-layout <?= $role !== 'superadmin' ? 'full-width' : '' ?>">
                <!-- Left Column: UAT Form -->
                <div class="uat-form-column">
                    <form method="POST" action="">
                        <div class="glass-card">
                            <h3 class="uat-section-title">
                                <i class="fas fa-user"></i>
                                Maklumat Pengguna
                            </h3>
                            <div class="form-group">
                                <label class="form-label">Nama Penuh</label>
                                <input type="text" class="form-control" value="<?= htmlspecialchars($current_user['fullname'] ?? '') ?>" readonly>
                            </div>
                            <div class="form-group">
                                <label class="form-label">Bahagian</label>
                                <input type="text" class="form-control" value="<?= htmlspecialchars($current_user['department'] ?? '') ?>" readonly>
                            </div>
                            <div class="form-group">
                                <label class="form-label">Jawatan</label>
                                <input type="text" name="position" class="form-control" value="<?= htmlspecialchars($user_info['position'] ?? $current_user['position'] ?? '') ?>" placeholder="Masukkan jawatan anda">
                            </div>
                            <div class="form-group">
                                <label class="form-label">No. Telefon</label>
                                <input type="text" name="phone" class="form-control" value="<?= htmlspecialchars($user_info['phone'] ?? '') ?>" placeholder="Masukkan no. telefon">
                            </div>
                            <div class="form-group">
                                <label class="form-label">Nota Tambahan (Optional)</label>
                                <textarea name="additional_notes" class="form-control" placeholder="Sebarang nota tambahan mengenai UAT"><?= htmlspecialchars($user_info['additional_notes'] ?? '') ?></textarea>
                            </div>
                        </div>

                        <div class="glass-card">
                            <h3 class="uat-section-title">
                                <i class="fas fa-clipboard-check"></i>
                                Checklist Modul
                            </h3>

                            <?php foreach ($checklist_modules as $module => $questions): ?>
                                <?php $stats = $module_stats[$module]; ?>
                                <div class="module-header">
                                    <div class="module-title">
                                        <i class="fas fa-cube"></i>
                                        <?= htmlspecialchars($module) ?>
                                    </div>
                                    <div class="module-stats">
                                        <div class="stat-item">
                                            <span class="stat-badge total"><?= count($questions) ?> item</span>
                                        </div>
                                        <div class="stat-item">
                                            <span class="stat-badge pass">Pass <?= $stats['pass'] ?></span>
                                        </div>
                                        <div class="stat-item">
                                            <span class="stat-badge fail">Fail <?= $stats['fail'] ?></span>
                                        </div>
                                        <div class="stat-item">
                                            <span class="stat-badge tidak_berkenaan">N/A <?= $stats['tidak_berkenaan'] ?></span>
                                        </div>
                                        <div class="stat-item">
                                            <span class="stat-badge belum">Belum <?= $stats['belum'] ?></span>
                                        </div>
                                    </div>
                                </div>
                                
                                <?php foreach ($questions as $qidx => $question): ?>
                                    <?php $qkey = $module . '_' . $qidx; ?>
                                    <?php $qid_hash = crc32($qkey); ?>
                                    <?php $existing_response = $user_responses[$qid_hash] ?? null; ?>
                                    <?php $current_status = $existing_response['status'] ?? 'belum_test'; ?>
                                    <div class="question-card">
                                        <div class="test-item-title">
                                            <?= htmlspecialchars($question['title']) ?>
                                        </div>
                                        <?php if ($question['expected']): ?>
                                            <div class="expected-result">
                                                Expected: <?= htmlspecialchars($question['expected']) ?>
                                            </div>
                                        <?php endif; ?>
                                        
                                        <div class="test-result-section">
                                            <div class="result-dropdown">
                                                <label style="display: block; margin-bottom: 5px; font-weight: 600; font-size: 0.9rem;">Keputusan</label>
                                                <select name="responses[<?= htmlspecialchars($qkey) ?>][status]">
                                                    <option value="belum_test" <?= $current_status === 'belum_test' ? 'selected' : '' ?>>Belum Test</option>
                                                    <option value="pass" <?= $current_status === 'pass' ? 'selected' : '' ?>>Pass</option>
                                                    <option value="fail" <?= $current_status === 'fail' ? 'selected' : '' ?>>Fail</option>
                                                    <option value="tidak_berkenaan" <?= $current_status === 'tidak_berkenaan' ? 'selected' : '' ?>>Tidak Berkenaan</option>
                                                </select>
                                            </div>
                                            <div class="result-notes">
                                                <label style="display: block; margin-bottom: 5px; font-weight: 600; font-size: 0.9rem;">Catatan isu jika ada</label>
                                                <textarea name="responses[<?= htmlspecialchars($qkey) ?>][notes]" placeholder="Masukkan nota jika ada isu..."><?= htmlspecialchars($existing_response['notes'] ?? '') ?></textarea>
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            <?php endforeach; ?>
                        </div>

                        <?php
                        $total_belum = 0;
                        foreach ($module_stats as $stats) {
                            $total_belum += $stats['belum'];
                        }
                        ?>
                        <div class="belum-test-summary" style="background: rgba(108, 117, 125, 0.08); padding: 12px 16px; border-radius: 8px; margin-bottom: 15px; border-left: 3px solid #6c757d;">
                            <div style="display: flex; align-items: center; gap: 8px; margin-bottom: 4px;">
                                <i class="fas fa-clock" style="color: #6c757d;"></i>
                                <strong style="color: #6c757d;">Belum Test</strong>
                            </div>
                            <div style="color: #6c757d; font-size: 0.9rem;">
                                Terdapat <?= $total_belum ?> soalan belum dijiwai
                            </div>
                        </div>

                        <div style="text-align: center; margin-top: 30px;">
                            <button type="submit" name="submit_uat" class="btn-submit">
                                <i class="fas fa-paper-plane"></i> Hantar UAT
                            </button>
                        </div>
                    </form>
                </div>

                <!-- Right Column: Tester Responses -->
                <?php if ($role === 'superadmin'): ?>
                <div class="uat-responses-column">
                    <div class="responses-header">
                        <h3><i class="fas fa-users"></i> Senarai Tester</h3>
                    </div>
                    
                    <div class="search-filter-bar">
                        <input type="text" id="searchTester" placeholder="Cari tester/sesi...">
                        <select id="filterStatus">
                            <option value="">Semua Status</option>
                            <option value="belum_lengkap">Belum Lengkap</option>
                            <option value="lengkap">Lengkap</option>
                            <option value="gagal">Gagal</option>
                        </select>
                    </div>
                    
                    <div id="testerList">
                        <?php if (empty($tester_responses)): ?>
                            <p style="color: var(--text-muted); text-align: center; padding: 20px;">Tiada respons UAT lagi.</p>
                        <?php else: ?>
                            <?php foreach ($tester_responses as $tester): ?>
                                <?php 
                                    $pass_count = (int) ($tester['pass_count'] ?? 0);
                                    $fail_count = (int) ($tester['fail_count'] ?? 0);
                                    $tidak_berkenaan_count = (int) ($tester['tidak_berkenaan_count'] ?? 0);
                                    $response_count = (int) ($tester['response_count'] ?? 0);
                                    $belum_count = $total_questions - $response_count;
                                    
                                    if ($belum_count > 0) {
                                        $status = 'belum_lengkap';
                                        $status_label = 'Belum Lengkap';
                                    } elseif ($fail_count > 0) {
                                        $status = 'gagal';
                                        $status_label = 'Gagal';
                                    } else {
                                        $status = 'lengkap';
                                        $status_label = 'Lengkap';
                                    }
                                ?>
                                <div class="tester-card" data-name="<?= strtolower(htmlspecialchars($tester['fullname'])) ?>" data-status="<?= $status ?>">
                                    <div class="tester-header">
                                        <div class="tester-name"><?= htmlspecialchars($tester['fullname']) ?></div>
                                        <div class="tester-time"><?= date('d/m/Y H:i', strtotime($tester['completed_at'])) ?></div>
                                    </div>
                                    <div class="tester-status <?= $status ?>"><?= $status_label ?></div>
                                    <div class="tester-stats">
                                        <span>Pass <?= $pass_count ?></span>
                                        <span>Fail <?= $fail_count ?></span>
                                        <span>N/A <?= $tidak_berkenaan_count ?></span>
                                    </div>
                                    <button class="btn-view-detail" onclick="viewTesterDetail(<?= $tester['user_id'] ?>)">
                                        <i class="fas fa-eye"></i> Lihat Detail
                                    </button>
                                </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </main>

    <script>
        // Sidebar Toggle
        document.getElementById('sidebarToggle').addEventListener('click', function() {
            document.getElementById('sidebar').classList.toggle('collapsed');
        });
        
        // Dropdown Menu Toggle
        document.querySelectorAll('.sidebar-menu .dropdown-toggle').forEach(toggle => {
            toggle.addEventListener('click', function(e) {
                e.stopPropagation();
                const dropdown = this.closest('.dropdown');
                dropdown.classList.toggle('active');
            });
        });
        
        // Close dropdowns when clicking outside
        document.addEventListener('click', function() {
            document.querySelectorAll('.sidebar-menu .dropdown.active').forEach(dropdown => {
                dropdown.classList.remove('active');
            });
        });
        
        // Search and filter functionality
        document.getElementById('searchTester').addEventListener('input', filterTesters);
        document.getElementById('filterStatus').addEventListener('change', filterTesters);
        
        function filterTesters() {
            const searchTerm = document.getElementById('searchTester').value.toLowerCase();
            const statusFilter = document.getElementById('filterStatus').value;
            const testerCards = document.querySelectorAll('.tester-card');
            
            testerCards.forEach(card => {
                const name = card.dataset.name;
                const status = card.dataset.status;
                
                const matchesSearch = name.includes(searchTerm);
                const matchesStatus = statusFilter === '' || status === statusFilter;
                
                if (matchesSearch && matchesStatus) {
                    card.style.display = 'block';
                } else {
                    card.style.display = 'none';
                }
            });
        }
        
        function viewTesterDetail(userId) {
            // Placeholder for viewing tester details
            // This can be implemented to show a modal or redirect to a detail page
            alert('View detail for user ID: ' + userId + '\n\nThis feature can be expanded to show detailed UAT responses.');
        }
    </script>
    <?php renderSessionTimeoutScript(); ?>
</body>
</html>
