<?php

namespace App\Controllers\Panel\Elearning\Rekap;

use App\Controllers\Panel\BC;
use CodeIgniter\API\ResponseTrait;
use GuzzleHttp\Client;

class Mengajar extends BC
{
    use ResponseTrait;
    public function index()
    {
        return view('Panel/Elearning/Rekap/Mengajar/index', ['page_title' => "Rekap Mengajar"]);
    }
    public function all()
    {
        $db = db_connect();
        $rpp = $db->table('rpp');
        $rpp->select('COUNT(rpp.id)');
        $rpp->join('kompetensi_dasar kd', 'kd.id=rpp.kompetensi_id');
        $rpp->where(['rpp.user_id' => "jp.user_id", "kd.mapel_id" => "jp.mapel_id"], null, false);
        $rpp = $rpp->getCompiledSelect();
        $jt = $db->table('jadwal_tugas jt');
        $jt->select('COUNT(jt.id)');
        $jt->join('kompetensi_dasar kd', 'kd.id=jt.kompetensi_id');
        $jt->where(['jt.jadwal_id' => "jp.id", "kd.mapel_id" => "jp.mapel_id"], null, false);
        $jt = $jt->getCompiledSelect();
        $jm = $db->table('jadwal_materi jm');
        $jm->select('COUNT(jm.id)');
        $jm->join('kompetensi_dasar kd', 'kd.id=jm.kompetensi_id');
        $jm->where(['jm.jadwal_id' => "jp.id", "kd.mapel_id" => "jp.mapel_id"], null, false);
        $jm = $jm->getCompiledSelect();
        $select = ["u.fullname,u.nomor_induk,mm.nama nama_mapel,jp.id jadwal_id"];
        $select[] = "($rpp) rpp_count";
        $select[] = "($jt) tugas_count";
        $select[] = "($jm) materi_count";
        $rm = $db->table('m_mapel mm');
        $rm->select(implode(',', $select), false);
        $rm->join('jadwal_pelajaran jp', 'jp.mapel_id = mm.id');
        $rm->join('users u', 'u.id = jp.user_id');
        $rm->join('tahun_pelajaran tp', 'tp.id = jp.tahun_id');
        $rm->groupBy("CONCAT(u.id, '-', mm.id)");
        $rm->where(['tp.is_active' => '1']);
        $data = $rm->get()->getResultArray();
        return $this->respond($data);
    }
}
