<?php

class ModelExtensionShippingChronepostpickuppt extends Model
{
	function getQuote($address)
	{
		//$this->load->language('extension/shipping/chronepostpickuppt');
		//$this->load->model('localisation/zone');

		/*
		$query = $this->db->query(
			"SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone 
				WHERE geo_zone_id = '" . (int)$this->config->get('shipping_chronepostpickuppt_geo_zone_id') . "' 
				AND country_id = '" . (int)$address['country_id'] . "' 
				AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");

		if (!$this->config->get('shipping_chronepostpickuppt_geo_zone_id') || $query->num_rows > 0) {
			$status = true;
		} else {
			$status = false;
		}

		$method_data = array();

		$cart_weight = $this->cart->getWeight();

		$shippings_weight_data = $this->config->get('shipping_chronepostpickuppt_weight_data');

		foreach ($shippings_weight_data as $k => $s) {
			if ($s['weight_from'] != 0 && $cart_weight <= $s['weight_from']) {
				unset($shippings_weight_data[$k]);
			} elseif ($s['weight_to'] != 0 && $s['weight_to'] < $cart_weight) {
				unset($shippings_weight_data[$k]);
			}
		}

		$sql = 'SELECT * FROM ' . DB_PREFIX . 'xx_chronepostpickuppt_shops ORDER BY location, name, address, postal_code LIMIT 20';
		$query = $this->db->query($sql);

		if ($status && count($shippings_weight_data)) {
			$checkbox = 'chronepostpickuppt.chronepostpickuppt_select';
			$checkbox_js = '';
			if (isset($this->session->data['shipping_method']) && strpos($this->session->data['shipping_method']['code'], 'chronepostpickuppt') !== false) {
				$checkbox = $this->session->data['shipping_method']['code'];
				$checkbox_js = $this->session->data['shipping_method']['code'];
			}

			ob_start();
			?>
			<select name="chronepostpickuppt_sel" id="chronepostpickuppt_sel"
					onchange="$('#chronepostpickuppt_sel').parent().find('input').eq(0).val($(this).val()); $('#chronepostpickuppt_sel').parent().find('input').eq(0).prop('checked',true).trigger('change');"
					style="width: 80%; display: inline;" class="form-control">
				<?php
				$reqion_old = '';
				foreach ($query->rows as $result) {
					$reqion_new = $result['location'];
					$terminal_name = $result['name'] . ' (' . $result['address'] . ', ' . $result['postal_code'] . ')';

					if ($reqion_new != $reqion_old && $reqion_old != '') {
						echo "</optgroup>";
					}

					if ($reqion_new != $reqion_old) {
						echo "<optgroup label='" . $reqion_new . "'>";
					}

					if (isset($this->session->data['shipping_method']) && $this->session->data['shipping_method']['code'] == 'chronepostpickuppt.chronepostpickuppt' . $result['id_pickme_shop']) {
						echo "<option value='" . 'chronepostpickuppt.chronepostpickuppt' . $result['id_pickme_shop'] . "' selected='selected'>" . $terminal_name . "</option>";
					} else {
						echo "<option value='" . 'chronepostpickuppt.chronepostpickuppt' . $result['id_pickme_shop'] . "'>" . $terminal_name . "</option>";
					}

					$reqion_old = $reqion_new;
				}

				if ($reqion_old != '') {
					echo "</optgroup>";
				}

				?>
			</select>
			<?php
			$dropSelect_js = ob_get_clean();

			$shipping_weight_data = reset($shippings_weight_data);
			$chronepostpickuppt_cost = $shipping_weight_data['cost'] + $shipping_weight_data['each_next'] * ($this->cart->countProducts() - 1);

			if ($this->config->get('shipping_chronepostpickuppt_freelimit') > 0 && $this->cart->getSubtotal() > $this->config->get('shipping_chronepostpickuppt_freelimit')) {
				$chronepostpickuppt_cost = 0.00;
			}

			$quote_data = array();

			$dropSelect = $this->language->get('text_title') . '&nbsp;';
			$dropSelect .= '<select name="chronepostpickuppt_sel" id="chronepostpickuppt_sel" style="width: 80%; display: inline;" class="form-control">';
			//$dropSelect .= 'onfocus="$(\'#chronepostpickuppt_sel\').parent().find(\'input\').eq(0).val($(this).val()); $(\'#chronepostpickuppt_sel\').parent().find(\'input\').eq(0).prop(\'checked\',true).trigger(\'change\');" ';
			$dropSelect .= 'onchange="$(\'#chronepostpickuppt_sel\').parent().find(\'input\').eq(0).val($(this).val()); $(\'#chronepostpickuppt_sel\').parent().find(\'input\').eq(0).prop(\'checked\',true).trigger(\'change\');">';

			$reqion_old = '';
			foreach ($query->rows as $result) {
				$reqion_new = $result['location'];
				$terminal_name = $result['name'] . ' (' . $result['address'] . ', ' . $result['postal_code'] . ')';
				if ($reqion_new != $reqion_old && $reqion_old != '') {
					$dropSelect .= "</optgroup>";
				}
				if ($reqion_new != $reqion_old) {
					$dropSelect .= "<optgroup label='" . $reqion_new . "'>";
				}
				if (isset($this->session->data['shipping_method']) && $this->session->data['shipping_method']['code'] == 'chronepostpickuppt.chronepostpickuppt' . $result['id_pickme_shop']) {
					$dropSelect .= "<option value='" . 'chronepostpickuppt.chronepostpickuppt' . $result['id_pickme_shop'] . "' selected='selected'>" . $terminal_name . "</option>";
				} else {
					$dropSelect .= "<option value='" . 'chronepostpickuppt.chronepostpickuppt' . $result['id_pickme_shop'] . "'>" . $terminal_name . "</option>";
				}
				$reqion_old = $reqion_new;
			}
			if ($reqion_old != '') {
				$dropSelect .= "</optgroup>";
			}

			$dropSelect .= "</select>";
			$quote_data['chronepostpickuppt.chronepostpickuppt'] = array(
				'code' => $checkbox . '" onclick="$(this).val($(\'#chronepostpickuppt_sel\').val());',
				'title' => $this->language->get('text_title') . '&nbsp;' . '<!--',
				'cost' => $chronepostpickuppt_cost,
				'tax_class_id' => $this->config->get('shipping_chronepostpickuppt_tax_class_id'),
				'text' => $this->currency->format($this->tax->calculate($this->currency->convert($chronepostpickuppt_cost, $this->config->get('config_currency'), $this->session->data['currency']), $this->config->get('shipping_chronepostpickuppt_tax_class_id'), $this->config->get('config_tax')), $this->session->data['currency'], 1.0000000)
			);


			// Start Build Selection
			$quote_data['chronepostpickuppt.chronepostpickuppt_begin'] = array(
				'code' => 'chronepostpickuppt.chronepostpickuppt_begin',
				'title' => '',
				'cost' => $chronepostpickuppt_cost,
				'tax_class_id' => $this->config->get('shipping_chronepostpickuppt_tax_class_id'),
				'text' => $this->currency->format($this->tax->calculate($this->currency->convert($chronepostpickuppt_cost, $this->config->get('config_currency'), $this->session->data['currency']), $this->config->get('shipping_chronepostpickuppt_tax_class_id'), $this->config->get('config_tax')), $this->session->data['currency'], 1.0000000)
			);


			foreach ($query->rows as $result) {
				$chronepostpickuppt[] = $result;
				$quote_data['chronepostpickuppt' . $result['id_pickme_shop']] = array(
					'code' => 'chronepostpickuppt.chronepostpickuppt' . $result['id_pickme_shop'],
					'title' => $this->language->get('text_title') . ' - ' . $result['location'] . ' - ' . $result['name'] . ' (' . $result['address'] . ', ' . $result['postal_code'] . ')',
					'cost' => $chronepostpickuppt_cost,
					'tax_class_id' => $this->config->get('shipping_chronepostpickuppt_tax_class_id'),
					'text' => $this->currency->format($this->tax->calculate($this->currency->convert($chronepostpickuppt_cost, $this->config->get('config_currency'), $this->session->data['currency']), $this->config->get('shipping_chronepostpickuppt_tax_class_id'), $this->config->get('config_tax')), $this->session->data['currency'], 1.0000000)
				);
			}

			$quote_data['chronepostpickuppt.chronepostpickuppt_end'] = array(
				'code' => 'chronepostpickuppt.chronepostpickuppt_end',
				'title' => '-->' . $dropSelect_js,
				'cost' => $chronepostpickuppt_cost,
				'tax_class_id' => $this->config->get('shipping_chronepostpickuppt_tax_class_id'),
				'text' => $this->currency->format($this->tax->calculate($this->currency->convert($chronepostpickuppt_cost, $this->config->get('config_currency'), $this->session->data['currency']), $this->config->get('shipping_chronepostpickuppt_tax_class_id'), $this->config->get('config_tax')), $this->session->data['currency'], 1.0000000)
			);
			// End Build Selection

			$method_data = array(
				'code' => 'chronepostpickuppt',
				'title' => $this->language->get('text_title'),
				'quote' => $quote_data,
				'sort_order' => $this->config->get('shipping_chronepostpickuppt_sort_order'),
				'error' => false
			);

		}*/

		return array();
		//return $method_data;
	}

	public function getLocations($keyword)
	{
		return $this->db->query(
			"SELECT * FROM `" . DB_PREFIX . "xx_chronepostpickuppt_shops` 
				WHERE `postal_code` LIKE '%" . $keyword . "%' 
				OR `name` LIKE '%" . $keyword . "%' 
				OR `location` LIKE '%" . $keyword . "%' 
				OR `address` LIKE '%" . $keyword . "%'"
		)->rows;
	}
}
