<?php

namespace App\Controllers\Panel\Elearning\Rekap\Siswa;

use App\Controllers\Panel\BC;
use CodeIgniter\API\ResponseTrait;
use DateInterval;
use DatePeriod;
use DateTime;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;

class Belajar extends BC
{
    use ResponseTrait;
    private $guru = [];
    private $siswa = [];
    private $tanggal = [];
    private $tanggalIndex = [];
    private $keterangan = [];
    private $hasCommented = [];
    private $jl;
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        return view('Panel/Elearning/Rekap/Siswa/belajar', ['page_title' => "Rekap Belajar Siswa"]);
    }
    public function preview()
    {
        $startDate = $this->request->getGet('startDate');
        $endDate = $this->request->getGet('endDate');
        $kelas_id = $this->request->getGet('kelas_id', FILTER_VALIDATE_INT);
        $mapel_id = $this->request->getGet('mapel_id', FILTER_VALIDATE_INT);
        $tahun_id = $this->request->getGet('tahun_id', FILTER_VALIDATE_INT);
        $delimiter = "^";
        if (strlen($startDate) < 10 || strlen($endDate) < 10) return $this->fail(['message' => "Tanggal Tidak Valid"]);
        $db = db_connect();
        $jlsd = date("Y-m-d", strtotime($startDate));
        $jled = date("Y-m-d", strtotime($endDate));
        $this->jl = $db->table('jadwal_libur')->where("(tanggal BETWEEN '$jlsd' AND '$jled')", null, false)->where('is_nasional', 1, false)->get()->getResultArray();
        $Qtahun = $db->table('tahun_pelajaran')->select("CONCAT(nama,'$delimiter',semester)", false)->where('id', $tahun_id)->getCompiledSelect();
        $Qmapel = $db->table('m_mapel')->select("CONCAT(kode,'$delimiter',nama)", false)->where('id', $mapel_id)->getCompiledSelect();
        $Qkelas = $db->table('m_kelas')->select("CONCAT(kode,'$delimiter',nama)", false)->where('id', $kelas_id)->getCompiledSelect();
        $sql = "SELECT ($Qtahun) tahun,($Qmapel) mapel,($Qkelas) kelas";
        $tkgm = $db->query($sql, [$tahun_id, $mapel_id, $kelas_id])->getRowArray();
        if (!$tkgm['kelas']) return $this->fail(['message' => "Kelas Tidak Valid"]);
        if (!$tkgm['mapel']) return $this->fail(['message' => "Mata Pelajaran Tidak Valid"]);
        if (!$tkgm['tahun']) return $this->fail(['message' => "Tahun Pelajaran Tidak Valid"]);
        $kelas_data = explode($delimiter, $tkgm['kelas']);
        $tahun_data = explode($delimiter, $tkgm['tahun']);
        $mapel_data = explode($delimiter, $tkgm['mapel']);
        $path = WRITEPATH . "/uploads/rekap-presensi-belajar-$kelas_data[0]-$startDate-$endDate.xlsx";
        $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        $worksheet = $spreadsheet->getActiveSheet();
        $startRow = 7;
        $s1 = $startRow - 3;
        $s2 = $startRow - 2;
        $s3 = $startRow - 1;
        $nMapelT = "Nama Mapel : ";
        $gMapelT = "Guru Mapel : ";
        // Set up some basic data
        $td1 = date("d", $startDate) . " " . bulanIndo(date('m', $startDate)) . " " . date('Y', $startDate);
        $td2 = date("d", $endDate) . " " . bulanIndo(date('m', $endDate)) . " " . date('Y', $endDate);
        $textDate = $td1 == $td2 ? $td1 : "$td1 - $td2";
        $worksheet
            ->setCellValue('A1', '')
            ->setCellValue('A2', '')
            ->setCellValue("A{$s3}", "No")->setCellValue("B{$s3}", "Nomor Induk")->setCellValue("C{$s3}", "Nama")->setTitle($kelas_data[0]);
        $getUser = $db->table('users u')->select('fullname')->where('u.id', 'ak.user_id', false)->orWhere('u.id', 'jp.user_id', false)->limit(1)->getCompiledSelect();
        $pertemuan = $db->table('m_mapel smm')->select('COUNT(*)')->join('jadwal_pelajaran sjp', 'sjp.mapel_id=smm.id', '', false)->join('jadwal_absensi_siswa sjas', 'sjas.jadwal_id=sjp.id', '', false)->where('sjas.siswa_id', 'ms.id', false)->where('smm.id', 'jp.mapel_id', false)->where("DATE_FORMAT(sjas.created_at,'%Y-%m-%d') <=", "DATE_FORMAT(jas.created_at,'%Y-%m-%d')", false)->getCompiledSelect();
        $select = ["jp.id,ms.nama,ms.nomor_induk,jas.created_at,jas.keterangan,ak.rangkuman"];
        $select[] = "($getUser) nama_guru,kd.nomor nomor_kompetensi,kd.nama nama_kompetensi";
        $select[] = "($pertemuan) pertemuan";
        $builder = $db->table('m_siswa ms');
        $builder->select(implode(',', $select));
        $builder->join('jadwal_absensi_siswa jas', 'jas.siswa_id=ms.id');
        $builder->join('jadwal_pelajaran jp', 'jp.id=jas.jadwal_id');
        $builder->join('agenda_kegiatan ak', "ak.jadwal_id=jp.id", "left", false);
        // AND DATE_FORMAT(ak.tanggal,'%Y-%m-%d')=DATE_FORMAT(jas.created_at,'%Y-%m-%d')
        $builder->join("rpp", 'rpp.id=ak.rpp_id', 'left');
        $builder->join("kompetensi_dasar kd", 'kd.id=rpp.kompetensi_id', 'left');
        $builder->where('jp.tahun_id', $tahun_id);
        $builder->where('jp.mapel_id', $mapel_id);
        $builder->where('jp.kelas_id', $kelas_id);
        $builder->where("(jas.created_at BETWEEN '$startDate' AND '$endDate')", null, false);
        $builder->orderBy("jas.created_at", 'ASC');
        $this->guru = [];
        $this->siswa = [];
        $this->tanggal = [];
        $this->tanggalIndex = [];
        $this->keterangan = [];
        $this->hasCommented = [];
        $ss1 = $startRow;
        foreach ($builder->get()->getResultArray() as $k => $d) {
            $tgl = date('d-M-Y', $d['created_at']);
            if (!in_array($d['nama_guru'], $this->guru)) $this->guru[] = $d['nama_guru'];
            if (!in_array($d['nomor_induk'], $this->siswa)) {
                $this->siswa[] = $d['nomor_induk'];
                $worksheet->setCellValue("A{$ss1}", $k + 1);
                $worksheet->setCellValue("B{$ss1}", $d['nomor_induk']);
                $worksheet->setCellValue("C{$ss1}", $d['nama']);
                $ss1++;
            }
            if (!isset($this->tanggal[$tgl])) {
                $this->tanggal[$tgl] = [];
                $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 3);
                $this->tanggalIndex[$tgl] = $maxCol;
                $worksheet->setCellValue("{$maxCol}{$s3}", $tgl);
            }
            $maxCol = $this->tanggalIndex[$tgl];
            if (!isset($this->hasCommented[$tgl])) {
                $this->hasCommented[$tgl]   = ['p' => 0, 'ik' => '', 'nk' => '', 'r' => '', 'i' => "$maxCol$s3", "text" => ''];
            }
            if ($this->hasCommented[$tgl]['p'] <= $d['pertemuan']) $this->hasCommented[$tgl]['p'] = $d['pertemuan'];
            if (!$this->hasCommented[$tgl]['ik']) $this->hasCommented[$tgl]['ik'] = $d['nomor_kompetensi'];
            if (!$this->hasCommented[$tgl]['nk']) $this->hasCommented[$tgl]['nk'] = $d['nama_kompetensi'];
            if (!$this->hasCommented[$tgl]['r']) $this->hasCommented[$tgl]['r'] = $d['rangkuman'];
            $si = $this->findSiswaIndex($d['nomor_induk']);
            if ($si >= 0) {
                $si = $si + $startRow;
                $col = $this->tanggalIndex[$tgl];
                $worksheet->setCellValue("{$col}{$si}", $this->getKeterangan($d['keterangan']));
            }
        }
        foreach ($this->hasCommented as $comment) {
            $text = "Pertemuan $comment[p]\n";
            $text .= "$comment[ik] $comment[nk]\n";
            $text .= "$comment[r]";
            $i = $comment['i'];
            $worksheet->getComment("{$i}")->getText()->createTextRun($text);
        }
        $maxRow = $startRow + count($this->siswa) - 1;
        $s = $startRow - 1;
        $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 7);
        $worksheet->getStyle("A{$s}:{$maxCol}{$maxRow}")
            ->getBorders()
            ->getInside()
            ->setBorderStyle(Border::BORDER_THIN)
            ->setColor(new Color('00000000'));
        $worksheet->getStyle("A{$s}:{$maxCol}{$maxRow}")
            ->getBorders()
            ->getOutline()
            ->setBorderStyle(Border::BORDER_THIN)
            ->setColor(new Color('00000000'));
        $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 4);
        $worksheet->setCellValue("{$maxCol}{$s}", "H");
        $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 5);
        $worksheet->setCellValue("{$maxCol}{$s}", "S");
        $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 6);
        $worksheet->setCellValue("{$maxCol}{$s}", "I");
        $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 7);
        $worksheet->setCellValue("{$maxCol}{$s}", "A");
        $worksheet->setCellValue("A{$s1}", "")->setCellValue("A{$s2}", "");
        foreach ($worksheet->toArray() as $k => $d) {
            if ($k < $startRow - 1) continue;
            foreach ($d as $sk => $sd) {
                if ($sk < 3) continue;

                if (count($this->tanggal) + 6 >= $sk && count($this->tanggal) + 3 <= $sk) {
                    $ss = $k + 1;
                    $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($sk + 1);
                    $smaxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 3);
                    switch ($sk) {
                        case count($this->tanggal) + 3:
                            $worksheet->setCellValue("{$maxCol}{$ss}", "=COUNTIF(D{$ss}:{$smaxCol}{$ss},\"H\")");
                            break;
                        case count($this->tanggal) + 4:
                            $worksheet->setCellValue("{$maxCol}{$ss}", "=COUNTIF(D{$ss}:{$smaxCol}{$ss},\"S\")");
                            break;
                        case count($this->tanggal) + 5:
                            $worksheet->setCellValue("{$maxCol}{$ss}", "=COUNTIF(D{$ss}:{$smaxCol}{$ss},\"I\")");
                            break;
                        case count($this->tanggal) + 6:
                            $worksheet->setCellValue("{$maxCol}{$ss}", "=COUNTIF(D{$ss}:{$smaxCol}{$ss},\"A\")");
                            break;
                    }
                    continue;
                }
                if (!$sd) {
                    $ssm = $k + 1;
                    $smmaxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($sk + 1);
                    $worksheet->setCellValue("{$smmaxCol}{$ssm}", "A");
                }
            }
        }
        return $this->respond(['absensi' => $worksheet->toArray()]);
    }
    public function export()
    {
        $startDate = $this->request->getGet('startDate');
        $endDate = $this->request->getGet('endDate');
        $kelas_id = $this->request->getGet('kelas_id', FILTER_VALIDATE_INT);
        $mapel_id = $this->request->getGet('mapel_id', FILTER_VALIDATE_INT);
        $tahun_id = $this->request->getGet('tahun_id', FILTER_VALIDATE_INT);
        $delimiter = "^";
        if (strlen($startDate) < 10 || strlen($endDate) < 10) return $this->fail(['message' => "Tanggal Tidak Valid"]);
        $db = db_connect();
        $jlsd = date("Y-m-d", strtotime($startDate));
        $jled = date("Y-m-d", strtotime($endDate));
        $this->jl = $db->table('jadwal_libur')->where("(tanggal BETWEEN '$jlsd' AND '$jled')", null, false)->where('is_nasional', 1, false)->get()->getResultArray();
        $Qtahun = $db->table('tahun_pelajaran')->select("CONCAT(nama,'$delimiter',semester)", false)->where('id', $tahun_id)->getCompiledSelect();
        $Qmapel = $db->table('m_mapel')->select("CONCAT(kode,'$delimiter',nama)", false)->where('id', $mapel_id)->getCompiledSelect();
        $Qkelas = $db->table('m_kelas')->select("CONCAT(kode,'$delimiter',nama)", false)->where('id', $kelas_id)->getCompiledSelect();
        $sql = "SELECT ($Qtahun) tahun,($Qmapel) mapel,($Qkelas) kelas";
        $tkgm = $db->query($sql, [$tahun_id, $mapel_id, $kelas_id])->getRowArray();
        if (!$tkgm['kelas']) return $this->fail(['message' => "Kelas Tidak Valid"]);
        if (!$tkgm['mapel']) return $this->fail(['message' => "Mata Pelajaran Tidak Valid"]);
        if (!$tkgm['tahun']) return $this->fail(['message' => "Tahun Pelajaran Tidak Valid"]);
        $kelas_data = explode($delimiter, $tkgm['kelas']);
        $tahun_data = explode($delimiter, $tkgm['tahun']);
        $mapel_data = explode($delimiter, $tkgm['mapel']);
        $range = $startDate == $endDate ? $startDate : "$startDate-$endDate";
        $path = WRITEPATH . "/uploads/rekap-presensi-belajar-$kelas_data[0]-$range.xlsx";
        $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        $worksheet = $spreadsheet->getActiveSheet();
        $startRow = 7;
        $s1 = $startRow - 3;
        $s2 = $startRow - 2;
        $s3 = $startRow - 1;
        $nMapelT = "Nama Mapel : ";
        $gMapelT = "Guru Mapel : ";
        // Set up some basic data
        $td1 = date("d", $startDate) . " " . bulanIndo(date('m', $startDate)) . " " . date('Y', $startDate);
        $td2 = date("d", $endDate) . " " . bulanIndo(date('m', $endDate)) . " " . date('Y', $endDate);
        $textDate = $td1 == $td2 ? $td1 : "$td1 - $td2";
        $worksheet
            ->setCellValue('A1', strtoupper("rekap presensi kehadiran siswa $textDate"))
            ->setCellValue('A2', strtoupper("tahun pelajaran $tahun_data[0] $tahun_data[1]"))
            ->setCellValue("A{$s3}", "No")->setCellValue("B{$s3}", "Nomor Induk")->setCellValue("C{$s3}", "Nama")->setTitle($kelas_data[0]);
        $getUser = $db->table('users u')->select('fullname')->where('u.id', 'ak.user_id', false)->orWhere('u.id', 'jp.user_id', false)->limit(1)->getCompiledSelect();
        $pertemuan = $db->table('m_mapel smm')->select('COUNT(*)');
        $pertemuan->join('jadwal_pelajaran sjp', 'sjp.mapel_id=smm.id', '', false);
        $pertemuan->join('jadwal_absensi_siswa sjas', 'sjas.jadwal_id=sjp.id', '', false);
        $pertemuan->where('sjas.siswa_id', 'ms.id', false)->where('smm.id', 'jp.mapel_id', false);
        $pertemuan->where("sjas.created_at <=", "jas.created_at", false);
        $pertemuan = $pertemuan->getCompiledSelect();
        $select = ["jp.id,ms.nama,ms.nomor_induk,jas.created_at,jas.keterangan,ak.rangkuman"];
        $select[] = "($getUser) nama_guru,kd.nomor nomor_kompetensi,kd.nama nama_kompetensi";
        $select[] = "($pertemuan) pertemuan";
        $builder = $db->table('m_siswa ms');
        $builder->select(implode(',', $select));
        $builder->join('jadwal_absensi_siswa jas', 'jas.siswa_id=ms.id');
        $builder->join('jadwal_pelajaran jp', 'jp.id=jas.jadwal_id');
        $builder->join('agenda_kegiatan ak', "ak.jadwal_id=jp.id", "left", false);
        // $builder->join('agenda_kegiatan ak', "ak.jadwal_id=jp.id AND DATE_FORMAT(ak.tanggal,'%Y-%m-%d')=DATE_FORMAT(jas.created_at,'%Y-%m-%d')", "left", false);
        $builder->join("rpp", 'rpp.id=ak.rpp_id', 'left');
        $builder->join("kompetensi_dasar kd", 'kd.id=rpp.kompetensi_id', 'left');
        $builder->where('jp.tahun_id', $tahun_id);
        $builder->where('jp.mapel_id', $mapel_id);
        $builder->where('jp.kelas_id', $kelas_id);
        $builder->where("(jas.created_at BETWEEN '$startDate' AND '$endDate')", null, false);
        // $builder->where("(DATE_FORMAT(jas.created_at,'%Y-%m-%d') BETWEEN '$startDate' AND '$endDate')", null, false);
        $builder->orderBy("jas.created_at", 'ASC');
        $this->guru = [];
        $this->siswa = [];
        $this->tanggal = [];
        $this->tanggalIndex = [];
        $this->keterangan = [];
        $this->hasCommented = [];
        $ss1 = $startRow;
        foreach ($builder->get()->getResultArray() as $k => $d) {
            $tgl = date('d-M-Y', $d['created_at']);
            if (!in_array($d['nama_guru'], $this->guru)) $this->guru[] = $d['nama_guru'];
            if (!in_array($d['nomor_induk'], $this->siswa)) {
                $this->siswa[] = $d['nomor_induk'];
                $worksheet->setCellValue("A{$ss1}", $k + 1);
                $worksheet->setCellValue("B{$ss1}", $d['nomor_induk']);
                $worksheet->setCellValue("C{$ss1}", $d['nama']);
                $ss1++;
            }
            if (!isset($this->tanggal[$tgl])) {
                $this->tanggal[$tgl] = [];
                $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 3);
                $this->tanggalIndex[$tgl] = $maxCol;
                $worksheet->setCellValue("{$maxCol}{$s3}", $tgl);
            }
            $maxCol = $this->tanggalIndex[$tgl];
            if (!isset($this->hasCommented[$tgl])) {
                $this->hasCommented[$tgl]   = ['p' => 0, 'ik' => '', 'nk' => '', 'r' => '', 'i' => "$maxCol$s3", "text" => ''];
            }
            if ($this->hasCommented[$tgl]['p'] <= $d['pertemuan']) $this->hasCommented[$tgl]['p'] = $d['pertemuan'];
            if (!$this->hasCommented[$tgl]['ik']) $this->hasCommented[$tgl]['ik'] = $d['nomor_kompetensi'];
            if (!$this->hasCommented[$tgl]['nk']) $this->hasCommented[$tgl]['nk'] = $d['nama_kompetensi'];
            if (!$this->hasCommented[$tgl]['r']) $this->hasCommented[$tgl]['r'] = $d['rangkuman'];
            $si = $this->findSiswaIndex($d['nomor_induk']);
            if ($si >= 0) {
                $si = $si + $startRow;
                $col = $this->tanggalIndex[$tgl];
                $worksheet->setCellValue("{$col}{$si}", $this->getKeterangan($d['keterangan']));
            }
        }
        foreach ($this->hasCommented as $comment) {
            $text = "Pertemuan $comment[p]\n";
            $text .= "$comment[ik] $comment[nk]\n";
            $text .= "$comment[r]";
            $i = $comment['i'];
            $worksheet->getComment("{$i}")->getText()->createTextRun($text);
        }
        $maxRow = $startRow + count($this->siswa) - 1;
        $s = $startRow - 1;
        $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 7);
        $worksheet->getStyle("A{$s}:{$maxCol}{$maxRow}")
            ->getBorders()
            ->getInside()
            ->setBorderStyle(Border::BORDER_THIN)
            ->setColor(new Color('00000000'));
        $worksheet->getStyle("A{$s}:{$maxCol}{$maxRow}")
            ->getBorders()
            ->getOutline()
            ->setBorderStyle(Border::BORDER_THIN)
            ->setColor(new Color('00000000'));
        $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 4);
        $worksheet->setCellValue("{$maxCol}{$s}", "H");
        $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 5);
        $worksheet->setCellValue("{$maxCol}{$s}", "S");
        $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 6);
        $worksheet->setCellValue("{$maxCol}{$s}", "I");
        $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 7);
        $worksheet->setCellValue("{$maxCol}{$s}", "A");
        $worksheet->setCellValue("A{$s1}", "$nMapelT $mapel_data[0] - $mapel_data[1]")->setCellValue("A{$s2}", "$gMapelT " . implode(',', $this->guru));
        foreach ($worksheet->toArray() as $k => $d) {
            if ($k < $startRow - 1) continue;
            foreach ($d as $sk => $sd) {
                if ($sk < 3) continue;

                if (count($this->tanggal) + 6 >= $sk && count($this->tanggal) + 3 <= $sk) {
                    $ss = $k + 1;
                    $maxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($sk + 1);
                    $smaxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($this->tanggal) + 3);
                    switch ($sk) {
                        case count($this->tanggal) + 3:
                            $worksheet->setCellValue("{$maxCol}{$ss}", "=COUNTIF(D{$ss}:{$smaxCol}{$ss},\"H\")");
                            break;
                        case count($this->tanggal) + 4:
                            $worksheet->setCellValue("{$maxCol}{$ss}", "=COUNTIF(D{$ss}:{$smaxCol}{$ss},\"S\")");
                            break;
                        case count($this->tanggal) + 5:
                            $worksheet->setCellValue("{$maxCol}{$ss}", "=COUNTIF(D{$ss}:{$smaxCol}{$ss},\"I\")");
                            break;
                        case count($this->tanggal) + 6:
                            $worksheet->setCellValue("{$maxCol}{$ss}", "=COUNTIF(D{$ss}:{$smaxCol}{$ss},\"A\")");
                            break;
                    }
                    continue;
                }
                if (!$sd) {
                    $ssm = $k + 1;
                    $smmaxCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($sk + 1);
                    $worksheet->setCellValue("{$smmaxCol}{$ssm}", "A");
                }
            }
        }
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
        $writer->save($path);
        return $this->response->download($path, null);
    }
    private function getKeterangan($i)
    {
        switch ($i) {
            case "0":
                return "A";
            case "2":
                return "A";
            case "1":
                return "H";
            case "3":
                return "I";
            case "4":
                return "S";
            default:
                return "A";
        }
    }
    private function findSiswaIndex($nomorInduk)
    {
        foreach ($this->siswa as $i => $ni) {
            if ($ni == $nomorInduk) return $i;
        }
        return false;
    }
    private function is_libur($tanggal)
    {
        foreach ($this->jl as $jl) {
            if ($jl['tanggal'] == $tanggal) return true;
        }
        return false;
    }
    public function mapel()
    {
        $builder = db_connect()->table('m_mapel mm');
        $builder->select('mm.*');
        if (!user()->hasRole('admin')) {
            $builder->join('jadwal_pelajaran jp', 'jp.mapel_id=mm.id');
            $builder->where('jp.user_id', user()->id);
        }
        return $this->respond(['data' => $builder->get()->getResultArray()]);
    }
}
