<?php
namespace App\Controller;

use Cake\I18n\FrozenTime;

/**
 * ReportFormatVariableValue Controller
 *
 * @property \App\Model\Table\ReportFormatVariableValuesTable $ReportFormatVariableValues
 * @property \App\Model\Table\ReportFormatVariablesTable $ReportFormatVariables
 * @property \App\Model\Table\ReportFormatGenerationHistoriesTable $ReportFormatGenerationHistories
 * @property \App\Model\Table\ReportFormatsTable $ReportFormats
 */
class ReportFormatVariableValueController extends AppsController
{

    public function initialize(): void
    {
        parent::initialize();
        $this->loadModel('ReportFormatVariableValues');
        $this->loadModel('ReportFormatVariables');
        $this->loadModel('ReportFormatGenerationHistories');
        $this->loadModel('ReportFormats');
    }

    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' => [
                'ReportFormatVariableValues.id' => 'asc'
            ]
        ];

        $search = '';
        $report_format_variable_values = $this->ReportFormatVariableValues->find('all', [
                'contain' => ['ReportFormats','ReportFormatVariables','ReportFormatGenerationHistories']]
        );

        $user_permissions = $this->request->session()->read('user_permissions');
        $this->set('user_permission', $user_permissions);
        $this->set('bulk_actions', [BULK_ACTION_DELETE => "Move to trash"]);
        $this->set('report_format_variable_values', $this->paginate($report_format_variable_values));
        $this->set('searchText', $search);
        $this->set('current_module', $this->getModules());
        $this->set('page_list', $this->getPages());
        $this->set('module_pages', $this->getPages());
        $nav_arr[0] = array('action' => 'add', 'page_id' => '3046', 'icon' => '<i class="fa fa-plus-circle"></i>', 'label' => 'Add');
        $this->set('nav_arr', $nav_arr);
        $this->set('page_limit', $page_limit);
    }

    public function add()
    {
        $report_formats = $this->ReportFormats->find('list', [
            'keyField' => 'id',
            'valueField' => 'name'
        ])->where(['status'=>1])->toArray();

        if ($this->request->is('post')) {
            $user_id = $this->Auth->user('id');
            $report_format = $this->ReportFormatVariableValues->newEntity();
            $request = $this->request->getData();
            $request['status'] = 1;
            $request['created_at'] = FrozenTime::now();
            $request['created_by_id'] = $user_id;
            $request['updated_at'] = FrozenTime::now();
            $request['updated_by_id'] = $user_id;
            $report_format = $this->ReportFormatVariableValues->patchEntity($report_format, $request);
            $errors = $report_format->errors();

            if (empty($errors)) {
                if ($this->ReportFormatVariableValues->save($report_format)) {

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

        $this->set(compact('report_formats'));
    }

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

        $report_formats = $this->ReportFormats->find('list', [
            'keyField' => 'id',
            'valueField' => 'name'
        ])->where(['status'=>1])->toArray();

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

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

            $report_format = $this->ReportFormatVariableValues->patchEntity($report_format_variable, $this->request->data);

            $errors = $report_format->errors();

            $report_format['updated_at'] = FrozenTime::now();
            $report_format['updated_by'] = $user_id;
            if (empty($errors)) {
                if ($this->ReportFormatVariableValues->save($report_format)) {
                    $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);
        }

        $user_permissions = $this->request->session()->read('user_permissions');
        $this->set('user_permission', $user_permissions);
        $this->set('report_format_variable', $report_format_variable);
        $this->set('report_formats', $report_formats);
        $this->set('current_module', $this->getModules());
        $nav_arr[0] = array('action' => 'index', 'page_id' => '3045', 'icon' => '<i class="fa fa-list"></i>', 'label' => 'List');
        $this->set('nav_arr', $nav_arr);
        $this->viewBuilder()->layout("custom_layout");
    }

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

        $report_format = $this->ReportFormats->get($report_format_variable['report_format_id'], [
            'contain' => []
        ]);

        $this->set('report_format_variable', $report_format_variable);
        $this->set('report_format', $report_format);

        $user_permissions = $this->request->session()->read('user_permissions');
        $this->set('languages', $this->languages);
        $this->set('module_pages', $this->getPages());
        $this->set('user_permission', $user_permissions);
    }


}
