<?= $this->include('admin/layout/header') ?>
<?= $this->include('admin/layout/navbar') ?>
<?= $this->include('admin/layout/topbar') ?>

<div class="pcoded-main-container">

    <div class="pcoded-content">

        <div class="card">

            <div class="card-header">

                <h5>Data Users</h5>

                <button
                    class="btn btn-primary float-right"
                    data-toggle="modal"
                    data-target="#modalTambah">

                    Tambah User

                </button>

            </div>

            <div class="card-body">

                <?php if (session()->getFlashdata('success')) : ?>

                    <div class="alert alert-success">

                        <?= session()->getFlashdata('success') ?>

                    </div>

                <?php endif; ?>

                <div class="table-responsive">

                    <table class="table table-bordered">

                        <thead>

                            <tr>

                                <th>No</th>
                                <th>Nama</th>
                                <th>Username</th>
                                <th>Role</th>
                                <th width="150">Aksi</th>

                            </tr>

                        </thead>

                        <tbody>

                            <?php $no = 1; ?>

                            <?php foreach ($users as $user) : ?>

                                <tr>

                                    <td><?= $no++ ?></td>

                                    <td><?= $user['nama_lengkap'] ?></td>

                                    <td><?= $user['username'] ?></td>

                                    <td><?= $user['role'] ?></td>

                                    <td>

                                        <button
                                            class="btn btn-warning btn-sm"
                                            data-toggle="modal"
                                            data-target="#edit<?= $user['id'] ?>">

                                            Edit

                                        </button>

                                        <button
                                            class="btn btn-danger btn-sm"
                                            data-toggle="modal"
                                            data-target="#hapus<?= $user['id'] ?>">

                                            Hapus

                                        </button>

                                    </td>

                                </tr>

                                <div class="modal fade" id="edit<?= $user['id'] ?>">

                                    <div class="modal-dialog">

                                        <div class="modal-content">

                                            <form action="<?= base_url('admin/users/update/' . $user['id']) ?>" method="post">

                                                <div class="modal-header">

                                                    <h5>Edit User</h5>

                                                </div>

                                                <div class="modal-body">

                                                    <input
                                                        type="text"
                                                        name="nama_lengkap"
                                                        value="<?= $user['nama_lengkap'] ?>"
                                                        class="form-control mb-2">

                                                    <input
                                                        type="text"
                                                        name="username"
                                                        value="<?= $user['username'] ?>"
                                                        class="form-control mb-2">

                                                    <input
                                                        type="password"
                                                        name="password"
                                                        class="form-control mb-2"
                                                        placeholder="Kosongkan jika tidak diubah">

                                                    <select
                                                        name="role"
                                                        class="form-control">

                                                        <option value="admin"
                                                            <?= $user['role'] == 'admin' ? 'selected' : '' ?>>
                                                            Admin
                                                        </option>

                                                        <option value="user"
                                                            <?= $user['role'] == 'user' ? 'selected' : '' ?>>
                                                            User
                                                        </option>

                                                    </select>

                                                </div>

                                                <div class="modal-footer">

                                                    <button
                                                        class="btn btn-success">

                                                        Update

                                                    </button>

                                                </div>

                                            </form>

                                        </div>

                                    </div>

                                </div>

                                <div class="modal fade" id="hapus<?= $user['id'] ?>">

                                    <div class="modal-dialog">

                                        <div class="modal-content">

                                            <div class="modal-body text-center">

                                                <h5>

                                                    Hapus User
                                                    <?= $user['nama_lengkap'] ?> ?

                                                </h5>

                                                <br>

                                                <a
                                                    href="<?= base_url('admin/users/delete/' . $user['id']) ?>"
                                                    class="btn btn-danger">

                                                    Ya Hapus

                                                </a>

                                            </div>

                                        </div>

                                    </div>

                                </div>
                            <?php endforeach ?>

                        </tbody>

                    </table>

                </div>

            </div>

        </div>

    </div>

</div>

<div class="modal fade" id="modalTambah">

    <div class="modal-dialog">

        <div class="modal-content">

            <form action="<?= base_url('admin/users/store') ?>" method="post">

                <div class="modal-header">

                    <h5>Tambah User</h5>

                </div>

                <div class="modal-body">

                    <div class="form-group">

                        <label>Nama Lengkap</label>

                        <input
                            type="text"
                            name="nama_lengkap"
                            class="form-control"
                            required>

                    </div>

                    <div class="form-group">

                        <label>Username</label>

                        <input
                            type="text"
                            name="username"
                            class="form-control"
                            required>

                    </div>

                    <div class="form-group">

                        <label>Password</label>

                        <input
                            type="password"
                            name="password"
                            class="form-control"
                            required>

                    </div>

                    <div class="form-group">

                        <label>Role</label>

                        <select
                            name="role"
                            class="form-control">

                            <option value="admin">Admin</option>
                            <option value="user">User</option>

                        </select>

                    </div>

                </div>

                <div class="modal-footer">

                    <button
                        type="submit"
                        class="btn btn-primary">

                        Simpan

                    </button>

                </div>

            </form>

        </div>

    </div>

</div>


<?= $this->include('admin/layout/footer') ?>