<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\LaporanModel;

class Laporan extends BaseController
{
    protected $laporan;

public function __construct()
    {
        $this->laporan = new LaporanModel();
    }

    public function index()
    {
        // Ambil data pengunjung 30 hari terakhir untuk grafik
        $pengunjung = $this->laporan->getPengunjungPerHari();
        $grafikLabel = [];
        $grafikData = [];
        foreach ($pengunjung as $p) {
            $grafikLabel[] = date('d-m-Y', strtotime($p['tanggal']));
            $grafikData[] = (int)$p['total_pengunjung'];
        }

        $data = [
            'title'          => 'Laporan Perpustakaan',
            'laporan'        => $this->laporan->getLaporanSiswa(),
            'totalPinjam'    => $this->laporan->totalPeminjaman(),
            'totalKembali'   => $this->laporan->totalPengembalian(),
            'belumKembali'   => $this->laporan->totalBelumKembali(),
            'totalDenda'     => $this->laporan->totalDenda(),
            'labels'         => $grafikLabel,
            'dataPengunjung' => $grafikData,
            'grafikLabel'    => $grafikLabel,
            'grafikData'     => $grafikData
        ];

        return view('laporan/index', $data);
    }

    public function detail($anggota_id)
    {
        $data = [
            'title'      => 'Detail Laporan',
            'detail'     => $this->laporan->getDetailSiswa($anggota_id),
            'totalDenda' => $this->laporan->getTotalDenda($anggota_id)
        ];

        return view('laporan/detail', $data);
    }

    public function rekapMingguan()
    {
        $awal  = $this->request->getGet('awal');
        $akhir = $this->request->getGet('akhir');

        if ($awal == null || $akhir == null) {
            $awal  = date('Y-m-d', strtotime('-6 day'));
            $akhir = date('Y-m-d');
        }

        $data = [
            'title' => 'Rekap Mingguan',
            'awal'  => $awal,
            'akhir' => $akhir,
            'rekap' => $this->laporan->getRekapMingguan($awal, $akhir)
        ];

        return view('laporan/rekap_mingguan', $data);
    }

    public function rekapBulanan()
    {
        $bulan = $this->request->getGet('bulan');
        $tahun = $this->request->getGet('tahun');

        if ($bulan == null) {
            $bulan = date('m');
        }
        if ($tahun == null) {
            $tahun = date('Y');
        }

        $data = [
            'title' => 'Rekap Bulanan',
            'bulan' => $bulan,
            'tahun' => $tahun,
            'rekap' => $this->laporan->getRekapBulanan($bulan, $tahun)
        ];

        return view('laporan/rekap_bulanan', $data);
    }

    public function cetakStruk($anggota_id)
    {
        $data = [
            'title'      => 'Struk Denda',
            'struk'      => $this->laporan->getStruk($anggota_id),
            'totalDenda' => $this->laporan->getTotalDenda($anggota_id)
        ];

        return view('laporan/struk_denda', $data);
    }

    public function cetak()
    {
        $data = [
            'title'       => 'Cetak Laporan',
            'laporan'     => $this->laporan->getLaporanSiswa(),
            'totalPinjam' => $this->laporan->totalPeminjaman(),
            'totalKembali' => $this->laporan->totalPengembalian(),
            'belumKembali' => $this->laporan->totalBelumKembali(),
            'totalDenda'  => $this->laporan->totalDenda()
        ];

        return view('laporan/cetak', $data);
    }

    public function exportPDF()
    {
        $data = [
            'title'   => 'Export PDF',
            'laporan' => $this->laporan->getLaporanSiswa()
        ];

        return view('laporan/pdf', $data);
    }

    public function exportExcel()
    {
        $data = [
            'laporan' => $this->laporan->getLaporanSiswa()
        ];

        return view('laporan/excel', $data);
    }

    // =====================================
    // LAPORAN PENGUNJUNG PER HARI
    // =====================================

    public function pengunjung()
    {
        $awal  = $this->request->getGet('awal');
        $akhir = $this->request->getGet('akhir');

        if ($awal == null) {
            $awal = date('Y-m-d', strtotime('-30 days'));
        }
        if ($akhir == null) {
            $akhir = date('Y-m-d');
        }

        $pengunjung = $this->laporan->getPengunjungPerHari($awal, $akhir);
        $total = 0;
        foreach ($pengunjung as $p) {
            $total += (int)($p['total_pengunjung'] ?? 0);
        }

        $data = [
            'title'      => 'Laporan Pengunjung',
            'awal'       => $awal,
            'akhir'      => $akhir,
            'pengunjung' => $pengunjung,
            'total'      => $total
        ];

        return view('laporan/pengunjung', $data);
    }
}
