<?php

namespace App\Models\Panel\Elearning;

use CodeIgniter\Model;

class JadwalPelajaran extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'jadwal_pelajaran';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $insertID         = 0;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'mapel_id',
        'kelas_id',
        'tahun_id',
        'user_id',
        'countJam',
        'startDay',
        'startTime',
        'endTime',
        'created_at',
        'updated_at'
    ];

    // Dates
    protected $useTimestamps = false;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';

    // Validation
    protected $validationRules      = [];
    protected $validationMessages   = [];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert   = [];
    protected $afterInsert    = [];
    protected $beforeUpdate   = [];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];
    public function getAll($opt = [])
    {
        $select = "$this->table.*,u.fullname nama_guru,mm.nama nama_mapel,mm.kode kode_mapel";
        $select .= ",mk.kode kode_kelas,mk.nama nama_kelas,mt.id tingkat_id,mt.kode kode_tingkat";
        $select .= ",mj.id jurusan_id,mj.kode kode_jurusan,mj.nama nama_jurusan";
        $select .= ",tp.kode kode_tahun,tp.nama nama_tahun,tp.semester semester_tahun";
        $builder = $this->db->table($this->table);
        $builder->join('m_mapel mm', "mm.id=$this->table.mapel_id");
        $builder->join('m_kelas mk', "mk.id=$this->table.kelas_id");
        $builder->join('kelas_data kd', "kd.kelas_id=mk.id", 'left');
        $builder->join("m_tingkat mt", "mt.id=kd.tingkat_id", "left");
        $builder->join("m_jurusan mj", "mj.id=kd.jurusan_id", "left");
        $builder->join('tahun_pelajaran tp', "tp.id=$this->table.tahun_id");
        $builder->join('users u', "u.id=$this->table.user_id");
        $builder->select($select);
        if (isset($opt['user_id'])) {
            $builder->where("$this->table.user_id", $opt['user_id']);
        }
        if (isset($opt['kelas_id'])) {
            $builder->where("$this->table.kelas_id", $opt['kelas_id']);
        }
        if (isset($opt['tahun_id'])) {
            $builder->where("$this->table.tahun_id", $opt['tahun_id']);
        }
        if (isset($opt['startDay'])) {
            $builder->where("$this->table.startDay", $opt['startDay']);
        }
        return $builder->get()->getResultArray();
    }

    public function getMapelIdFromJadwal($id, $tahun_id = null)
    {
        $builder = $this->db->table($this->table);
        $builder->select("$this->table.mapel_id");
        $builder->join('tahun_pelajaran tp', "tp.id=$this->table.tahun_id");
        if ($tahun_id) $builder->where('tp.id', $tahun_id);
        $builder->where("$this->table.id", $id);
        return $builder->get()->getRowArray();
    }
    public function findId($id)
    {
        $select = "$this->table.*,u.fullname nama_guru,mm.nama nama_mapel,mm.kode kode_mapel";
        $select .= ",mk.kode kode_kelas,mk.nama nama_kelas,mt.id tingkat_id,mt.kode kode_tingkat";
        $select .= ",mj.id jurusan_id,mj.kode kode_jurusan,mj.nama nama_jurusan";
        $select .= ",tp.kode kode_tahun,tp.nama nama_tahun,tp.semester semester_tahun";
        $builder = $this->db->table($this->table);
        $builder->join('m_mapel mm', "mm.id=$this->table.mapel_id");
        $builder->join('m_kelas mk', "mk.id=$this->table.kelas_id");
        $builder->join('kelas_data kd', "kd.kelas_id=mk.id", 'left');
        $builder->join("m_tingkat mt", "mt.id=kd.tingkat_id", "left");
        $builder->join("m_jurusan mj", "mj.id=kd.jurusan_id", "left");
        $builder->join('tahun_pelajaran tp', "tp.id=$this->table.tahun_id");
        $builder->join('users u', "u.id=$this->table.user_id");
        $builder->select($select);
        $builder->where("$this->table.id", $id);
        if (!user()->hasRole('admin')) {
            $builder->where("$this->table.user_id", user()->id);
        }
        return $builder->get()->getRowArray();
    }
}
