<?php
include "koneksi.php";

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

    $id_anggota        = $_POST['id_anggota'];
    $tanggal_pinjam    = $_POST['tanggal_pinjam'];
    $tanggal_kembali   = $_POST['tanggal_kembali'];
    $status_transaksi  = "Dipinjam";
    $id_petugas        = 1;

    // SIMPAN KE TABEL PEMINJAMAN
    mysqli_query($koneksi,"
    INSERT INTO peminjaman
    VALUES(
        '',
        '$id_anggota',
        '$tanggal_pinjam',
        '$tanggal_kembali',
        '$status_transaksi',
        '$id_petugas'
    )
    ");

    // AMBIL ID PEMINJAMAN TERAKHIR
    $id_peminjaman = mysqli_insert_id($koneksi);

    // AMBIL DATA BUKU
    $id_buku = $_POST['id_buku'];

    // SIMPAN DETAIL PEMINJAMAN
    mysqli_query($koneksi,"
    INSERT INTO detail_peminjaman
    VALUES(
        '',
        '$id_peminjaman',
        '$id_buku',
        NULL,
        'Dipinjam'
    )
    ");

    // UPDATE STOK BUKU
    mysqli_query($koneksi,"
    UPDATE buku
    SET stok_tersedia = stok_tersedia - 1
    WHERE id_buku='$id_buku'
    ");

    // UPDATE STATUS BUKU
    mysqli_query($koneksi,"
    UPDATE buku
    SET status='Dipinjam'
    WHERE id_buku='$id_buku'
    AND stok_tersedia <= 0
    ");

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

// PENGEMBALIAN
if(isset($_GET['kembali'])){

    $id_detail = $_GET['kembali'];

    // AMBIL DATA DETAIL
    $detail = mysqli_fetch_assoc(mysqli_query($koneksi,"
    SELECT * FROM detail_peminjaman
    WHERE id_detail='$id_detail'
    "));

    $id_buku = $detail['id_buku'];

    // UPDATE DETAIL
    mysqli_query($koneksi,"
    UPDATE detail_peminjaman
    SET
        tanggal_kembali_aktual = NOW(),
        status_buku='Dikembalikan'
    WHERE id_detail='$id_detail'
    ");

    // UPDATE PEMINJAMAN
    mysqli_query($koneksi,"
    UPDATE peminjaman
    SET status_transaksi='Dikembalikan'
    WHERE id_peminjaman='".$detail['id_peminjaman']."'
    ");

    // TAMBAH STOK
    mysqli_query($koneksi,"
    UPDATE buku
    SET stok_tersedia = stok_tersedia + 1,
        status='Tersedia'
    WHERE id_buku='$id_buku'
    ");

    echo "
    <script>
        alert('Buku berhasil dikembalikan');
        window.location='peminjaman.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 Peminjaman</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">
                        Peminjaman
                    </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 Peminjaman

    </button>

</div>

<div class="card-body">

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

<thead>

<tr>

    <th>No</th>
    <th>Nama Anggota</th>
    <th>Judul Buku</th>
    <th>Tanggal Pinjam</th>
    <th>Tanggal Kembali</th>
    <th>Status</th>
    <th>Aksi</th>

</tr>

</thead>

<tbody>

<?php
$no = 1;

$query = mysqli_query($koneksi,"
SELECT 
    p.*,
    a.nama,
    b.judul,
    d.id_detail,
    d.status_buku,
    d.tanggal_kembali_aktual
FROM peminjaman p
JOIN anggota a
ON p.id_anggota = a.id_anggota
JOIN detail_peminjaman d
ON p.id_peminjaman = d.id_peminjaman
JOIN buku b
ON d.id_buku = b.id_buku
ORDER BY p.id_peminjaman DESC
");

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

<tr>

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

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

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

    <td>
        <?= date('d-m-Y', strtotime($data['tanggal_pinjam'])) ?>
    </td>

    <td>
        <?= date('d-m-Y', strtotime($data['tanggal_kembali'])) ?>
    </td>

    <td>

        <?php if($data['status_transaksi'] == 'Dipinjam'){ ?>

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

        <?php } else { ?>

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

        <?php } ?>

    </td>

    <td>

        <?php if($data['status_transaksi'] == 'Dipinjam'){ ?>

        <a href="peminjaman.php?kembali=<?= $data['id_detail'] ?>"
           class="btn btn-success btn-sm"
           onclick="return confirm('Konfirmasi pengembalian buku?')">

            <i class="fas fa-undo"></i>
            Kembalikan

        </a>

        <?php } else { ?>

            <span class="text-success">
                Selesai
            </span>

        <?php } ?>

    </td>

</tr>

<?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">

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

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

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

        &times;

    </button>

</div>

<div class="modal-body">

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

    <label>Anggota</label>

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

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

        <?php
        $anggota = mysqli_query($koneksi,"
        SELECT * FROM anggota
        WHERE status_aktif='Aktif'
        ");

        while($a = mysqli_fetch_assoc($anggota)){
        ?>

        <option value="<?= $a['id_anggota'] ?>">
            <?= $a['nama'] ?>
        </option>

        <?php } ?>

    </select>

</div>

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

    <label>Buku</label>

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

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

        <?php
        $buku = mysqli_query($koneksi,"
        SELECT * FROM buku
        WHERE stok_tersedia > 0
        ");

        while($b = mysqli_fetch_assoc($buku)){
        ?>

        <option value="<?= $b['id_buku'] ?>">
            <?= $b['judul'] ?> 
            (Stok : <?= $b['stok_tersedia'] ?>)
        </option>

        <?php } ?>

    </select>

</div>

<!-- TANGGAL PINJAM -->
<div class="form-group">

    <label>Tanggal Pinjam</label>

    <input type="date"
           name="tanggal_pinjam"
           class="form-control"
           required>

</div>

<!-- TANGGAL KEMBALI -->
<div class="form-group">

    <label>Tanggal Kembali</label>

    <input type="date"
           name="tanggal_kembali"
           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"; ?>