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

class Vote extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library('layouts');
		
		checkDashboard();
		$this->session->set_userdata('menu', "vote");
		
	}
	
	public function index(){
		
		$this->lang->load("vote",$this->session->userdata('language'));
		
		$this->load->model('vote_model');
		
		$data['voteList'] = $this->vote_model->vote_all();
			
		//$this->layouts->view('vote', array(), $data);
		
		
		$this->templates->setData($data);
			
		$this->layouts->view('vote/index', array(), $data);

	}
	
	
	
	
	public function deleteVote(){
		
		$this->load->model('vote_model');
		
		$userID = $this->input->post('userID');
		
		$data = array(
					'status' => 0
				);
		
		$updateStatusUser = $this->vote_model->update('user', $userID, $data); 
		$sql = $this->db->last_query(); 
		$deleteVote = $this->vote_model->delete('vote', $userID);
		
		
		if($deleteVote){
			print json_encode(array("status"=>"success", "sql"=>$sql));
		}
		else{
			print json_encode(array("status"=>"failed", "sql"=>$sql));
		}
		
	}
	
	
	
	
	
	public function printVote($valuedatatable, $mode=false){
		
		$this->lang->load("vote",$this->session->userdata('language'));
		
		$this->load->model('vote_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(b.name, c.companyName) LIKE '%".$valuedatatable3[$i]."%'";
				}
				else if($count1 < $length){
					$valuedatatable4 .= " AND CONCAT(b.name, c.companyName) LIKE '%".$valuedatatable3[$i]."%'";
				}
				else if($count1 == $length){
					$valuedatatable4 .= " AND CONCAT(b.name, c.companyName) LIKE '%".$valuedatatable3[$i]."%'";
				}
				
				$count1++;
			}
			
			$valuedatatable5 = $valuedatatable4;
		
		
			$bodyData['rptDataDetail'] = $this->vote_model->vote_filterbydatatables($valuedatatable5);
		
		}
		else{
			$bodyData['rptDataDetail'] = $this->vote_model->vote_all();
		}
		
			$headerData['rptTitle'] = $this->lang->line('voteList');
			
			$bodyData['tableHeader'] = array($this->lang->line('entityName'), $this->lang->line('companyName'));
			
			$bodyData['selectFields'] = array("name", "companyName");
			//$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 viewVote($userID, $mode=false){
		
		$this->lang->load("vote",$this->session->userdata('language'));
		
		$this->load->model('vote_model');
		
		
			$bodyData['rptDataDetail'] = $this->vote_model->vote_byID($userID);
			
			$headerData['rptTitle'] = $this->lang->line('viewvote');
			
			$bodyData['memberNo'] = $bodyData['rptDataDetail'][0]->noAhli;
			$bodyData['entityName'] = $bodyData['rptDataDetail'][0]->name;
			$bodyData['companyName'] = $bodyData['rptDataDetail'][0]->companyName;
			
			$bodyData['tableHeader'] = array($this->lang->line('designation'), $this->lang->line('candidate'));
			
			$bodyData['selectFields'] = array("positionName", "candidateName");
			//$bodyData ['tdAmount'] = array("elected", "ordering");
			
			if (!$mode) {
				//print
				$this->load->view('layouts/report/header', $headerData);
				$this->load->view('layouts/report/bodyviewvote', $bodyData);
				$this->load->view('layouts/report/footer', "");
			} else {
				//download excel
				$this->load->helper('excel');
				excel($headerData, array($bodyData), '');
			}
		
		
		
	}
	
	
	
	
}

?>