<?php namespace App\Controllers;
use App\ThirdParty\Captcha\Captcha;
class Enquiry extends FrontController
{	 
	public function index(){		
		$data['meta_data']['title'] = "Enquiry";
		$data['meta_data']['description'] = "Enquiry";
		$data['meta_data']['keywords'] = "Enquiry";
		
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules();
			if($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$id = $this->mObj->insert($data_in);
				$this->send_mail();
				$this->session->setFlashdata('enquiry_status',"Your enquiry sent successfully!");
				return redirect()->to(base_url('enquiry'));
			}
		}
		$captcha = new Captcha();
		$data['captcha'] = $captcha->get_captcha();
		$this->session->set('captcha_code',$data['captcha']['code']);
		$data['footer_includes'] = ['enquiry_js'];
		echo $this->view_inner_template('enquiry',$data);
	} 
	
	private function get_column_values(){
		$data['name'] = $this->request->getPost('name');
		$data['email_id'] = $this->request->getPost('email_id');
		$data['country_id'] = $this->request->getPost('country_id');
		$data['phone_no'] = $this->request->getPost('phone_no');
		// $data['publish_date'] = date('Y-m-d H:i:s', strtotime($this->request->getPost('publish_date')));
		// $data['publish_time'] = date('H:i:s',strtotime($this->request->getPost('publish_time')));
		$data['products'] = "";
			    if($this->request->getPost('product_arr')){
			        $data['products'] = implode(", ",$this->request->getPost('product_arr'));    
			    }
		$data['company_name'] = $this->request->getPost('company_name');
		$data['address'] = $this->request->getPost('address');
		$data['comment'] = $this->request->getPost('comment');
		$data['drinking_system'] = $this->request->getPost('drinking_system');
		$data['feeding_system'] = $this->request->getPost('feeding_system');
		$data['no_of_birds'] = $this->request->getPost('no_of_birds');
		$data['bird_type'] = $this->request->getPost('bird_type');
		// Get country dtl
		$country_dtl = $this->common->getOneRow('country',"WHERE id='".$this->request->getPost('country_id')."'");
		$data['country'] = $country_dtl['name'];
		// $this->save_uploaded_file($data);
		return $data;
	}
	private function get_category_by_url($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,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_product_by_url($url,$lang_code){
		$builder = $this->cmObj->get_table_builder('products AS c');
		$query = $builder->join('products_ln AS cl',"c.product_id=cl.product_id AND cl.language_code='".$lang_code."'",'left')
			->select('c.product_id,cl.title,cl.*')
			->where('c.product_url',$url)
			->where('c.status','Active')
			->where('c.is_deleted','No')
			->get();
		$rec = $query->getRowArray();
		return $rec;
	}
	
	private function get_validation_rules(){
		
		$rules['name'] = ['label'=>'Name', 'rules' => 'trim|required'];
		$rules['email_id'] = ['label'=>'Email ID', 'rules' => 'trim|required'];
		$rules['phone_no'] = ['label'=>'Phone No.', 'rules' => 'trim|required'];
		$rules['subject'] = ['label'=>'Subject', 'rules' => 'trim|required'];
		$rules['message'] = ['label'=>'Message', 'rules' => 'trim|required'];
		$rules['captcha_code'] = ['label'=>'Security Code', 'rules' => 'trim|required|pr_callback['.$this->validate_captcha($this->request->getPost('captcha_code')).']'];
		return $rules;
	}
	
	private function send_mail(){
		$data['rec'] = $this->request->getPost();
		$cat_dtl = $this->get_category_by_id($data['rec']['category_id'],$this->project->default_lang_code);
		$prod_dtl = $this->get_product_by_id($data['rec']['product_id'],$this->project->default_lang_code);
		$data['rec']['category_name'] = $cat_dtl['title'];
		$data['rec']['product_name'] = $prod_dtl['title'];
		$html = $this->view_email_template('enquiry',$data);
		$email = $this->cmObj->get_email_object();
		$email->setTo('design@chittaranjan.co.in');
		// $email->setBCC('enquiry@chittaranjan.co.in');
		$email->setSubject('Enquiry '.$data['rec']['name']);
		$email->setMessage($html);	
		$email->send();
		//var_dump($email->printDebugger());
	}
	
	private function get_category_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,cl.title,cl.*')
			->where('c.product_category_id',$id)
			->where('c.status','Active')
			->where('c.is_deleted','No')
			->get();
		$rec = $query->getRowArray();
		return $rec;
	}
	
	private function get_product_by_id($id,$lang_code){
		$builder = $this->cmObj->get_table_builder('products AS c');
		$query = $builder->join('products_ln AS cl',"c.product_id=cl.product_id AND cl.language_code='".$lang_code."'",'left')
			->select('c.product_id,cl.title,cl.*')
			->where('c.product_id',$id)
			->where('c.status','Active')
			->where('c.is_deleted','No')
			->get();
		$rec = $query->getRowArray();
		return $rec;
	}
	
	public function modal(){
		$data['form_heading'] = "Enquiry";
		$data['action'] = "add";
		$data['success'] = false;
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules();
			if($this->validate($rules)){ 
				$this->send_mail();
				$data['success'] = true;
			}
		}
		echo $this->view_partial('enquiry_modal',$data);		
	}
	
	public function get_cat_prod_options($cat_id){
		echo $this->cmObj->get_category_product_options($cat_id);
	}
	
	private function validate_captcha($code){
		if($code==$this->session->get('captcha_code')){
			$response = "TRUE|";			
		}else{
			$response = "FALSE|Invalid Security Code!";
		}
		return $response;
	}
}