<?php
namespace App\Controller;

use Cake\I18n\FrozenTime;

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

        $search = '';
        if (isset($this->request->query['search'])) {
            $search = $this->request->query('search');
            $condition = '(variable_name LIKE ' . "'%" . $search  . "%' "
                . "OR data_type LIKE '%" . $search . "%' OR table_name LIKE '%" . $search . "%' OR column_name LIKE '%" . $search . "%'" .')' ;
        }
        $report_format_variables = $this->ReportFormatVariables->find('all', [
            'contain' => ['ReportFormats']
        ])->where($condition);

//        dd($report_format_variables->sql());

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

    }

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

        $variable_list = [];
        if($id != null){
            $variable_list = $this->getSchemaValue($id);
        }

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

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

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

    public function edit($id=null)
    {
        $report_format_variable = $this->ReportFormatVariables->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->ReportFormatVariables->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->ReportFormatVariables->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_variable', $report_format_variable);
        $this->set('report_formats', $report_formats);
        $this->setNextPage();
    }

    public function view($id)
    {
        $report_format_variable = $this->ReportFormatVariables->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);
        $this->setNextPage();
    }

    public function getSchemaValue($table_name){
        $this->loadModel($table_name);
        $schema = $this->$table_name->getSchema();
        $columns = $schema->columns();
        $column_data = [];
        foreach ($columns as $key => $value) {
            if(!in_array($value,AVOID_FROM_REPORT)) {
                $column_data[$value] = $value;
            }
        }

        return $column_data;

    }


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