<?php
include 'template/header.php';
include 'template/h_menu.php';
include 'sidebar.php';
include 'koneksi.php';
?>

<div class="content-wrapper p-3">

    <!-- Header -->
    <section class="content-header">
        <div class="container-fluid">

            <div class="row mb-2">

                <div class="col-sm-6">
                    <h1>Manajemen User</h1>
                </div>

                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item">
                            <a href="index.php">Dashboard</a>
                        </li>
                        <li class="breadcrumb-item active">
                            User
                        </li>
                    </ol>
                </div>

            </div>

        </div>
    </section>

    <!-- Content -->
    <section class="content">

        <div class="container-fluid">

            <!-- FORM TAMBAH -->
            <div class="card">

                <div class="card-header bg-primary">
                    <h3 class="card-title text-white">
                        Tambah User
                    </h3>
                </div>

                <div class="card-body">

                    <form method="POST">

                        <div class="row">

                            <!-- Username -->
                            <div class="col-md-4">

                                <div class="form-group">
                                    <label>Username</label>

                                    <input type="text"
                                           name="username"
                                           class="form-control"
                                           placeholder="Masukkan username"
                                           required>
                                </div>

                            </div>

                            <!-- Password -->
                            <div class="col-md-4">

                                <div class="form-group">
                                    <label>Password</label>

                                    <input type="password"
                                           name="password"
                                           class="form-control"
                                           placeholder="Masukkan password"
                                           required>
                                </div>

                            </div>

                            <!-- Role -->
                            <div class="col-md-4">

                                <div class="form-group">
                                    <label>Role</label>

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

                                        <option value="">
                                            -- Pilih Role --
                                        </option>

                                        <option value="Admin">
                                            Admin
                                        </option>

                                        <option value="Petugas">
                                            Petugas
                                        </option>

                                    </select>

                                </div>

                            </div>

                        </div>

                        <button type="submit"
                                name="tambah"
                                class="btn btn-success">

                            <i class="fas fa-save"></i>
                            Simpan

                        </button>

                    </form>

                </div>

            </div>

            <?php

            // TAMBAH USER
            if (isset($_POST['tambah'])) {

                $username = mysqli_real_escape_string($koneksi, $_POST['username']);
                $password = md5($_POST['password']);
                $role     = mysqli_real_escape_string($koneksi, $_POST['role']);

                mysqli_query($koneksi,
                    "INSERT INTO user(username,password,role)
                     VALUES('$username','$password','$role')"
                );

                echo "
                <script>
                    alert('User berhasil ditambahkan');
                    location='user.php';
                </script>";
            }

            ?>

            <!-- TABEL USER -->
            <div class="card">

                <div class="card-header">
                    <h3 class="card-title">
                        Data User
                    </h3>
                </div>

                <div class="card-body">

                    <table class="table table-bordered table-striped">

                        <thead class="bg-light">

                            <tr>
                                <th width="5%">No</th>
                                <th>Username</th>
                                <th>Role</th>
                                <th width="20%">Aksi</th>
                            </tr>

                        </thead>

                        <tbody>

                        <?php

                        $no = 1;

                        $data = mysqli_query($koneksi,
                            "SELECT * FROM user ORDER BY id_user DESC"
                        );

                        while ($d = mysqli_fetch_assoc($data)) {

                        ?>

                            <tr>

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

                                <td><?= $d['username']; ?></td>

                                <td>

                                    <?php if($d['role'] == 'Admin'){ ?>

                                        <span class="badge badge-primary">
                                            Admin
                                        </span>

                                    <?php } else { ?>

                                        <span class="badge badge-success">
                                            Petugas
                                        </span>

                                    <?php } ?>

                                </td>

                                <td>

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

                                        <i class="fas fa-edit"></i>
                                        Edit

                                    </button>

                                    <a href="?hapus=<?= $d['id_user']; ?>"
                                       class="btn btn-danger btn-sm"
                                       onclick="return confirm('Yakin hapus user?')">

                                        <i class="fas fa-trash"></i>
                                        Hapus

                                    </a>

                                </td>

                            </tr>

                            <!-- MODAL EDIT -->
                            <div class="modal fade"
                                 id="edit<?= $d['id_user']; ?>">

                                <div class="modal-dialog">

                                    <div class="modal-content">

                                        <form method="POST">

                                            <div class="modal-header bg-warning">

                                                <h5 class="modal-title">
                                                    Edit User
                                                </h5>

                                                <button type="button"
                                                        class="close"
                                                        data-dismiss="modal">

                                                    <span>&times;</span>

                                                </button>

                                            </div>

                                            <div class="modal-body">

                                                <input type="hidden"
                                                       name="id_user"
                                                       value="<?= $d['id_user']; ?>">

                                                <!-- Username -->
                                                <div class="form-group">

                                                    <label>Username</label>

                                                    <input type="text"
                                                           name="username"
                                                           class="form-control"
                                                           value="<?= $d['username']; ?>"
                                                           required>

                                                </div>

                                                <!-- Role -->
                                                <div class="form-group">

                                                    <label>Role</label>

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

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

                                                        <option value="Petugas"
                                                        <?= $d['role']=='Petugas' ? 'selected' : ''; ?>>
                                                            Petugas
                                                        </option>

                                                    </select>

                                                </div>

                                            </div>

                                            <div class="modal-footer">

                                                <button type="submit"
                                                        name="update"
                                                        class="btn btn-success">

                                                    Update

                                                </button>

                                            </div>

                                        </form>

                                    </div>

                                </div>

                            </div>

                        <?php } ?>

                        </tbody>

                    </table>

                </div>

            </div>

        </div>

    </section>

</div>

<?php include 'template/footer.php'; ?>

<?php

// UPDATE
if (isset($_POST['update'])) {

    $id_user = $_POST['id_user'];
    $username = mysqli_real_escape_string($koneksi, $_POST['username']);
    $role = mysqli_real_escape_string($koneksi, $_POST['role']);

    mysqli_query($koneksi,
        "UPDATE user SET
         username='$username',
         role='$role'
         WHERE id_user='$id_user'"
    );

    echo "
    <script>
        alert('User berhasil diupdate');
        location='user.php';
    </script>";
}

// HAPUS
if (isset($_GET['hapus'])) {

    mysqli_query($koneksi,
        "DELETE FROM user
         WHERE id_user='$_GET[hapus]'"
    );

    echo "
    <script>
        alert('User berhasil dihapus');
        location='user.php';
    </script>";
}

?>