<?php
namespace App\Controllers;
use App\ThirdParty\Captcha\Captcha;
class Home extends FrontController{ 
	public function index(){
		$data['meta_data'] = $this->get_meta_data();
		$data['blog_list'] = $this->get_blog_list();

		$data['controller_name'] = 'home';
		$data['banner_list'] = $this->get_banner_list();
		$data['testimonial_list']= $this->get_testimonial_list();
		echo $this->view_inner_template('home',$data);
	}
	
	
	private function get_quick_enquiry_validation_rules(){
		$rules['name'] = ['label'=>'Name', 'rules' => 'trim|required'];
		$rules['mobile_no'] = ['label'=>'Mobile No.', 'rules' => 'trim|required|exact_length[10]'];
		return $rules;
	}
	private function get_meta_data(){
		$data['title'] = "Welcome to BOSS SECURITY & SERVICES";
		$data['description'] = "BOSS SECURITY & SERVICES";
		$data['keywords'] = "BOSS SECURITY & SERVICES";
		return $data;
		
	}
	
	private function get_blog_list(){
		$builder = $this->cmObj->get_table_builder('blog AS b');
		$builder->join('blog_ln AS ln',"b.blog_id=ln.blog_id","inner");
		$query  = $builder->select('b.blog_id,ln.title,b.photo_image,b.blog_url,b.publish_date')
				->where('b.is_deleted','No')
				->where('b.status','Active')
				->get();
		return $query->getResultArray();
	}

	private function get_banner_list(){
		$builder = $this->cmObj->get_table_builder('banner');
		$query = $builder->select('banner_id,title,image,video_url,banner_type,
			link,open_in_new_tab')
			->where('status','Active')
			->where('is_deleted','No')
			->orderBy('sort_order','ASC')
			->get();
		return $query->getResultArray();
	}
	
	private function get_testimonial_list(){
		$builder = $this->cmObj->get_table_builder('testimonial AS mt');
		$builder->join('testimonial_ln AS ln',"mt.testimonial_id=ln.testimonial_id AND ln.language_code='en'","inner");
		$query = $builder->select('mt.testimonial_id,ln.name,ln.city,ln.description')
			->where('mt.status','Active')
			->where('mt.is_deleted','No')
			->orderBy('RAND()')
			->get();
		return $query->getResultArray();
	}

	
	public function appointement_modal(){
		$data['form_heading'] = 'Book';
		
		if($this->request->getMethod() === 'post'){
			$rules['name'] = ['label'=>'Name', 'rules' => 'trim|required'];
			$rules['email'] = ['label'=>'Email ID', 'rules' => 'trim|required'];
			$rules['phone'] = ['label'=>'Phone No.', 'rules' => 'trim|required'];
			$rules['city'] = ['label'=>'City', 'rules' => 'trim|required'];
			$rules['state'] = ['label'=>'State', 'rules' => 'trim|required'];
			$rules['profession'] = ['label'=>'Customer Profile', 'rules' => 'trim|required'];
			$rules['looking_for'] = ['label'=>'Looking For', 'rules' => 'trim|required'];
			$rules['captcha_code'] = ['label'=>'Security Code', 'rules' => 'trim|required|pr_callback['.$this->validate_captcha($this->request->getPost('captcha_code')).']'];
			if($this->validate($rules)){
				$data_in['name'] = $this->request->getPost('name');
				$data_in['email'] = $this->request->getPost('email');
				$data_in['phone'] = $this->request->getPost('phone');
				$data_in['city'] = $this->request->getPost('city');
				$data_in['state'] = $this->request->getPost('state');
				$data_in['profession'] = $this->request->getPost('profession');
				$data_in['looking_for'] = $this->request->getPost('looking_for');
				$data_in['status'] = 'Active';
				$data_in['is_deleted'] = 'No';
				$data_in['added_date'] = $data_in['last_updated_date'] = date('Y-m-d H:i:s');
				$this->send_mail($data_in);
				echo 'SUCCESS|';
				exit();
			}								
		}
		$captcha = new Captcha();
		$data['captcha'] = $captcha->get_captcha();
		$this->session->set('captcha_code',$data['captcha']['code']);
		
		echo $this->view_partial('appointement_modal',$data);
	}

	private function send_mail($data){
		$data['rec'] = $data;
		$html = $this->view_email_template('appointment',$data);
		$email = $this->cmObj->get_email_object();
		$email->setTo('ravi.borade@chittaranjan.co.in');
		$email->setBCC('enquiry@chittaranjan.co.in');
		$email->setSubject('Enquiry from '.$data['rec']['name']);
		$email->setMessage($html);	
		$email->send();
		//var_dump($email->printDebugger());
	}
	
	private function validate_captcha($code){
		if($code==$this->session->get('captcha_code')){
			$response = "TRUE|";			
		}else{
			$response = "FALSE|Invalid Security Code!";
		}
		return $response;
	}
	
}
