<?php

namespace App\Controllers;

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

class Role extends BaseController
{


    private $RoleModel;

    public function __construct()
    {

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

    public function index()
    {

        haveAccess(6,"is_read", session()->get('role_id'));

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

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

    public function add()
    {


        haveAccess(6,"is_create", session()->get('role_id'));

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

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

    public function save()
    {


        haveAccess(6,"is_create", session()->get('role_id'));

        $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));

        if(empty($request->name)){
            return $this->response->setJSON([
                'code'      => 200,
                'success'   => false,
                'message'   => [
                    'name'      => "Nama tidak boleh kosong"
                ]
            ]);
        }

        if(strlen(trim($request->name))<3){
            return $this->response->setJSON([
                'code'      => 200,
                'success'   => false,
                'message'   => [
                    'name'      => "Nama tidak boleh kurang dari 3 karakter"
                ]
            ]);
        }


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

        if(!$this->RoleModel->insert($data)){

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

        $userId = session()->get('id');
        $nameUser = session()->get('name');
        insertLog("Role","Create","User $nameUser berhasil membuat role $request->name ",$userId);

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


    }


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

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

    public function delete()
    {


        haveAccess(6,"is_delete", session()->get('role_id'));

        $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->RoleModel->where('id', $request->id);
        if(!$this->RoleModel->delete()){

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


        $userId = session()->get('id');
        $nameUser = session()->get('name');
        insertLog("Role","Delete","User $nameUser berhasil menghapus data role ",$userId);

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

    }


    public function edit($id = null)
    {


        haveAccess(6,"is_update", session()->get('role_id'));

        $data = [
            'title' => "Edit Role",
            'row'   => $this->RoleModel->find($id)
        ];

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



    public function update()
    {


        haveAccess(6,"is_update", session()->get('role_id'));

        $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));

        if(empty($request->name)){
            return $this->response->setJSON([
                'code'      => 200,
                'success'   => false,
                'message'   => [
                    'name'      => "Nama tidak boleh kosong"
                ]
            ]);
        }

        if(strlen(trim($request->name))<3){
            return $this->response->setJSON([
                'code'      => 200,
                'success'   => false,
                'message'   => [
                    'name'      => "Nama tidak boleh kurang dari 3 karakter"
                ]
            ]);
        }

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

        if(!$this->RoleModel->update($request->id,$data)){

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


        $userId = session()->get('id');
        $nameUser = session()->get('name');
        insertLog("Role","Update","User $nameUser berhasil mengubah data role $request->name ",$userId);

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


    }


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

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

        echo $this->RoleModel->getAllRoleName();
    }


}

