<?php
require_once __DIR__ . '/includes/layout.php';
require_login();
require_roles(['Pengguna Biasa', 'Admin Audit', 'Pentadbir Sistem']);
require_department_access($conn, 'senarai_pengguna', 'read');

$userId = (int) ($_GET['id'] ?? 0);
if ($userId <= 0) {
    header('Location: senarai_pengguna.php');
    exit();
}

$stmt = $conn->prepare("
    SELECT id, nama, ic, emel, role, jabatan, approval_status, account_status, created_at, deactivated_at
    FROM users
    WHERE id = ?
    LIMIT 1
");
$stmt->bind_param('i', $userId);
$stmt->execute();
$selectedUser = $stmt->get_result()->fetch_assoc();

if (!$selectedUser) {
    header('Location: senarai_pengguna.php');
    exit();
}

add_current_user_log($conn, 'Melihat detail pengguna ' . ($selectedUser['nama'] ?? 'Tidak Diketahui'));

[$approvalLabel, $approvalClass] = approval_badge_meta($selectedUser['approval_status'] ?? 'pending');
[$accountLabel, $accountClass] = account_badge_meta($selectedUser['account_status'] ?? 'inactive');

render_layout_start('Detail Pengguna - Dashboard Audit MAINPP');
?>
<div class="content-card">
    <div style="display:flex; justify-content:space-between; gap:16px; flex-wrap:wrap; margin-bottom:22px; border-bottom:1px solid #e8eef5; padding-bottom:18px;">
        <div>
            <h3><i class="fa-solid fa-circle-info"></i> Detail Pengguna</h3>
            <div class="button-row" style="margin-top:12px;">
                <span class="role-badge"><i class="fa-solid fa-user-shield"></i><?= htmlspecialchars($selectedUser['role']) ?></span>
                <span class="status-badge <?= $approvalClass ?>"><i class="fa-solid fa-circle"></i><?= htmlspecialchars($approvalLabel) ?></span>
                <span class="status-badge <?= $accountClass ?>"><i class="fa-solid fa-circle"></i><?= htmlspecialchars($accountLabel) ?></span>
            </div>
        </div>
    </div>

    <div class="form-grid">
        <div><label>Nama</label><input type="text" value="<?= htmlspecialchars($selectedUser['nama']) ?>" readonly></div>
        <div><label>No. Kad Pengenalan</label><input type="text" value="<?= htmlspecialchars($selectedUser['ic']) ?>" readonly></div>
        <div><label>Emel</label><input type="text" value="<?= htmlspecialchars($selectedUser['emel']) ?>" readonly></div>
        <div><label>Bahagian / Unit</label><input type="text" value="<?= htmlspecialchars($selectedUser['jabatan']) ?>" readonly></div>
        <div><label>Status Kelulusan</label><input type="text" value="<?= htmlspecialchars($approvalLabel) ?>" readonly></div>
        <div><label>Status Akaun</label><input type="text" value="<?= htmlspecialchars($accountLabel) ?>" readonly></div>
        <div><label>Tarikh Daftar</label><input type="text" value="<?= !empty($selectedUser['created_at']) ? date('d/m/Y h:i A', strtotime($selectedUser['created_at'])) : '-' ?>" readonly></div>
        <div><label>Tarikh Deactivate</label><input type="text" value="<?= !empty($selectedUser['deactivated_at']) ? date('d/m/Y h:i A', strtotime($selectedUser['deactivated_at'])) : '-' ?>" readonly></div>
    </div>
    <div class="button-row">
        <a href="senarai_pengguna.php" class="primary-btn">Kembali ke Senarai Pengguna</a>
    </div>
</div>
<?php render_layout_end(); ?>
