<?php namespace App\Controllers;
use App\ThirdParty\Captcha\Captcha;

class Career extends FrontController
{	   
	public function index(){	 	
		$data['meta_data']['title'] = "Career Opportunities at Window Expert Tuff Glaze | Join Our Team";
		$data['meta_data']['description'] = "Career Opportunities";
		$data['meta_data']['keywords'] = "Career Opportunities";
		
		$data['career_list'] = $this->get_career_list(); 

		
		echo $this->view_inner_template('career',$data);
	}

	private function get_career_list(){
		$builder = $this->cmObj->get_table_builder('career');
		$query = $builder->where('status','Active')
				->where('is_deleted','No')
				->orderBy('career_id','DESC')
				->get();
		return $query->getResultArray();
	}

	public function apply($url){
		$data['meta_data']['title'] = "Career";
		$data['meta_data']['description'] = "Manufacturing jobs offer a ton of entry-level opportunities. You can actually see the result of your hard work.";
		$data['meta_data']['keywords'] = "Career in Window and Door Manufacturing, Manufacturing Company of Window and Door, Deliver and Installation Window, Career with Window Expert, Vacancy in Manufacturing";
		
		$data['url'] = $url;
		$data['rec'] = $this->get_career_dtl($url);
		
		if($this->request->getPost()){
			$rules['name'] = ['label'=>'First Name', 'rules' => 'trim|required'];
			$rules['email_id'] = ['label'=>'Email ID', 'rules' => 'trim|required'];
			$rules['phone_no'] = ['label'=>'Phone No.', 'rules' => 'trim|required|regex_match[/^[0-9]{10}$/]'];
			//$rules['file'] = ['label'=>'Upload CV', 'rules' => 'trim|required'];
			$rules['city'] = ['label'=>'City', 'rules' => 'trim|required'];
			$rules['age'] = ['label'=>'Age', 'rules' => 'trim|required'];
			//$rules['start_date'] = ['label'=>'Start Date', 'rules' => 'trim|required'];
			$rules['looking_for'] = ['label'=>'Looking For', 'rules' => 'trim'];
			$rules['cv_file'] = ['label'=>'CV File', 'rules'=>'uploaded[cv_file]|max_size[cv_file,2048]|ext_in[cv_file,png,jpg,jpeg,pdf,doc]'];
			$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['career_id'] = $data['rec']['career_id'];
				$data_in['name'] = $this->request->getPost('name');
				$data_in['email_id'] = $this->request->getPost('email_id');
				$data_in['phone_no'] = $this->request->getPost('phone_no');		
				$data_in['city'] = $this->request->getPost('city');
				$data_in['age'] = $this->request->getPost('age');
				$data_in['expected_salary'] = $this->request->getPost('expected_salary');
				//$data_in['start_date'] = date('Y-m-d', strtotime($this->request->getPost('start_date')));
				$data_in['experience'] = $this->request->getPost('experience');
				$data_in['looking_for'] = $this->request->getPost('looking_for');
				$data_in['added_date'] = $data_in['last_updated_date'] = date('Y-m-d H:i:s');
				$this->save_uploaded_file($data_in);
				
				$builder = $this->cmObj->get_table_builder('career_application');
				$builder->insert($data_in);
				$this->send_mail($data['rec'],$data_in);
				$this->session->setFlashdata('action_status',"Application sent successfully");
				
				return redirect()->to(base_url('career/apply/'.$url));
			}
		}
		$data['footer_includes'] = ['career_apply_js'];
		$captcha = new Captcha();
		$data['captcha'] = $captcha->get_captcha();
		$this->session->set('captcha_code',$data['captcha']['code']);
		echo $this->view_inner_template('career_apply',$data);
	}
	
	private function get_career_dtl($url){
		$builder = $this->cmObj->get_table_builder('career');
		$query = $builder->where('career_url',$url)->get();
		return $query->getRowArray();
	}
	
	private function save_uploaded_file(&$data){
		$file = $this->request->getFile('cv_file');
		if($file->getName()){
			$file->move(ROOTPATH . 'public/uploads/',$file->getRandomName());
			$data['cv_file'] = $file->getName();
		}
	}
	
	private function send_mail($career,$application_data){
		$data['career'] = $career;
		$data['rec'] = $application_data;
		$html = $this->view_email_template('career',$data);
		$email = $this->cmObj->get_email_object();
		$email->setTo('boss.backoffice@gmail.com');
		// $email->setBCC('enquiry@chittaranjan.co.in');
		$email->setSubject('Career Application '.$data['rec']['name']);
		$email->setMessage($html);	
		if($data['rec']['cv_file']){
			$email->attach(ROOTPATH.'public/uploads/'.$data['rec']['cv_file'], 'attachment', 'cv_file.pdf');
		}
		$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;
	}
}