<?php
	defined('BASEPATH') OR exit('No direct script access allowed');

	class Hc_products extends My_Model{
		protected $_table_name = 'hc_products';
		protected $_order_by = 'hc_products.id';

		public function __construct(){
			parent::__construct();
		}


		public function get_new(){
			$products = new stdClass();
			$products->title = '';
			$products->is_active = '1';

			//Find out order of slider
			$count_records = $this->products->get_count(array("is_active != " => 2));
			$products->display_order = $count_records + 1;

			return $products;
		}


		public $rules = array(

			'title' => array(
				'field' => 'title', 
				'label' => 'title', 
				'rules' => 'trim|required|xss_clean'
			)
		);


		public function get_data_for_sorting(){
			$data = $this->db->get($this->_table_name)->result_array();

			$array = array();
			foreach ($data as $key => $value) {
				$array[$value['id']] = $value;
			}

			return $array;
		}

		public function save_order($array){
			if(count($array)){
				foreach ($array as $key => $value) {
					$data = array('id' => $value, 'display_order' => ($key + 1));
					$this->db->set($data)->where($this->_primary_key, $value)->update($this->_table_name);
				}
			}
		}

		public function get_data($data = array()){
			if(isset($data['id']) && $data['id'] != ""){
				if(isset($data["is_active"])){
					$this->db->where(array("is_active" => $data["is_active"]));
				}
				$result = $this->products->get_by(array("id" => $data["id"]), TRUE);

				return $result;
			}

			return array();
		}

		public function get_products_list($filter = array()){
			$current_date = date('Y-m-d');

			$filter['in_array'] = 1;

			$conditions = $limit = $left_join = "";

			$sorting = "ORDER BY aaa.display_order ASC";

			/* Limit */

	        if(isset($filter['limit']) && $filter['limit'] != -1){
	            $limit = 'LIMIT '.$filter['offset'].', '.$filter['limit'];
	        }
	        /* Limit */


	        if(isset($filter["slug"]) && $filter["slug"] != ""){
	        	$conditions .= " AND p.slug = '".$filter["slug"]."'";
	        }

	        if(isset($filter["id"]) && $filter["id"] != ""){
	        	$conditions .= " AND p.id IN (".$filter["id"].")";
	        }

	        $left_join = '';

	        if($filter_conditions != ""){
	        	$filter_conditions = " AND (".$filter_conditions.")";
	        }

	        $query = "SELECT
	        	aaa.*
	        	FROM
	        	(
	        		SELECT
	        			p.*
	        		FROM
	        			$this->_table_name AS p
	        			$left_join
	        		WHERE 
	        			p.is_active = 1
	        		$conditions
	        		$filter_conditions

	        	)
	        	AS aaa
	        	GROUP BY aaa.id
	        	$sorting
	        ";
	        //echo "<pre>";print_r($query);die;

	        $total_count = 0;
	        if(isset($filter['count'])){
	            $total_count = $filter['count'];
	        }else{
	            $count_result = $this->db->query($query);
	            $total_count = $count_result->num_rows();
	        }

	        if(!isset($filter['get_count'])){

	            $query .= " $limit";
	        }

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

	        //dump($this->db->last_query());die;

	        $response = array();

	        if($result->num_rows()){
	        	
	        	$data = $result->result();

	        	foreach ($data as $key => $value) {
	        		$data[$key]->slug = base_url().'products/'.$value->slug;
	        	}

	        	$response['status'] = TRUE;
	        	$response['message'] = "Success.";

	            $response['data'] = $data;
	            $response['total_count'] = $total_count;
	        }else{
	        	$response['status'] = FALSE;
	        	$response['message'] = "No data Found.";
	        	$response['total_count'] = 0;
	        }
	        //dump($response);die;
	        return $response;

		}
	}
?>