<?php
require_once __DIR__ . '/includes/layout.php';
require_login();

$userId = (int) ($_SESSION['user_id'] ?? 0);
$bahagianList = audit_bahagian_list();

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

if (!$user) {
    session_unset();
    session_destroy();
    header('Location: login.php');
    exit();
}

$extraJs = <<<HTML
<script>
function togglePasswordVisibility(fieldId, icon) {
    const input = document.getElementById(fieldId);
    input.type = input.type === 'password' ? 'text' : 'password';
    icon.classList.toggle('fa-eye');
    icon.classList.toggle('fa-eye-slash');
}
</script>
HTML;

render_layout_start('Profile Pengguna - Dashboard Audit MAINPP');
?>
<?php if (isset($_GET['success']) && $_GET['success'] === 'profile_updated'): ?>
    <div class="status-message status-success">Maklumat profil berjaya disimpan.</div>
<?php elseif (isset($_GET['success']) && $_GET['success'] === 'password_updated'): ?>
    <div class="status-message status-success">Kata laluan berjaya dikemaskini.</div>
<?php elseif (isset($_GET['error']) && $_GET['error'] === 'email_exists'): ?>
    <div class="status-message status-danger">Emel telah digunakan oleh pengguna lain.</div>
<?php elseif (isset($_GET['error']) && $_GET['error'] === 'password_mismatch'): ?>
    <div class="status-message status-danger">Kata laluan baru tidak sepadan.</div>
<?php elseif (isset($_GET['error']) && $_GET['error'] === 'wrong_password'): ?>
    <div class="status-message status-danger">Kata laluan semasa tidak betul.</div>
<?php endif; ?>

<div class="content-card">
    <h3><i class="fa-solid fa-id-card"></i> Kemaskini Profil</h3>
    <form action="proses_profile_pengguna.php" method="POST">
        <input type="hidden" name="action_type" value="profile">
        <div class="form-grid">
            <div class="full-width">
                <label for="nama">Nama</label>
                <input type="text" id="nama" name="nama" value="<?= htmlspecialchars($user['nama']) ?>" required>
            </div>
            <div>
                <label>No. Kad Pengenalan</label>
                <input type="text" value="<?= htmlspecialchars($user['ic']) ?>" readonly>
            </div>
            <div>
                <label for="emel">Emel</label>
                <input type="email" id="emel" name="emel" value="<?= htmlspecialchars($user['emel']) ?>" required>
            </div>
            <div class="full-width">
                <label for="jabatan">Bahagian / Unit</label>
                <select id="jabatan" name="jabatan" required>
                    <?php foreach ($bahagianList as $bahagian): ?>
                        <option value="<?= htmlspecialchars($bahagian) ?>" <?= $user['jabatan'] === $bahagian ? 'selected' : '' ?>><?= htmlspecialchars($bahagian) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="button-row">
            <button type="submit" class="primary-btn">Simpan Maklumat</button>
        </div>
    </form>
</div>

<div class="content-card">
    <h3><i class="fa-solid fa-key"></i> Keselamatan Akaun</h3>
    <form action="proses_profile_pengguna.php" method="POST">
        <input type="hidden" name="action_type" value="password">
        <div class="form-grid">
            <div class="full-width">
                <label for="old_password">Kata Laluan Semasa</label>
                <div class="password-container">
                    <input type="password" id="old_password" name="old_password" required>
                    <i class="fa-solid fa-eye-slash toggle-password" onclick="togglePasswordVisibility('old_password', this)"></i>
                </div>
            </div>
            <div>
                <label for="new_password">Kata Laluan Baru</label>
                <div class="password-container">
                    <input type="password" id="new_password" name="new_password" required>
                    <i class="fa-solid fa-eye-slash toggle-password" onclick="togglePasswordVisibility('new_password', this)"></i>
                </div>
            </div>
            <div>
                <label for="confirm_password">Sahkan Kata Laluan Baru</label>
                <div class="password-container">
                    <input type="password" id="confirm_password" name="confirm_password" required>
                    <i class="fa-solid fa-eye-slash toggle-password" onclick="togglePasswordVisibility('confirm_password', this)"></i>
                </div>
            </div>
        </div>
        <div class="button-row">
            <button type="submit" class="primary-btn">Kemaskini Kata Laluan</button>
        </div>
    </form>
</div>
<?php render_layout_end($extraJs); ?>
