<?php

namespace App\Controllers\Panel;

use App\Controllers\Panel\BC;
use App\Models\Panel\Dashboard as PanelDashboard;
use App\Models\Panel\MasterData\Pengumuman;
use CodeIgniter\API\ResponseTrait;

class Dashboard extends BC
{
    use ResponseTrait;
    private $dashboardModel;
    public function __construct()
    {
        parent::__construct();
        $this->dashboardModel = new PanelDashboard();
    }
    public function index()
    {
        helper("AttendanceSolution");

        $dataSiswa['siswa'] = db_connect()->table('m_siswa')->get()->getResultArray();
        $dataSiswa['kelas'] = db_connect()->table('m_kelas mk')->select('mk.*,COUNT(*) jumlah')->join('m_siswa ms', 'ms.kelas_id=mk.id')->groupBy('mk.id')->get()->getResultArray();
        $dataSiswa['tingkat'] = db_connect()->table('m_tingkat mt')->select('mt.kode,COUNT(*) jumlah')->join('kelas_data kd', 'kd.tingkat_id=mt.id')->join('m_siswa ms', 'ms.kelas_id=kd.kelas_id')->groupBy('mt.id')->get()->getResultArray();
        $dataSiswa['jurusan'] = db_connect()->table('m_jurusan mj')->select('mj.kode,COUNT(*) jumlah')->join('kelas_data kd', 'kd.jurusan_id=mj.id')->join('m_siswa ms', 'ms.kelas_id=kd.kelas_id')->groupBy('mj.id')->get()->getResultArray();
        $dataSiswa['jurusan-tingkat'] = db_connect()->table('m_jurusan mj')->select('mj.kode kode_jurusan,mt.kode kode_tingkat,COUNT(*) jumlah')->join('kelas_data kd', 'kd.jurusan_id=mj.id')->join('m_tingkat mt', 'mt.id=kd.tingkat_id')->join('m_siswa ms', 'ms.kelas_id=kd.kelas_id')->groupBy("CONCAT(mj.id,'-',mt.id)", false)->get()->getResultArray();
        $siswa = count($dataSiswa['siswa']);
        $mdlSiswa = "= = = = = = DATA SISWA = = = = = =<br>";
        $mdlSiswa .= "Total Siswa : $siswa<br><br>";
        $mdlSiswa .= "*DATA PERTINGKAT*<br>";
        foreach ($dataSiswa['tingkat'] as $k) {
            $mdlSiswa .= "$k[kode] : $k[jumlah]<br>";
        }
        $mdlSiswa .= "<br>";
        $mdlSiswa .= "*DATA PERJURUSAN*<br>";
        foreach ($dataSiswa['jurusan'] as $k) {
            $mdlSiswa .= "$k[kode] : $k[jumlah]<br>";
        }
        $mdlSiswa .= "<br>";
        $mdlSiswa .= "*DATA JURUSAN PER TINGKAT*<br>";
        foreach ($dataSiswa['jurusan-tingkat'] as $k) {
            $mdlSiswa .= "$k[kode_tingkat] - $k[kode_jurusan] : $k[jumlah]<br>";
        }
        $mdlSiswa .= "<br>";
        $mdlSiswa .= "*DATA PERKELAS*<br>";
        foreach ($dataSiswa['kelas'] as $k) {
            $mdlSiswa .= "$k[nama] : $k[jumlah]<br>";
        }
        $data = [
            'page_title' => "Dashboard",
            'countSiswa' => $this->dashboardModel->countSiswa(),
            'countGuru' => $this->dashboardModel->countGuru(),
            'countKaryawan' => $this->dashboardModel->countKaryawan(),
            'countKelas' => $this->dashboardModel->countKelas(),
            'dataSiswa' => $dataSiswa,
            'mdlSiswa' => $mdlSiswa,
        ];
        return view('Panel/dashboard', $data);
    }
    public function announcement()
    {
        return view("Panel/announcement", ['page_title' => "Pengumuman"]);
    }
    public function announcement_all()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = (new Pengumuman())->select("*,DATE_FORMAT(created_at,'%b %d, %H:%i') parsedTime")->where('tujuan', 1)->orderBy('created_at', 'DESC')->findAll();
        return $this->respond($data);
    }
    public function osc()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $osc = json_decode(options('onesignal_config'), true);
        if (logged_in()) {
            $osc['euid'] = md5("s" . user()->id);
        }
        return $this->respond($osc);
    }
}
