<?php
loadcontroller('base/base_controller');
abstract class ControllerInsertList extends BaseController
{
    protected $arrayJson = [];
    protected $fieldsToRemoveMask = [];
    protected $settings = [];

    protected function loadScript()
    {
        $this->document->addScript("admin/public/js/$this->referencePathFile.js");

    }

    protected function breadcrumbs()
    {
        $type = $this->request->get['type'];

        $this->dataVue['breadcrumbs'] = [
            [
                'href' => $this->url->link('common/home', 'token=' . $this->token, 'SSL'),
                'text' => $this->language->get('text_home'),
            ],
            [
                'href' => $this->url->linkAnchor('extension/module', 'token=' . $this->token, 'tab-info=' . $type, 'SSL'),
                'text' => $this->language->get('text_' . $this->pathName),
            ],
            [
                'href' => $this->url->link($this->referencePathFile, 'token=' . $this->token . '&type=' . $type, 'SSL'),
                'text' => $this->language->get('heading_title'),
            ]
        ];
    }

    protected function actionUrls()
    {
        $this->dataVue['actionFilter']  = $this->url->link("$this->referencePathFile/filter", "token=" . $this->session->data['token'] , "SSL");
        $this->dataVue['actionDelete']  = $this->url->link("$this->referencePathFile/delete", "token=" . $this->session->data['token'] , "SSL");
        $this->dataVue['actionSave']    = $this->url->link("$this->referencePathFile/save", "token=" . $this->session->data['token'] , "SSL");

    }


    protected function loadData()
    {
        $this->dataVue['configurations'] = $this->model_setting_setting->getSetting($this->fileName);
        $this->dataVue['configurations']['bInsert'] = false;

    }

    public function index($settings = [])
    {

        $this->settings = $settings;

        $this->loadLanguage();
        $this->breadcrumbs();

        $this->document->setTitle($this->language->get('heading_title'));
        $this->loadScript();

        $this->loadModel();

        $this->loadController();

        $this->loadData();

        $this->childrens();

        $this->actionUrls();

        $this->response->setOutput($this->render($this->referencePathFile));
    }

    protected function saveSetting()
    {
        $this->load->model('setting/setting');
        $this->model_setting_setting->editSetting($this->fileName, $this->request->post, 0, $this->arrayJson);
    }

    protected function saveTable()
    {
        // Não implementar aqui, opcional implementar nos filhos
    }

    public function save()
    {
        $this->loadLanguage();
        $this->loadModel();

        $this->hasPermission();

        $this->errors = $this->validate();

        if (!$this->errors) {
            $this->removeMask();
            $this->removeMaskExec();

            $this->saveSetting();
            $this->saveTable();

            $this->session->data['success'] = $this->language->get('text_success');
            $this->responseJson['success'] = $this->language->get('text_success');
        } else {
            $this->responseJson['errors'] = $this->errors;
        }

        $this->response->setOutputJson($this->responseJson);
    }


    protected function delete()
    {
    // Não implementar aqui, opcional implementar nos filhos
    }

    protected function filter(){

    }

    protected function renderPagination($total,$page = 0){

        $pagination = new Pagination();
        $pagination->total = $total;
        $pagination->page = $page != null ? $page: 0;
        $pagination->limit = $this->config->get('config_admin_limit');
        $pagination->text = $this->language->get('text_pagination');
        $pagination->url = $this->url->link($this->referencePathFile, 'token=' . $this->session->data['token']  . '&page={page}', 'SSL');

        return $pagination->render(true,true);

    }

}
