<?php

namespace App\Controllers;

use App\Models\ModelGuru;
use App\Models\ModelKelas;
use App\Models\ModelSiswa;
use App\Models\ModelRekap;
use App\Models\ModelRekapPenghargaan;
use Dompdf\Dompdf;
use Dompdf\Options;

class RekapPelanggaran extends BaseController
{
    protected $kelasModel;
    protected $guruModel;
    protected $siswaModel;
    protected $rekapModel;
    protected $rekapPenghargaanModel;

    public function __construct()
    {
        $this->kelasModel             = new ModelKelas();
        $this->guruModel              = new ModelGuru();
        $this->siswaModel             = new ModelSiswa();
        $this->rekapModel             = new ModelRekap();
        $this->rekapPenghargaanModel  = new ModelRekapPenghargaan();
    }

    public function index($id_kelas)
    {
        $data = [
            'kelas'        => $this->kelasModel->find($id_kelas),
            'walikelas'    => $this->guruModel->getWaliKelas($id_kelas),
            'gurubk'       => $this->guruModel->getGuruBK(),
            'tahunajaran'  => '2026 / 2027',
            'siswa'        => $this->siswaModel->getByKelas($id_kelas),
            'pelanggaran'  => $this->rekapModel->getPelanggaranKelas($id_kelas),
            'penghargaan'  => $this->rekapPenghargaanModel->getPenghargaanKelas($id_kelas)
        ];

        return view('RekapPelanggaran', $data);
    }

    // ==========================
    // PDF REKAP SKOR
    // ==========================
    public function exportSkorPdf($id_kelas)
    {
        date_default_timezone_set('Asia/Jakarta');

        $data = [
            'kelas'       => $this->kelasModel->find($id_kelas),
            'walikelas'   => $this->guruModel->getWaliKelas($id_kelas),
            'gurubk'      => $this->guruModel->getGuruBK(),
            'siswa'       => $this->siswaModel->getByKelas($id_kelas),
            'tanggal'     => date('d F Y'),
            'jam'         => date('H:i:s')
        ];

        $html = view('pdf/RekapSkorPDF', $data);

        $options = new Options();
        $options->set('isRemoteEnabled', true);

        $dompdf = new Dompdf($options);
        $dompdf->loadHtml($html);
        $dompdf->setPaper('A4', 'landscape');
        $dompdf->render();

        $dompdf->stream(
            'Rekap_Skor_' . $data['kelas']['nama_kelas'] . '.pdf',
            ['Attachment' => true]
        );
    }

    // ==========================
    // PDF REKAP PELANGGARAN
    // ==========================
    public function exportPelanggaranPdf($id_kelas)
    {
        date_default_timezone_set('Asia/Jakarta');

        // Format tanggal Indonesia
        $formatter = new \IntlDateFormatter(
            'id_ID',
            \IntlDateFormatter::LONG,
            \IntlDateFormatter::NONE,
            'Asia/Jakarta'
        );

        $data = [
            'kelas'        => $this->kelasModel->find($id_kelas),
            'walikelas'    => $this->guruModel->getWaliKelas($id_kelas),
            'gurubk'       => $this->guruModel->getGuruBK(),
            'pelanggaran'  => $this->rekapModel->getPelanggaranKelas($id_kelas),
            'tanggal'      => $formatter->format(new \DateTime()),
            'jam'          => date('H:i:s') . ' WIB'
        ];

        $html = view('pdf/RekapPelanggaranPDF', $data);

        $options = new Options();
        $options->set('isRemoteEnabled', true);

        $dompdf = new Dompdf($options);
        $dompdf->loadHtml($html);
        $dompdf->setPaper('A4', 'landscape');
        $dompdf->render();

        $dompdf->stream(
            'Rekap_Pelanggaran_' . $data['kelas']['nama_kelas'] . '.pdf',
            ['Attachment' => true]
        );
    }

    public function exportPenghargaanPdf($id_kelas)
    {
        date_default_timezone_set('Asia/Jakarta');

        $formatter = new \IntlDateFormatter(
            'id_ID',
            \IntlDateFormatter::LONG,
            \IntlDateFormatter::NONE,
            'Asia/Jakarta'
        );

        $data = [

            'kelas' => $this->kelasModel->find($id_kelas),

            'walikelas' => $this->guruModel->getWaliKelas($id_kelas),

            'gurubk' => $this->guruModel->getGuruBK(),

            'penghargaan' => $this->rekapPenghargaanModel->getByKelas($id_kelas),

            'tanggal' => $formatter->format(new \DateTime()),

            'jam' => date('H:i:s') . ' WIB'

        ];

        $html = view('pdf/RekapPenghargaanPDF', $data);

        $options = new Options();
        $options->set('isRemoteEnabled', true);

        $dompdf = new Dompdf($options);

        $dompdf->loadHtml($html);

        $dompdf->setPaper('A4', 'landscape');

        $dompdf->render();

        $dompdf->stream(
            'Rekap_Penghargaan_' . $data['kelas']['nama_kelas'] . '.pdf',
            ['Attachment' => true]
        );
    }
}
