<?php namespace App\Models\Admin;

use App\Models\Project\DatatableModel;
class ProductsModel extends AdminModel{
	private $table;
	
    public function __construct(){
		parent::__construct();
		$this->table = "products";
    }
	
	private function get_mptt(){
		$mptt = new Zebra_Mptt($this->db->connID,'site_menu','site_menu_id','title','lft','rgt','parent_site_menu_id');
		return $mptt;
	}
	
	public function get_datatable(){
		$dtObj = new DatatableModel($this->db);
		$dtObj->set_table($this->table." AS p");
		// Set Datatable columns 
		$dtObj->set_column('p.product_id', 'desc|sort|search', 'RecordID');
		$dtObj->set_column('p.product_url', '');
		$dtObj->set_column('p.status', 'sort|search');
		$dtObj->set_column('pl.title', 'sort|search');
		$builder = $dtObj->get_builder();
		$builder->join($this->table."_ln AS pl","p.product_id=pl.product_id AND pl.language_code='".$this->project->default_lang_code."'",'left');
		$builder->where('p.is_deleted','No');
		return $dtObj->get_data($builder);
	} 
	
	public function get($language_list,$product_id){
		$builder = $this->db->table($this->table." AS mt");
		$columns = ["mt.*"];
		$ln_columns = ['product_ln_id',
			'title',
			'short_description',
			'description',
			'description_2',
			'dose',
			'compatibility',
			'benefit',
			'packing',
			'meta_title',
			'meta_keywords',
			'meta_description',
			'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_id=".$language['code'].".product_id AND ".$language['code'].".language_code='".$language['code']."'",'left');
			}
		}
		$builder->select(implode(", ",$columns));
		$builder->where('mt.product_id',$product_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, $product_id){
		$this->add_update_log($data);
		$builder = $this->db->table($this->table);
		$builder->where('product_id',$product_id);
		$builder->update($data);
	}
	
	public function delete($product_id){
		$data['is_deleted'] = "Yes";
		$this->add_update_log($data);
		$builder = $this->db->table($this->table);
		$builder->where('product_id',$product_id)
				->update($data);
	}
	
	public function is_unique_product_url($url,$product_id){
		$builder = $this->db->table($this->table);
		$builder->where('is_deleted','No')
			->where('product_url',$url);
		if($product_id!=""){
			$builder->where('product_id!=',$product_id);
		}
		$query = $builder->get();
		if($query->getRowArray()){
			$response = "FALSE|The product url already exist!";
		}else{
			$response = "TRUE|";
		}
		return $response;
	}
	
	public function update_language_data($data,$product_ln_id){
		$builder = $this->db->table($this->table.'_ln');
		$builder->where('product_ln_id',$product_ln_id)
		->update($data);
	}
	
	public function insert_language_data($data){
		$builder = $this->db->table($this->table.'_ln');
		$builder->insert($data);
	}
}