<?php

class ControllerModuleListaescolar extends Controller
{

    private $error = array();

    public function index()
    {
        $language = $this->language->load('module/listaescolar');
        $this->data = array_merge($this->data, $language);
        $this->document->setTitle($this->language->get('heading_title'));
        $this->language->load('setting/setting');
        $this->load->model('setting/setting');
        $this->load->model('setting/efutura');
        $this->load->helper('ftr_geral');
        $this->load->model('design/banner');
        $this->load->dmodel('listaescolar/listaescolar');
        $this->load->dmodel('listaescolar/listaescolar');
        $this->load->dmodel('design/layout');

        $this->data['school_layout'] = $this->dmodel_design_layout->getLayoutByName('lista escolar produtos');
        $this->data['banners'] = $this->model_design_banner->getBanners();

        $escolas = $this->dmodel_listaescolar_listaescolar->getEscolas();
        $this->data['escolas'] = array();

        if ($escolas) {
            foreach ($escolas as $key => $escola) {
                $this->data['escolas'][] = array(
                    'school_id' => $escola['id'],
                    'descricao' => $escola['descricao']
                );
            }
        }

        $this->data = array_merge($this->data, $language);

        $this->data['languageForJS'] = array(
            'module/banner' => $language
        );

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $this->model_setting_setting->editSetting('listaescolar', $this->request->post);

            if ($this->request->post['listaescolar_dominio']) {
                $dominios = array(
                    'dominio' => $this->config->get('config_linkproducao_ssl'),
                    'dominio_lista_escolar' => $this->request->post['listaescolar_dominio']
                );
            }

            $this->session->data['success'] = $this->language->get('text_success');
            $this->redirect($this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'));
        }

        $this->document->addScript(array(
            'view/javascript/module.listaescolar.js'
        ));

        $this->document->addTypesForJs(array(
            Type_Tamanho::getTypeForJs(),
            Type_DsimDnao::getTypeForJs(),
            TypeProduct_Status::getTypeForJs(),
            Type_ModulePosition::getTypeForJs(),
        ));

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_module'),
            'href' => $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('module/listaescolar', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL'),
            'separator' => ' :: '
        );

        if (isset($this->request->post['listaescolar_status'])) {
            $this->data['listaescolar_status'] = $this->request->post['listaescolar_status'];
        } else {
            $this->data['listaescolar_status'] = $this->config->get('listaescolar_status');
        }

        if (isset($this->request->post['listaescolar_exibe_menu'])) {
            $this->data['listaescolar_exibe_menu'] = $this->request->post['listaescolar_exibe_menu'];
        } else {
            $this->data['listaescolar_exibe_menu'] = $this->config->get('listaescolar_exibe_menu');
        }

        if (isset($this->request->post['listaescolar_exibir_descricao'])) {
            $this->data['listaescolar_exibir_descricao'] = $this->request->post['listaescolar_exibir_descricao'];
        } else {
            $this->data['listaescolar_exibir_descricao'] = $this->config->get('listaescolar_exibir_descricao');
        }

        if (isset($this->request->post['listaescolar_disable_similar_item'])) {
            $this->data['listaescolar_disable_similar_item'] = $this->request->post['listaescolar_disable_similar_item'];
        } else {
            $this->data['listaescolar_disable_similar_item'] = $this->config->get('listaescolar_disable_similar_item');
        }

        if (isset($this->request->post['listaescolar_disable_substitute_item'])) {
            $this->data['listaescolar_disable_substitute_item'] = $this->request->post['listaescolar_disable_substitute_item'];
        } else {
            $this->data['listaescolar_disable_substitute_item'] = $this->config->get('listaescolar_disable_substitute_item');
        }

        if (isset($this->request->post['listaescolar_pesquisa_gruposubgrupo'])) {
            $this->data['listaescolar_pesquisa_gruposubgrupo'] = $this->request->post['listaescolar_pesquisa_gruposubgrupo'];
        } else {
            $this->data['listaescolar_pesquisa_gruposubgrupo'] = $this->config->get('listaescolar_pesquisa_gruposubgrupo');
        }

        if (isset($this->request->post['listaescolar_pesquisa_tag'])) {
            $this->data['listaescolar_pesquisa_tag'] = $this->request->post['listaescolar_pesquisa_tag'];
        } else {
            $this->data['listaescolar_pesquisa_tag'] = $this->config->get('listaescolar_pesquisa_tag');
        }

        if (isset($this->request->post['listaescolar_dominio'])) {
            $this->data['listaescolar_dominio'] = $this->request->post['listaescolar_dominio'];
        } else {
            $this->data['listaescolar_dominio'] = $this->config->get('listaescolar_dominio');
        }

        if (isset($this->request->post['listaescolar_ordenacao'])) {
            $this->data['listaescolar_ordenacao'] = $this->request->post['listaescolar_ordenacao'];
        } else {
            $this->data['listaescolar_ordenacao'] = $this->config->get('listaescolar_ordenacao');
        }

        if (isset($this->request->post['listaescolar_redirect_checkout'])) {
            $this->data['listaescolar_redirect_checkout'] = $this->request->post['listaescolar_redirect_checkout'];
        } else {
            $this->data['listaescolar_redirect_checkout'] = $this->config->get('listaescolar_redirect_checkout');
        }

        if (isset($this->request->post['listaescolar_cart_obs'])) {
            $this->data['listaescolar_cart_obs'] = $this->request->post['listaescolar_cart_obs'];
        } else {
            $this->data['listaescolar_cart_obs'] = $this->config->get('listaescolar_cart_obs');
        }

        if (isset($this->request->post['listaescolar_cart_obs_is_required'])) {
            $this->data['listaescolar_cart_obs_is_required'] = $this->request->post['listaescolar_cart_obs_is_required'];
        } else {
            $this->data['listaescolar_cart_obs_is_required'] = $this->config->get('listaescolar_cart_obs_is_required');
        }

        if (isset($this->request->post['listaescolar_cart_obs_title'])) {
            $this->data['listaescolar_cart_obs_title'] = $this->request->post['listaescolar_cart_obs_title'];
        } else {
            $this->data['listaescolar_cart_obs_title'] = $this->config->get('listaescolar_cart_obs_title');
        }

        if (isset($this->request->post['listaescolar_substitute_item_table_price'])) {
            $this->data['listaescolar_substitute_item_table_price'] = $this->request->post['listaescolar_substitute_item_table_price'];
        } else {
            $this->data['listaescolar_substitute_item_table_price'] = $this->config->get('listaescolar_substitute_item_table_price');
        }

        foreach (Type_OrdenacaoProductsListaEscolar::getEnumList() as $key => $value) {
            $this->data['ordenacao_lista'][] = array(
                'key' => $key,
                'selected' => ftr_geral::isSelected($key, $this->data['listaescolar_ordenacao']),
                'value' => $value
            );
        }

        $dominiosDisponiveis = $this->model_setting_efutura->getDominiosDisponiveis();
        if ($dominiosDisponiveis) {
            foreach ($dominiosDisponiveis as $dominioDisponivel) {
                $this->data['dominio_list'][] = array(
                    'selected' => ftr_geral::isSelected($dominioDisponivel['dominio'], $this->data['listaescolar_dominio']),
                    'value' => $dominioDisponivel['dominio']
                );
            }
        }

        foreach (Type_Status::getEnumList() as $key => $value) {
            $this->data['status_list'][] = array(
                'key' => $key,
                'selected' => ftr_geral::isSelected($key, $this->data['listaescolar_status']),
                'value' => $value
            );
        }

        foreach (Type_Status::getEnumList() as $key => $value) {
            $this->data['status_menu_list'][] = array(
                'key' => $key,
                'selected' => ftr_geral::isSelected($key, $this->data['listaescolar_exibe_menu']),
                'value' => $value
            );
        }

        foreach (Type_Status::getEnumList() as $key => $value) {
            $this->data['status_descricao_list'][] = array(
                'key' => $key,
                'selected' => ftr_geral::isSelected($key, $this->data['listaescolar_exibir_descricao']),
                'value' => $value
            );
        }

        foreach (Type_Status::getEnumList() as $key => $value) {
            $this->data['status_add_similar'][] = array(
                'key' => $key,
                'selected' => ftr_geral::isSelected($key, $this->data['listaescolar_disable_similar_item'], Type_Status::Tp_Habilitado),
                'value' => $value
            );
        }

        foreach (Type_Status::getEnumList() as $key => $value) {
            $this->data['status_substitute_item'][] = array(
                'key' => $key,
                'selected' => ftr_geral::isSelected($key, $this->data['listaescolar_disable_substitute_item'], Type_Status::Tp_Habilitado),
                'value' => $value
            );
        }

        foreach (Type_Status::getEnumList() as $key => $value) {
            $this->data['redirect_to_checkout_list'][] = array(
                'key' => $key,
                'selected' => ftr_geral::isSelected($key, $this->data['listaescolar_redirect_checkout'] ?? '', Type_Status::Tp_Desabilitado),
                'value' => $value
            );
        }

        foreach (Type_Status::getEnumList() as $key => $value) {
            $this->data['status_cart_obs_list'][] = array(
                'key' => $key,
                'selected' => ftr_geral::isSelected($key, $this->data['listaescolar_cart_obs'] ?? '', Type_Status::Tp_Desabilitado),
                'value' => $value
            );
        }

        foreach (Type_Status::getEnumList() as $key => $value) {
            $this->data['status_cart_obs_is_required'][] = array(
                'key' => $key,
                'selected' => ftr_geral::isSelected($key, $this->data['listaescolar_cart_obs_is_required'] ?? '', Type_Status::Tp_Desabilitado),
                'value' => $value
            );
        }

        foreach (Type_Status::getEnumList() as $key => $value) {
            $this->data['status_substitute_item_table_price'][] = array(
                'key' => $key,
                'selected' => ftr_geral::isSelected($key, $this->data['listaescolar_substitute_item_table_price'] ?? '', Type_Status::Tp_Desabilitado),
                'value' => $value
            );
        }

        if (isset($this->error['error_pesquisa_similar'])) {
            $this->data['error_pesquisa_similar'] = $this->error['error_pesquisa_similar'];
        } else {
            $this->data['error_pesquisa_similar'] = '';
        }

        $this->data['link_listaescolar'] = $this->urlloja->link('listaescolar/listaescolar');
        $this->data['action'] = $this->url->link('module/listaescolar', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL');
        $this->data['cancel'] = $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL');

        $this->template = 'module/listaescolar.twig';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->data['listaescolar_module'] = array();
        if (isset($this->request->post['listaescolar_module'])) {
            $this->data['listaescolar_module'] = $this->request->post['listaescolar_module'];
        } elseif ($this->config->get('listaescolar_module')) {
            $this->data['listaescolar_module'] = $this->config->get('listaescolar_module');
        }

        $banner_position = Type_ModulePosition::getRemoveArray(
            [
                Type_ModulePosition::Tp_ColumnLeft,
                Type_ModulePosition::Tp_AppMobile
            ],
            true
        );

        foreach ($this->data['listaescolar_module'] as $key => $module) {

            $this->data['listaescolar_module'][$key]['size'] = ftr_geral::setTypeInArray([
                'type' => Type_Tamanho::getEnumList(),
                'compare' => isset($module['tamanho']) ? $module['tamanho'] : null,
                'compare_type' => 'selected',
                'default' => Type_Tamanho::Tp_Pequeno
            ]);

            $this->data['listaescolar_module'][$key]['type'] = ftr_geral::setTypeInArray([
                'type' => Type_DsimDnao::getEnumList(),
                'compare' => isset($module['type']) ? $module['type'] : null,
                'compare_type' => 'selected',
                'default' => Type_DsimDnao::Tp_Nao
            ]);

            $this->data['listaescolar_module'][$key]['status'] = ftr_geral::setTypeInArray([
                'type' => TypeProduct_Status::getEnumList(),
                'compare' => $module['status'],
                'compare_type' => 'selected',
                'default' => TypeProduct_Status::Tp_Desabilitado
            ]);

            $this->data['listaescolar_module'][$key]['position'] = ftr_geral::setTypeInArray([
                'type' => $banner_position,
                'compare' => $module['position'],
                'compare_type' => 'selected',
            ]);
        }

        $this->response->setOutput($this->render('module/listaescolar'));
    }


    protected function validate()
    {
        if (!$this->user->hasPermission('modify', 'module/listaescolar')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        $banners = isset($this->request->post['listaescolar_module']) ? $this->request->post['listaescolar_module'] : [];
        $errors = [];
        $targetIsDuplicatedObject = '';

        foreach ($banners as $id => $banner) {
            $error = $this->validate->execute([
                'listaescolar_module[' . $id . '][width]' => [
                    'value' => $banner['width'],
                    'validates' => ['isEmpty'],
                ],
                'listaescolar_module[' . $id . '][height]' => [
                    'value' => $banner['height'],
                    'validates' => ['isEmpty'],
                ],
            ]);
            if ($error) {
                $this->error['warning'] = $this->language->get('error_dimension');
            }

        }
        $listaModulos = [];

        foreach ($banners as $key => $value) {
            if ($value['status'] == Type_Status::Tp_Habilitado)
                array_push($listaModulos, $value);
        }

        $targetIsDuplicatedObject = $this->validate->targetIsDuplicatedObject(
            $listaModulos,
            $this->language->get('error_duplicate'),
            ["school_id", "banner_id"]
        );

        if ($targetIsDuplicatedObject) {
            $this->error['warning'] = $targetIsDuplicatedObject;
        }

        if (
            (!isset($this->request->post['listaescolar_pesquisa_gruposubgrupo']) && !isset($this->request->post['listaescolar_pesquisa_tag']))
            || ((isset($this->request->post['listaescolar_pesquisa_gruposubgrupo']) && !$this->request->post['listaescolar_pesquisa_gruposubgrupo'])
                && (isset($this->request->post['listaescolar_pesquisa_tag']) && !$this->request->post['listaescolar_pesquisa_tag']))
        ) {
            $this->error['warning'] = $this->language->get('error_pesquisa_similar');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }
}

?>