<?php

namespace App\Controllers\Panel\MasterData;

use App\Controllers\Panel\BC;
use App\Models\Panel\MasterData\JenisUjian as JenisUjianModel;
use CodeIgniter\API\ResponseTrait;

class JenisUjian extends BC
{
    use ResponseTrait;
    private $jenisUjian;
    public function __construct()
    {
        parent::__construct();
        $this->jenisUjian = new JenisUjianModel();
    }
    public function index()
    {
        return view('Panel/MasterData/JenisUjian/index', ['page_title' => "Jenis Ujian"]);
    }
    public function all()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = $this->jenisUjian->findAll();
        return $this->respond(['data' => $data]);
    }
    public function detail($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = $this->jenisUjian->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->jenisUjian->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_jenisujian.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');
        $jenis = [
            'kode' => $kode,
            'nama' => $nama,
        ];
        $id = $this->jenisUjian->insert($jenis);
        if (!$id) return $this->fail(["csrf" => csrf_hash(), 'message' => 'Gagal Menambah Jenis Ujian']);
        return $this->respond(["csrf" => csrf_hash(), 'message' => 'Berhasil Menambah Jenis Ujian']);
    }
    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_jenisujian.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');
        $jenis = [
            "id" => $id,
            'kode' => $kode,
            'nama' => $nama,
        ];
        $state = $this->jenisUjian->save($jenis);
        if (!$state) return $this->fail(["csrf" => csrf_hash(), 'message' => 'Gagal Mengubah Jenis Ujian']);
        return $this->respond(["csrf" => csrf_hash(), 'message' => 'Berhasil Mengubah Jenis Ujian']);
    }
}
