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

class ControllerShippingJadlogtransport extends ControllerBaseModule
{

  protected $referencePathFile = 'shipping/jadlog_transport';

  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_ModJadlog']         = Type_JadlogModalidade::getEnumList();
    $this->dataVue['Type_JadlogEntrega']     = Type_JadlogEntrega::getEnumList();
    $this->dataVue['Type_JadlogSeguro']      = Type_JadlogSeguro::getEnumList();
    $this->dataVue['Type_DsDn']              = Type_DsDn::getEnumList();

    $token = $this->session->data['token'];

    $url_action = $this->url->link(
      'shipping/jadlog_transport/saveSetting',
      'token=' . $token . '&type=' . $this->request->get['type'],
      'SSL'
    );

    $url_back = $this->url->linkAnchor(
      'extension/module',
      'token=' . $token,
      'tab-info=' . $this->request->get['type'],
      'SSL'
    );

    $this->dataVue['data'] = [
      'configurations' => $this->model_setting_setting->getSetting('jadlog_transport'),
      'action' => str_replace('&amp;', '&', $url_action),
      'back' => str_replace('&amp;', '&', $url_back),
    ];

    $this->setDefaultValues();
  }

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

  private function setDefaultValues()
  {
    $arrayDefault = array(
      'jadlog_transport_sort_order'
    );

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

  public function saveSetting()
  {
    $this->loadLanguage();
    $this->removeMask();
    $response = [];

    if (!$this->user->hasPermission('modify', 'shipping/jadlog_transport')) {
      $response['warning'] = $this->language->get('error_permission');
      return $this->response->setOutputJson($response);
    }

    $validate = $this->validate();
    if ($validate) {
      $response['errors'] = $validate;
      return $this->response->setOutputJson($response);
    }

    $this->load->model('setting/setting');
    $this->model_setting_setting->editSetting('jadlog_transport', $this->request->post);

    $response['success'] = $this->language->get('text_success');

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

  public function removeMask() {

    $this->request->post['jadlog_transport_vcoleta'] = ftr_geral::removeMaskToDouble($this->request->post['jadlog_transport_vcoleta']);
    $this->request->post['jadlog_transport_cnpj']    = ftr_geral::removeMaskToInt($this->request->post['jadlog_transport_cnpj']);
    $this->request->post['jadlog_transport_cep']     = ftr_geral::removeMaskToInt($this->request->post['jadlog_transport_cep']);

  }  


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

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

    $errors = [];

    $errors = $this->validate->execute([
      'jadlog_transport_status' => [
        'value' => $jadlog_transport_status,
        'validates' => ['isEmpty', 'isEnabledOrDisabled']
      ],
      'jadlog_transport_sort_order' => [
        'value' => $jadlog_transport_sort_order,
        'validates' => ['isIntegerNumber']
      ],
    ]);

    return $errors;
  }
}
