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

class Voter extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library('layouts');
		$this->load->model('voter_model');
		
		checkDashboard();
		$this->session->set_userdata('menu', "voter");
		
	}
	
	public function index(){
		$projectID = $this->session->userdata('projectID');
		$this->lang->load("voter",$this->session->userdata('language'));
		
		$data['projectID'] = $this->session->userdata('projectID');
		$data['voterList'] = $this->voter_model->voter_byprojectID($data['projectID']);
		$data['companyList'] = $this->voter_model->company_all();
		
		$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('voter/index', array(), $data);
	}
	
	public function voterlist(){
		$projectID = $this->session->userdata('projectID');
		
		$this->lang->load("voter",$this->session->userdata('language'));
		
		$status = $this->uri->segment(3);
		
		$data['status'] = $status;
		
		if($status == "99"){
			$data['voterList'] = $this->voter_model->voter_byprojectID($projectID);

			// var_dump($data['voterList']);
			// exit();
		}
		else{
			$data['voterList'] = $this->voter_model->voter_bystatus($status);
		}
		
		$data['companyList'] = $this->voter_model->company_all();
			
		$this->layouts->view('voter', array(), $data);
	}
	
	public function saveVoter(){ // NEW BY ZUL 17/04/2017
		$this->load->model('voter/votermain','votermain');
		
		$data['projectID'] = $this->session->userdata('projectID');
		$data['userID'] = (trim($_POST['userID'])) ? trim($_POST['userID']) : 0;
		$data['noAhli'] = (trim($_POST['noAhli'])) ? trim($_POST['noAhli']) : NULL;
		$data['voterName'] = (trim($_POST['voterName'])) ? trim($_POST['voterName']) : NULL;
		$data['companyID'] = (trim($_POST['companyID'])) ? trim($_POST['companyID']) : NULL;
		$data['userPassword'] = (trim($_POST['userPassword'])) ? trim($_POST['userPassword']) : NULL;
		$data['confirmuserPassword'] = (trim($_POST['confirmuserPassword'])) ? trim($_POST['confirmuserPassword']) : NULL;
		
		$proceed = TRUE;
		$message = NULL;
		
		if(!trim($data['noAhli'])) { $proceed = FALSE; $message[] = "Invalid or Empty Voter ID."; }
		if($this->votermain->checkVoterNoInUse($data['noAhli'], $data['projectID'], $data['userID'])) { $proceed = FALSE; $message[] = "Voter ID is in used."; }
		
		if(trim($data['userPassword'])) {				
			if(trim( $data['confirmuserPassword'] ) === "") { $proceed = FALSE; $message[] = "Please enter voter password confirmation."; }
			if(trim($data['userPassword']) != trim($data['confirmuserPassword'])) { $proceed = FALSE; $message[] = "Password confirmation not matched."; }
		}		
		
		if(!$data['userID']) {			
			if(trim( $data['userPassword'] ) === "") { $proceed = FALSE; $message[] = "Please enter your voter password."; }
			if(trim( $data['confirmuserPassword'] ) === "") { $proceed = FALSE; $message[] = "Please enter voter password confirmation."; }
			if(trim($data['userPassword']) != trim($data['confirmuserPassword'])) { $proceed = FALSE; $message[] = "Password confirmation not matched."; }
		}
		
		if($proceed) {
			$data['voterName'] = (trim($data['voterName'])) ? trim($data['voterName']) : 'ANONYM'.(rand(0,1000));
			if($this->votermain->saveVoter($data)) {
				$proceed = TRUE;
				$message = NULL;
			} else {
				$proceed = FALSE;
				$message[] = "Fail to save voter detail 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()."voter";
		redirect($url);
	}
	
	public function saveVoter2(){ // ORIGINAL BY FAD
		$projectID = $this->session->userdata('projectID');
		$userID = $this->input->post('userID');
		$voterID = $this->input->post('noAhli');
		
		// die(var_dump($userID));

		if($this->input->post('voterName')){
			$voterName = $this->input->post('voterName');
		}
		else{
			$voterName = "NA";
		}

		$companyID = $this->input->post('companyID');

		$userPassword = $this->input->post('userPassword');
		$confirmuserPassword = $this->input->post('confirmuserPassword');
		
		$this->load->model('voter_model');

		if($userID == "" || $userID == NULL){
			// check if voterID is unique
			if($this->voter_model->checkVoter($voterID) == false){
				if($userPassword != "" || $confirmuserPassword != ""){
					$data = array(
								'noAhli' => $voterID,
								'name' => $voterName,
								'projectID' => $projectID,
								'status' => 0,
								'companyID' => $companyID,
								'userPassword' => md5($userPassword)
							);
						
					$insertPosition = $this->voter_model->insert('user', $data);
					
					redirect('voter', 'refresh');
					
				}
				else{
					echo "<script>alert('error occured')</script>";
					redirect('voter', 'refresh');
				}
			} else {
				$message = "This VoterID has already been taken";
				echo "<script type='text/javascript'>alert('$message');window.location = ('".base_url('voter')."') </script>";
			}
		}
		else{
			if($userPassword == ""){
				$data = array(
							'noAhli' => $voterID,
							'name' => $voterName,
							'status' => 0,
							'companyID' => $companyID
						);
			}
			else{
				$data = array(
							'noAhli' => $voterID,
							'name' => $voterName,
							'status' => 0,
							'companyID' => $companyID,
							'userPassword' => md5($userPassword)
						);
			}
			$updatePosition = $this->voter_model->update('user', $userID, $data);
		
			redirect('voter', 'refresh');
		}
	}

	public function checkVoterID(){
		$this->load->model('voter_model');
		
		$noAhli = $this->input->post('noAhli');

		$voter = $this->voter_model->getVoterbyVoterID($noAhli);

		 if($voter == null){ //proceed
			print json_encode(array("status"=>"success"));
		 }
		 else{
			 //check if self 1st
			 if($voter['userID'] == $this->input->post('userID')){
				print json_encode(array("status"=>"success"));
			 } 
			 else{
				print json_encode(array("status"=>"failed"));
			 }
		 }
		
	}
	
	public function deleteVoter(){
		
		$this->load->model('voter_model');
		
		$userID = $this->input->post('userID');
				
		$deleteVoter = $this->voter_model->delete('user', $userID);
		
		if($deleteVoter){
			print json_encode(array("status"=>"success"));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}
		
	}
	
	public function printVoter($status, $valuedatatable, $mode=false){
		
		$this->lang->load("voter",$this->session->userdata('language'));
		
		$this->load->model('voter_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.noAhli, a.name, c.companyName) LIKE '%".$valuedatatable3[$i]."%'";
					}
					else if($count1 < $length){
						$valuedatatable4 .= " AND CONCAT(a.noAhli, a.name, c.companyName) LIKE '%".$valuedatatable3[$i]."%'";
					}
					else if($count1 == $length){
						$valuedatatable4 .= " AND CONCAT(a.noAhli, a.name, c.companyName) LIKE '%".$valuedatatable3[$i]."%'";
					}
				
				$count1++;
			}
			
			$valuedatatable5 = $valuedatatable4;
			
			if($status == 99){
				$bodyData['rptDataDetail'] = $this->voter_model->voter_filterbydatatables($valuedatatable5);
			}
			else{
				$bodyData['rptDataDetail'] = $this->voter_model->voter_filterbydatatablesandstatus($status, $valuedatatable5);
			}
		
		}
		else{
			
			if($status == 99){
				$bodyData['rptDataDetail'] = $this->voter_model->voter_all();
			}
			else{
				$bodyData['rptDataDetail'] = $this->voter_model->voter_bystatus($status);
			}
			
		}
			
			$headerData['rptTitle'] = $this->lang->line('voterList');
			
			$bodyData['tableHeader'] = array($this->lang->line('voterNo'), $this->lang->line('voterName'), $this->lang->line('companyName'), $this->lang->line('status'));
			
			$bodyData['selectFields'] = array("noAhli", "name", "companyName", "statusName");
			//$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 getcompany(){
		
		$this->load->model('voter_model');
		
		$companyList = $this->voter_model->company_all();
		
		print json_encode(array("status"=>"success", "message"=>$companyList));
		

	}

	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));
	}
}

?>