<div class="content-wrapper">
    <div class="content-header bg-link bg-light">
        <div class="container-fluid">
            <div class="row mb-2" mb-2>
                <div class="col-sm-6">
                    <h1 class="m-0 text-dark">Admin</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a class="text-dark" href="#">Home</a></li>
                        <li class="breadcrumb-item active"><a class="text-dark" href="#">Admin</a>
                        </li>
                    </ol>
                </div>
            </div>
        </div>
    </div>



    <div class="card ml-3 mr-3">
        <div class="card-header bg-success">
            <h1>Tabel Admin</h1>
        </div>

        <div class="card-body">
            <div class="table-responsive">

                <button data-toggle="modal" title=" Tambah Data" data-target="#tambah" class="btn btn-success mb-3"><i class="fa fa-plus"></i></button>

                <table class="table table-bordered" id='example1' style="text-align: center;">
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>Nama Admin</th>
                            <th>Username Admin</th>
                            <th>Password Admin</th>
                            <th>Alamat Admin</th>
                            <th>Tanggal Lahir Admin</th>
                            <th>Foto Admin</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>



                        <?php

                        $dataadmin = $koneksi->query("SELECT * FROM tbl_admin");
                        // var_dump($dataadmin);
                        // exit;

                        foreach ($dataadmin as $no => $pecah) :

                        ?>
                            <tr>
                                <td><?= ++$no ?></td>
                                <td><?= $pecah['admin_nama'] ?></td>
                                <td><?= $pecah['admin_username'] ?></td>
                                <td><?= $pecah['admin_password'] ?></td>
                                <td><?= $pecah['admin_alamat'] ?></td>
                                <td><?= $pecah['admin_ttl'] ?></td>
                                <td>

                                    <img width="100" height="100" src="dist/img/imgadmin/<?= $pecah['admin_foto']; ?>" alt="">

                                </td>
                                <td>
                                    <a href="?page=pages/admin/edit&idedit=<?= $pecah['admin_id'] ?>" data-toggle="tooltip" title="Edit Admin" class="btn btn-warning mr-2"><i class="fa fa-edit"></i></a>
                                    <a onclick="return confirm('Apa Anda Yakin Ingin Menghapus ?')" href="?page=pages/admin/hapus&idhapus=<?= $pecah['admin_id'] ?>" data-toggle="tooltip" title="Hapus Admin" class="btn btn-danger"><i class="fa fa-trash"></i></a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>

                </table>
            </div>
        </div>
    </div>
</div>
<style>
    .page-item.active .page-link {
        background-color: green;
        border-color: green;
    }
</style>


<!-- Modal -->
<div class="modal fade" id="tambah" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-success">
                <h5 class="modal-title" id="exampleModalLabel">Tambah Data</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form action="" method="POST" enctype="multipart/form-data">

                    <div class="form-group">
                        <label>Nama Admin</label>
                        <input type="text" name="nama" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>Username Admin</label>
                        <input type="text" name="username" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>Password Admin</label>
                        <input type="password" name="password" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>Alamat Admin</label>
                        <input type="text" name="alamat" class="form-control">
                    </div>
                    <div class="form-row">
                        <div class="col form-group">
                            <label for="">Tempat Lahir Admin</label>
                            <input type="text" name="tempat" class="form-control">
                        </div>
                        <div class="col form-group">
                            <label>Tanggal lahir Admin</label>
                            <input type="date" name="tanggal" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Foto Admin</label>
                        <input type="file" name="foto" class="form-control">
                    </div>



            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary" name="simpan">Simpan</button>
            </div>

            </form>
            <?php
            if (isset($_POST['simpan'])) {
                $nama          =  $_POST['nama'];
                $username      =  $_POST['username'];
                $password      =  md5($_POST['password']);
                $alamat        =  $_POST['alamat'];
                $tanggal        =  $_POST['tanggal'];
                $tempat         = $_POST['tempat'];

                $ttl = [$tempat, $tanggal];
                $ttl = implode(' ', $ttl);


                $namafoto           = $_FILES['foto']['name'];
                $lokasifoto         = $_FILES['foto']['tmp_name'];

                if ($namafoto !== "") {
                    $namafoto = uniqid() . '-' . $namafoto;
                } else {
                    echo "<script>
                            alert('Masukan Foto Terlebih Dahulu');
                            window.location= '?page=pages/admin/view';
                        </script>";
                    die;
                }

                move_uploaded_file($lokasifoto, "dist/img/imgadmin/" . $namafoto);

                $simpan = $koneksi->query("INSERT INTO tbl_admin (admin_nama,
                    admin_username,
                    admin_password,
                    admin_alamat,
                    admin_ttl,
                    admin_foto) VALUES('$nama','$username','$password','$alamat','$ttl','$namafoto')");


                if ($simpan == TRUE) {
                    echo "<script>
                                    alert ('Data Berhasil Di Simpan')
                                    window.location= '?page=pages/admin/view'
                                    </script>";
                } else {
                    echo "<script>
                                    alert ('Data Gagal Di Simpan')
                                    window.location= '?page=pages/admin/view'
                                </script>";
                }
            }
            ?>

        </div>
    </div>
</div>