<?php
namespace App\Controller;

use Cake\I18n\FrozenTime;
use Cake\View\View;

/**
 * ReportFormat Controller
 *
 * @property \App\Model\Table\ReportFormatsTable $ReportFormats
 * @property \App\Model\Table\ReportFormatTypesTable $ReportFormatTypes
 * @property \App\Model\Table\ReportFormatVariablesTable $ReportFormatVariables
 */
class ReportFormatController extends AppsController
{
    private  $controller = "report-format";
    public function initialize(): void
    {
        parent::initialize();
        $this->loadModel('ReportFormats');
        $this->loadModel('ReportFormatTypes');
        $this->loadModel('ReportFormatVariables');
        $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' => [
                'ReportFormats.id' => 'asc'
            ]
        ];

        $search = '';
        if (isset($this->request->query['search'])) {
            $search = $this->request->query('search');
            $condition = '(name LIKE ' . "'%" . $search  . "%'" .')' ;
        }
        $report_formats = $this->ReportFormats->find('all')->where($condition);


        $this->set('report_formats', $this->paginate($report_formats));
        $this->set('searchText', $search);
        $this->set('page_limit', $page_limit);
        $nav_arr[0] = array('action' => 'add', 'page_id' => '10139', 'icon' => '<i class="fa fa-plus-circle"></i>', 'label' => 'Add');
        $this->set('nav_arr', $nav_arr);
    }

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

        if ($this->request->is('post')) {
            $user_id = $this->Auth->user('id');
            $report_format = $this->ReportFormats->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;
            $report_format = $this->ReportFormats->patchEntity($report_format, $request);
            $errors = $report_format->errors();

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

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

        $this->set(compact('report_format_types'));
        $this->setNextPage();
    }

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

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

        $report_format_variables = $this->ReportFormatVariables->find('all')->where(['report_format_id'=>$id]);
//dd($report_format['content']);
        if($report_format->report_format_type_id == 4 && empty($report_format['content'])){
            $report_format['content'] = $this->visaLetterTemplate("sfsfsf");
        }

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

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

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

            $errors = $report_format->errors();

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


        $this->set('report_format', $report_format);
        $this->set('report_format_types', $report_format_types);
        $this->set('report_format_variables', $report_format_variables);
        $this->setNextPage();
    }

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

        $report_format_type = $this->ReportFormatTypes->get($report_format['report_format_type_id'], [
            'contain' => []
        ]);

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

        $this->set([
            'response' => $report_format,
            '_serialize' => ['response']
        ]);
        $this->setNextPage();

    }

    public  function  visaLetterTemplate($content)
    {
        $view = new View($this->request, $this->response);
        return $view->element('letter_templates/visa_letter_template', compact('content'));

    }

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

}
