<?php

namespace App\Controllers;

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

class Category extends BaseController
{


    private $CategoryModel;

    public function __construct()
    {

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

    public function index()
    {
    

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

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

    }

    public function add()
    {

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

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


    public function detail($id = null)
    {


        $this->GoodModel->select('tbl_good.*, tbl_area.name AS area_name');
        $this->GoodModel->join('tbl_area','tbl_area.id=tbl_good.area_id');
        $row = $this->GoodModel->find($id);


        $data = [
            'title' => "Detail Barang",
            'code'  => $row->unique_id
        ];

        return  view('app/good/detail-qrcode/detail-qrcode-css', $data) .
        view('app/good/detail-qrcode/detail-qrcode', $data) .
        view('app/good/detail-qrcode/detail-qrcode-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->CategoryModel->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->CategoryModel->draw     = $this->request->getPOST('draw');
        $this->CategoryModel->start    = $this->request->getPOST('start');
        $this->CategoryModel->limit    = $this->request->getPOST('length');
        $this->CategoryModel->search   = $this->request->getPOST('search')['value'];
        $this->CategoryModel->columns  = $this->request->getPOST('columns');
        $this->CategoryModel->order    = $this->request->getPOST('order');

        echo $this->CategoryModel->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->CategoryModel->where('id', $request->id);
        if(!$this->CategoryModel->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->CategoryModel->find($id);

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

        return  view('app/category/edit/category-edit-css', $data) .
        view('app/category/edit/category-edit', $data) .
        view('app/category/edit/category-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->CategoryModel->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 getAllCategoryName()
    {
        $page  = $this->request->getPOST('page') ? $this->request->getPOST('page') - 1 : 0;
        $limit = $this->request->getPOST('limit') ? $this->request->getPOST('limit') : 30;

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

        echo $this->CategoryModel->getAllCategoryName();
    }



}

