<?php

namespace App\Controllers\Panel\MasterData;

use App\Controllers\Panel\BC;
use App\Models\Panel\MasterData\RuangUjian as RuangUjianModel;
use CodeIgniter\API\ResponseTrait;

class RuangUjian extends BC
{
    use ResponseTrait;
    private $ruangUjian;
    public function __construct()
    {
        parent::__construct();
        $this->ruangUjian = new RuangUjianModel();
    }
    public function index()
    {
        return view('Panel/MasterData/RuangUjian/index', ['page_title' => "Ruang Ujian"]);
    }
    public function all()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = $this->ruangUjian->getAll();
        return $this->respond(['data' => $data, 'csrf' => ['name' => csrf_token(), 'value' => csrf_hash()]]);
    }
    public function detail($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = $this->ruangUjian->findId($id);
        return $this->respond(['data' => $data]);
    }

    public function delete($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $state = $this->ruangUjian->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_ruangujian.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');
        $ruang = [
            'kode' => $kode,
            'nama' => $nama,
        ];
        $id = $this->ruangUjian->insert($ruang);
        if (!$id) return $this->fail(["csrf" => csrf_hash(), 'message' => 'Gagal Menambah Ruang Ujian']);
        return $this->respond(["csrf" => csrf_hash(), 'message' => 'Berhasil Menambah Ruang 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_ruangujian.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');
        $ruang = [
            "id" => $id,
            'kode' => $kode,
            'nama' => $nama,
        ];
        $state = $this->ruangUjian->save($ruang);
        if (!$state) return $this->fail(["csrf" => csrf_hash(), 'message' => 'Gagal Mengubah Ruang Ujian']);
        return $this->respond(["csrf" => csrf_hash(), 'message' => 'Berhasil Mengubah Ruang Ujian']);
    }
}
