<?php

session_start();
$level = $_SESSION["level"] == 'admin';
if (!$level) {
    header("Location: index.php");
    exit;
}

require_once(dirname(__FILE__) . '\header.php');
$akun = query("SELECT * FROM user");

// tombol cari ditekan
if (isset($_POST["cari"])) {
    $akun = cari($_POST["keyword"]);
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kelola Akun</title>
    <style>
        .loader {
            width: 100px;
            position: absolute;
            top: 120px;
            left: 400px;
            z-index: -1;
            display: none;
        }

        .iii {
            width: 100px;
            height: 100px;
        }
    </style>
</head>

<body>
    <h1>Daftar Akun</h1>
    <div class="container">
        <form action="" method="POST">
            <div class="input-group flex-nowrap">
                <div class="input-group-prepend">
                    <span class="input-group-text" id="addon-wrapping">Search</span>
                </div>
                <input type="text" class="form-control" placeholder="Nama / Username" aria-label="Username" aria-describedby="addon-wrapping" autocomplete="off" id="keyword" name="keyword" autofocus>
                <button type="submit" name="cari" id="tombol-cari">Cari</button>
            </div>
        </form>

        <div id="container">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th scope="col">No.</th>
                        <th scope="col">Aksi</th>
                        <th scope="col">Photo</th>
                        <th scope="col">Nama</th>
                        <th scope="col">Alamat</th>
                        <th scope="col">NIM</th>
                        <th scope="col">Tempat/Tanggal Lahir</th>
                        <th scope="col">Jenis Kelamin</th>
                        <th scope="col">No. Telepon</th>
                        <th scope="col">Email</th>
                        <th scope="col">Pekerjaan</th>
                        <th scope="col">Username</th>
                        <th scope="col">Password</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <?php $i = 1 ?>
                        <?php foreach ($akun as $row) : ?>
                            <td><?php echo $i; ?></td>
                            <td>
                                <?php $level = $row["level"] == 'admin';
                                if (!$level) : ?>
                                    <a href="Admin_UbahAkunUser.php?id=<?php echo $row["id"] ?>">ubah</a> 
                                    <a href="hapus.php?id=<?php echo $row["id"] ?>" onclick="return confirm('yakin?');">hapus</a>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php $level = $row["level"] == 'admin';
                                if (!$level) : ?>
                                    <img class="iii" src="img/<?php echo $row["gambar"] ?>">
                                <?php endif; ?>
                            </td>
                            <td><?= $row["nama"]; ?></td>
                            <td><?= $row["alamat"]; ?></td>
                            <td><?= $row["nim"]; ?></td>
                            <td><?= $row["TTL"]; ?></td>
                            <td><?= $row["jenis"]; ?></td>
                            <td><?= $row["telepon"]; ?></td>
                            <td><?= $row["email"]; ?></td>
                            <td><?= $row["pekerjaan"]; ?></td>
                            <td><?= $row["username"]; ?></td>
                            <td>***********</td>
                    </tr>
                    <?php $i++; ?>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <div>
        <center>
            <a href="pdf.php" class="btn btn-primary btn-lg active" role="button" aria-pressed="true">Cetak?</a>
    </div>
    <script src="js/jquery-3.5.1.min.js"></script>
    <script src="js/script.js"></script>
    <?php
    require_once(dirname(__FILE__) . '\footer.php');
    ?>

</body>

</html>