<?php

namespace App\Controllers;

use App\Models\ModelSiswa;
use App\Models\ModelGuru;
use App\Models\ModelRekap;
use App\Models\ModelRekapPenghargaan;

use Dompdf\Dompdf;
use Dompdf\Options;

class RekapPelanggaranPerMurid extends BaseController
{
    protected $siswaModel;
    protected $guruModel;
    protected $rekapModel;
    protected $rekapPenghargaanModel;

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

    public function index($id_siswa)
    {
        // Biodata murid
        $murid = $this->siswaModel->getDetailSiswa($id_siswa);

        // Wali kelas
        $walikelas = $this->guruModel->getWaliKelas($murid['id_kelas']);

        // Riwayat pelanggaran
        $pelanggaran = $this->rekapModel->getRekapPerMurid($id_siswa);

        // Riwayat penghargaan
        $penghargaan = $this->rekapPenghargaanModel->getBySiswa($id_siswa);

        // Total skor pelanggaran
        $totalSkor = 0;
        foreach ($pelanggaran as $p) {
            $totalSkor += $p['skor'];
        }

        // Total poin penghargaan
        $totalPoin = 0;
        foreach ($penghargaan as $p) {
            $totalPoin += $p['poin'];
        }

        $data = [
            'murid'        => $murid,
            'walikelas'    => $walikelas,

            'pelanggaran'  => $pelanggaran,
            'penghargaan'  => $penghargaan,

            'totalSkor'    => $totalSkor,
            'totalPoin'    => $totalPoin
        ];

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

    public function exportPdf($id_siswa)
    {
        date_default_timezone_set('Asia/Jakarta');

        // ===============================
        // Ambil Data
        // ===============================
        $murid = $this->siswaModel->getDetailSiswa($id_siswa);

        if (!$murid) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('Data murid tidak ditemukan.');
        }

        $walikelas = $this->guruModel->getWaliKelas($murid['id_kelas']);
        $gurubk    = $this->guruModel->getGuruBK();

        // Riwayat pelanggaran
        $pelanggaran = $this->rekapModel->getRekapPerMurid($id_siswa);

        // Riwayat penghargaan
        $penghargaan = $this->rekapPenghargaanModel->getBySiswa($id_siswa);

        // ===============================
        // Hitung Total Skor Pelanggaran
        // ===============================
        $totalSkor = 0;

        foreach ($pelanggaran as $row) {
            $totalSkor += (int)$row['skor'];
        }

        // ===============================
        // Hitung Total Poin Penghargaan
        // ===============================
        $totalPoin = 0;

        foreach ($penghargaan as $row) {
            $totalPoin += (int)$row['poin'];
        }

        // ===============================
        // Data ke View
        // ===============================
        $data = [

            'murid'        => $murid,
            'walikelas'    => $walikelas,
            'gurubk'       => $gurubk,

            'pelanggaran'  => $pelanggaran,
            'penghargaan'  => $penghargaan,

            'totalSkor'    => $totalSkor,
            'totalPoin'    => $totalPoin,

            'tanggal'      => date('d F Y'),
            'jam'          => date('H:i:s') . ' WIB'
        ];

        // ===============================
        // Generate PDF
        // ===============================
        $html = view('pdf/RekapPelanggaranPerMuridPDF', $data);

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

        $dompdf = new Dompdf($options);

        $dompdf->loadHtml($html);

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

        $dompdf->render();

        $dompdf->stream(
            'Rekap_Pelanggaran_' . preg_replace('/[^A-Za-z0-9_\-]/', '_', $murid['nama']) . '.pdf',
            ['Attachment' => true]
        );
    }
}
