<?php
namespace App\Controller;

use Cake\I18n\FrozenTime;

/**
 * CourseCategories Controller
 *
 * @property \App\Model\Table\CourseCategoriesTable $CourseCategories
 */
class CourseCategoriesController extends AppsController
{
    private  $controller = "course-categories";
    public function initialize()
    {
        parent::initialize();
        $this->loadModel('CourseCategories');
        $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 . "%'" .')' ;
        }
        $course_categories = $this->CourseCategories->find('all')->where($condition);
        $this->set('course_categories', $this->paginate($course_categories));
        $this->set('searchText', $search);
        $this->set('page_limit', $page_limit);
        $nav_arr[0] = array('action' => 'add', 'page_id' => '10172', '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');
            $course_category = $this->CourseCategories->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;
            $course_category = $this->CourseCategories->patchEntity($course_category, $request);
            $errors = $course_category->errors();

            if (empty($errors)) {
                if ($this->CourseCategories->save($course_category)) {

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

        $this->setNextPage();
    }

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

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

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

            $course_category = $this->CourseCategories->patchEntity($course_category, $this->request->data);

            $errors = $course_category->errors();

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

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

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

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

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

}
