<?php
class ModelRewardTier extends Model {
	public function addTier($title, $description, $term_and_condition,  $achieve_tier_amount, 
	$maintain_tier_type_id, $maintain_tier_amount, $multiply_regular_item, $multiply_sales_item,
	$point_expiry, $tier_expiry, $is_hidden,  $status = 1, $custom_field) {
		$this->db->query("INSERT INTO `" . DB_PREFIX . "tier_setting` 
		SET `title` = '" . $this->db->escape($title) . "', 
		`description` = '" . $this->db->escape($description) . "', 
		`term_and_condition` = '" . $this->db->escape($term_and_condition) . "', 
		`achieve_tier_amount` = '" . (float)$achieve_tier_amount . "', 
		`maintain_tier_type_id` = '" . (int)$maintain_tier_type_id. "', 
		`maintain_tier_amount` = '" . (float)$maintain_tier_amount. "', 
		`multiply_regular_item` = '" . (float) $multiply_regular_item . "', 
		`multiply_sales_item` = '" . (float) $multiply_sales_item . "', 
		`point_expiry` = '" . (int) $point_expiry . "', 
		`tier_expiry` = '" . (int) $tier_expiry . "', 
		`created_by` = '" . $this->user->getUserName(). "',
		`created_date` = NOW() , 
		updated_by = '" . $this->user->getUserName() . "',
		updated_date = NOW() , 
		is_hidden = '" .(bool)($is_hidden == 1)."', 
		`status` = '" . (int)$status . "',
		`custom_field` = '" . json_encode($custom_field) . "'");
	
		 $tier_setting_id =  $this->db->getLastId();
		 $this->insertTierSetingLog($tier_setting_id);
	}

	public function editTier($tier_setting_id, $title, $description, $term_and_condition,  
	$achieve_tier_amount, $maintain_tier_type_id, $maintain_tier_amount, $multiply_regular_item,
	$multiply_sales_item, $point_expiry, $tier_expiry, $is_hidden,  $status = 1, $custom_field) {
		$this->db->query("UPDATE `" . DB_PREFIX . "tier_setting` 
		SET title = '" . $this->db->escape($title) . "', 
		description = '" . $this->db->escape($description) . "', 
		term_and_condition = '" . $this->db->escape($term_and_condition) . "', 
		achieve_tier_amount = '" . (float)$achieve_tier_amount . "', 
		maintain_tier_type_id = '" . (int)$maintain_tier_type_id . "', 
		maintain_tier_amount = '" . (float)$maintain_tier_amount . "', 
		multiply_regular_item = '" . (float)$multiply_regular_item . "', 
		multiply_sales_item = '" . (float)$multiply_sales_item . "', 
		point_expiry = '" . (int)$point_expiry . "',
		tier_expiry = '" . (int)$tier_expiry . "',
		is_hidden = '" .(bool)($is_hidden == 1)."', 
		status = '" . (int)$status . "', 
		`custom_field` = '" . json_encode($custom_field) . "',
		updated_by = '" . $this->user->getUserName() . "',
		updated_date = NOW() 
		WHERE tier_setting_id = '" . (int)$tier_setting_id . "'");

		$this->insertTierSetingLog($tier_setting_id);
	}

	public function deleteTier($tier_setting_id) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "tier_setting` WHERE `tier_setting_id` = '" . (int)$tier_setting_id . "'");
	}

	public function insertTierSetingLog($tier_setting_id) {
		$this->db->query("INSERT INTO `" . DB_PREFIX . "tier_setting_log` (
			title, description, term_and_condition, achieve_tier_amount, maintain_tier_type_id, 
			maintain_tier_amount, multiply_regular_item, multiply_sales_item, point_expiry, tier_expiry, status,
		    created_by, created_date) 
			SELECT title, description, term_and_condition, achieve_tier_amount, maintain_tier_type_id, 
			maintain_tier_amount, multiply_regular_item, multiply_sales_item, point_expiry, tier_expiry, status,
		    updated_by, updated_date FROM `" . DB_PREFIX . "tier_setting` 
			WHERE tier_setting_id = '" . $tier_setting_id . "';");
	}

	public function getTier($tier_setting_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "tier_setting` WHERE `tier_setting_id` = '" . (int)$tier_setting_id . "' LIMIT 1");

		return $query->row;
	}

	public function getTierByTitle($title) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "tier_setting` WHERE title = '" . $this->db->escape($title) . "'");

		return $query->row;
	}

	public function getTiers($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "tier_setting`";

		$sort_data = array(
			'title',
			'status',
			'created_date'
		);

		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY `" . $data['sort'] . "`";
		} else {
			$sql .= " ORDER BY `tier_setting_id`";
		}

		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}

		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTiersLevel(){
		$sql = "SELECT tier_setting_id, title FROM `" . DB_PREFIX . "tier_setting` where `status` = 1 ";
		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalTiers() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "tier_setting`");
		return $query->row['total'];
	}

	public function getTierTotalMemberCount($tier_setting_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "tier_setting` ts 
		INNER JOIN `" . DB_PREFIX . "member` m ON ts.tier_setting_id = m.tier_setting_id  
		WHERE ts.tier_setting_id = '" . $this->db->escape($tier_setting_id) . "'");
		return $query->row['total'];
	}
}