<?php
include "asset/session_check.php";

$nama_pegawai = $_SESSION['nama_pegawai'];
$akses = $_SESSION['akses'];
?>
<!doctype html>
<html lang="en">

<head>
    <?php
    include "asset/head.php";
    ?>
</head>

<body>
    <!-- NAVIGASI -->
    <?php
    include "asset/navigasi.php";
    ?>
    <div class="container">
        <div class="row">
            <div class="d-flex bd-highlight mb-3">
                <div class="me-auto p-2 bd-highlight">
                    <h1>Data Pegawai</h1>
                </div>
                <div class="p-2 bd-highlight" style="align-items: center;display: flex;">
                    <!-- <a class="tmbdata" href="pegawai-tambah.php">Tambah Data</a> -->
                    <button
                        class="btn tmbdata"
                        data-bs-toggle="modal"
                        data-bs-target="#tambah">
                        Tambah Data
                    </button>
                </div>
            </div>
        </div>
        <div class="row">
            <table class="table">
                <thead>
                    <tr>
                        <th scope="col">Nama</th>
                        <th scope="col">Username</th>
                        <th scope="col">Akses</th>
                        <th scope="col">Handle</th>
                        <?php
                        if ($nama_pegawai == "Dika" || $nama_pegawai == "Ivan IT") {
                        ?>
                            <th scope="col">Password</th>
                        <?php
                        }
                        ?>
                        <th scope="col">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    include "koneksi.php";
                    $sql = mysqli_query($konek, "SELECT * FROM pegawai");
                    while ($row = mysqli_fetch_array($sql)) {
                    ?>
                        <tr>
                            <td><?php echo $row['nama_pegawai'] ?></td>
                            <td><?php echo $row['username'] ?></td>
                            <td><?php echo $row['akses'] ?></td>
                            <td><?php echo $row['handle'] ?></td>
                            <?php
                            if ($nama_pegawai == "Dika" || $nama_pegawai == "Ivan IT") {
                            ?>
                                <td><?php echo $row['passe'] ?></td>
                            <?php
                            }
                            ?>
                            <td>
                                <!-- <a href="" class="btn btn-primary btn-sm">+</a>
                                <a href="" class="btn btn-danger btn-sm">-</a> -->

                                <button
                                    class="btn btn-warning btn-sm"
                                    data-bs-toggle="modal"
                                    data-bs-target="#edit<?= $row['idpegawai']; ?>">
                                    <i class="bi bi-pencil-fill"></i>
                                </button>
                                <a href="" class="btn btn-danger btn-sm"><i class="bi bi-trash"></i></a>
                            </td>
                            <div class="modal fade" id="edit<?= $row['idpegawai']; ?>" tabindex="-1">
                                <div class="modal-dialog modal-lg">
                                    <div class="modal-content">
                                        <form action="function/function-pegawai.php?idpegawai='<?= $row['idpegawai'] ?>'" method="post">
                                            <div class="modal-header">
                                                <h5 class="modal-title">Edit Data Pegawai</h5>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                            </div>
                                            <div class="modal-body">
                                                <input type="text" name="idgadspen" value="<?= $row['idpegawai']; ?>" hidden>
                                                <div class="mb-2">
                                                    <label>Nama Pegawai</label>
                                                    <input type="text" name="nama_pegawai" class="form-control" value="<?= $row['nama_pegawai']; ?>">
                                                </div>
                                                <div class="mb-2">
                                                    <label>Username</label>
                                                    <input type="text" name="username" class="form-control" value="<?= $row['username']; ?>">
                                                </div>
                                                <div class="mb-2">
                                                    <label>Password</label>
                                                    <input type="text" name="passe" class="form-control" value="<?= $row['passe']; ?>">
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">Akses</label>
                                                    <select class="form-select" name="akses">
                                                        <option value="Raxi" <?= ($row['akses'] == 'Raxi') ? 'selected' : '' ?>>Raxi</option>
                                                        <option value="Bisman" <?= ($row['akses'] == 'Bisman') ? 'selected' : '' ?>>Bisman</option>
                                                        <option value="Semua" <?= ($row['akses'] == 'Semua') ? 'selected' : '' ?>>Semua</option>
                                                        <option value="Ga Semua" <?= ($row['akses'] == 'Ga Semua') ? 'selected' : '' ?>>Ga Semua</option>
                                                    </select>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">Handle</label>
                                                    <select class="form-select" name="handle">
                                                        <option value="Semua" <?= ($row['handle'] == 'Semua') ? 'selected' : '' ?>>Semua</option>
                                                        <option value="Penjul" <?= ($row['handle'] == 'Penjul') ? 'selected' : '' ?>>Penjul</option>
                                                        <option value="Service" <?= ($row['handle'] == 'Service') ? 'selected' : '' ?>>Service</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="submit" name="editpeg" class="btn tmbdata">Update</button>
                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                                            </div>
                                        </form>

                                    </div>
                                </div>
                            </div>
                        </tr>
                    <?php
                    }
                    ?>
                </tbody>
            </table>
            <div class="modal fade" id="tambah" tabindex="-1">
                <div class="modal-dialog modal-lg">
                    <div class="modal-content">
                        <form action="function/function-pegawai.php" method="post">
                            <div class="modal-header">
                                <h5 class="modal-title">Edit Data Pegawai</h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                            </div>

                            <div class="modal-body">
                                <div class="row">
                                    <div class="col">
                                        <div class="mb-3">
                                            <label class="form-label">Nama Pegawai</label>
                                            <input type="text" class="form-control" name="nama_pegawai" placeholder="Masukkan Nama Pegawai">
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Akses</label>
                                            <select class="form-select" name="akses">
                                                <option selected>Open this select menu</option>
                                                <option value="Raxi">Raxi</option>
                                                <option value="Bisman">Bisman</option>
                                                <option value="Semua">Semua</option>
                                                <option value="Ga Semua">Ga Semua</option>
                                            </select>
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Handle</label>
                                            <select class="form-select" name="akses">
                                                <option selected>Open this select menu</option>
                                                <option value="Semua">Semua</option>
                                                <option value="Penjul">Penjul</option>
                                                <option value="Service">Service</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col">
                                        <div class="mb-3">
                                            <label class="form-label">Username</label>
                                            <input type="text" class="form-control" name="username" placeholder="Masukkan Nama Username">
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Password</label>
                                            <input type="text" class="form-control" name="password" placeholder="Masukkan Nama Username">
                                        </div>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="submit" name="tambahpeg" class="btn tmbdata">Simpan</button>
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                                </div>
                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- INI FOOTER -->
    <?php
    include "asset/footer.php";
    ?>
</body>

</html>