<?php

loadcontroller('base/module');

class ControllerShippingItem extends ControllerBaseModule {

    protected $referencePathFile = 'shipping/item';

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

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

        $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['configurations'] = $this->model_setting_setting->getSetting('item');
        $this->setDefaultValues();
    }

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

    private function setDefaultValues() {
        $arrayDefault = array('item_prazo_extra', 'item_cost');

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

    public function saveSetting() {
        $this->loadLanguage();

        $response = [];

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

        $this->errors = $this->validate();
        if (!$this->errors) {
            //setando tokens pra não apagar com o post
 
            if (!isset($this->request->post['item_cost']) or empty($this->request->post['item_cost'])) {
                $this->request->post['item_cost'] = 0;
            }

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

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

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

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

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

        $errors = [];

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

        return $errors;
    }

    public function install() {
        $this->load->model('setting/setting');

        $this->model_setting_setting->editSetting('item', $itemAuthInfo);
    }

}
