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

class Position extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library('layouts');
		
		checkDashboard();
		$this->session->set_userdata('menu', "designation");
		
	}
	
	public function index(){
		
		$this->lang->load("position",$this->session->userdata('language'));
		
		$this->load->model('position_model');
		
		$data['positionList'] = $this->position_model->position_all();
		
		$this->templates->setData($data);
			
		$this->layouts->view('position/index', array(), $data);
		

	}
	
	
	public function savePosition(){
		
		$positionID = $this->input->post('positionID');
		$positionName = $this->input->post('positionName');
		$elected = $this->input->post('elected');
		$ordering = $this->input->post('ordering');
		
		$this->load->model('position_model');
		
		$data = array(
					'positionName' => $positionName,
					'elected' => $elected,
					'ordering' => $ordering,
					'projectID' => $this->session->userdata('projectID'),
				);
				
		if($positionID){
			$updatePosition = $this->position_model->update('position', $positionID, $data);
		}
		else{
			$insertPosition = $this->position_model->insert('position', $data);
		}
		
		redirect('position', 'refresh');
		

	}
	
	
	public function savepositionajax(){
		
		$positionID = $this->input->post('positionID');
		$positionName = $this->input->post('positionName');
		$elected = $this->input->post('elected');
		$ordering = $this->input->post('ordering');
		
		$this->load->model('position_model');
		
		$data = array(
					'positionName' => $positionName,
					'elected' => $elected,
					'ordering' => $ordering,
					'projectID' => $this->session->userdata('projectID'),
				);
		
		$result = array('status'=>'FAIL');
		if($positionID){
			if($id = $this->position_model->update('position', $positionID, $data)) $result = array('status'=>'SUCCESS', 'positionID'=>$id);
		} else {
			if($id = $this->position_model->insert('position', $data)) $result = array('status'=>'SUCCESS', 'positionID'=>$id);
		}
		
		echo json_encode($result);
	}
	
	
	public function deletePosition(){
		
		$this->load->model('position_model');
		
		$positionID = $this->input->post('positionID');
				
		$deletePosition = $this->position_model->delete('position', $positionID);
		
		if($deletePosition){
			print json_encode(array("status"=>"success"));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}
		
	}
	
	
	public function checkorderingajax() {
		$projectID = $this->session->userdata('projectID');
		$positionID = $_POST['positionID'];
		$order = $_POST['order'];
		
		$this->load->model('position_model');
		echo ($this->position_model->checkOrderingExists($order, $projectID, $positionID)) ? "FAIL" : "SUCCESS";
	}
	
	
	
	
	public function printPosition($valuedatatable, $mode=false){
		
		$this->lang->load("position",$this->session->userdata('language'));
		
		$this->load->model('position_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.positionName, a.elected, a.ordering) LIKE '%".$valuedatatable3[$i]."%'";
				}
				else if($count1 < $length){
					$valuedatatable4 .= " AND CONCAT(a.positionName, a.elected, a.ordering) LIKE '%".$valuedatatable3[$i]."%'";
				}
				else if($count1 == $length){
					$valuedatatable4 .= " AND CONCAT(a.positionName, a.elected, a.ordering) LIKE '%".$valuedatatable3[$i]."%'";
				}
				
				$count1++;
			}
			
			$valuedatatable5 = $valuedatatable4;
			
			
				$bodyData['rptDataDetail'] = $this->position_model->position_filterbydatatables($valuedatatable5);
				
				
		}
		else{
			$bodyData['rptDataDetail'] = $this->position_model->position_all();
		}
			
			$headerData['rptTitle'] = $this->lang->line('designationList');
			
			$bodyData['tableHeader'] = array($this->lang->line('designationName'), $this->lang->line('noOfCandidate'), $this->lang->line('designationRank'));
			
			$bodyData['selectFields'] = array("positionName", "elected", "ordering");
			//$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), '');
			}
		
		
		
	}
	
	
	
	
}

?>