<?php

namespace App\Controllers\Panel\Elearning;

use App\Controllers\Panel\BC;
use App\Models\Panel\Elearning\TahunPelajaran as ElearningTahunPelajaran;
use CodeIgniter\API\ResponseTrait;

class TahunPelajaran extends BC
{
    use ResponseTrait;
    private $TPModel;
    public function __construct()
    {
        parent::__construct();
        $this->TPModel = new ElearningTahunPelajaran();
    }
    public function index()
    {
        return view('Panel/Elearning/TahunPelajaran/index', ['page_title' => "Tahun Pelajaran"]);
    }
    public function all()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = $this->TPModel->findAll();
        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->TPModel->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->TPModel->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['nama'] = [
            "alias" => "Nama",
            'rules' => 'string|max_length[255]|is_unique[tahun_pelajaran.kode]',
            'errors' => [
                'string' => '{value} Karakter Tidak Valid',
                'max_length' => '{value} Maksimal 255 Karakter',
                'is_unique' => '{value} Sudah ada',
            ]
        ];
        $rules['kode'] = [
            "alias" => "Kode",
            'rules' => 'string|required',
            'errors' => [
                'string' => '{value} Karakter Tidak Valid',
                'required' => "Kode Dibutuhkan",
            ]
        ];
        $rules['semester'] = [
            "alias" => "Semester",
            'rules' => "string|in_list[Ganjil,Genap]",
            'errors' => [
                'string' => '{value} Karakter Tidak Valid',
                'in_list' => '{value} Tidak Valid!. Kode Hanya Boleh Ganjil dan Genap'
            ]
        ];
        $rules['is_active'] = [
            "alias" => "Semester",
            'rules' => 'integer|in_list[1,0]',
            'errors' => [
                'integer' => '{value} Karakter Tidak Valid',
                'in_list' => '{value} Tidak Valid!. Status Hanya Boleh 0 dan 1'
            ]
        ];

        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]);
        }
        $data = [
            "nama" => $this->request->getVar('nama'),
            "kode" => $this->request->getVar('kode'),
            "semester" => $this->request->getVar('semester'),
            "is_active" => $this->request->getVar('is_active'),
        ];
        $id = $this->TPModel->insert($data);
        if (!$id) return $this->fail(["csrf" => csrf_hash(), 'message' => 'Gagal Menambah Tahun Pelajaran']);
        if ($data['is_active'] == 1) $this->TPModel->where('id !=', $id)->set('is_active', '0')->update();
        return $this->respond(["csrf" => csrf_hash(), 'message' => 'Berhasil Menambah Tahun Pelajaran']);
    }
    public function edit($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $rules['nama'] = [
            "alias" => "Nama",
            'rules' => 'string|max_length[255]',
            'errors' => [
                'string' => '{value} Karakter Tidak Valid',
                'max_length' => '{value} Maksimal 255 Karakter'
            ]
        ];
        $rules['kode'] = [
            "alias" => "Kode",
            'rules' => "string|required|is_unique[tahun_pelajaran.kode,id,$id]",
            'errors' => [
                'string' => '{value} Karakter Tidak Valid',
                'required' => "Kode Dibutuhkan",
                'is_unique' => "Tahun Pelajaran",
            ]
        ];
        $rules['semester'] = [
            "alias" => "Semester",
            'rules' => 'string|in_list[Ganjil,Genap]',
            'errors' => [
                'string' => '{value} Karakter Tidak Valid',
                'in_list' => '{value} Tidak Valid!. Kode Hanya Boleh Ganjil dan Genap'
            ]
        ];
        $rules['is_active'] = [
            "alias" => "Semester",
            'rules' => 'integer|in_list[1,0]',
            'errors' => [
                'integer' => '{value} Karakter Tidak Valid',
                'in_list' => '{value} Tidak Valid!. Status Hanya Boleh 0 dan 1'
            ]
        ];

        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]);
        }
        $data = [
            "nama" => $this->request->getVar('nama'),
            "kode" => $this->request->getVar('kode'),
            "semester" => $this->request->getVar('semester'),
            "is_active" => $this->request->getVar('is_active'),
        ];
        $updated = $this->TPModel->update($id, $data);
        if (!$updated) return $this->fail(["csrf" => csrf_hash(), 'message' => 'Gagal Mengubah Tahun Pelajaran']);
        if ($data['is_active'] == 1) $this->TPModel->where('id !=', $id)->set('is_active', '0')->update();
        return $this->respond(["csrf" => csrf_hash(), 'message' => 'Berhasil Mengubah Tahun Pelajaran']);
    }
}
