<?php namespace App\Controllers;
use App\ThirdParty\Captcha\Captcha;
class Work_with_us extends FrontController{	 
	public function index(){		
		$data['meta_data']['title'] = "Get a Quote";
		$data['meta_data']['description'] = "Request a quote";
		$data['meta_data']['keywords'] = "Get a Quote";
		$data['enq_msg'] = "";
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules();
			if($this->validate($rules)) { 
				$this->send_quick_enquiry_mail();
				$this->session->setFlashdata('enquiry_status',"Your enquiry sent successfully!");
				return redirect()->to(base_url('work_with_us'));
			}
		}

		$captcha = new Captcha();
		$data['captcha'] = $captcha->get_captcha();
		$this->session->set('captcha_code',$data['captcha']['code']);
		echo $this->view_inner_template('work_with_us',$data);
	} 

	public function send_enquiry(){
		$data['enq_msg'] = "";
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_send_enquiry_rules();
			if($this->validate($rules)) { 
				unset($_POST['name']);
				$this->send_mail();
				$data['enq_msg'] = "Your enquiry send successfully";
				return $this->response->setBody('SUCCESS');
			}
		}
		echo $this->view_partial('enquiry_form',$data);
	}
	

	private function get_validation_rules(){
		$rules['name'] = ['label'=>'Name', 'rules' => 'trim|required'];
		$rules['phone'] = ['label'=>'Mobile No.', 'rules' => 'trim|required|exact_length[10]'];
		$rules['email'] = ['label'=>'Email', 'rules' => 'trim|required'];
		$rules['services'] = ['label'=>'Services', 'rules' => 'trim|required'];
		$rules['captcha_code'] = ['label'=>'Security Code', 'rules' => 'trim|required|pr_callback['.$this->validate_captcha($this->request->getPost('captcha_code')).']'];

		$rules['message'] = ['label'=>'Any Request', 'rules' => 'trim'];
		return $rules;
	}
	private function get_send_enquiry_rules(){
		$rules['name'] = ['label'=>'Name', 'rules' => 'trim|required'];
		$rules['email'] = ['label'=>'Email ID', 'rules' => 'trim|required'];
		$rules['phone'] = ['label'=>'Mobile No.', 'rules' => 'trim|required|exact_length[10]'];
		$rules['services'] = ['label'=>'Services', 'rules' => 'trim|required'];
		$rules['message'] = ['label'=>'Any Request', 'rules' => 'trim'];
		return $rules;
	}
	
	private function send_mail(){
		$data['rec'] = $this->request->getPost();
	
		$html = $this->view_email_template('enquiry',$data);
		$email = $this->cmObj->get_email_object();
		$email->setTo('boss.backoffice@gmail.com');
		// $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;
	}
	private function send_quick_enquiry_mail(){
		$data['rec'] = $this->request->getPost();
		
		$html = $this->view_email_template('enquiry',$data);
		$email = $this->cmObj->get_email_object();
		$email->setTo('boss.backoffice@gmail.com');
		// $email->setBCC('enquiry@chittaranjan.co.in');
		$email->setSubject('Enquiry from '.$data['rec']['name']);
		$email->setMessage($html);	
		$email->send();
		//var_dump($email->printDebugger());
	}
}