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

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

	}
	
	
	public function saveCompany(){
		
		$companyID = $this->input->post('companyID');
		$companyName = $this->input->post('companyName');
		$companyAddress = $this->input->post('companyAddress');
		
		$this->load->model('company_model');
		
		$data = array(
					'companyName' => $companyName,
					'companyAddress' => $companyAddress,
					'projectID' => $this->session->userdata('projectID'),
				);
				
		if($companyID){
			$updateCompany = $this->company_model->update('company', $companyID, $data);
		}
		else{
			$insertCompany = $this->company_model->insert('company', $data);
		}
		
		redirect('company', 'refresh');
		

	}
	
	
	public function savecompanyajax(){
		
		$companyID = $this->input->post('companyID');
		$companyName = $this->input->post('companyName');
		$companyAddress = $this->input->post('companyAddress');
		
		$this->load->model('company_model');
		
		$data = array(
					'companyName' => $companyName,
					'companyAddress' => $companyAddress,
					'projectID' => $this->session->userdata('projectID'),
				);
		
		$result = array('status'=>'FAIL');
		if($companyID){
			if($id = $this->company_model->update('company', $companyID, $data)) $result = array('status'=>'SUCCESS', 'companyID'=>$id);
		} else {
			if($id = $this->company_model->insert('company', $data)) $result = array('status'=>'SUCCESS', 'companyID'=>$id);
		}
		
		echo json_encode($result);
	}
	
	
	public function deleteCompany(){
		
		$this->load->model('company_model');
		
		$companyID = $this->input->post('companyID');
				
		$deleteCompany = $this->company_model->delete('company', $companyID);
		
		if($deleteCompany){
			print json_encode(array("status"=>"success"));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}
		
	}
	
	
	
	
	
	public function printCompany($valuedatatable, $mode=false){
		
		$this->lang->load("company",$this->session->userdata('language'));
		
		$this->load->model('company_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.companyName) LIKE '%".$valuedatatable3[$i]."%'";
				}
				else if($count1 < $length){
					$valuedatatable4 .= " AND CONCAT(a.companyName) LIKE '%".$valuedatatable3[$i]."%'";
				}
				else if($count1 == $length){
					$valuedatatable4 .= " AND CONCAT(a.companyName) LIKE '%".$valuedatatable3[$i]."%'";
				}
				
				$count1++;
			}
			
			$valuedatatable5 = $valuedatatable4;
		
		
			$bodyData['rptDataDetail'] = $this->company_model->company_filterbydatatables($valuedatatable5);
			
		}
		else{
			$bodyData['rptDataDetail'] = $this->company_model->company_all();
		}
		
			$headerData['rptTitle'] = $this->lang->line('companyList');
			
			$bodyData['tableHeader'] = array($this->lang->line('companyName'));
			
			$bodyData['selectFields'] = array("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), '');
			}
		
		
		
	}
	
	
	
	
}

?>