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

class V extends CI_Controller {
	public function __construct(){
		parent::__construct();
		
		$this->load->model('voting/voting_main');
		
		$this->templates->setPath('templates');
		$this->templates->setLayout('voting');
		
		$this->lang->load('voting_main','en');
	}
	
	public function index($p){
		$data = $this->templates->getData();
		
		$data['error'] = 0;
		$data['message'] = NULL;
		if($data['project'] = $this->voting_main->getProject($p)) {
			if(!$this->voting_main->isProjectPaid($p)) $data['error'] = 2; // PROJECT NOT PAID
			if(!$this->voting_main->isProjectStarted($p)) $data['error'] = 3; // PROJECT NOT STARTED
			if($this->voting_main->isProjectStopped($p)) $data['error'] = 4; // PROJECT IS STOPPED
			if(!$this->voting_main->isProjectSetupComplete($p)) $data['error'] = 5; // PROJECT NOT COMPLETE
		} else {
			$data['error'] = 1; // PROJECT NOT FOUND
		}
		$data['lines'] = $this->lang->language;
		
		$this->templates->setView('voting/index')->setData($data)->render();
	}
	
	public function checkloginajax() {
		$projectID = ($_POST['projectID']) ? trim($_POST['projectID']) : NULL;
		$voterID = ($_POST['voterID']) ? trim($_POST['voterID']) : NULL;
		$key = ($_POST['key']) ? trim($_POST['key']) : NULL;
		$lang = ($_POST['lang']) ? trim($_POST['lang']) : 'EN';
		
		$this->lang->load('voting_main',strtolower($lang));
		
		if($voterID == "") { echo $this->lang->line('voter_id_empty'); return; }
		if($key == "") { echo $this->lang->line('voter_key_empty'); return; }
		
		$v = $this->voting_main->getVoter($projectID, $voterID);
		
		if(!$v) { echo $this->lang->line('voter_not_exists'); return; }
		if(md5($key) != $v['userPassword']) { echo $this->lang->line('voter_key_incorrect'); return; }
		if($v['status']) { echo $this->lang->line('voter_voted'); return; }
		if($v['validity']) { echo $this->lang->line('voter_invalid'); return; }
		
		echo "SUCCESS";
	}
		
	public function loadsegmentajax() {
		$projectID = ($_POST['projectID']) ? trim($_POST['projectID']) : NULL;
		$voterID = ($_POST['voterID']) ? trim($_POST['voterID']) : NULL;
		$lang = ($_POST['lang']) ? trim($_POST['lang']) : NULL;
		$step = ($_POST['step']) ? trim($_POST['step']) : 0;
		$pos = ($_POST['pos']) ? trim($_POST['pos']) : 0;
		$userID = ($_POST['userID']) ? $_POST['userID'] : $this->voting_main->getVoterUserID($projectID, $voterID);
		
		$this->lang->load('voting_main',strtolower($lang));
		$data['lines'] = $this->lang->language;
		
		$data['step'] = $step;
		$data['pos'] = $pos;
		
		if($step == 0 && $pos == 0) $this->voting_main->clearVoting($projectID, $userID);
		
		if($step == 1) {
			if($pos == 1) $this->voting_main->startVoting($projectID, $userID);
			
			if($pos) {
				if($data['position'] = $this->voting_main->getPositionByOrder($projectID, $pos)) {
					$data['candidates'] = $this->voting_main->getPositionCandidates($projectID, $data['position']['positionID']);
				}
			}
		
			$prev = $this->voting_main->getPrevPositionOrder($projectID, $data['position']['positionID']);
			$data['prev_step'] = $prev ? 1 : 0;
			$data['prev_pos'] = $prev ? $prev : 0;
			unset($prev);
		}
		
		if($step == 2) {
			$this->voting_main->endVoting($projectID, $userID);
			$data['summary'] = $this->voting_main->getVoteSummary($projectID, $userID);
		}
		
		$voter = $this->voting_main->getVoter($projectID, $voterID);
		$data['profile']['voterID'] = $voterID;
		$data['profile']['userID'] = $voter['userID'];
		$data['profile']['voterName'] = $voter['name'];
		$data['profile']['companyName'] = $voter['companyName'];
		$data['profile']['lang'] = $lang;
		$data['profile']['startDT'] = ($voter['start_time']) ? date('d-m-Y h:i:s a', strtotime($voter['start_time'])) : NULL;
		$data['profile']['endDT'] = ($voter['end_time']) ? date('d-m-Y h:i:s a', strtotime($voter['end_time'])) : NULL;
		
		$this->templates->setData($data);
		$this->load->view('voting/segments/header',$data);
		$this->load->view('voting/segments/step'.$step,$data);
	}
	
