<?php
// Session Guard
if (empty($_SESSION['namauser']) && empty($_SESSION['passuser'])) {
    echo "
    <div class='adm-card text-center py-5' style='max-width: 480px; margin: 40px auto;'>
        <div class='mb-3' style='font-size: 3rem; color: var(--adm-primary);'>
            <i class='fa fa-lock'></i>
        </div>
        <h4 style='color: var(--adm-text); font-weight: 700;' class='mb-2'>Akses Terbatas</h4>
        <p style='color: var(--adm-text-sub); font-size: 0.9rem;' class='mb-4'>
            Silahkan login terlebih dahulu untuk mengakses modul ini.
        </p>
        <a href='../../index.php' class='adm-nav-item active d-inline-flex justify-content-center' style='width: auto; padding: 10px 24px;'>
            <i class='fa fa-sign-in-alt me-2'></i> Login Sekarang
        </a>
    </div>";
    exit();
}

include "../config/koneksi.php";

$aksi = "modul/mod_user/aksi_user.php";
$act  = isset($_GET['act']) ? $_GET['act'] : ''; 

switch ($act) {
    default:
?>
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold m-0" style="color: var(--adm-text);">Ubah Password & Profil</h4>
            <small style="color: var(--adm-text-sub);">Kelola akun autentikasi login Anda</small>
        </div>
    </div>

    <div class="adm-card p-4">
        <div class="table-responsive">
            <table class="table align-middle text-nowrap mb-0" style="color: var(--adm-text); border-color: var(--adm-border);">
                <thead>
                    <tr style="border-bottom: 2px solid var(--adm-border-active); color: var(--adm-text-sub); font-size: 0.8rem; text-transform: uppercase;">
                        <th scope="col" class="py-3" style="width: 60px;">NO</th>
                        <th scope="col" class="py-3">USERNAME</th>
                        
                        <th scope="col" class="py-3">PASSWORD</th>
                        <th scope="col" class="py-3 text-center" style="width: 100px;">AKSI</th>
                    </tr>
                </thead>
                <tbody style="font-size: 0.88rem;">
                    <?php
                    if ($_SESSION['leveluser'] == 'admin') {
                        $query  = "SELECT * FROM users ORDER BY username ASC";
                    } else {
                        $username_sess = mysqli_real_escape_string($konek, $_SESSION['namauser']);
                        $query  = "SELECT * FROM users WHERE username='$username_sess'";
                    }
                    
                    $tampil = mysqli_query($konek, $query);
                    $no     = 1;

                    while ($r = mysqli_fetch_array($tampil)) {
                        echo "
                        <tr style='border-bottom: 1px solid var(--adm-border);'>
                            <td class='py-3'>{$no}</td>
                            <td class='py-3 fw-semibold'>" . htmlspecialchars($r['username']) . "</td>
                           
                            <td class='py-3'><span class='text-muted' style='letter-spacing: 2px;'>••••••••</span></td>
                            <td class='py-3 text-center'>
                                <a href='?module=user&act=edituser&id={$r['id_session']}' class='btn btn-sm btn-outline-danger d-inline-flex align-items-center gap-1'>
                                    <i class='fa fa-edit'></i> Edit
                                </a>
                            </td>
                        </tr>";
                        $no++;
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
<?php
    break;

    case "edituser":
        $id     = mysqli_real_escape_string($konek, $_GET['id']);
        $query  = "SELECT * FROM users WHERE id_session='$id'";
        $hasil  = mysqli_query($konek, $query);
        $r      = mysqli_fetch_array($hasil);

        // Keamanan Tambahan: Mencegah user biasa mengedit id_session punya orang lain
        if ($_SESSION['leveluser'] != 'admin' && $r['username'] != $_SESSION['namauser']) {
            echo "<script>alert('Akses Ditolak! Anda tidak memiliki izin untuk mengedit akun ini.'); window.location='media.php?module=user';</script>";
            exit();
        }
?>
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold m-0" style="color: var(--adm-text);">Edit Kredensial Login</h4>
            <small style="color: var(--adm-text-sub);">Perbarui username atau password login Anda</small>
        </div>
        <button onclick="self.history.back()" class="btn btn-sm btn-secondary d-inline-flex align-items-center gap-2">
            <i class="fa fa-arrow-left"></i> Kembali
        </button>
    </div>

    <div class="row">
        <div class="col-12 col-md-8 col-lg-6">
            <div class="adm-card p-4">
                <form method="POST" action="<?= $aksi ?>?module=user&act=update">
                    <input type="hidden" name="id" value="<?= htmlspecialchars($r['id_session']) ?>">

                    <div class="mb-3">
                        <label class="form-label text-sm fw-semibold" style="color: var(--adm-text);">Username</label>
                        <input type="text" name="username" class="form-control adm-input" value="<?= htmlspecialchars($r['username']) ?>" required>
                    </div>

                    <div class="mb-4">
                        <label class="form-label text-sm fw-semibold" style="color: var(--adm-text);">Password Baru</label>
                        <div class="position-relative">
                            <input type="password" id="inputPass" name="password" class="form-control adm-input pe-5" placeholder="Kosongkan jika tidak ingin diubah">
                            <button type="button" class="btn btn-link position-absolute end-0 top-0 text-decoration-none" style="color: var(--adm-text-sub); height: 100%;" onclick="togglePassword()">
                                <i class="fa fa-eye" id="eyeIcon"></i>
                            </button>
                        </div>
                        <small class="text-muted mt-1 d-block" style="font-size: 0.75rem;">Biarkan kosong jika hanya ingin mengubah username.</small>
                    </div>

                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-danger px-4 d-inline-flex align-items-center gap-2">
                            <i class="fa fa-save"></i> Simpan Perubahan
                        </button>
                        <button type="button" onclick="self.history.back()" class="btn btn-outline-secondary px-4">
                            Batal
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
    function togglePassword() {
        const passInput = document.getElementById('inputPass');
        const eyeIcon = document.getElementById('eyeIcon');
        
        if (passInput.type === 'password') {
            passInput.type = 'text';
            eyeIcon.classList.remove('fa-eye');
            eyeIcon.classList.add('fa-eye-slash');
        } else {
            passInput.type = 'password';
            eyeIcon.classList.remove('fa-eye-slash');
            eyeIcon.classList.add('fa-eye');
        }
    }
    </script>
<?php
    break;
}
?>