<?= $this->extend('v_template/layout') ?>
<?= $this->section('content') ?>

<?php 
// Mendeteksi apakah ini form EDIT atau form TAMBAH
$is_edit = isset($user); 
?>

<div class="row mb-4 align-items-center">
    <div class="col-md-6">
        <h4 class="fw-bold text-success">
            <i class="fa-solid <?= $is_edit ? 'fa-user-pen' : 'fa-user-plus' ?> me-2"></i> 
            <?= $is_edit ? 'Edit Akun Pengguna' : 'Tambah Akun Baru' ?>
        </h4>
    </div>
    <div class="col-md-6 text-md-end mt-3 mt-md-0">
        <a href="<?= base_url('akun') ?>" class="btn btn-outline-secondary rounded-pill"><i class="fa-solid fa-arrow-left me-2"></i> Batal</a>
    </div>
</div>

<div class="card shadow-sm border-0">
    <div class="card-body p-4">
        
        <form action="<?= $is_edit ? base_url('akun/update/'.$user['id_user']) : base_url('akun/simpan') ?>" method="POST">
            
            <div class="row mb-3">
                <div class="col-md-6">
                    <label class="form-label fw-bold">Nama Lengkap</label>
                    <input type="text" class="form-control" name="nama_lengkap" value="<?= $is_edit ? $user['nama_lengkap'] : '' ?>" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label fw-bold">Level Akses (Role)</label>
                    <select class="form-select" name="role" required>
                        <option value="Polhut" <?= ($is_edit && $user['role'] == 'Polhut') ? 'selected' : '' ?>>Polhut (Fokus Laporan Patroli & Peta)</option>
                        <option value="Penyuluh" <?= ($is_edit && $user['role'] == 'Penyuluh') ? 'selected' : '' ?>>Penyuluh</option>
                        <option value="Kepala Seksi" <?= ($is_edit && $user['role'] == 'Kepala Seksi') ? 'selected' : '' ?>>Kepala Seksi (Bisa Upload Arsip)</option>
                        <option value="Kepala KPH" <?= ($is_edit && $user['role'] == 'Kepala KPH') ? 'selected' : '' ?>>Kepala KPH (Pemantauan Penuh)</option>
                        <option value="Admin" <?= ($is_edit && $user['role'] == 'Admin') ? 'selected' : '' ?>>Admin Sistem</option>
                    </select>
                </div>
            </div>

            <div class="row mb-4">
                <div class="col-md-6">
                    <label class="form-label fw-bold">Username Login</label>
                    <input type="text" class="form-control" name="username" value="<?= $is_edit ? $user['username'] : '' ?>" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label fw-bold">Password Login</label>
                    <input type="password" class="form-control" name="password" <?= $is_edit ? '' : 'required' ?> placeholder="<?= $is_edit ? 'Kosongkan jika tidak ingin mengubah password' : 'Wajib diisi' ?>">
                    <?php if($is_edit): ?>
                        <small class="text-danger">*Biarkan kosong jika password tidak diganti.</small>
                    <?php endif; ?>
                </div>
            </div>

            <hr>
            <div class="text-end">
                <button type="submit" class="btn btn-success px-4 rounded-pill">
                    <i class="fa-solid fa-save me-2"></i> Simpan Akun
                </button>
            </div>
        </form>

    </div>
</div>

<?= $this->endSection() ?>