<?php
namespace App\Controller;

use Cake\I18n\FrozenTime;

/**
 * CheckList Controller
 *
 * @property \App\Model\Table\CheckListsTable $CheckLists
 */
class CheckListController extends AppsController
{
    private  $controller = "check-list";
    public function initialize(): void
    {
        parent::initialize();
        $this->loadModel('CheckLists');
        $this->set('controller', $this->controller);
        $this->setPermissions();
    }

    public function index()
    {
        $page_limit = 10;
        if (isset($this->request->query['page_limit'])) {
            $page_limit = $this->request->query('page_limit');
        }

        $this->paginate = [
            'limit' => $page_limit,
            'order' => [
                'id' => 'asc'
            ]
        ];

        $search = '';
        if (isset($this->request->query['search'])) {
            $search = $this->request->query('search');
            $condition = '(name LIKE ' . "'%" . $search  . "%'" . 'OR description LIKE ' . "'%" . $search  . "%'".')' ;
        }
        $CheckLists = $this->CheckLists->find('all')->where($condition);


        $this->set('check_lists', $this->paginate($CheckLists));
        $this->set('searchText', $search);
        $this->set('page_limit', $page_limit);
        $nav_arr[0] = array('action' => 'add', 'page_id' => '10163', 'icon' => '<i class="fa fa-plus-circle"></i>', 'label' => 'Add');
        $this->set('nav_arr', $nav_arr);
    }

    public function add()
    {
        if ($this->request->is('post')) {
            $user_id = $this->Auth->user('id');
            $CheckList = $this->CheckLists->newEntity();
            $request = $this->request->getData();
            $request['status'] = 1;
            $request['created_at'] = FrozenTime::now();
            $request['created_by'] = $user_id;
            $request['updated_at'] = FrozenTime::now();
            $request['updated_by'] = $user_id;
            $CheckList = $this->CheckLists->patchEntity($CheckList, $request);
            $errors = $CheckList->errors();

            if (empty($errors)) {
                if ($this->CheckLists->save($CheckList)) {

                    $this->Flash->success(__('The CheckLists has been saved.'));
                    return $this->redirect(['action' => 'index']);
                }
            }
            $this->set('errors', $errors);
            $this->Flash->error(__('Unable to add the CheckLists.'));
        }
        $this->setNextPage();
    }

    public function edit($id=null)
    {
        $check_list = $this->CheckLists->get($id, [
            'contain' => []
        ]);

        if ($this->request->is(['patch', 'post', 'put'])) {

            $user_id = $this->Auth->user('id');

            $check_list = $this->CheckLists->patchEntity($check_list, $this->request->data);

            $errors = $check_list->errors();

            $check_list['updated_at'] = FrozenTime::now();
            $check_list['updated_by'] = $user_id;
            if (empty($errors)) {
                if ($this->CheckLists->save($check_list)) {
                    $this->Flash->success(__('The check list has been updated.'));
                    return $this->redirect(['action' => 'index']);
                } else {
                    $this->Flash->error(__('The check list could not be saved. Please, try again.'));
                }
            }
            $this->set('errors', $errors);
        }

        $this->set('check_list', $check_list);
        $this->set('_serialize', ['check_list']);
        $this->setNextPage();

    }

    public function view($id)
    {
        $check_list = $this->CheckLists->get($id, [
            'contain' => []
        ]);

        $this->set('check_list', $check_list);
        $this->set('_serialize', ['check_list']);
        $this->setNextPage();
    }
    private function setNextPage(){
        $nav_arr[0] = array('action' => 'index', 'page_id' => '10160', 'icon' => '<i class="fa fa-list"></i>', 'label' => 'List');
        $this->set('nav_arr', $nav_arr);
    }

}
