<?php
include "koneksi.php";

// TAMBAH DATA
if(isset($_POST['simpan'])){

    $judul          = $_POST['judul'];
    $status         = $_POST['status'];
    $penulis        = $_POST['penulis'];
    $penerbit       = $_POST['penerbit'];
    $tahun_terbit   = $_POST['tahun_terbit'];
    $kategori       = $_POST['kategori'];
    $stok_total     = $_POST['stok_total'];
    $stok_tersedia  = $_POST['stok_tersedia'];

    $cover = '';

    if(!empty($_FILES['cover']['name'])){

        $cover = time().'_'.$_FILES['cover']['name'];

        move_uploaded_file(
            $_FILES['cover']['tmp_name'],
            'upload/'.$cover
        );
    }

    mysqli_query($koneksi,"
    INSERT INTO buku(
        id_buku,
        judul,
        status,
        penulis,
        penerbit,
        tahun_terbit,
        kategori,
        stok_total,
        stok_tersedia,
        cover
    )
    VALUES(
        '',
        '$judul',
        '$status',
        '$penulis',
        '$penerbit',
        '$tahun_terbit',
        '$kategori',
        '$stok_total',
        '$stok_tersedia',
        '$cover'
    )
    ");

    echo "
    <script>
        alert('Data buku berhasil ditambahkan');
        window.location='buku.php';
    </script>
    ";
}

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

    $id_buku        = $_POST['id_buku'];
    $judul          = $_POST['judul'];
    $status         = $_POST['status'];
    $penulis        = $_POST['penulis'];
    $penerbit       = $_POST['penerbit'];
    $tahun_terbit   = $_POST['tahun_terbit'];
    $kategori       = $_POST['kategori'];
    $stok_total     = $_POST['stok_total'];
    $stok_tersedia  = $_POST['stok_tersedia'];

    $update_cover = "";

    if(!empty($_FILES['cover']['name'])){

        $cover = time().'_'.$_FILES['cover']['name'];

        move_uploaded_file(
            $_FILES['cover']['tmp_name'],
            'upload/'.$cover
        );

        $update_cover = ", cover='$cover'";
    }

    mysqli_query($koneksi,"
    UPDATE buku SET
        judul='$judul',
        status='$status',
        penulis='$penulis',
        penerbit='$penerbit',
        tahun_terbit='$tahun_terbit',
        kategori='$kategori',
        stok_total='$stok_total',
        stok_tersedia='$stok_tersedia'
        $update_cover
    WHERE id_buku='$id_buku'
    ");

    echo "
    <script>
        alert('Data buku berhasil diubah');
        window.location='buku.php';
    </script>
    ";
}

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

    $id = $_GET['hapus'];

    mysqli_query($koneksi,"
    DELETE FROM buku
    WHERE id_buku='$id'
    ");

    echo "
    <script>
        alert('Data buku berhasil dihapus');
        window.location='buku.php';
    </script>
    ";
}
?>

<?php include "template/header.php"; ?>

<div class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">

<?php include "template/h_menu.php"; ?>
<?php include "sidebar.php"; ?>

<!-- CONTENT -->
<div class="content-wrapper">

<section class="content-header">

    <div class="container-fluid">

        <div class="row mb-2">

            <div class="col-sm-6">
                <h1>Data Buku</h1>
            </div>

            <div class="col-sm-6">

                <ol class="breadcrumb float-sm-right">

                    <li class="breadcrumb-item">
                        <a href="index.php">Home</a>
                    </li>

                    <li class="breadcrumb-item active">
                        Data Buku
                    </li>

                </ol>

            </div>

        </div>

    </div>

</section>

<!-- MAIN CONTENT -->
<section class="content">

<div class="container-fluid">

<div class="card">

<div class="card-header">

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

        <i class="fas fa-plus"></i>
        Tambah Buku

    </button>

</div>

<div class="card-body">

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

<thead>

<tr>

    <th>No</th>
<th>Cover</th>
<th>Judul Buku</th>
    <th>Status</th>
    <th>Penulis</th>
    <th>Penerbit</th>
    <th>Tahun</th>
    <th>Kategori</th>
    <th>Stok</th>
    <th>Tersedia</th>
    <th>Aksi</th>

</tr>

</thead>

<tbody>

<?php
$no = 1;

$query = mysqli_query($koneksi,"
SELECT * FROM buku
ORDER BY id_buku DESC
");

while($data = mysqli_fetch_assoc($query)){
?>

<tr>

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

    <td>

<?php if(!empty($data['cover'])){ ?>

<img src="upload/<?= $data['cover'] ?>"
     width="70"
     height="90"
     style="
     object-fit:cover;
     border-radius:8px;
     box-shadow:0 2px 8px rgba(0,0,0,.2);
     ">

<?php }else{ ?>

<span class="badge badge-secondary">
Tidak Ada
</span>

<?php } ?>

</td>

<td><?= htmlspecialchars($data['judul']) ?></td>

    <td>

        <?php if($data['status'] == 'Tersedia'){ ?>

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

        <?php } else { ?>

            <span class="badge badge-danger">
                Dipinjam
            </span>

        <?php } ?>

    </td>

    <td><?= htmlspecialchars($data['penulis']) ?></td>

    <td><?= htmlspecialchars($data['penerbit']) ?></td>

    <td><?= htmlspecialchars($data['tahun_terbit']) ?></td>

    <td><?= htmlspecialchars($data['kategori']) ?></td>

    <td><?= htmlspecialchars($data['stok_total']) ?></td>

    <td><?= htmlspecialchars($data['stok_tersedia']) ?></td>

    <td>

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

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

        </button>

        <a href="buku.php?hapus=<?= $data['id_buku'] ?>"
           class="btn btn-danger btn-sm"
           onclick="return confirm('Yakin ingin menghapus data?')">

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

        </a>

    </td>

</tr>

<!-- MODAL EDIT -->
<div class="modal fade"
     id="edit<?= $data['id_buku'] ?>">

<div class="modal-dialog">

<div class="modal-content">

<form method="POST" enctype="multipart/form-data">

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

    <h4 class="modal-title">
        Edit Buku
    </h4>

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

        &times;

    </button>

</div>

<div class="modal-body">

<input type="hidden"
       name="id_buku"
       value="<?= $data['id_buku'] ?>">

<div class="form-group">

    <label>Judul Buku</label>

    <input type="text"
           name="judul"
           class="form-control"
           value="<?= $data['judul'] ?>"
           required>

</div>

<div class="form-group">

    <label>Status</label>

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

        <option value="Tersedia"
        <?= $data['status']=='Tersedia'?'selected':'' ?>>
        Tersedia
        </option>

        <option value="Dipinjam"
        <?= $data['status']=='Dipinjam'?'selected':'' ?>>
        Dipinjam
        </option>

    </select>

</div>

<div class="form-group">

    <label>Penulis</label>

    <input type="text"
           name="penulis"
           class="form-control"
           value="<?= $data['penulis'] ?>"
           required>

</div>

<div class="form-group">

    <label>Penerbit</label>

    <input type="text"
           name="penerbit"
           class="form-control"
           value="<?= $data['penerbit'] ?>"
           required>

</div>

<div class="form-group">

    <label>Tahun Terbit</label>

    <input type="number"
           name="tahun_terbit"
           class="form-control"
           value="<?= $data['tahun_terbit'] ?>"
           required>

</div>

<div class="form-group">

    <label>Kategori</label>

    <input type="text"
           name="kategori"
           class="form-control"
           value="<?= $data['kategori'] ?>"
           required>

</div>

<div class="form-group">

    <label>Stok Total</label>

    <input type="number"
           name="stok_total"
           class="form-control"
           value="<?= $data['stok_total'] ?>"
           required>

</div>

<div class="form-group">

    <label>Stok Tersedia</label>

    <input type="number"
           name="stok_tersedia"
           class="form-control"
           value="<?= $data['stok_tersedia'] ?>"
           required>

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

    <label>Cover Buku</label>

    <input type="file"
           name="cover"
           class="form-control"
           accept="image/*">

    <?php if(!empty($data['cover'])){ ?>

        <br>

        <img src="upload/<?= $data['cover'] ?>"
             width="100">

    <?php } ?>

</div>

</div>

<div class="modal-footer">

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

        Update

    </button>

    <button type="button"
            class="btn btn-secondary"
            data-dismiss="modal">

        Batal

    </button>

</div>

</form>

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

<?php } ?>

</tbody>

</table>

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

<!-- MODAL TAMBAH -->
<div class="modal fade" id="modalTambah">

<div class="modal-dialog">

<div class="modal-content">

<form method="POST" enctype="multipart/form-data">

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

    <h4 class="modal-title">
        Tambah Buku
    </h4>

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

        &times;

    </button>

</div>

<div class="modal-body">
    <div class="form-group">

    <label>Cover Buku</label>

    <input type="file"
           name="cover"
           class="form-control"
           accept="image/*">

</div>

<div class="form-group">

    <label>Judul Buku</label>

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

</div>

<div class="form-group">

    <label>Status</label>

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

        <option value="">-- Pilih --</option>
        <option value="Tersedia">Tersedia</option>
        <option value="Dipinjam">Dipinjam</option>

    </select>

</div>

<div class="form-group">

    <label>Penulis</label>

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

</div>

<div class="form-group">

    <label>Penerbit</label>

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

</div>

<div class="form-group">

    <label>Tahun Terbit</label>

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

</div>

<div class="form-group">

    <label>Kategori</label>

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

</div>

<div class="form-group">

    <label>Stok Total</label>

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

</div>

<div class="form-group">

    <label>Stok Tersedia</label>

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

</div>

</div>

<div class="modal-footer">

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

        Simpan

    </button>

    <button type="button"
            class="btn btn-secondary"
            data-dismiss="modal">

        Batal

    </button>

</div>

</form>

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

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