<?php

namespace App\Controllers\Referensi;

use App\Controllers\BaseController;
use CodeIgniter\API\ResponseTrait;

class Pdkjabatan extends BaseController
{
    use ResponseTrait;

    public function __construct()
    {
        $this->PdkjabatanModel = model('App\Models\Referensi\PdkjabatanModel');
    }

    private function setJsonHeader()
    {
        return $this->response->setHeader('Content-Type', 'application/json');
    }

    //Validation Rule
    private function ruleData($id = null)
    {
        return [
            'id' => 'trim',
            'nama_jabatan'              => $id 
                ? "required|regex_match[/^[a-zA-Z0-9\s.,@-]+$/]|max_length[150]|is_unique[pdk_jabatan.nama_jabatan,id,{$id}]"
                : "required|regex_match[/^[a-zA-Z0-9\s.,@-]+$/]|max_length[150]|is_unique[pdk_jabatan.nama_jabatan]",
            'nama_sjab'              => $id 
                ? "required|regex_match[/^[a-zA-Z0-9\s.,@-]+$/]|max_length[100]|is_unique[pdk_jabatan.nama_sjab,id,{$id}]"
                : "required|regex_match[/^[a-zA-Z0-9\s.,@-]+$/]|max_length[100]|is_unique[pdk_jabatan.nama_sjab]",
            'nama_alt'              => $id 
                ? "required|regex_match[/^[a-zA-Z0-9\s.,@-]+$/]|max_length[100]|is_unique[pdk_jabatan.nama_alt,id,{$id}]"
                : "required|regex_match[/^[a-zA-Z0-9\s.,@-]+$/]|max_length[100]|is_unique[pdk_jabatan.nama_alt]",
            'urutan'              => $id 
                ? "required|numeric|is_unique[pdk_jabatan.urutan,id,{$id}]"
                : "required|numeric|is_unique[pdk_jabatan.urutan]",
        ];
    }

    public function index()
    {
        //function($modul, $menu) --------
        $links = genlinks('referensi', 'pdkjabatan');

        $rekam = $links['rekam']['status'];
        $link_rekam = $links['rekam']['link'];

        $ubah = $links['ubah']['status'];
        $link_ubah = $links['ubah']['link'];

        $hapus = $links['hapus']['status'];
        $link_hapus = $links['hapus']['link'];

        return view('referensi/pdkjabatan/pdkjabatan_list', [
            'rekam' => $rekam,
            'link_rekam' => $link_rekam,
            'ubah' => $ubah,
            'link_ubah' => $link_ubah,
            'hapus' => $hapus,
            'link_hapus' => $link_hapus,
        ]);
    }

    public function datagrid()
    {
        $this->setJsonHeader();

        $page           = trim($this->request->getPost('page')) ? trim($this->request->getPost('page')) : 1;
        $rows           = trim($this->request->getPost('rows')) ? trim($this->request->getPost('rows')) : 10;
        $sort           = trim($this->request->getPost('sort'));
        $order          = trim($this->request->getPost('order'));
        $filterRules    = trim($this->request->getPost('filterRules'));
        $start          = ($page - 1) * $rows;

        if ($sort && $order) {
            $sortOrder = multi_sort($sort, $order);
        } else {
            $sortOrder = multi_sort('id', 'ASC');
        }

        $datas = $this->PdkjabatanModel->get_rows($rows, $start, $filterRules, $sortOrder);
        foreach ($datas as $k => $v)
        {
            $datas[$k]->id = encrypt_url($v->id);
        }


        $res['total']  = $this->PdkjabatanModel->get_total($filterRules);
        $res['rows']   = $datas;

        return $this->response->setJSON($res);
    }

    //menampilkan laman manual
    public function manualx()
    {
        return view('referensi/manualx');
    }

    public function show($id)
    {
        $row = $this->PdkjabatanModel->find(decrypt_url($id));

        if (!$row) {
            exit('Data tidak ditemukan');
        }

        return view('referensi/pdkjabatan/pdkjabatan_show', [
			'nama_jabatan' => $row->nama_jabatan,
			'nama_sjab' => $row->nama_sjab,
			'nama_alt' => $row->nama_alt,
			'urutan' => $row->urutan,
        ]);
    }

