<?php
class Category_model extends CI_Model {
function __construct()
	{
		parent::__construct();

	}
	public function get_category_list()
	{	//$searchtext, $column_name, $order_by
        $sql = "SELECT * FROM category where IsActive=0";
        $record = $this->db->query($sql);
        return $record->result_array();
	}	

	public function save_category($post_data)
	{	
				$category=$post_data['category'];
				$sqlc = "SELECT count(*) as count,category_no FROM category where IsActive=0 and category='$category'";
				$recordc = $this->db->query($sqlc);
				$cc=$recordc->row('count');
				$category_no=$recordc->row('category_no');
				if($cc > 0)
				{
				//	echo '131';
				$category_no=$recordc->row('category_no');
				}
				else
				{
				//	echo '22';
				$sql = "SELECT max(category_no) as category_no FROM category";
				$record = $this->db->query($sql);
				$category_no1=$record->row('category_no');
				$category_no=$category_no1+1;				
				}
			
				$count2 = count($post_data['master_product']);
			//echo $count2;
				$query_values2 = array();

				for($i=0; $i<$count2; $i++)
				{
				//	echo $i;
				$db = get_instance()->db->conn_id;

				
				$master_product=mysqli_real_escape_string($db,$post_data['master_product'][$i]);
				
				$category=$post_data['category'];
				$order=$post_data['order'];
				
				$data=array('category'=>$category,'sorder'=>$order,'master_product'=>$master_product,'category_no'=>$category_no);
				$in=$this->db->insert('category',$data);
				}
				
				
				return TRUE;
				
	}	


	public function update_category($post_data)
	{	
					$category_no=$post_data['id'];

				$this->db->where('category_no',$category_no);
				 $in= $this->db->delete('category');
				 
				$count2 = count($post_data['master_product']);
			//echo $count2;
		
				$query_values2 = array();

				for($i=0; $i<$count2; $i++)
				{
				//	echo $i;
				$db = get_instance()->db->conn_id;

				
				$master_product=mysqli_real_escape_string($db,$post_data['master_product'][$i]);
				
				$category=$post_data['category'];
				$category_no=$post_data['id'];
				$order=$post_data['order'];
			//	$master_product=$post_data['master_product'];
				
				
				 
				$data=array('category'=>$category,'sorder'=>$order,'master_product'=>$master_product,
				'category_no'=>$category_no);
				$in=$this->db->insert('category',$data);
				}
			//	exit;
				return TRUE;
				
	}	

	public function delete_category($id)
	{	
	
				$data=array('IsActive'=>'1');
				$this->db->where('category_no',$id);
				 return $this->db->update('category',$data);
			
				
	}	

	public function get_master_product_list()
	{	//$searchtext, $column_name, $order_by
        $sql = "SELECT * FROM product where IsActive=0";
        $record = $this->db->query($sql);
        return $record->result_array();
	}	

}?>