<?php

namespace App\Models\Panel\Elearning;

use CodeIgniter\Model;

class RPP extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'rpp';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $insertID         = 0;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'file',
        'kompetensi_id',
        'tahun_id',
        'user_id',
    ];

    // Dates
    protected $useTimestamps = true;
    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 = [])
    {
        $filePath = base_url(config('app')->pdfPath . "/elearning/rpp");
        $select = "$this->table.*,CASE WHEN $this->table.file IS NOT NULL THEN CONCAT('$filePath/',$this->table.file) ELSE NULL END file,u.fullname created_by,kd.nama nama_kompetensi,kd.kode kode_kompetensi,(CASE WHEN kd.kode = 'KP' THEN CONCAT('3.',kd.nomor) ELSE CONCAT('4.',kd.nomor) END) nomor_kompetensi,kd.mapel_id,CONCAT(kd.tingkat_id,'-',kd.mapel_id) tingmap,tp.is_active tahun_active,tp.kode kode_tahun,tp.nama nama_tahun,tp.semester semester_tahun";
        $builder = $this->db->table($this->table);
        $builder->join('kompetensi_dasar kd', "kd.id=$this->table.kompetensi_id");
        $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['tingkat_id'])) {
            if (is_array($opt['tingkat_id'])) {
                $builder->whereIn("kd.tingkat_id", $opt['tingkat_id']);
            } else {
                $builder->where("kd.tingkat_id", $opt['tingkat_id']);
            }
        }
        if (isset($opt['mapel_id'])) {
            if (is_array($opt['mapel_id'])) {
                $builder->whereIn("kd.mapel_id", $opt['mapel_id']);
            } else {
                $builder->where("kd.mapel_id", $opt['mapel_id']);
            }
        }
        if (isset($opt['tingmap'])) {
            if (is_array($opt['tingmap'])) {
                $builder->havingIn("tingmap", $opt['tingmap']);
            } else {
                $builder->having("tingmap", $opt['tingmap']);
            }
        }
        return $builder->get()->getResultArray();
    }
    public function findId($id)
    {
        $filePath = base_url(config('app')->pdfPath . "/elearning/rpp");
        $select = "$this->table.*,CASE WHEN $this->table.file IS NOT NULL THEN CONCAT('$filePath/',$this->table.file) ELSE NULL END file,u.fullname created_by,kd.nama nama_kompetensi,kd.kode kode_kompetensi,kd.nomor nomor_kompetensi,kd.tingkat_id,kd.mapel_id,tp.is_active tahun_active,tp.kode kode_tahun,tp.nama nama_tahun,tp.semester semester_tahun";
        $builder = $this->db->table($this->table);
        $builder->join('kompetensi_dasar kd', "kd.id=$this->table.kompetensi_id");
        $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();
    }
}
