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

$canReadRegistrationForm = has_department_access($conn, 'daftar_pengguna', 'read');
$canCreateRegistrationForm = has_department_access($conn, 'daftar_pengguna', 'create');
if (!$canReadRegistrationForm && !$canCreateRegistrationForm) {
    header('Location: akses_dilarang.php');
    exit();
}

$bahagianList = audit_bahagian_list();
$currentRole = current_user_role();
$availableRoles = $currentRole === 'Pentadbir Sistem'
    ? ['Pengguna Biasa', 'Admin Audit', 'Pentadbir Sistem']
    : ['Pengguna Biasa', 'Admin Audit'];

render_layout_start('Daftar Pengguna - Dashboard Audit MAINPP');
?>
<?php if (isset($_GET['error']) && $_GET['error'] === 'duplicate'): ?>
    <div class="status-message status-danger">IC atau emel telah digunakan oleh pengguna lain.</div>
<?php elseif (isset($_GET['error']) && $_GET['error'] === 'invalid_role'): ?>
    <div class="status-message status-danger">Role yang dipilih tidak dibenarkan.</div>
<?php endif; ?>

<div class="content-card">
    <h3><i class="fa-solid fa-user-plus"></i> Daftar Pengguna</h3>
    <form action="proses_daftar_pengguna.php" method="POST">
        <div class="form-grid">
            <div class="full-width">
                <label for="nama">Nama</label>
                <input type="text" id="nama" name="nama" required>
            </div>
            <div>
                <label for="ic">No. Kad Pengenalan</label>
                <input type="text" id="ic" name="ic" required>
            </div>
            <div>
                <label for="emel">Emel</label>
                <input type="email" id="emel" name="emel" required>
            </div>
            <div>
                <label for="role">Role</label>
                <select id="role" name="role" required>
                    <option value="">-- Pilih Role --</option>
                    <?php foreach ($availableRoles as $role): ?>
                        <option value="<?= htmlspecialchars($role) ?>"><?= htmlspecialchars($role) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label for="jabatan">Bahagian / Unit</label>
                <select id="jabatan" name="jabatan" required>
                    <option value="">-- Pilih Bahagian / Unit --</option>
                    <?php foreach ($bahagianList as $bahagian): ?>
                        <option value="<?= htmlspecialchars($bahagian) ?>"><?= htmlspecialchars($bahagian) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label for="password">Kata Laluan</label>
                <div class="password-container">
                    <input type="password" id="password" name="password" required>
                    <i class="fa-solid fa-eye-slash toggle-password" onclick="togglePassword('password', this)"></i>
                </div>
            </div>
            <div>
                <label for="confirm_password">Sahkan Kata Laluan</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="togglePassword('confirm_password', this)"></i>
                </div>
            </div>
        </div>
        <div class="button-row">
            <?php if ($canCreateRegistrationForm): ?>
            <button type="submit" class="primary-btn">Daftar Pengguna</button>
            <?php else: ?>
            <span class="field-note">Anda hanya dibenarkan melihat borang daftar pengguna.</span>
            <?php endif; ?>
            <a href="dashboard.php" class="secondary-btn">Kembali</a>
        </div>
    </form>
</div>
<?php render_layout_end('<script>function togglePassword(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>'); ?>
