<?php
loadcontroller('base/module');

class ControllerModuleProcessoVenda extends ControllerBaseModule
{
    protected $referencePathFile = 'module/processo_venda';

    private $error = array();

    protected function loadData()
    {
        $this->dataVue['TypesForJs'] = array (
            Type_DsimDnao::getTypeForJs(),
        );

        $this->dataVue['configurations'] = $this->model_setting_setting->getSetting('module_processo_venda');


        $this->dataVue['order_statuses'] = array();
        $this->load->model('localisation/order_status');
        $order_statuses = $this->model_localisation_order_status->getOrderStatuses();
        foreach ($order_statuses as $order_status) {
        $this->dataVue['order_statuses'][] = array(
            'order_status_id' => $order_status['order_status_id'],
            'name'            => $order_status['name']
        );
        }

        $this->load->dmodel('wsceo/pedido/wsprocessosvenda');

        $processosVenda = $this->dmodel_wsceo_pedido_wsprocessosvenda->getProcessosVenda();
        if (is_object($processosVenda)) {
            $this->dataVue['error_ceo'] = $processosVenda;
        } else {
            if (is_array($processosVenda)){
                $this->dataVue['processosVenda'] = array ();
                foreach ($processosVenda as $key => $processoVenda) {
                    $this->dataVue['processosVenda'][] = array(
                    'id_processo_ecommerce'  => $processoVenda['id_processo_ecommerce'],
                    'razao_social' => $processoVenda['razao_social'],
                    'id_empresa' => $processoVenda['id_empresa'],
                    'select' => (ftr_geral::isIsset($this->config->get('processo_venda_id')) && ($this->config->get('processo_venda_id') == $processoVenda['id_processo_ecommerce'])) ? true : false
                    );
                }
            }else{
                if($processosVenda == Type_ServidorStatus::Tp_ErrorServer){
                    $this->dataVue['processosVenda']	= $this->language->get('error_ceo_status_server');
                }else{
                    $this->dataVue['processosVenda']	= $this->language->get('error_ceo_processos_venda');
                }
            }
        }

    }

    protected function breadcrumbs()
    {
         //Quando o usuario tem somente marketplace 
        $showBreadcrumb = !$this->config->get('config_sistema_ecommerce') && $this->config->get('config_sistema_marketplace');

        $type = $this->request->get['type'];

        $this->dataVue['breadcrumbs'] = $showBreadcrumb  ? [
            [
                'href' => $this->url->link('common/home', 'token=' . $this->token, 'SSL'),
                'text' => $this->language->get('text_home'),
            ],
            [
                'href' => $this->url->link($this->referencePathFile, 'token=' . $this->token . '&type=' . $type, 'SSL'),
                'text' => $this->language->get('heading_title'),
            ]
        ] : [
            [
                '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 validate()
    {
        $errors = [];

        $errors = $this->validate->execute([
        'processo_venda_id' => [
            'value' => $this->request->post['processo_venda_id'],
            'validates' => ['isEmpty'],
        ],
        'processo_venda_status' => [
            'value' => $this->request->post['processo_venda_status'],
            'validates' => ['isEmpty'],
        ],
        ]);

        if ($this->request->post['processo_venda_status'] == 1) {
            $errors = array_merge($errors, $this->validate->execute([
                'processo_venda_order_status_started' => [
                    'value' => $this->request->post['processo_venda_order_status_started'],
                    'validates' => ['isEmpty'],
                ],
            ]));
        } else {

        }

        return $errors;
    }

    public function saveSetting() {
        $this->post = $this->request->post;

        $this->model_setting_setting->editSetting('module_processo_venda', $this->post);
    }
}
