<?php

namespace App\Controllers\Panel\MasterData;

use App\Controllers\Panel\BC;
use App\Models\Panel\MasterData\Tingkat as TingkatModel;
use CodeIgniter\API\ResponseTrait;

class Tingkat extends BC
{
    use ResponseTrait;
    private $tingkatModel;
    public function __construct()
    {
        parent::__construct();
        $this->tingkatModel = new TingkatModel();
    }
    public function index()
    {
        return view('Panel/MasterData/Tingkat/index', ['page_title' => "Tingkat"]);
    }
    public function all()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = $this->tingkatModel->findAll();
        return $this->respond(['data' => $data]);
    }
    public function detail($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = $this->tingkatModel->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->tingkatModel->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_tingkat.kode]',
            'errors' => [
                'alpha_dash' => 'Karakter Tidak Valid',
                'max_length' => 'Maksimal 128 Karakter',
                'is_unique' => '"{value}" Sudah Ada'
            ]
        ];
        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');
        $tingkat = [
            'kode' => $kode,
        ];
        $id = $this->tingkatModel->insert($tingkat);
        if (!$id) return $this->fail(["csrf" => csrf_hash(), 'message' => 'Gagal Menambah Tingkat']);
        return $this->respond(["csrf" => csrf_hash(), 'message' => 'Berhasil Menambah Tingkat']);
    }
    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_tingkat.kode,id,$id]",
            'errors' => [
                'alpha_dash' => 'Karakter Tidak Valid',
                'max_length' => 'Maksimal 128 Karakter',
                'is_unique' => '"{value}" Sudah Ada'
            ]
        ];
        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');
        $tingkat = [
            "id" => $id,
            'kode' => $kode,
        ];
        $state = $this->tingkatModel->save($tingkat);
        if (!$state) return $this->fail(["csrf" => csrf_hash(), 'message' => 'Gagal Mengubah Tingkat']);
        return $this->respond(["csrf" => csrf_hash(), 'message' => 'Berhasil Mengubah Tingkat']);
    }
}
