<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Candidate extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library('layouts');
		
		checkDashboard();

		$this->session->set_userdata('menu', "candidate");
		
	}
	
	public function index(){
		$this->lang->load("company",$this->session->userdata('language'));
		$this->lang->load("position",$this->session->userdata('language'));
		$this->lang->load("candidate",$this->session->userdata('language'));
		
		$this->load->model('candidate_model');
		
		$data['candidateList'] = $this->candidate_model->candidate_all();
		$data['positionList'] = $this->candidate_model->get_position();
		$data['companyList'] = $this->candidate_model->get_company();
		
		$data['respond']['type'] = NULL;
		$data['respond']['message'] = NULL;
		if($flashdata = $this->flashdata->get()) {
			$data['respond']['type'] = (isset($flashdata['type'])) ? strtoupper($flashdata['type']) : NULL;
			$data['respond']['message'] = (isset($flashdata['message'])) ? $flashdata['message'] : NULL;
		}
		
		$this->templates->setData($data);
			
		$this->layouts->view('candidate/index', array(), $data);
		

	}
	
	/**
	* MODIFIED BY ZUL ZOLKAFFLY
	*/
	public function saveCandidate(){		
		$data['candidateID'] = (isset($_POST['candidateID']) && trim($_POST['candidateID'])) ? trim($_POST['candidateID']) : 0;
		$data['candidateName'] = (isset($_POST['candidateName']) && trim($_POST['candidateName'])) ? trim($_POST['candidateName']) : NULL;
		$data['positionID'] = (isset($_POST['positionID']) && trim($_POST['positionID'])) ? trim($_POST['positionID']) : 0;
		$data['companyID'] = (isset($_POST['companyID']) && trim($_POST['companyID'])) ? trim($_POST['companyID']) : 0;
		
		$resetuserfile = (isset($_POST['resetuserfile']) && trim($_POST['resetuserfile'])) ? TRUE : FALSE;
		
		$proceed = TRUE;
		$message = NULL;
		
		if(trim( $data['candidateName'] ) === "") { $proceed = FALSE; $message[] = "Please enter candidate name."; }	
		if(!$data['positionID']) { $proceed = FALSE; $message[] = "Please select candidate designation."; }
		
		if($proceed) {
			$user = $this->session->userdata('logged_in');
			$userid = ($user && $user['userID']) ? $user['userID'] : 0;
			$data['projectID'] = $this->session->userdata('projectID');
			
			$this->load->model('candidate/candidatemain','candmain');
			
			if($b = $this->candmain->saveCandidate($data, $userid)) {
				if($resetuserfile) {
					if($this->candmain->resetImage($b, $data['projectID'])) {
						$proceed = TRUE;
						$message = NULL;
					} else {
						$proceed = FALSE;
						$message[] = "Fail to reset candidate image";
					}
				} else {
					if($_FILES && !empty($_FILES) && isset($_FILES['userfile']) && $_FILES['userfile']['size']) {
						$r = $this->uploadImage($b);
						if($r['message'] == 'SUCCESS') {
							if($this->candmain->saveImage($b, $data['projectID'], $r['file'])) {
								$proceed = TRUE;
								$message = NULL;
							} else {
								$proceed = FALSE;
								$message[] = "Fail to save candidate image";
							}
						} else {
							$proceed = FALSE;
							$message[] = "Fail to upload candidate image";
						}
					} else {
						$proceed = TRUE;
						$message = NULL;
					}
				}
			} else {
				$proceed = FALSE;
				$message[] = "Fail to save due to technical issue";
			}
		
		}
		
		if(!$proceed && $message && !empty($message)) {
			$this->flashdata->clear();
			$this->flashdata->set(implode("<br />",$message),'message');
			$this->flashdata->set('FAIL','type');
		} else {
			$this->flashdata->clear();
			$this->flashdata->set('SUCCESS','message');
			$this->flashdata->set('SUCCESS','type');
		}
		
		$url = base_url()."candidate";
		redirect($url, 'refresh');
	}
	
	private function uploadImage($id) {
		$ext = array('jpg','jpe','jpeg','png','gif','ico','bmp','svg');
		
		$rel = "img/candidate";
		$virt = base_url().$rel;
		$abso = str_replace('\\','/',FCPATH);
		$dir = str_replace('\\','/',$abso).$rel;
		$key = 'userfile';
		$config = array(
			'allowed_types' => implode("|",$ext),
			'upload_path' => $dir,
			'file_name' => $id,
			'max_size' => '6048',
			'overwrite' => TRUE
		);
	
		$return['path'] = NULL;
		$return['file'] = NULL;
		$return['message'] = NULL;
		if(file_exists($dir)) {				
			chmod($dir, 0755);
			if (is_uploaded_file($_FILES[$key]['tmp_name'])) {
				$this->load->library('upload', $config);			
				if ($this->upload->do_upload($key)) {
					if($data = $this->upload->data()) {	
						$return['path'] = $data['full_path'];
						$return['file'] = $data['file_name'];
						$return['message'] = "SUCCESS";
					} else {
						$return['message'] = $this->upload->display_errors();
						$return['file'] = NULL;
						$return['path'] = NULL;
					}
				} else {
					$return['message'] = $this->upload->display_errors();
					$return['file'] = NULL;
					$return['path'] = NULL;
				}
			}
		} else {
			$return['message'] = "Location to upload does not exist";
			$return['file'] = NULL;
			$return['path'] = NULL;
		}
		
		return $return;
	}
	/**
	* MODIFIED BY ZUL ZOLKAFFLY
	*/
	
	public function deleteCandidate(){
		
		$this->load->model('candidate_model');
		
		$candidateID = $this->input->post('candidateID');
				
		$deleteCandidate = $this->candidate_model->delete('candidate', $candidateID);
		
		if($deleteCandidate){
			print json_encode(array("status"=>"success"));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}
		
	}
	
	public function printCandidate($valuedatatable, $position = NULL, $company = NULL, $mode=false){
		
		$this->lang->load("candidate",$this->session->userdata('language'));
		
		$this->load->model('candidate_model');
		
		if($valuedatatable != "all"){
			$valuedatatable2 = str_replace("'", '', $valuedatatable);
			
			$valuedatatable3 = explode('%20', $valuedatatable2);
			
			$length = sizeof($valuedatatable3);
			
			$valuedatatable4 = "";
			
			$count1 = 1;
			
			for($i=0;$i<$length;$i++){
				if($count1 == 1){
					$valuedatatable4 = "CONCAT(a.candidateName, c.positionName, d.companyName) LIKE '%".$valuedatatable3[$i]."%'";
				}
				else if($count1 < $length){
					$valuedatatable4 .= " AND CONCAT(a.candidateName, c.positionName, d.companyName) LIKE '%".$valuedatatable3[$i]."%'";
				}
				else if($count1 == $length){
					$valuedatatable4 .= " AND CONCAT(a.candidateName, c.positionName, d.companyName) LIKE '%".$valuedatatable3[$i]."%'";
				}
				
				$count1++;
			}
			
			$valuedatatable5 = $valuedatatable4;
			
			if($position) $valuedatatable5 .= " AND a.positionID = ".$position." ";
			if($company) $valuedatatable5 .= " AND a.companyID = ".$company." ";
		
			$bodyData['rptDataDetail'] = $this->candidate_model->candidate_filterbydatatables($valuedatatable5);
		}
		else{
			if(!$position && !$company) {
				$bodyData['rptDataDetail'] = $this->candidate_model->candidate_all();
			} else {
				$where = NULL;
				if($position) $where .= " a.positionID = ".$position." ";
				if($company) $where .= " AND a.companyID = ".$company." ";
				
				$bodyData['rptDataDetail'] = $this->candidate_model->candidate_filterbydatatables($where);
			}
		}
			
			$headerData['rptTitle'] = $this->lang->line('candidateList');
			
			$bodyData['tableHeader'] = array($this->lang->line('candidateName'), $this->lang->line('designation'), $this->lang->line('companyName'), $this->lang->line('picture'));
			
			$bodyData['selectFields'] = array("candidateName", "positionName", "companyName", "candidate_picture");
			//$bodyData ['tdAmount'] = array("elected", "ordering");
			
			if (!$mode) {
				//print
				$this->load->view('layouts/report/header', $headerData);
				$this->load->view('layouts/report/body', $bodyData);
				$this->load->view('layouts/report/footer', "");
			} else {
				//download excel
				$this->load->helper('excel');
				excel($headerData, array($bodyData), '');
			}
		
		
		
	}
	
	public function getposition(){
		
		$this->load->model('candidate_model');
		
		$positionList = $this->candidate_model->get_position();
		
		print json_encode(array("status"=>"success", "message"=>$positionList));
		

	}
	
	public function getcompany(){
		
		$this->load->model('candidate_model');
		
		$companyList = $this->candidate_model->get_company();
		
		print json_encode(array("status"=>"success", "message"=>$companyList));
		

	}

	public function savePosition(){
		$positionData = array(
			'projectID'		=> $this->session->userdata('projectID'),
			'positionName' 	=> $this->input->post('positionName'),
			'elected' 		=> $this->input->post('elected')
		);

		$this->db->insert('position', $positionData);

		$fresh = $this->db->insert_id();

		print json_encode(array("status"=>"success", "insertedID"=>$fresh));
	}

	public function saveCompany(){
		$companyData = array(
			'projectID'		=> $this->session->userdata('projectID'),
			'companyName' 	=> $this->input->post('companyName')
		);

		$this->db->insert('company', $companyData);

		$fresh = $this->db->insert_id();

		print json_encode(array("status"=>"success", "insertedID"=>$fresh));
	}
	
	
	
}

?>