<?php

loadController('base/module');

class ControllerShippingIntelipost extends ControllerBaseModule  {

	protected $referencePathFile = 'shipping/intelipost';

	protected $fieldsToRemoveMask = [
		'intelipost_valor_extra' => 'removeMaskToDouble'
	];

	protected function loadData() 
	{
		$this->load->type('tipocomum');
		$this->load->type('tipofrete');

		$this->dataVue['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
        $this->dataVue['Type_Status'] = Type_Status::getEnumList();
        $this->dataVue['Type_DsimDnao'] = Type_DsimDnao::getEnumList();
        $this->dataVue['Type_AcrescimoDesconto'] = Type_AcrescimoDesconto::getEnumList();
        $this->dataVue['Type_TipoCotacao'] = Type_IntelipostTipoCote::getEnumList();
		$this->dataVue['configurations'] = $this->model_setting_setting->getSetting('intelipost');

		$this->setDefaultValues();
	}

	protected function loadModel() 
	{
		$this->load->model('setting/setting');
		$this->load->model('localisation/geo_zone');
	}

	private function setDefaultValues()
	{
		$arrayDefault = array('intelipost_prazo_extra', 'intelipost_valor_extra');

        foreach ($arrayDefault as $value) {
            if (!in_array($value, array_keys($this->dataVue['configurations']))) {
                $this->dataVue['configurations'][$value] = "0";
            }
        }
	}

	protected function validate() 
	{
		$this->loadLanguage();
		$this->load->type('tipocomum');

		extract($this->request->post);

		$errors = [];

		$errors = $this->validate->execute([
			'intelipost_descricao' => [
				'value' => $intelipost_descricao,
				'validates' =>['isEmpty']
			],
			'intelipost_api_key' => [
				'value' => $intelipost_api_key,
				'validates' =>['isEmpty']
			],
			'intelipost_cep_origem' => [
				'value' => $intelipost_cep_origem,
				'validates' =>['isEmpty', 'isCep']
			],
			'intelipost_valor_extra' => [
				'value' => $intelipost_valor_extra,
				'validates' =>['isEmpty']
			],
			'intelipost_prazo_extra' => [
				'value' => $intelipost_prazo_extra,
				'validates' =>['isEmpty']
			],
			'intelipost_tipo_cotacao' => [
				'value' => $intelipost_tipo_cotacao,
				'validates' =>['isEmpty']
			],
			'intelipost_tipo_valor_extra' => [
				'value' => $intelipost_tipo_valor_extra,
				'validates' =>['isEmpty']
			],
			'intelipost_sort_order' => [
				'value' => $intelipost_sort_order,
				'validates' =>['isIntegerNumber']
			],
			'intelipost_geo_zone_id' => [
				'value' => $intelipost_geo_zone_id,
				'validates' =>['isEmpty']
			],
			'intelipost_status' => [
				'value' => $intelipost_status,
				'validates' =>['isEmpty', 'isEnabledOrDisabled']
			],
		]);
		
		return $errors;
	}
}
