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

	public function index() {
		$this->language->load('tool/error_log_mercadolivre');

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

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

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

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

  		$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('heading_title'),
			'href'      => $this->url->link('tool/error_log_mercadolivre', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);

		$this->data['clear'] = $this->url->link('tool/error_log_mercadolivre/clear', 'token=' . $this->session->data['token'], 'SSL');

		$file = DIR_LOGS . date('Y-m-d').'_'.'errorMercadoLivre.txt';

		if (isset($this->session->data['log_filtro_ini'])){
			$this->data['data_log_inicio'] = $this->session->data['log_filtro_ini'];
			unset($this->session->data['log_filtro_ini']);
		}else{
			$this->data['data_log_inicio'] = date('d/m/Y');
		}
		if (isset($this->session->data['log_filtro_fim'])){
			$this->data['data_log_fim'] = $this->session->data['log_filtro_fim'];
			unset($this->session->data['log_filtro_fim']);
		}else{
			$this->data['data_log_fim'] = date('d/m/Y');
		}

		if (isset($this->session->data['log_erro_periodo'])){
			$this->data['log'] = $this->session->data['log_erro_periodo'];
			unset($this->session->data['log_erro_periodo']);
		}else	if (file_exists($file)) {
			$this->data['log'] = file_get_contents($file, FILE_USE_INCLUDE_PATH, null);
		} else {
			$this->data['log'] = '';
		}

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

		$this->response->setOutput($this->render('tool/error_log_mercadolivre'));
	}

	public function clear() {
		$this->language->load('tool/error_log_mercadolivre');
		$this->load->helper('ftr_geral');

		$datalog = ftr_geral::formatDate($this->request->get['data_log_inicio'],'us');

		$this->session->data['log_filtro_ini'] = $this->request->get['data_log_inicio'];
		$this->session->data['log_filtro_fim'] = $this->request->get['data_log_fim'];

		$dateStart 		 = new DateTime(ftr_geral::formatDate($this->request->get['data_log_inicio'],'us'));
    $dateEnd 		   = new DateTime(ftr_geral::formatDate($this->request->get['data_log_fim'],'us'));

    while($dateStart <= $dateEnd){
			$file = DIR_LOGS .$dateStart->format('Y-m-d').'_'.'errorMercadoLivre.txt';
			if (file_exists($file)){
				unlink($file);
			}
      $dateStart = $dateStart->modify('+1day');
    }

		$this->session->data['success'] = $this->language->get('text_success');
		$this->redirect($this->url->link('tool/error_log_mercadolivre', 'token=' . $this->session->data['token'], 'SSL'));
	}

	public function listlogs() {
		$this->language->load('tool/error_log_mercadolivre');
		$this->load->helper('ftr_geral');

		$datalog = ftr_geral::formatDate($this->request->get['data_log_inicio'],'us');
		$this->session->data['log_filtro_ini'] = $this->request->get['data_log_inicio'];
		$this->session->data['log_filtro_fim'] = $this->request->get['data_log_fim'];

		$dateStart 		 = new DateTime(ftr_geral::formatDate($this->request->get['data_log_inicio'],'us'));
    $dateEnd 		   = new DateTime(ftr_geral::formatDate($this->request->get['data_log_fim'],'us'));

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

    while($dateStart <= $dateEnd){
			$file = DIR_LOGS .$dateStart->format('Y-m-d').'_'.'errorMercadoLivre.txt';
			if (file_exists($file)){
				$this->session->data['log_erro_periodo'] .= file_get_contents($file, FILE_USE_INCLUDE_PATH, null);
			}
      $dateStart = $dateStart->modify('+1day');
    }
		$this->redirect($this->url->link('tool/error_log_mercadolivre', 'token=' . $this->session->data['token'], 'SSL'));
	}

}
