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

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

		// if (!$this->session->userdata('logged_in')) {
        //     echo '<script>window.location.replace("'.base_url().'login");</script>';
        // }
		$this->session->set_userdata('menu', "block");
		
	}
	
	public function index(){
		$this->lang->load("voter",$this->session->userdata('language'));
		$this->lang->load("block",$this->session->userdata('language'));
		$this->load->model('block_model');
		
		$data['projectID'] = $this->session->userdata('projectID');
		$data['blockList'] = $this->block_model->getListVoters($data['projectID']);
		
		$this->layouts->view('block/index', array(), $data);
		//redirect('block/blocklist/1');
	}
	
	public function blocklist(){
		
		$this->lang->load("block",$this->session->userdata('language'));
		
		$this->load->model('block_model');
		
		$status = $this->uri->segment(3);
		
		$data['status'] = $status;
		
		$data['blockList'] = $this->block_model->block_byvalidity($status);
			
		
		$this->layouts->view('block', array(), $data);
		
	}
	
	
	public function updatestatus(){
		
		$userID = $this->input->post('userID');
		$validity = $this->input->post('validity');
		
		$this->load->model('block_model');
		
		$data = array(
					'validity' => $validity
				);
				
		
		$updatePosition = $this->block_model->update('user', $userID, $data);
		
		if($updatePosition){
			print json_encode(array("status"=>"success"));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}
		

	}
	
	
	public function printblock($status, $valuedatatable, $mode=false){
		
		$this->lang->load("voter",$this->session->userdata('language'));
		$this->lang->load("block",$this->session->userdata('language'));
		
		$this->load->model('block_model');
		
		$status = $this->uri->segment(3);
		
		
		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, b.companyName) LIKE '%".$valuedatatable3[$i]."%'";
				}
				else if($count1 < $length){
					$valuedatatable4 .= " AND CONCAT(a.noAhli, b.companyName) LIKE '%".$valuedatatable3[$i]."%'";
				}
				else if($count1 == $length){
					$valuedatatable4 .= " AND CONCAT(a.noAhli, b.companyName) LIKE '%".$valuedatatable3[$i]."%'";
				}
				
				$count1++;
			}
			
			$valuedatatable5 = $valuedatatable4;
		
		
			$bodyData['rptDataDetail'] = $this->block_model->block_filterbydatatablesandstatus($status, $valuedatatable5);
			
		}
		else{
			$bodyData['rptDataDetail'] = $this->block_model->block_byvalidity($status);
		}
		
			$headerData['rptTitle'] = $this->lang->line('blockList');
			
			$bodyData['tableHeader'] = array($this->lang->line('voterID'), $this->lang->line('voterName'), $this->lang->line('companyName'), $this->lang->line('status'));
			
			$bodyData['selectFields'] = array("noAhli", "name", "companyName", "blockstatus");
			//$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), '');
			}
		
		
		
	}
	
	
	
	
}

?>