<?php

class ControllerExtensionTotalTrackingDiscountEvent extends Controller {

	public function eventAddEditAffiliate(&$route, &$args, &$output) {
		if(!$this->config->get('total_tracking_discount_status')) return;
		if(sizeof($args) === 2) {
			$customer_id = $args[0];
			$data = $args[1];
		} else {
			$customer_id = $output;
			$data = $args[0];
		}
		if(!$customer_id || !preg_match("/^\d+$/", $customer_id)) return;

		$this->load->model('extension/tracking_discount');
		$this->model_extension_tracking_discount->deleteAffiliate($customer_id);
		
		if(isset($data['tracking_discount_amount']) && preg_match("/^\d+(\.\d+)?$/", $data['tracking_discount_amount'])) $this->model_extension_tracking_discount->addAffiliate($customer_id, $data['tracking_discount_amount'], $data['tracking_discount_type']);			
	}

	public function eventGetAffiliate(&$route, &$_id, &$output) {
		if($output && $this->config->get('total_tracking_discount_status') && isset($output['customer_id'])) {
			$customer_id = $output['customer_id'];
			if($customer_id && preg_match("/^\d+$/", $customer_id)) {
				$this->load->model('extension/tracking_discount');
				$res = $this->model_extension_tracking_discount->getAffiliate($customer_id);
				return array_merge($output, $res);
			}
		} 
	}	

	public function eventDeleteAffiliate(&$route, &$customer_id) {
		if($this->config->get('total_tracking_discount_status')) {
			$cid = is_array($customer_id) && isset($customer_id[0]) ? $customer_id[0] : $customer_id;
			if($cid && preg_match("/^\d+$/", $cid)) {
				$this->load->model('extension/tracking_discount');
				$this->model_extension_tracking_discount->deleteAffiliate($cid);
			}
		}
	}	
	
	public function eventCustomerFormTBefore(&$route, &$data, &$template) {
		if (!$this->config->get('total_tracking_discount_status')) {	
			$data['tracking_discount_status'] = false;
			return;
		}
		
		$data['tracking_discount_status'] = true;
		$this->load->language('extension/total/tracking_discount_affiliate');
		
		foreach(array('entry_tracking_discount_amount', 'help_tracking_discount_amount', 'entry_tracking_discount_type', 'option_tracking_discount_fixed', 'option_tracking_discount_percent') as $_v) {
			$data[$_v] = preg_replace("/[\n\r\s]+/", ' ', addcslashes($this->language->get($_v), "'"));
		}
 		if ($this->config->get('_error_tracking_discount_amount')) {
			$data['error_tracking_discount_amount'] = $this->config->get('_error_tracking_discount_amount');
		} else {
			$data['error_tracking_discount_amount'] = '';
		}
		
		if (isset($this->request->get['customer_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$this->load->model('customer/customer');
			$affiliate_info = $this->model_customer_customer->getAffiliate($this->request->get['customer_id']);
		}		
		
		foreach(array('amount', 'type') as $k) {		
			if (isset($this->request->post['tracking_discount_' . $k])) {
				$data['tracking_discount_' . $k] = $this->request->post['tracking_discount_' . $k];
			} elseif (!empty($affiliate_info) && isset($affiliate_info['tracking_discount_' . $k])) { 
				$data['tracking_discount_' . $k] = $affiliate_info['tracking_discount_' . $k];
			} else {
				$data['tracking_discount_' . $k] = '';
			}
		}		
	}
	

}
