<?php

namespace App\Controllers\Panel\MasterData;

use App\Controllers\Panel\BC;
use App\Models\Panel\MasterData\Jurusan as JurusanModel;
use CodeIgniter\API\ResponseTrait;

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