<?php

class ControllerEredeLogs extends Controller
{
	public function index()
	{

		$this->_addRedeLibrary();
		$this->language->load('payment/erede');
		$this->document->setTitle('e-Rede Logs');
		$this->load->model('erede/logs');

		$logs = $this->model_erede_logs->getLogs();
		$this->data['status'] = array();

		foreach ($logs as $log) 
        {
        	$this->data['status'][] = $this->language->get($log['status']);
        	//echo $log['message']."<br>";
        	if ($log['message'] == "Erro na transação")
        	{
        		$this->data['message'][] = $this->language->get('erro_transacao');
        	}
        	else if ($log['message'] == "Transação Aprovada na Análise de Risco")
        	{
        		$this->data['message'][] = $this->language->get('sucesso_analise');
        	}
        	else if (strrpos($log['message'], "token") > 0)
        	{
        		$this->data['message'][] = $this->language->get('erro_webservice');
        	}
        	else if (strrpos($log['message'], "Risco") > 0)
        	{
        		$this->data['message'][] = $this->language->get('erro_risco_produto');
        	}
        	else
        	{
        		$this->data['message'][] = $this->traduzLogMsg($log['message'],1);
        	}
        }

		$this->data['erede_logo'] = $this->language->get('erede_logo');
		$this->data['logs'] = $logs;
		$this->data['txt_id_log'] = $this->language->get('txt_id_log');
		$this->data['txt_id_order'] = $this->language->get('txt_id_order');
		$this->data['txt_date'] = $this->language->get('txt_date');
		$this->data['txt_status'] = $this->language->get('txt_status');
		$this->data['txt_message'] = $this->language->get('txt_message');
		$this->data['txt_action'] = $this->language->get('txt_action');
		$this->data['txt_view'] = $this->language->get('txt_view');
		//$this->data['status'] = $this->language->get($logs[0]['status']);
		$this->data['token'] = $this->session->data['token'];
		$this->data['back'] = $this->url->link('payment/erede', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['button_back'] = $this->language->get('button_back');

		$this->_createBreadcrumbs();
		$this->_createLink();

		$this->template = 'erede/logs.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	private function _addRedeLibrary()
	{
		include_once DIR_CATALOG . 'controller/payment/RedeLibrary/RedeLibrary.php';
	}

	private function _createBreadcrumbs()
	{

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

		$this->data['breadcrumbs'][] = array(
			'text'		 => $this->language->get('heading_title'),
			'href'		 => $this->url->link('payment/erede', 'token=' . $this->session->data['token'], 'SSL'),
			'separator'	 => ' :: '
		);
	}

	private function _createLink()
	{
		$this->data['action'] = $this->url->link('payment/erede', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL');
	}

	private function traduzLogMsg($mensagem, $portion = 1) {
		$message_parts = explode(":", $mensagem);
		for ($i=0; $i < $portion; $i++) {
			if(($message_parts[$i]))
				//echo $this->limpaString($message_parts[$i]);
				$message_parts[$i] = $this->language->get($message_parts[$i]);
			}
			$mensagem = implode(": ", $message_parts);
		return $mensagem;
	}

	private function limpaString($string)
	{
		$string = strtolower($string);
		$string = str_replace(" ", "_", $string);

		return $string;
	}
}
?>