<?php
class ControllerShippingJadLog extends Controller {
	public function index() {
		$language = $this->load->language('shipping/jadlog');
    $this->data = array_merge($this->data, $language);

    $this->document->setTitle($this->language->get('heading_title'));
    $this->document->addScript('admin/public/js/shipping/jadlog.js');

    $this->load->model('setting/setting');
    $this->load->helper('ftr_geral');
    $this->load->model('localisation/geo_zone');
    $this->load->type('tipocomum');

    $this->data['Type_ModJadlog'] = Type_ModJadlog::getEnumList();
    $this->data['Type_JadLogPeso'] = Type_JadLogPeso::getEnumList();
		$this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

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

    $this->data['breadcrumbs'] = [
      [
	      'text' => $this->language->get('text_home'),
				'href' => $this->url->link('common/home', 'token=' . $token, 'SSL'),
      ],
      [
        'text' => $this->language->get('text_shipping'),
        'href' => $this->url->linkAnchor('extension/module', 'token=' . $token, 'tab-info=' . $this->request->get['type'], 'SSL'),
      ],
      [
        'text' => $this->language->get('heading_title'),
        'href' => $this->url->link('shipping/jadlog', 'token=' . $token . '&type=' . $this->request->get['type'], 'SSL'),
      ]
    ];

    $url_action = $this->url->link(
      'shipping/jadlog/save',
      '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->data['data'] = [
      'configurations' => $this->model_setting_setting->getSetting('jadlog'),
      'action' => str_replace('&amp;', '&', $url_action),
      'back' => str_replace('&amp;', '&', $url_back),
    ];

		$this->children = [
			'common/header',
			'common/footer'
    ];

		$this->response->setOutput($this->render('shipping/jadlog'));
  }

  public function save() {
    $response = array();

    if (!$this->user->hasPermission('modify', 'shipping/jadlog')) {
      $response['errors']['warning'] = 'Acesso negado!';
      return $this->response->setOutputJson($response);
		}

    if (!$this->validate()) {
      $this->load->model('setting/setting');

      $this->removeMask();
      $this->model_setting_setting->editSetting('jadlog', $this->request->post);

      $this->session->data['success'] = $this->language->get('text_success');
      $response['success'] = $this->language->get('text_success');
    } else {
      $response['errors'] = $this->validate();
    }

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

  private function removeMask() {
    $this->request->post['jadlog_taxa'] = ftr_geral::removeMaskToDouble($this->request->post['jadlog_taxa']);
    $this->request->post['jadlog_cep'] = ftr_geral::removeMaskToInt($this->request->post['jadlog_cep']);
    $this->request->post['jadlog_cnpj'] = ftr_geral::removeMaskToInt($this->request->post['jadlog_cnpj']);
  }

  private function validate() {
    extract($this->request->post);

    return $this->validate->execute([
      'jadlog_nome' => [
        'value' => $jadlog_nome,
        'validates' => ['isEmpty'],
      ],
      'jadlog_senha' => [
        'value' => $jadlog_senha,
        'validates' => ['isEmpty'],
      ],
      'jadlog_cnpj' => [
        'value' => $jadlog_cnpj,
        'validates' => ['isCnpj'],
      ],
      'jadlog_cep' => [
        'value' => $jadlog_cep,
        'validates' => ['isCep'],
      ],
      'jadlog_mod' => [
        'value' => $jadlog_mod,
        'validates' => ['isEmpty'],
      ],
    ]);
  }
}
