<?php
class ControllerModuleSpecialAction extends Controller {
	private $error = array();

	public function index() {
    $language = $this->language->load('module/special_action');

    $this->data = array_merge($this->data, $language);

    $this->data['languageForJS'] = array(
      'module/special_action' => $language
    );
		$this->load->type('tipocomum');

    $this->document->addTypesForJs(array(
      Type_DsimDnao::getTypeForJs(),
      Type_Status::getTypeForJs(),
      Type_ActionPromotionStyle::getTypeForJs(),
      Type_ModulePosition::getTypeForJs()
    ));

    $this->document->addScript('admin/public/js/module/special-action.js');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('setting/setting');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('special_action', $this->request->post);

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

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

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_content_top'] = $this->language->get('text_content_top');
		$this->data['text_content_bottom'] = $this->language->get('text_content_bottom');
		$this->data['text_column_left'] = $this->language->get('text_column_left');
		$this->data['text_column_right'] = $this->language->get('text_column_right');
		$this->data['text_title'] = $this->language->get('text_title');

		$this->data['entry_product'] = $this->language->get('entry_product');
		$this->data['entry_limit'] = $this->language->get('entry_limit');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_layout'] = $this->language->get('entry_layout');
		$this->data['entry_position'] = $this->language->get('entry_position');
		$this->data['entry_style'] = $this->language->get('entry_style');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');

		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_module'] = $this->language->get('button_add_module');
		$this->data['button_remove'] = $this->language->get('button_remove');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->error['image'])) {
			$this->data['error_image'] = $this->error['image'];
		} else {
			$this->data['error_image'] = array();
		}

		if (isset($this->error['limit'])) {
			$this->data['error_limit'] = $this->error['limit'];
		} else {
			$this->data['error_limit'] = array();
		}

		if (isset($this->error['sort_order'])) {
			$this->data['error_sort_order'] = $this->error['sort_order'];
		} else {
			$this->data['error_sort_order'] = array();
		}

		$this->data['breadcrumbs'] = array();

 		$this->data['breadcrumbs'][] = array(
     		'text'      => $this->language->get('text_home'),
				'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
    		'separator' => false
 		);

 		$this->data['breadcrumbs'][] = array(
     		'text'      => $this->language->get('text_module'),
				'href'      => $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'),
    		'separator' => ' :: '
 		);

 		$this->data['breadcrumbs'][] = array(
     		'text'      => $this->language->get('heading_title'),
				'href'      => $this->url->link('module/special_action', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL'),
    		'separator' => ' :: '
 		);

		$this->data['action'] = $this->url->link('module/special_action', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL');

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

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

		if (isset($this->request->post['special_action_item'])) {
			$this->data['special_action_item'] = $this->request->post['special_action_item'];
		} else {
			$this->data['special_action_item'] = $this->config->get('special_action_item');
		}

		if(isset($this->request->post['special_action_title'])){
				$this->data['special_action_title'] = $this->request->post['special_action_title'];
		}else{
				$this->data['special_action_title'] = $this->config->get('special_action_title');
		}

		if (isset($this->request->post['special_action_item'])) {
			$special_actions = explode(',', $this->request->post['special_action_item']);
		} else {
			$special_actions = explode(',', $this->config->get('special_action_item'));
		}

		$this->load->model('catalog/special_action');

		$this->data['special_actions'] = array();

		foreach ($special_actions as $special_action) {
			if($special_action){
				$special_info = $this->model_catalog_special_action->getSpecialAction($special_action);

				if ($special_info) {
					$this->data['special_actions'][] = array(
						'order_promotion_id' => $special_info['order_promotion_id'],
						'description'        => $special_info['description']
					);
				}
			}
		}

		$this->data['modules'] = array();

		if (isset($this->request->post['special_action_module'])) {
			$this->data['modules'] = $this->request->post['special_action_module'];
		} elseif ($this->config->get('special_action_module')) {
			$this->data['modules'] = $this->config->get('special_action_module');
		}

		$this->load->dmodel('design/layout');

		// Remove as opções de layout que não são usadas nos módulos de lista de produtos
		$data = array(
			'notarray' => Type_NotListaProdutos::getEnumList()
		);

		$this->data['layouts'] = $this->dmodel_design_layout->getLayouts($data);

    $this->data['positions'] = Type_ModulePosition::getRemoveArray([Type_ModulePosition::Tp_ColumnRight, Type_ModulePosition::Tp_ColumnLeft], true);

		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render('module/special_action'));
	}

	protected function validate() {
		if (!$this->user->hasPermission('modify', 'module/special_action')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (isset($this->request->post['special_action_module'])) {
      $layoutArray = array(
        'layout' => array(),
        'layout_app' => array()
      );
      $layoutArrayApp = array();
		foreach ($this->request->post['special_action_module'] as $key => $value) {
        if ($value['position'] == Type_ModulePosition::Tp_AppMobile) {
          $layoutArray['layout_app'][$key] = $value['layout_id'];
        } else {
          $layoutArray['layout'][$key] = $value['layout_id'];
        }

        if (isset($value['limit'])) {
          if ($value['limit'] > Type_DefaultValues::Tp_LimitModules99) {
            $this->error['limit'][$key] = $this->language->get('error_limit');
          }
        }

				if($value['sort_order']==""){
					$this->error['sort_order'][$key]	= $this->language->get('error_sort_order');
				}

			}
      foreach ($layoutArray as $key => $value) {
        $validalayoutArray = array_unique($value);
  			if(count($validalayoutArray) != count($value)) {
  				$this->error['warning'] = $this->language->get('error_layout');
  			}
      }
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}


	public function autocomplete() {
		$json = array();

		$this->load->helper('ftr_geral');
		$this->load->type('tipocomum');

		if (isset($this->request->get['filter_special'])) {

			$this->load->model('catalog/special_action');

			if (isset($this->request->get['filter_special'])) {
				$filter_special = $this->request->get['filter_special'];
			} else {
				$filter_special = '';
			}

			if (isset($this->request->get['limit'])) {
				$limit = $this->request->get['limit'];
			} else {
				$limit = 20;
			}

			$data = array(
				'filter_special'   => $filter_special,
				'start'            => 0,
				'limit'            => $limit
			);

			$data = ftr_geral::ConvertCaracter($data);

			$results = $this->model_catalog_special_action->getSpecialActions($data);
			foreach ($results as $result) {
				$json[] = array(
					'order_promotion_id' => $result['order_promotion_id'],
					'description'     => strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))
				);
			}

		}

		$this->response->setOutput(json_encode($json));

	}

}
