<?php
/*
* 2007-2014 Addonline
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
*  @author Addonline <contact@addonline.fr>
*  @copyright  2007-2014 Addonline
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*/

require_once(dirname(__FILE__).'/gls.php');

class AdminGls extends AdminTab
{
	private $gls = NULL;

	public $post_errors = array();

	public function __construct()
	{
		$this->gls = new Gls();

		parent::__construct();
		$this->context = Context::getContext();
	}

	private function displayOrdersTable()
	{	        
		$order_state = new OrderState((int)(Configuration::get('GLS_ORDER_STATE')), $this->context->language->id);
		$orders = Gls::getOrders(array(), Gls::NO_FILTER); //, $this->gls->account_shop['GLS_WEIGHT_COEFFICIENT']);		
		
		foreach($orders as &$order)
		{
			$order['display_total_price'] = Tools::displayPrice($order['total'], new Currency($order['id_currency']));
			$order['display_shipping_price'] = Tools::displayPrice($order['shipping'], new Currency($order['id_currency']));
			$order['display_date'] = Tools::displayDate($order['date'], $order['id_lang']);
			$order['weight'] = (!empty($order['gls_weight']) && $order['gls_weight'] > 0) ? $order['gls_weight'] : $order['order_weight'];
		}

		$controller = (_PS_VERSION_ < '1.5') ? 'AdminContact' : 'AdminStores';

		$this->context->smarty->assign(array(
				'GLS_token_admin_module' => Tools::getAdminToken('AdminModules'.(int)(Tab::getIdFromClassName('AdminModules')).(int)$this->context->employee->id),
				'GLS_token_admin_contact' => array(
				'controller_name' => $controller,
				'token' => Tools::getAdminToken($controller.(int)(Tab::getIdFromClassName($controller)).(int)$this->context->employee->id)),
				'GLS_token_admin_orders' => Tools::getAdminToken('AdminOrders'.(int)(Tab::getIdFromClassName('AdminOrders')).(int)$this->context->employee->id),
				'GLS_order_state_name' => $order_state->name,
				'GLS_orders' => $orders,
				'GLS_PS_IMG_DIR_' => _PS_IMG_DIR_,
				//'GLS_errors_type' => $GLSCreateTicket->checkPreValidation()
				)
		);

		unset($order_state);
		echo $this->gls->fetchTemplate('/tpl/admintab/', 'export_orders');
	}

	public function displaySettings($post_action)
	{
		$curr_order_state = new OrderState((int)$this->gls->account_shop['GLS_ORDER_STATE']);
		$order_state = array(
			'id_order_state' => $this->gls->account_shop['GLS_ORDER_STATE'],
			'name' => $curr_order_state->name[$this->context->language->id]
		);

		$order_state_import = array(
				'id_order_state' => $this->gls->account_shop['GLS_ORDER_STATE_IMPORT'],
				'name' => $curr_order_state->name[$this->context->language->id]
		);

		$this->context->smarty->assign(array(
			'GLS_token_admin_gls' => Tools::getAdminToken('AdminGls'.(int)(Tab::getIdFromClassName('AdminGls')).(int)$this->context->employee->id),
			'GLS_account_set' => Gls::isAccountSet(),
			'GLS_order_state' =>  $order_state,
			'GLS_order_state_import' =>  Configuration::get('GLS_ORDER_STATE_IMPORT'),
			'GLS_orders_states_list' => OrderState::getOrderStates($this->context->language->id),
			'GLS_form_action' => $post_action,
			'GLS_error_list' => $this->post_errors
		));

		echo $this->gls->fetchTemplate('/tpl/admintab/', 'settings');
	}

	public function postProcess()
	{
		$post_action = array(
			'type' => Tools::getValue('GLS_action_name'),
			'message_success' => $this->l('Action Succeed'),
			'had_errors' => false
		);

		parent::postProcess();

		if (Tools::isSubmit('submit_order_state'))
			if (($order_state = (int)Tools::getValue('id_order_state')))
			{
				$this->gls->account_shop['GLS_ORDER_STATE'] = $order_state;

				if ($this->gls->updateAccountShop())
					$post_action['message_success'] = $this->l('Order State has been updated');
				else
					$this->post_errors[] = $this->l('Cannot Update the account shop');
			}

		if (count($this->post_errors))
			$post_action['had_errors'] = true;

		return $post_action;
	}

	public function display()
	{	    	    
	    
		$post_action = count($_POST) ? $this->postProcess() : NULL;

		$this->displaySettings($post_action);		
		
		if (Gls::isAccountSet() && (int)$this->gls->account_shop['GLS_ORDER_STATE'])
		{		      
			$this->displayOrdersTable();
		}
	}
}
