<?= $this->extend('layout/default') ?>

<?= $this->section('content') ?>
<style>
    .rounded-4 { border-radius: 15px !important; }
    .modal-content { border-radius: 20px; border: none; }
    /* Styling untuk input group password agar melengkung rapi */
    .input-group-pill-left { border-top-left-radius: 50px !important; border-bottom-left-radius: 50px !important; }
    .input-group-pill-right { border-top-right-radius: 50px !important; border-bottom-right-radius: 50px !important; cursor: pointer; background: white; }
    .form-control-pill { border-radius: 50px !important; padding-left: 20px; }
    
    /* MEMASTIKAN TULISAN ROLE BERWARNA HITAM */
    .role-badge { 
        color: #000 !important; 
        font-weight: bold;
        letter-spacing: 0.5px;
    }
</style>

<div class="container-fluid pt-4 pb-5">
    <div class="row">
        <div class="col-12">
            <div class="card shadow-sm border-0 rounded-4">
                <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
                    <h5 class="fw-bold mb-0 text-success"><i class="fa fa-users me-2"></i> KELOLA PENGGUNA</h5>
                    <button type="button" class="btn btn-success btn-sm rounded-pill px-4 shadow-sm" data-toggle="modal" data-target="#modalTambah">
                        <i class="fa fa-plus-circle me-1"></i> Tambah User
                    </button>
                </div>
                <div class="card-body">
                    
                    <?php if (session()->getFlashdata('success')) : ?>
                        <div class="alert alert-success alert-dismissible fade show border-0 shadow-sm" role="alert">
                            <?= session()->getFlashdata('success') ?>
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                    <?php endif; ?>

                    <div class="table-responsive">
                        <table class="table table-hover border-0">
                            <thead>
                                <tr class="bg-light">
                                    <th width="5%">No</th>
                                    <th>Username</th>
                                    <th>Nama Lengkap</th>
                                    <th>Role</th>
                                    <th width="15%" class="text-center">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $no = 1; foreach ($users as $u) : ?>
                                    <tr>
                                        <td><?= $no++ ?></td>
                                        <td><strong><?= $u['username'] ?></strong></td>
                                        <td><?= $u['nama_lengkap'] ?></td>
                                        <td>
                                            <span class="badge badge-info px-3 py-2 rounded-pill role-badge">
                                                <?= strtoupper($u['role']) ?>
                                            </span>
                                        </td>
                                        <td class="text-center">
                                            <button type="button" class="btn btn-warning btn-sm text-white shadow-sm" data-toggle="modal" data-target="#modalEdit<?= $u['id_user'] ?>">
                                                <i class="fa fa-edit"></i>
                                            </button>
                                            <a href="<?= base_url('admin/user/hapus/' . $u['id_user']) ?>" class="btn btn-danger btn-sm shadow-sm" onclick="return confirm('Hapus user ini?')">
                                                <i class="fa fa-trash"></i>
                                            </a>
                                        </td>
                                    </tr>

                                    <div class="modal fade" id="modalEdit<?= $u['id_user'] ?>" tabindex="-1" role="dialog" aria-hidden="true">
                                        <div class="modal-dialog modal-dialog-centered" role="document">
                                            <div class="modal-content shadow-lg">
                                                <form action="<?= base_url('admin/user/update/' . $u['id_user']) ?>" method="post">
                                                    <?= csrf_field() ?>
                                                    <div class="modal-header border-0 bg-warning text-white">
                                                        <h5 class="modal-title fw-bold">Edit User : <?= $u['username'] ?></h5>
                                                        <button type="button" class="close text-white" data-dismiss="modal">&times;</button>
                                                    </div>
                                                    <div class="modal-body p-4">
                                                        <div class="form-group mb-3">
                                                            <label class="small fw-bold text-dark">Username (Kunci Login)</label>
                                                            <input type="text" name="username" class="form-control form-control-pill bg-light" value="<?= $u['username'] ?>" readonly>
                                                        </div>
                                                        <div class="form-group mb-3">
                                                            <label class="small fw-bold text-dark">Nama Lengkap</label>
                                                            <input type="text" name="nama_lengkap" class="form-control form-control-pill" value="<?= $u['nama_lengkap'] ?>" required>
                                                        </div>
                                                        <div class="form-group mb-3">
                                                            <label class="small fw-bold text-dark">Role</label>
                                                            <select name="role" class="form-control form-control-pill">
                                                                <option value="admin" <?= $u['role'] == 'admin' ? 'selected' : '' ?>>Admin</option>
                                                                <option value="penulis" <?= $u['role'] == 'penulis' ? 'selected' : '' ?>>Penulis</option>
                                                                <option value="kaprodi" <?= $u['role'] == 'kaprodi' ? 'selected' : '' ?>>Kaprodi</option>
                                                            </select>
                                                        </div>
                                                        <div class="form-group mb-0">
                                                            <label class="small fw-bold text-dark">Password Baru <small class="text-muted">(Kosongkan jika tidak diganti)</small></label>
                                                            <div class="input-group">
                                                                <input type="password" name="new_password" id="passEdit<?= $u['id_user'] ?>" class="form-control input-group-pill-left" placeholder="******">
                                                                <div class="input-group-append">
                                                                    <span class="input-group-text input-group-pill-right" onclick="togglePass('passEdit<?= $u['id_user'] ?>', this)">
                                                                        <i class="fa fa-eye-slash text-dark"></i>
                                                                    </span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="modal-footer border-0 p-4">
                                                        <button type="submit" class="btn btn-warning btn-block text-white fw-bold rounded-pill shadow">UPDATE DATA</button>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="modalTambah" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content shadow-lg">
            <form action="<?= base_url('admin/user/simpan') ?>" method="post">
                <?= csrf_field() ?>
                <div class="modal-header border-0 bg-success text-white">
                    <h5 class="modal-title fw-bold">Tambah User Baru</h5>
                    <button type="button" class="close text-white" data-dismiss="modal">&times;</button>
                </div>
                <div class="modal-body p-4">
                    <div class="form-group mb-3">
                        <label class="small fw-bold text-dark">Username</label>
                        <input type="text" name="username" class="form-control form-control-pill" required placeholder="admin_baru">
                    </div>
                    <div class="form-group mb-3">
                        <label class="small fw-bold text-dark">Nama Lengkap</label>
                        <input type="text" name="nama_lengkap" class="form-control form-control-pill" required placeholder="Nama Lengkap">
                    </div>
                    <div class="form-group mb-3">
                        <label class="small fw-bold text-dark">Role</label>
                        <select name="role" class="form-control form-control-pill">
                            <option value="penulis">Penulis</option>
                            <option value="admin">Admin</option>
                            <option value="kaprodi">Kaprodi</option>
                        </select>
                    </div>
                    <div class="form-group mb-0">
                        <label class="small fw-bold text-dark">Password</label>
                        <div class="input-group">
                            <input type="password" name="password" id="passTambah" class="form-control input-group-pill-left" required placeholder="******">
                            <div class="input-group-append">
                                <span class="input-group-text input-group-pill-right" onclick="togglePass('passTambah', this)">
                                    <i class="fa fa-eye-slash text-dark"></i>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-0 p-4">
                    <button type="submit" class="btn btn-success btn-block fw-bold rounded-pill shadow">SIMPAN USER</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>

<script>
    function togglePass(inputId, el) {
        const input = document.getElementById(inputId);
        const icon = el.querySelector('i');
        if (input.type === "password") {
            input.type = "text";
            icon.classList.replace('fa-eye-slash', 'fa-eye');
            icon.classList.replace('text-dark', 'text-success');
        } else {
            input.type = "password";
            icon.classList.replace('fa-eye', 'fa-eye-slash');
            icon.classList.replace('text-success', 'text-dark');
        }
    }

    $(document).ready(function() {
        $('[data-toggle="modal"]').click(function() {
            var target = $(this).attr('data-target');
            $(target).modal('show');
        });
    });
</script>

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