<?php

namespace App\Controllers\Panel\Ujian\Jadwal\Detail\Result;

use App\Controllers\Panel\BC;
use App\Models\Panel\BankSoal;
use App\Models\Panel\Elearning\TahunPelajaran;
use App\Models\Panel\Ujian\Jadwal;
use CodeIgniter\API\ResponseTrait;

class Jawaban extends BC
{
    use ResponseTrait;
    private $jadwalModel;
    public function __construct()
    {
        parent::__construct();
        $this->jadwalModel = new Jadwal();
    }
    public function index($id)
    {
        $uid = $this->request->getGet('id');
        if (!$uid) return redirect()->to('/panel/ujian/jadwal/' . $id);
        $uid = explode(',', $uid);
        $id = base64_decode($id);
        $jadwalData = $this->jadwalModel->findId($id);
        if (!$jadwalData) return $this->failNotFound();
        $db = db_connect();
        $data['page_title'] = "Print Jawaban - " . $jadwalData['kode_ruang'] . " - " . $jadwalData['nama_draft'];
        $data['mapel'] = $jadwalData['nama_mapel'];
        $data['logo'] = logo();
        $sql['ujian'] = $db->table('ujian_siswa us')
            ->select('us.id,us.nilai_pg,nilai_esai,mt.kode tingkat,mk.nama kelas,ms.nama,ms.nomor_induk')
            ->whereIn('us.id', $uid)
            ->join('m_siswa ms', 'ms.id = us.siswa_id')
            ->join('m_kelas mk', 'mk.id = ms.kelas_id', 'left')
            ->join('kelas_data kd', 'kd.kelas_id = mk.id', 'left')
            ->join('m_tingkat mt', 'mt.id = kd.tingkat_id', 'left')
            ->get()->getResultArray();
        $sql['pg'] = $db->table('jawaban_pg_ujian_siswa jpus')
            ->select('jpus.*,sd.question,so.body response')
            ->join('soal_data sd', 'sd.id = jpus.soal_id')
            ->join('soal_opsi so', 'so.id = jpus.opsi_id')
            ->whereIn('jpus.ujian_id', $uid)
            ->where('sd.jenis_soal', (new BankSoal())->jenis('pg'))
            ->get()->getResultArray();
        $sql['pgmulti'] = $db->table('jawaban_pgmulti_ujian_siswa jpus')
            ->select("jpus.*,sd.question,GROUP_CONCAT(so.body SEPARATOR '⥈') responses")
            ->join('soal_data sd', 'sd.id = jpus.soal_id')
            ->join('soal_opsi so', 'so.id = jpus.opsi_id')
            ->whereIn('jpus.ujian_id', $uid)
            ->where('sd.jenis_soal', (new BankSoal())->jenis('pgmulti'))
            ->groupBy("CONCAT(jpus.ujian_id,'-',jpus.soal_id)", false)
            ->get()->getResultArray();
        $sql['pgcouple'] = $db->table('jawaban_pgcouple_ujian_siswa jpus')
            ->select("jpus.*,sd.question,GROUP_CONCAT(CONCAT(sdc.body,'⟺',so.body) SEPARATOR '⥈') responses")
            ->join('soal_data sd', 'sd.id = jpus.soal_id')
            ->join('soal_data_couple sdc', 'sdc.id = jpus.couple_id')
            ->join('soal_opsi so', 'so.id = jpus.opsi_id')
            ->whereIn('jpus.ujian_id', $uid)
            ->where('sd.jenis_soal', (new BankSoal())->jenis('pgcouple'))
            ->groupBy("CONCAT(jpus.ujian_id,'-',jpus.soal_id)", false)
            ->get()->getResultArray();
        $sql['pgtf'] = $db->table('jawaban_pgtf_ujian_siswa jpus')
            ->select("jpus.*,sd.question,GROUP_CONCAT(CONCAT(so.body,'⟺',jpus.tf) SEPARATOR '⥈') responses")
            ->join('soal_data sd', 'sd.id = jpus.soal_id')
            ->join('soal_opsi so', 'so.id = jpus.opsi_id')
            ->whereIn('jpus.ujian_id', $uid)
            ->where('sd.jenis_soal', (new BankSoal())->jenis('pgtf'))
            ->groupBy("CONCAT(jpus.ujian_id,'-',jpus.soal_id)", false)
            ->get()->getResultArray();
        $sql['shortentry'] = $db->table('jawaban_shortentry_ujian_siswa jsus')
            ->select('jsus.*,sd.question')
            ->join('soal_data sd', 'sd.id = jsus.soal_id')
            ->whereIn('jsus.ujian_id', $uid)
            ->where('sd.jenis_soal', (new BankSoal())->jenis('shortentry'))
            ->get()->getResultArray();
        $sql['esai'] = $db->table('jawaban_esai_ujian_siswa jeus')
            ->select('jeus.*,sd.question')
            ->join('soal_data sd', 'sd.id = jeus.soal_id')
            ->whereIn('jeus.ujian_id', $uid)
            ->where('sd.jenis_soal', (new BankSoal())->jenis('esai'))
            ->get()->getResultArray();
        $data['ujian'] = [];
        foreach ($sql['ujian'] as $u) {
            $ujian['person'] = $u;
            $ujian['pg'] = [];
            $ujian['pgmulti'] = [];
            $ujian['pgcouple'] = [];
            $ujian['pgtf'] = [];
            $ujian['shortentry'] = [];
            $ujian['esai'] = [];
            foreach ($sql['pg'] as $pg) {
                if ($pg['ujian_id'] == $u['id']) {
                    $ujian['pg'][] = $pg;
                }
            }
            foreach ($sql['pgmulti'] as $pgm) {
                if ($pgm['ujian_id'] == $u['id']) {
                    $pgm['responses'] = explode('⥈', $pgm['responses']);
                    $ujian['pgmulti'][] = $pgm;
                }
            }
            foreach ($sql['pgcouple'] as $pgc) {
                if ($pgc['ujian_id'] == $u['id']) {
                    $responses = explode('⥈', $pgc['responses']);
                    $pgc['responses'] = [];
                    foreach ($responses as $k => $v) {
                        $pgc['responses'][] = explode('⟺', $v);
                    }
                    $ujian['pgcouple'][] = $pgc;
                }
            }
            foreach ($sql['pgtf'] as $pgtf) {
                if ($pgtf['ujian_id'] == $u['id']) {
                    $r = [];
                    foreach (explode('⥈', $pgtf['responses']) as $pr) {
                        $r[] = explode('⟺', $pr);
                    }
                    $pgtf['responses'] = $r;
                    $ujian['pgtf'][] = $pgtf;
                }
            }
            foreach ($sql['shortentry'] as $se) {
                if ($se['ujian_id'] == $u['id']) {
                    $ujian['shortentry'][] = $se;
                }
            }
            foreach ($sql['esai'] as $e) {
                if ($e['ujian_id'] == $u['id']) {
                    $ujian['esai'][] = $e;
                }
            }
            $data['ujian'][] = $ujian;
        }
        $data['tp'] = (new TahunPelajaran())->getActiveName();
        return view('Panel/Ujian/Jadwal/Detail/jawaban-ujian', $data);
    }
}
