<?php namespace App\Controllers;
  
class Product extends FrontController
{		
	public function document_download($product_id){
		$data['form_heading'] = "Download Document";
		$data['action'] = "add";
		$data['show_download_btn'] = false;
		$data['prod'] = $this->get_prod_dtl($product_id);
		if($this->request->getMethod() === 'post'){
			$rules['name'] = ['label'=>'Name', 'rules' => 'trim|required'];
			$rules['mobile_no'] = ['label'=>'Mobile No.', 'rules' => 'trim|required'];
			$rules['email_id'] = ['label'=>'Email ID', 'rules' => 'trim|required'];
			if($this->validate($rules)){ 
				$data_in['name'] = $this->request->getPost('name');
				$data_in['mobile_no'] = $this->request->getPost('mobile_no');
				$data_in['email_id'] = $this->request->getPost('email_id');
				$data_in['product_id'] = $product_id;
				$data_in['doc_file'] = $data['prod']['product_doc_file'];
				$this->insert_doc_downloaded($data_in);	
				$data['show_download_btn'] = true;
			}
		}
		echo $this->view_partial('product_document_download_modal',$data);		
	}
	
	public function index($product_url){
		$data['language_code'] = $this->get_requested_lang_code();
		$data['rec'] = $this->get_detail($product_url,$data['language_code']);
		$data['rec']['benefit_arr'] = json_decode($data['rec']['benefit'],true);
		$data['rec']['packing_arr'] = json_decode($data['rec']['packing'],true);
		$data['meta_data']['title'] = $data['rec']['meta_title'];
		$data['meta_data']['description'] = $data['rec']['meta_description'];
		$data['meta_data']['keywords'] = $data['rec']['meta_keywords'];
		$data['prod_cat'] = $this->get_prod_cat_by_id($data['rec']['product_category_id'],$data['language_code']);
		//$data['products_list']= $this->get_products_list();
		 /*echo '<pre>';
		 print_r($data['rec']['benefit_arr']);
		 print_r($data['rec']['packing_arr']);*/
		//$data['footer_includes'] = ['product_js'];
		echo $this->view_inner_template('product',$data);
	}
	
	public function category($prod_cat_url){
		$data['language_code'] = $this->get_requested_lang_code();
		$data['cat'] = $this->get_cat_detail($prod_cat_url,$data['language_code']);
		$data['meta_data']['title'] = $data['cat']['meta_title'];
		$data['meta_data']['description'] = $data['cat']['meta_description'];
		$data['meta_data']['keywords'] = $data['cat']['meta_keywords'];
		
		$data['product_list']= $this->get_products_list($data['cat']['product_category_id'],$data['language_code']);
		echo $this->view_inner_template('product_category',$data);
	}
	
	public function category_list(){
		$data['language_code'] = $this->get_requested_lang_code();
		$data['meta_data']['title'] = "Product Category";
		$data['meta_data']['description'] = "Product Category";
		$data['meta_data']['keywords'] = "Product Category";
		
		$data['cat_list'] = $this->get_cat_list($data['language_code']);
		if($data['cat_list']){
			foreach($data['cat_list'] as $c => $cat){
				$data['cat_list'][$c]['product_list'] = $this->get_products_list($cat['product_category_id'],$data['language_code']);
			}
		}
		echo $this->view_inner_template('product_category_list',$data);	
	}
	
	private function get_cat_list($lang_code){
		$builder = $this->cmObj->get_table_builder('product_category AS c');
		$query = $builder->join('product_category_ln AS cl',"c.product_category_id=cl.product_category_id AND cl.language_code='".$lang_code."'",'left')
			->select('c.product_category_id,cl.title,cl.*')
			->where('c.status','Active')
			->where('c.is_deleted','No')
			->orderBy('cl.title','ASC')
			->get();
		$rec = $query->getResultArray();
		return $rec;
	}
	
	private function get_cat_detail($url,$lang_code){
		$builder = $this->cmObj->get_table_builder('product_category AS c');
		$query = $builder->join('product_category_ln AS cl',"c.product_category_id=cl.product_category_id AND cl.language_code='".$lang_code."'",'left')
			->select('c.product_category_id,c.banner_file,cl.title,cl.*')
			->where('c.product_category_url',$url)
			->where('c.status','Active')
			->where('c.is_deleted','No')
			->get();
		$rec = $query->getRowArray();
		return $rec;
	}
	
	private function get_detail($product_url,$lang_code){
		$builder = $this->cmObj->get_table_builder('products AS p');
		$query = $builder->join('products_ln AS pl',"p.product_id=pl.product_id AND pl.language_code='".$lang_code."'",'left')
			->select('p.product_id,p.product_category_id,p.product_url,p.banner_image,p.product_image_1,p.product_image_2,p.product_image_3,p.product_image_4,p.product_doc_file,pl.*')
			->where('p.product_url',$product_url)
			->where('p.status','Active')
			->where('p.is_deleted','No')
			->get();
		$rec = $query->getRowArray();
		return $rec;
	}
	
	private function get_products_list($product_category_id,$language_code){
		$builder = $this->cmObj->get_table_builder('products AS mt');
		$builder->join('products_ln AS ln',"mt.product_id=ln.product_id AND ln.language_code='".$language_code."'","inner");
		$query = $builder->select('mt.product_id,mt.product_url,mt.product_icon_img_1,mt.product_image_1,ln.title,ln.short_description')
			->where('mt.status','Active')
			->where('mt.is_deleted','No')
			->where('mt.product_category_id',$product_category_id)
			->orderBy('ln.title','ASC')
			->get();
		return $query->getResultArray();
	} 	
	
	private function insert_doc_downloaded($data_in){	
		$data_in['added_date'] = date('Y-m-d H:i:s');
		$builder = $this->cmObj->get_table_builder('product_document_downloaded');
		$builder->insert($data_in);
	}
	
	private function get_prod_dtl($prod_id){
		$builder = $this->cmObj->get_table_builder('products');
		$query = $builder->select('product_id, 
			product_doc_file,
			product_url')
			->where('product_id',$prod_id)
			->get();
		return $query->getRowArray();
	}
	
	private function get_prod_cat_by_id($id,$lang_code){
		$builder = $this->cmObj->get_table_builder('product_category AS c');
		$query = $builder->join('product_category_ln AS cl',"c.product_category_id=cl.product_category_id AND cl.language_code='".$lang_code."'",'left')
			->select('c.product_category_id,c.product_category_url,cl.*')
			->where('c.product_category_id',$id)
			->where('c.status','Active')
			->where('c.is_deleted','No')
			->get();
		$rec = $query->getRowArray();
		return $rec;
	}
}