<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\UnitModel;

class Unit extends BaseController
{


    private $UnitModel;

    public function __construct()
    {

        $this->UnitModel      = new UnitModel();   
    }

    public function index()
    {
    

        $data   = [
            'title' => "Satuan",
        ];

        return  view('app/unit/main/unit-main-css', $data) .
        view('app/unit/main/unit-main', $data) .
        view('app/unit/main/unit-main-js', $data);

    }

    public function add()
    {

        $data = [
            'title' => "Tambah Satuan"
        ];

        return  view('app/unit/add/unit-add-css', $data) .
        view('app/unit/add/unit-add', $data) .
        view('app/unit/add/unit-add-js', $data);
    }


    public function save()
    {

        $request    = json_decode(json_encode($this->request->getPOST()));
        $rules = [
            'name' => [
                'rules' => 'required|min_length[3]|max_length[256]',
                'errors' => [
                    'required'   => 'Nama wajib diisi',
                    'min_length' => 'Nama minimal 3 karakter',
                    'max_length' => 'Nama tidak boleh lebih dari 256 karakter',
                ]
            ],
        ];


        if (!$this->validate($rules)) {
            $message = $this->validator->getErrors();
            return $this->response->setJSON(['code' => 200, 'success' => false, 'message' => $message]);
        }

            

        $data = [
            'name'    => $request->name,
            'created_by'    => session()->get('id'),
            'created_at'    => date('Y-m-d H:i:s'),
        ];

        if(!$this->UnitModel->insert($data)){
            return $this->response->setJSON([
                'code'      => 200,
                'success'   => false,
                'message'   => "Data gagal disimpan. pastikan anda terkoneksi dengan internet"
            ]);
        }

        return $this->response->setJSON([
            'code'      => 200,
            'success'   => true,
            'message'   => "Data berhasil disimpan"
        ]);

    }


    public function getAllData()
    {
        
        $this->UnitModel->draw     = $this->request->getPOST('draw');
        $this->UnitModel->start    = $this->request->getPOST('start');
        $this->UnitModel->limit    = $this->request->getPOST('length');
        $this->UnitModel->search   = $this->request->getPOST('search')['value'];
        $this->UnitModel->columns  = $this->request->getPOST('columns');
        $this->UnitModel->order    = $this->request->getPOST('order');

        echo $this->UnitModel->getAllData();
    }

    public function delete()
    {

        $request;
        if ($this->request->hasHeader('Content-Type') && strpos($this->request->getHeaderLine('Content-Type'), 'application/json') !== false) {
            $request    = $this->request->getJSON();
        }else{
            $request    = $this->request->getPOST();
        }

        $request = json_decode(json_encode($request));


        $this->UnitModel->where('id', $request->id);
        if(!$this->UnitModel->delete()){

            return $this->response->setJSON([
                'code'      => 200,
                'success'   => false,
                'message'   => "Data gagal dihapus. pastikan anda terkoneksi dengan internet"
            ]);
        }

        return $this->response->setJSON([
            'code'      => 200,
            'success'   => true,
            'message'   => "Data berhasil dihapus"
        ]);

    }


    public function edit($id = null)
    {


        $row = $this->UnitModel->find($id);

        $data = [
            'title' => "Edit Satuan",
            'row'   => $row
        ];

        return  view('app/unit/edit/unit-edit-css', $data) .
        view('app/unit/edit/unit-edit', $data) .
        view('app/unit/edit/unit-edit-js', $data);
        
    }



    public function update()
    {

        

        $request    = json_decode(json_encode($this->request->getPOST()));
        $rules = [
            'name' => [
                'rules' => 'required|min_length[3]|max_length[256]',
                'errors' => [
                    'required'   => 'Nama wajib diisi',
                    'min_length' => 'Nama minimal 3 karakter',
                    'max_length' => 'Nama tidak boleh lebih dari 256 karakter',
                ]
            ]
        ];


        if (!$this->validate($rules)) {
            $message = $this->validator->getErrors();
            return $this->response->setJSON(['code' => 200, 'success' => false, 'message' => $message]);
        }
            

        
        $data = [
            'name'          => $request->name,
            'updated_by'         => session()->get('id'),
            'updated_at'         => date('Y-m-d H:i:s'),
        ];

        if(!$this->UnitModel->update($request->id, $data)){
            return $this->response->setJSON([
                'code'      => 200,
                'success'   => false,
                'message'   => "Data gagal disimpan. pastikan anda terkoneksi dengan internet"
            ]);
        }

        return $this->response->setJSON([
            'code'      => 200,
            'success'   => true,
            'message'   => "Data berhasil disimpan"
        ]);


    }




    public function getAllUnitName()
    {
        $page  = $this->request->getPOST('page') ? $this->request->getPOST('page') - 1 : 0;
        $limit = $this->request->getPOST('limit') ? $this->request->getPOST('limit') : 30;

        $this->UnitModel->name          = $this->request->getPOST('name');
        $this->UnitModel->limit         = $limit;
        $this->UnitModel->start         = ($page * $limit);

        echo $this->UnitModel->getAllUnitName();
    }





}

