<?php
loadcontroller('base/base_controller');
abstract class ControllerBaseModule 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()
    {
        $type = $this->request->get['type'];

        $this->dataVue['action'] = $this->url->link(
            "$this->referencePathFile/save",
            'token=' . $this->token . '&type=' . $type,
            'SSL'
        );

        $this->dataVue['back'] = $this->url->linkAnchor(
            'extension/module',
            'token=' . $this->token,
            'tab-info=' . $type,
            'SSL'
        );
    }


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

    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);
    }
}
 