<?php
include 'template/header.php';
include 'template/h_menu.php';
include 'sidebar.php';
include 'koneksi.php';
?>

<div class="content-wrapper p-3">

<!-- HEADER -->
<section class="content-header">

    <div class="container-fluid">

        <div class="row mb-2">

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

                <h1>Laporan Peminjaman 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">
                        Laporan
                    </li>

                </ol>

            </div>

        </div>

    </div>

</section>

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

<div class="container-fluid">

<!-- FILTER -->
<div class="card">

    <div class="card-header bg-primary text-white">

        <i class="fas fa-filter"></i>
        Filter Laporan

    </div>

    <div class="card-body">

        <form method="GET">

            <div class="row">

                <!-- DARI -->
                <div class="col-md-4">

                    <label>Dari Tanggal</label>

                    <input type="date"
                           name="dari"
                           class="form-control"
                           value="<?= $_GET['dari'] ?? '' ?>">

                </div>

                <!-- SAMPAI -->
                <div class="col-md-4">

                    <label>Sampai Tanggal</label>

                    <input type="date"
                           name="sampai"
                           class="form-control"
                           value="<?= $_GET['sampai'] ?? '' ?>">

                </div>

                <!-- BUTTON -->
                <div class="col-md-4 d-flex align-items-end">

                    <button class="btn btn-success w-100">

                        <i class="fas fa-search"></i>
                        Tampilkan

                    </button>

                </div>

            </div>

        </form>

    </div>

</div>

<?php

/* FILTER */
$where = "";

if(
    isset($_GET['dari']) &&
    isset($_GET['sampai']) &&
    $_GET['dari'] != "" &&
    $_GET['sampai'] != ""
){

    $dari   = $_GET['dari'];
    $sampai = $_GET['sampai'];

    $where = "
    WHERE DATE(p.tanggal_pinjam)
    BETWEEN '$dari'
    AND '$sampai'
    ";
}

/* TOTAL PEMINJAMAN */
$query_pinjam = "
SELECT COUNT(*) AS total
FROM peminjaman p
$where
";

$total_pinjam = mysqli_fetch_assoc(
    mysqli_query($koneksi, $query_pinjam)
)['total'] ?? 0;


/* TOTAL DIKEMBALIKAN */
$query_kembali = "
SELECT COUNT(*) AS total
FROM peminjaman p
";

/* JIKA ADA FILTER */
if($where != ""){

    $query_kembali .= "
    $where
    AND p.status_transaksi='Dikembalikan'
    ";

}else{

    $query_kembali .= "
    WHERE p.status_transaksi='Dikembalikan'
    ";

}

$total_kembali = mysqli_fetch_assoc(
    mysqli_query($koneksi, $query_kembali)
)['total'] ?? 0;

?>

<!-- RINGKASAN -->
<div class="row mt-3">

    <!-- TOTAL PEMINJAMAN -->
    <div class="col-md-6">

        <div class="small-box bg-info">

            <div class="inner">

                <h3>
                    <?= number_format($total_pinjam) ?>
                </h3>

                <p>Total Peminjaman</p>

            </div>

            <div class="icon">

                <i class="fas fa-book-reader"></i>

            </div>

        </div>

    </div>

    <!-- TOTAL PENGEMBALIAN -->
    <div class="col-md-6">

        <div class="small-box bg-success">

            <div class="inner">

                <h3>
                    <?= number_format($total_kembali) ?>
                </h3>

                <p>Total Pengembalian</p>

            </div>

            <div class="icon">

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

            </div>

        </div>

    </div>

</div>

<!-- TABEL -->
<div class="card mt-3">

    <div class="card-header">

        <i class="fas fa-table"></i>
        Data Laporan

    </div>

    <div class="card-body">

        <table id="tableLaporan"
               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>Kembali Aktual</th>
                    <th>Status</th>

                </tr>

            </thead>

            <tbody>

            <?php

            $no = 1;

            $data = mysqli_query($koneksi,"
            SELECT

                p.*,
                a.nama,
                b.judul,
                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

            $where

            ORDER BY p.tanggal_pinjam DESC
            ");

            while($d = mysqli_fetch_assoc($data)){

            ?>

            <tr>

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

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

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

                <td>

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

                </td>

                <td>

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

                </td>

                <td>

                    <?php
                    if(
                        $d['tanggal_kembali_aktual'] != NULL &&
                        $d['tanggal_kembali_aktual'] != ''
                    ){
                    ?>

                        <?= date(
                            'd-m-Y',
                            strtotime($d['tanggal_kembali_aktual'])
                        ) ?>

                    <?php } else { ?>

                        <span class="text-danger">
                            Belum Dikembalikan
                        </span>

                    <?php } ?>

                </td>

                <td>

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

                        <span class="badge badge-warning">

                            Dipinjam

                        </span>

                    <?php } else { ?>

                        <span class="badge badge-success">

                            Dikembalikan

                        </span>

                    <?php } ?>

                </td>

            </tr>

            <?php } ?>

            </tbody>

        </table>

    </div>

</div>

</div>

</section>

</div>

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

<script>

$(document).ready(function(){

    $('#tableLaporan').DataTable({

        responsive: true,

        autoWidth: false,

        dom: 'Bfrtip',

        buttons: [
            'excel',
            'print'
        ]

    });

});

</script>