    public function new()
    {
        return view('referensi/pdkjabatan/pdkjabatan_form', [
            'action' => base_url('referensi/pdkjabatan/create'),
            'button' => 'SIMPAN DATA',
			'id' => '',
			'nama_jabatan' => '',
			'nama_sjab' => '',
			'nama_alt' => '',
			'urutan' => '',
        ]);
    }

    public function create()
    {
        $this->setJsonHeader();

        //jika tdk memiliki akses utk rekam data
        if (!priv_menu('pdkjabatan', 'rekam')) {
            return $this->fail(['message' => NO_AKSES], 403);
        }

        //Collect form data
        $data = [
			'nama_jabatan' => $this->request->getPost('nama_jabatan', FILTER_SANITIZE_SPECIAL_CHARS),
			'nama_sjab' => $this->request->getPost('nama_sjab', FILTER_SANITIZE_SPECIAL_CHARS),
			'nama_alt' => $this->request->getPost('nama_alt', FILTER_SANITIZE_SPECIAL_CHARS),
			'urutan' => $this->request->getPost('urutan', FILTER_SANITIZE_NUMBER_INT),
        ];

        $vrData = $this->ruleData();
        //jika validasi gagal
        if (!$this->validate($vrData)) {
            return $this->fail($this->validator->getErrors(), 400);
        }

        if (!$this->PdkjabatanModel->insert($data)) {
            return $this->fail($this->PdkjabatanModel->errors(), 500);
        }

        return $this->respond(['message' => B_SIMPAN], 201);
    }

    public function edit($id)
    {
        $row = $this->PdkjabatanModel->find(decrypt_url($id));

        if (!$row) {
            exit('Data tidak ditemukan');
        }

        return view('referensi/pdkjabatan/pdkjabatan_form', [
            'action' => base_url('referensi/pdkjabatan/update'),
            'button' => 'UBAH DATA', 
			'id' => encrypt_url($row->id),
			'nama_jabatan' => $row->nama_jabatan,
			'nama_sjab' => $row->nama_sjab,
			'nama_alt' => $row->nama_alt,
			'urutan' => $row->urutan,
        ]);
    }

    public function update()
    {
        $this->setJsonHeader();

        //jika tdk memiliki akses utk rekam data
        if (!priv_menu('pdkjabatan', 'ubah')) {
            return $this->fail(['message' => NO_AKSES], 403);
        }

        //collect form data
        $id = decrypt_url(trim($this->request->getPost('id')));
        $data = [
            'id'  => $id,
			'nama_jabatan' => $this->request->getPost('nama_jabatan', FILTER_SANITIZE_SPECIAL_CHARS),
            'nama_sjab' => $this->request->getPost('nama_sjab', FILTER_SANITIZE_SPECIAL_CHARS),
            'nama_alt' => $this->request->getPost('nama_alt', FILTER_SANITIZE_SPECIAL_CHARS),
            'urutan' => $this->request->getPost('urutan', FILTER_SANITIZE_NUMBER_INT),
        ];

        $vrData = $this->ruleData($id);
        //jika validasi gagal
        if (!$this->validate($vrData)) {
            return $this->fail($this->validator->getErrors(), 400);
        }

        if (!$this->PdkjabatanModel->update($id, $data)) {
            return $this->fail($this->PdkjabatanModel->errors(), 500);
        }

        return $this->respond(['message' => B_UBAH], 200);
    }

    public function delete()
    {
        $this->setJsonHeader();

        $ids  = $this->request->getPost('ids');

        $del = array();
        foreach ($ids as $id) {
            $del[] = decrypt_url($id);
        }

        if (!$this->PdkjabatanModel->delete($del)) {
            return $this->fail($this->PdkjabatanModel->errors(), 500);
        } else {
            return $this->respond(['message' => count($del).lang('App.delete_success')]);
        }
    }

}