<?php namespace App\Models\Admin;

use App\Models\Project\DatatableModel;

class Product_categoryModel extends AdminModel{
	private $table;
	
    public function __construct(){
		parent::__construct();
		$this->table = "product_category";
    }
	 
	public function get_datatable(){
		$dtObj = new DatatableModel($this->db);
		$dtObj->set_table($this->table." AS pa");
		// Set Datatable columns 
		$dtObj->set_column('pa.product_category_id', 'desc|sort|search', 'RecordID');
		$dtObj->set_column('pal.title', 'sort|search');
		$dtObj->set_column('pa.status', 'sort|search');
		$builder = $dtObj->get_builder();
		$builder->where('pa.is_deleted','No');
		$builder->join($this->table."_ln AS pal","pa.product_category_id=pal.product_category_id AND pal.language_code='".$this->project->default_lang_code."'",'left');
		return $dtObj->get_data($builder);
	}
	
	public function get($language_list,$product_category_id){
		$builder = $this->db->table($this->table." AS mt");
		$columns = ["mt.*"];
		$ln_columns = ['product_category_ln_id',
			'title',
			'meta_title',
			'meta_description',
			'meta_keywords',
			'language_code'];
		if($language_list){
			foreach($language_list as $language){
				foreach($ln_columns as $col_name){
					array_push($columns,$language['code'].".".$col_name." AS ".$col_name."_".$language['code']);
				}
				$builder->join($this->table."_ln AS ".$language['code'],"mt.product_category_id=".$language['code'].".product_category_id AND ".$language['code'].".language_code='".$language['code']."'",'left');
			}
		}
		$builder->select(implode(", ",$columns));
		$builder->where('mt.product_category_id',$product_category_id);
		$query = $builder->get();
		//echo $builder->getCompiledSelect();
		return $query->getRowArray();
	}
	
	public function insert($data){
		$this->add_insert_log($data);
		$builder = $this->db->table($this->table);
		$builder->insert($data);
		return $this->db->insertID();
	}
	
	public function update($data, $id){
		$this->add_update_log($data);
		$builder = $this->db->table($this->table);
		$builder->where('product_category_id',$id);
		$builder->update($data);
	}
	
	public function delete($product_category_id){
		$data['is_deleted'] = "Yes";
		$this->add_update_log($data);
		$builder = $this->db->table($this->table);
		$builder->where('product_category_id',$product_category_id)
				->update($data);
	}
	
	public function update_language_data($data,$product_category_ln_id){
		$builder = $this->db->table($this->table.'_ln');
		$builder->where('product_category_ln_id',$product_category_ln_id)
		->update($data);
	}
	
	public function insert_language_data($data){
		$builder = $this->db->table($this->table.'_ln');
		$builder->insert($data);
	}
	
	public function is_unique_product_category_url($product_category_url,$product_category_id){
		$builder = $this->db->table($this->table);
		$builder->where('is_deleted','No')
				->where('product_category_url',$product_category_url);
		if($product_category_id){
			$builder->where('product_category_id!=',$product_category_id);
		}
		$query = $builder->get();
		if($query->getRowArray()){
			$response = "FALSE|The Product Category Url already exist!";
		}else{
			$response = "TRUE|";
		}
		return $response;
	}
	
	public function is_unique_title($title,$id){
		$builder = $this->db->table("product_category_ln AS pal");		
		$builder->join("product_category AS pa","pa.product_category_id=pal.product_category_id",'inner')
				->where('pa.is_deleted','No')
				->where('pal.title',$title);
		if($id!=""){
			$builder->where('pal.product_category_id!=',$id);
		}
		$query = $builder->get();
		if($query->getRowArray()){
			$response = "FALSE|The Title already exist!";
		}else{
			$response = "TRUE|";
		}
		return $response;
	}
}