	public function loadresultajax() {
		$projectID = ($_POST['projectID']) ? trim($_POST['projectID']) : NULL;
		$voterID = ($_POST['voterID']) ? trim($_POST['voterID']) : NULL;
		$lang = ($_POST['lang']) ? trim($_POST['lang']) : NULL;
		
		$this->lang->load('voting_main',strtolower($lang));
		$data['lines'] = $this->lang->language;		
		$data['result'] = $this->voting_main->getVoteResults($projectID);
		
		$this->templates->setData($data);
		$this->load->view('voting/result',$data);
	}
	
	public function loadlangajax() {
		$lang = ($_POST['lang']) ? trim($_POST['lang']) : 'EN';		
		$this->lang->load('voting_main',strtolower($lang));		
		$lines = $this->lang->language;		
		echo json_encode($lines);
	}
	
	public function submitvoteajax() {
		$projectID = ($_POST['projectID']) ? trim($_POST['projectID']) : NULL;
		$voterID = ($_POST['voterID']) ? trim($_POST['voterID']) : NULL;
		$lang = ($_POST['lang']) ? trim($_POST['lang']) : NULL;
		$step = ($_POST['step']) ? trim($_POST['step']) : 0;
		$pos = ($_POST['pos']) ? trim($_POST['pos']) : 0;
		$userID = ($_POST['userID']) ? $_POST['userID'] : $this->voting_main->getVoterUserID($projectID, $voterID);
		$positionID = ($_POST['positionID']) ? trim($_POST['positionID']) : 0;
		$chosen = (isset($_POST['chosen']) && !empty($_POST['chosen'])) ? $_POST['chosen'] : NULL;
		
		$status = "SUCCESS";
		$message = NULL;
		if($position = $this->voting_main->getPositionByID($projectID, $positionID)) {
			$limit = $position['elected'] ? $position['elected'] : 1;
			$selected = count($chosen);
			
			if($selected > $limit) {
				$status = "FAIL";
				$message = 'Please choose '.$limit.' candidate'.(($limit > 1) ? 's' : NULL).' for this designation.';
			} else {
				$status = "SUCCESS";
				$message = "Success";
				if($this->voting_main->clearVoterPositionVote($projectID, $userID, $positionID)) {
					foreach($chosen as $candidateID) {
						if(!$this->voting_main->saveVote($projectID, $userID, $positionID, $candidateID)) {
							$status = "FAIL";
							$message = "Fail to save vote selection";
							break;
						}
					}
				} else {
					$status = "FAIL";
					$message = "Fail to prepare voter record for saving.";
				}
			}
		} else {
			$status = "FAIL";
			$message = "Unable to find designation.";
		}
		
		$next_step = $step;
		$next_pos = $pos;
		if($status == 'SUCCESS') {
			$next = $this->voting_main->getNextPositionOrder($projectID, $positionID);
			$next_step = $next ? 1 : 2;
			$next_pos = $next ? $next : 0;
			unset($next);
		}
		
		$return = array( 'status' => $status, 'message' => $message, 'next-step' => $next_step, 'next-pos' => $next_pos );
		echo json_encode($return);		
	}
	
	public function confirmvoteajax() {
		$projectID = ($_POST['projectID']) ? trim($_POST['projectID']) : NULL;
		$voterID = ($_POST['voterID']) ? trim($_POST['voterID']) : NULL;
		$lang = ($_POST['lang']) ? trim($_POST['lang']) : NULL;
		$step = ($_POST['step']) ? trim($_POST['step']) : 0;
		$pos = ($_POST['pos']) ? trim($_POST['pos']) : 0;
		$userID = ($_POST['userID']) ? $_POST['userID'] : $this->voting_main->getVoterUserID($projectID, $voterID);
		
		$status = ($this->voting_main->confirmVoting($projectID, $userID)) ? "SUCCESS" : "FAIL";
		$showResult = ($status == "SUCCESS" && $this->voting_main->showResultToVoter($projectID)) ? 1 : 0;
		
		$return = array( 'status' => $status, 'showResult' => $showResult );
		echo json_encode($return);	
	}
	
	public function p($p){
		return $this->index($p);
		/*
		$projectID = $this->uri->segment(3);	
		redirect('voting/vote/'.$projectID, 'refresh');
		*/
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}

?>