<?php
namespace App\Controller;

use Cake\I18n\FrozenTime;

/**
 * ReportFormatType Controller
 *
 * @property \App\Model\Table\ReportFormatTypesTable $ReportFormatTypes
 */
class ReportFormatTypeController extends AppsController
{
    private  $controller = "report-format-type";
    public function initialize(): void
    {
        parent::initialize();
        $this->loadModel('ReportFormatTypes');
        $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 . "%'" .')' ;
        }
        $ReportFormatTypes = $this->ReportFormatTypes->find('all')->where($condition);

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

            if (empty($errors)) {
                if ($this->ReportFormatTypes->save($ReportFormatType)) {

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

        $this->setNextPage();
    }

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

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

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

            $report_format_type = $this->ReportFormatTypes->patchEntity($report_format_type, $this->request->data);

            $errors = $report_format_type->errors();

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

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

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

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

}
