<?php
namespace App\Controller;

use Cake\I18n\FrozenTime;

/**
 * ReportFormatGenerationHistory Controller
 *
 * @property \App\Model\Table\ReportFormatGenerationHistoriesTable $ReportFormatGenerationHistories
 * @property \App\Model\Table\ReportFormatsTable $ReportFormats
 */
class ReportFormatGenerationHistoryController extends AppsController
{
    private  $controller = "report-format-generation-history";
    public function initialize(): void
    {
        parent::initialize();
        $this->loadModel('ReportFormatGenerationHistories');
        $this->loadModel('ReportFormats');
        $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' => [
                'ReportFormatGenerationHistories.id' => 'ASC'
            ]
        ];

        $search = '';
        $report_format_histories = $this->ReportFormatGenerationHistories->find('all');


        $this->set('report_format_histories', $this->paginate($report_format_histories));
        $this->set('searchText', $search);
        $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->ReportFormatGenerationHistories->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->ReportFormatGenerationHistories->patchEntity($report_format, $request);
            $errors = $report_format->errors();
            if (empty($errors)) {
                if ($this->ReportFormatGenerationHistories->save($report_format)) {

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

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

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

        $report_format = $this->ReportFormats->get($report_format_generation_history['report_format_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->ReportFormatGenerationHistories->patchEntity($report_format_generation_history, $this->request->data);

            $errors = $report_format->errors();

            $report_format['updated_at'] = FrozenTime::now();
            $report_format['updated_by'] = $user_id;
            if (empty($errors)) {
                if ($this->ReportFormatGenerationHistories->save($report_format)) {
                    $this->Flash->success(__('The report format generation history 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_generation_history', $report_format_generation_history);
        $this->set('report_formats', $report_formats);
        $this->set('report_format', $report_format);
    }

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

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

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

    }


}
