<?php
namespace App\Controller;

use Cake\I18n\FrozenTime;

/**
 * QualificationType Controller
 *
 * @property \App\Model\Table\QualificationTypesTable $QualificationTypes
 */
class QualificationTypesController extends AppsController
{
    private  $controller = "qualification-types";
    public function initialize()
    {
        parent::initialize();
        $this->loadModel('QualificationTypes');
        $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 . "%'" .')' ;
        }
        $qualification_types = $this->QualificationTypes->find('all')->where($condition);

        $this->set('qualification_types', $this->paginate($qualification_types));
        $this->set('searchText', $search);
        $this->set('page_limit', $page_limit);
        $nav_arr[0] = array('action' => 'add', 'page_id' => '10178', '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');
            $qualification_type = $this->QualificationTypes->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;
            $qualification_type = $this->QualificationTypes->patchEntity($qualification_type, $request);
            $errors = $qualification_type->errors();

            if (empty($errors)) {
                if ($this->QualificationTypes->save($qualification_type)) {

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

        $this->setNextPage();
    }

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

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

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

            $qualification_type = $this->QualificationTypes->patchEntity($qualification_type, $this->request->data);

            $errors = $qualification_type->errors();

            $qualification_type['updated_at'] = FrozenTime::now();
            $qualification_type['updated_by'] = $user_id;
            if (empty($errors)) {
                if ($this->QualificationTypes->save($qualification_type)) {
                    $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('qualification_type', $qualification_type);
        $this->set('_serialize', ['qualification_type']);
        $this->setNextPage();
    }

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

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

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

}
