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

class Voting extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library('layoutsclient');
		checkFront();
		if(!$this->session->userdata('language')){
			$this->session->set_userdata('language', 'en');
		}
		
	}
	
	public function index(){
		
		
	}
	
	public function vote(){
	
		$this->lang->load("voting",$this->session->userdata('language'));
		
		$this->load->model('voting_model');
		
		$this->session->unset_userdata('voterUserID');
		$this->session->unset_userdata('voterStatus');
		$this->session->unset_userdata('voterNoAhli');
		
		
		$data['projectID'] = $this->uri->segment(3);
		
			
		$this->layoutsclient->view('client/clientvalidation', array(), $data);
	
	}
	
	
	public function checkvote(){
	
		$noAhli = $this->input->post('noAhli');
		$password = $this->input->post('password');
		$projectID = $this->input->post('projectID');
		
		$this->load->model('voting_model');
		
		$checkvote = $this->voting_model->checkmemberNo($noAhli, $projectID);
		
		
		if($checkvote){
			
			$status = $checkvote[0]->status;
			$validity = $checkvote[0]->validity;
			$userPassword = $checkvote[0]->userPassword;
			$projectID = $checkvote[0]->projectID;
			
			$stopped = $checkvote[0]->stopped;
			$startDT = strtotime(date("Y-m-d H:i:s", strtotime($checkvote[0]->startDT)));
			$endDT = strtotime(date("Y-m-d H:i:s", strtotime($checkvote[0]->endDT)));
			$paid = $checkvote[0]->paid;
			$currentDT = strtotime(date('Y-m-d H:i:s'));
			
			if($status == 1){
				print json_encode(array("status"=>"failed1"));
			}
			else if($validity == 1){
				print json_encode(array("status"=>"failed3"));
			}
			else if($userPassword != md5($password)){
				print json_encode(array("status"=>"failed10"));
			}
			else if($projectID != $this->input->post('projectID')){
				print json_encode(array("status"=>"failed11"));
			}
			else if($stopped){
				print json_encode(array("status"=>"failed13"));
			}
			else if($startDT > $currentDT || $paid == 0){
				print json_encode(array("status"=>"failed12"));
			}
			else{
				
				$this->session->set_userdata('voterUserID', $checkvote[0]->userID);
				$this->session->set_userdata('voterNoAhli', $checkvote[0]->noAhli);
				$this->session->set_userdata('voterStatus', $checkvote[0]->status);
				
				print json_encode(array("status"=>"success", "message"=>$noAhli."/".$projectID));
			}
		}
		else{
			print json_encode(array("status"=>"failed2"));
		}
	
	}
	
	
	public function confirmvoter(){
		
		if (!$this->session->userdata('voterUserID') || !$this->session->userdata('voterNoAhli')|| $this->session->userdata('voterStatus') == 1) {
            echo '<script>window.location.replace("'.base_url().'voting");</script>';
        }
		
		$this->lang->load("voting",$this->session->userdata('language'));
	
		$noAhli = $this->uri->segment(3);
		$projectID = $this->uri->segment(4);
		
		$this->load->model('voting_model');
		
		$data['projectID'] = $projectID;
		$data['voter'] = $this->voting_model->checkmemberNo($noAhli,$projectID);
		
		$dataupdate = array(
						'start_time' => date("Y-m-d H:i:s"),
					);
		
		$updatestarttime = $this->voting_model->update('user', $data['voter'][0]->userID, $dataupdate);
		
		$data['position'] = $this->voting_model->get_position($projectID);
			
		$this->layoutsclient->view('client/clientconfirm', array(), $data);
	
	}
	
	
	public function selectcandidate(){
	
		if (!$this->session->userdata('voterUserID') || !$this->session->userdata('voterNoAhli')|| $this->session->userdata('voterStatus') == 1) {
            echo '<script>window.location.replace("'.base_url().'voting");</script>';
        }
	
		$positionID = $this->uri->segment(3);
		
		$data['currentpositionID'] = $positionID;
		
		$next = $this->uri->segment(4);
		$projectID = $this->uri->segment(5);
		
		$this->lang->load("voting",$this->session->userdata('language'));
		
		$this->load->model('voting_model');
		
		
		$data['projectID'] = $projectID;
		
		/*if($next == 0){
			
			$getPositionID = $this->voting_model->get_position();
			
			$data['nextID'] = $next + 1;
			
			$data['positionID'] = $getPositionID[$data['nextID']]->positionID;
				
			$data['candidateList'] = $this->voting_model->get_candidate($positionID);
			$data['countcandidate'] = sizeof($data['candidateList']);
			
			$data['positionName'] = $data['candidateList'][0]->positionName;
		
		}
		else{*/
			
			$getPositionID = $this->voting_model->get_position($projectID);
			
			$data['nextID'] = $next + 1;
			
			if($data['nextID'] <> sizeof($this->voting_model->get_position($projectID))){
				$data['positionID'] = $getPositionID[$data['nextID']]->positionID;
			}
			else{
				$data['positionID'] = "";
			}
			
			$data['candidateList'] = $this->voting_model->get_candidate($positionID, $projectID);
			$data['countcandidate'] = sizeof($data['candidateList']);
			$data['elected'] = $data['candidateList'][0]->elected;
			
			$data['positionName'] = $data['candidateList'][0]->positionName;
		
		//}
		
			
		$this->layoutsclient->view('client/clientvote', array(), $data);
	
	}
	
	
	public function getdataby_currentpositionID(){
	
		$this->load->model('voting_model');
		
		$positionID = $this->input->post('positionID');
		$projectID = $this->input->post('projectID');
		
		$getdata = $this->voting_model->get_candidateselectIDandName($positionID, $projectID);
		
		if($getdata){
			print json_encode(array("status"=>"success", "message"=>$getdata));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}
	
	}
	
	
	public function saveradiobutton(){
	
		$userID = $this->input->post('userID');
		$positionID = $this->input->post('positionID');
		$projectID = $this->input->post('projectID');
		$candidateID = $this->input->post('candidateID');
		
		$countcandidateID = sizeof($this->input->post('candidateID'));
		
		$this->load->model('voting_model');
		
		
		$checkdataexist = $this->voting_model->checkvoteby_userID_positionID($userID, $positionID, $projectID);
		
		if($checkdataexist){
			$this->voting_model->deletevoteby_userID_positionID('vote', $userID, $positionID, $projectID);
		}
		
		for($i=0;$i<$countcandidateID;$i++){
			
			$data = array(
						'userID' => $userID,
						'positionID' => $positionID,
						'projectID' => $projectID,
						'candidateID' => $candidateID[$i]
					);
			
			$insertVote = $this->voting_model->insert('vote', $data);
			
		}
		
		if($insertVote){
			print json_encode(array("status"=>"success"));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}
		
	
	}
	
	
	public function savecheckbox(){
	
		$userID = $this->input->post('userID');
		$positionID = $this->input->post('positionID');
		$projectID = $this->input->post('projectID');
		$candidateID = $this->input->post('candidateID');
		
		$countcandidateID = sizeof($this->input->post('candidateID'));
		
		$this->load->model('voting_model');
		
		
		$checkdataexist = $this->voting_model->checkvoteby_userID_positionID($userID, $positionID, $projectID);
		
		if($checkdataexist){
			$this->voting_model->deletevoteby_userID_positionID('vote', $userID, $positionID, $projectID);
		}
		
		for($i=0;$i<$countcandidateID;$i++){
			$data = array(
						'userID' => $userID,
						'positionID' => $positionID,
						'projectID' => $projectID,
						'candidateID' => $candidateID[$i]
					);
			
			$insertVote = $this->voting_model->insert('vote', $data);
		}
		
		if($insertVote){
			print json_encode(array("status"=>"success"));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}
		
	
	}
	
	
	public function votesummary($projectID){
		
		if (!$this->session->userdata('voterUserID') || !$this->session->userdata('voterNoAhli')|| $this->session->userdata('voterStatus') == 1) {
            echo '<script>window.location.replace("'.base_url().'voting");</script>';
        }
		
		$this->lang->load("voting",$this->session->userdata('language'));
	
		$userID = $this->session->userdata('voterUserID');
		
		$this->load->model('voting_model');
		
		$data['projectID'] = $projectID;
		
		$data['votesummarylist'] = $this->voting_model->get_vote($userID, $projectID);
			
		$this->layoutsclient->view('client/clientvotesummary', array(), $data);
	
	}
	
	public function confirmvote($projectID){
		
		$userID = $this->session->userdata('voterUserID');
		
		$this->load->model('voting_model');
		
		$data = array(
					'status' => 1,
					'end_time' => date('Y-m-d H:i:s')
				);
		
		$confirmvote = $this->voting_model->update('user', $userID, $data);
		
		if($confirmvote){
			
			$this->session->set_userdata('voterStatus', 1);
			
			print json_encode(array("status"=>"success"));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}
	
	}
	
	public function cancelvote($projectID){
		
		$userID = $this->session->userdata('voterUserID');
		
		$this->load->model('voting_model');
		
		$cancelvote = $this->voting_model->delete('vote', $userID);
		
		if($cancelvote){
			print json_encode(array("status"=>"success"));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}
	
	}
	
}

?>