<?php

namespace App\Controllers\Panel\MasterData;

use App\Controllers\Panel\BC;
use App\Models\Panel\MasterData\MataPelajaran as Mapel;
use CodeIgniter\API\ResponseTrait;

class MataPelajaran extends BC
{
    use ResponseTrait;
    private $mapelModel;
    public function __construct()
    {
        parent::__construct();
        $this->mapelModel = new Mapel();
    }
    public function index()
    {
        return view('Panel/MasterData/MataPelajaran/index', ['page_title' => "Mata Pelajaran"]);
    }
    public function all()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = $this->mapelModel->findAll();
        return $this->respond(['data' => $data]);
    }
    public function detail($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = $this->mapelModel->find($id);
        return $this->respond(['data' => $data]);
    }

    public function delete($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $state = $this->mapelModel->delete($id);
        return $this->respond(['deleted' => $state, 'message' => $state ? "Berhasil Menghapus" : "Gagal Menghapus"]);
    }

    public function add()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }

        $rules['kode'] = [
            "alias" => "Kode",
            'rules' => 'alpha_dash|max_length[128]|is_unique[m_mapel.kode]',
            'errors' => [
                'alpha_dash' => 'Karakter Tidak Valid',
                'max_length' => 'Maksimal 128 Karakter',
                'is_unique' => '"{value}" Sudah Ada'
            ]
        ];
        $rules['nama'] = [
            "alias" => "Nama",
            'rules' => 'string|max_length[255]',
            'errors' => [
                'string' => '{value} Karakter Tidak Valid',
                'max_length' => '{value} Maksimal 255 Karakter'
            ]
        ];
        if (!$this->validate($rules)) {
            $errors = $this->validator->getErrors();
            $err = '';
            foreach ($errors as $key => $error) {
                $err = $err . $rules[$key]['alias'] . " : " . $error . '<br>';
            }
            return $this->fail(["csrf" => csrf_hash(), 'message' => $err]);
        }
        $kode = $this->request->getVar('kode');
        $nama = $this->request->getVar('nama');
        $mapel = [
            'kode' => $kode,
            'nama' => $nama,
        ];
        $id = $this->mapelModel->insert($mapel);
        if (!$id) return $this->fail(["csrf" => csrf_hash(), 'message' => 'Gagal Menambah Pelajaran']);
        return $this->respond(["csrf" => csrf_hash(), 'message' => 'Berhasil Menambah Pelajaran']);
    }
    public function edit($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }

        $rules['kode'] = [
            "alias" => "Kode",
            'rules' => "alpha_dash|max_length[128]|is_unique[m_mapel.kode,id,$id]",
            'errors' => [
                'alpha_dash' => 'Karakter Tidak Valid',
                'max_length' => 'Maksimal 128 Karakter',
                'is_unique' => '"{value}" Sudah Ada'
            ]
        ];
        $rules['nama'] = [
            "alias" => "Nama",
            'rules' => 'string|max_length[255]',
            'errors' => [
                'string' => '{value} Karakter Tidak Valid',
                'max_length' => '{value} Maksimal 255 Karakter'
            ]
        ];
        if (!$this->validate($rules)) {
            $errors = $this->validator->getErrors();
            $err = '';
            foreach ($errors as $key => $error) {
                $err = $err . $rules[$key]['alias'] . " : " . $error . '<br>';
            }
            return $this->fail(["csrf" => csrf_hash(), 'message' => $err]);
        }
        $kode = $this->request->getVar('kode');
        $nama = $this->request->getVar('nama');
        $mapel = [
            "id" => $id,
            'kode' => $kode,
            'nama' => $nama,
        ];
        $state = $this->mapelModel->save($mapel);
        if (!$state) return $this->fail(["csrf" => csrf_hash(), 'message' => 'Gagal Mengubah Pelajaran']);
        return $this->respond(["csrf" => csrf_hash(), 'message' => 'Berhasil Mengubah Pelajaran']);
    }
}
