<?= $this->extend('layout/template') ?>
<?= $this->section('content') ?>

<div class="container-fluid">

    <!-- Header -->
    <div class="row mb-3">
        <div class="col-md-12">
            <div class="card shadow border-0">
                <div class="card-body">

                    <div class="d-flex justify-content-between align-items-center">

                        <div>
                            <h3 class="font-weight-bold text-primary mb-1">
                                <i class="fas fa-book-reader"></i>
                                Laporan Perpustakaan
                            </h3>

                            <small class="text-muted">
                                Rekap seluruh aktivitas perpustakaan
                            </small>
                        </div>

                        <div class="text-right">
                            <h6><?= date('d F Y') ?></h6>
                        </div>

                    </div>

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


    <!-- Statistik -->
    <div class="row">

        <div class="col-lg-3 col-md-6">
            <div class="small-box bg-primary shadow">
                <div class="inner">
                    <h3><?= $totalPinjam ?? 0 ?></h3>
                    <p>Total Peminjaman</p>
                </div>
                <div class="icon">
                    <i class="fas fa-book"></i>
                </div>
            </div>
        </div>

        <div class="col-lg-3 col-md-6">
            <div class="small-box bg-success shadow">
                <div class="inner">
                    <h3><?= $totalKembali ?? 0 ?></h3>
                    <p>Sudah Dikembalikan</p>
                </div>
                <div class="icon">
                    <i class="fas fa-check-circle"></i>
                </div>
            </div>
        </div>

        <div class="col-lg-3 col-md-6">
            <div class="small-box bg-warning shadow">
                <div class="inner">
                    <h3><?= $belumKembali ?? 0 ?></h3>
                    <p>Belum Dikembalikan</p>
                </div>
                <div class="icon">
                    <i class="fas fa-clock"></i>
                </div>
            </div>
        </div>

        <div class="col-lg-3 col-md-6">
            <div class="small-box bg-danger shadow">
                <div class="inner">
                    <h3>
                        Rp <?= number_format($totalDenda ?? 0,0,',','.') ?>
                    </h3>
                    <p>Total Denda</p>
                </div>
                <div class="icon">
                    <i class="fas fa-money-bill-wave"></i>
                </div>
            </div>
        </div>

    </div>



    <!-- Menu -->
    <div class="card card-primary shadow">

        <div class="card-header">
            <h3 class="card-title">
                <i class="fas fa-chart-bar"></i>
                Jenis Laporan
            </h3>
        </div>

        <div class="card-body">

            <div class="row">

                <div class="col-md-3 mb-3">
                    <a href="<?= base_url('laporan') ?>"
                        class="btn btn-primary btn-block">

                        <i class="fas fa-user-graduate"></i>
                        Laporan Per Siswa
                    </a>
                </div>

                <div class="col-md-3 mb-3">
                    <a href="<?= base_url('laporan/rekap-mingguan') ?>"
                        class="btn btn-warning btn-block">

                        <i class="fas fa-calendar-week"></i>
                        Rekap Mingguan
                    </a>
                </div>

                <div class="col-md-3 mb-3">
                    <a href="<?= base_url('laporan/rekap-bulanan') ?>"
                        class="btn btn-info btn-block">

                        <i class="fas fa-calendar-alt"></i>
                        Rekap Bulanan
                    </a>
                </div>

                <div class="col-md-3 mb-3">
                    <a href="<?= base_url('laporan/pengunjung') ?>"
                        class="btn btn-secondary btn-block">

                        <i class="fas fa-users"></i>
                        Pengunjung Harian
                    </a>
                </div>

            </div>

        </div>

    </div>



<!-- Grafik -->
<div class="card shadow">

    <div class="card-header bg-white">
        <h3 class="card-title">
            <i class="fas fa-chart-line text-primary"></i>
            Grafik Pengunjung Per Hari
        </h3>
    </div>

    <div class="card-body">

        <?php if(empty($labels)){ ?>

            <div class="text-center py-5">

                <i class="fas fa-chart-line fa-5x text-secondary mb-3"></i>

                <h4>Belum Ada Data Pengunjung</h4>

                <p class="text-muted">
                    Grafik akan otomatis tampil setelah ada data pengunjung.
                </p>

            </div>

        <?php }else{ ?>

            <canvas id="grafikPengunjung" height="120"></canvas>

        <?php } ?>

    </div>

</div>

<br>

<!-- Tabel -->
<div class="card shadow">

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

        <h3 class="card-title text-white">

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

            Data Laporan

        </h3>

    </div>

    <div class="card-body">

        <div class="mb-3">

            <a href="<?= base_url('laporan/cetak') ?>" target="_blank" class="btn btn-primary">
                <i class="fas fa-print"></i> Cetak
            </a>

            <a href="<?= base_url('laporan/pdf') ?>" class="btn btn-danger">
                <i class="fas fa-file-pdf"></i> PDF
            </a>

            <a href="<?= base_url('laporan/excel') ?>" class="btn btn-success">
                <i class="fas fa-file-excel"></i> Excel
            </a>

        </div>

        <div class="table-responsive">

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

                <thead class="bg-primary text-center">

                <tr>

                    <th>No</th>
                    <th>Nama</th>
                    <th>Jenis</th>
                    <th>Kelas</th>
                    <th>Total Pinjam</th>
                    <th>Sudah Kembali</th>
                    <th>Belum Kembali</th>
                    <th>Total Denda</th>

                </tr>

                </thead>

                <tbody>

                <?php if(!empty($laporan)): ?>

                    <?php $no=1; foreach($laporan as $row): ?>

                    <tr>

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

                        <td><?= esc($row['nama']) ?></td>

                        <td><?= esc($row['jenis']) ?></td>

                        <td><?= esc($row['kelas']) ?></td>

                        <td><?= $row['total_pinjam'] ?></td>

                        <td><?= $row['sudah_kembali'] ?></td>

                        <td><?= $row['belum_kembali'] ?></td>

                        <td>
                            Rp <?= number_format($row['total_denda'],0,',','.') ?>
                        </td>

                    </tr>

                    <?php endforeach; ?>

                <?php else: ?>

                    <tr>

                        <td colspan="8" class="text-center">

                            Belum ada data laporan.

                        </td>

                    </tr>

                <?php endif; ?>

                </tbody>

            </table>

        </div>

    </div>

</div>

<script src="<?= base_url('plugins/chart.js/Chart.min.js') ?>"></script>

<?php if(!empty($labels)){ ?>

<script>

const ctx=document.getElementById('grafikPengunjung');

new Chart(ctx,{

type:'line',

data:{

labels:<?= json_encode($labels) ?>,

datasets:[{

label:'Jumlah Pengunjung',

data:<?= json_encode($dataPengunjung) ?>,

borderColor:'#0d6efd',

backgroundColor:'rgba(13,110,253,.15)',

fill:true,

tension:.4,

borderWidth:3,

pointRadius:5

}]

},

options:{

responsive:true,

maintainAspectRatio:false,

plugins:{
legend:{
display:false
}
},

scales:{
y:{
beginAtZero:true
}
}

}

});

</script>

<?php } ?>

<?= $this->endSection() ?>