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

class Home extends CI_Controller {
	protected $data = NULL;
	
	public function __construct() {
		parent::__construct();
		$this->load->library('layouts');
    $this->load->model('general_model');
		
		if ($this->session->userdata('companyID')) {
			$data['selectedCompany'] = $this->general_model->get_byFilter('company_main',array('*'), array('ID'=>$this->session->userdata('companyID')));
		}
	}

	/**
	* INDEX
	* @author : Zul Zolkaffly
	* @last-update : 08/08/2016 (Zul Zolkaffly)
	*								 23/08/2016 (Zul Zolkaffly)
	*/
	public function index() {
		$this->lang->load('general', $this->session->userdata('language'));
		$this->lang->load('company', $this->session->userdata('language'));
		
		$this->load->model('home/homedashboard');		
		$this->load->model('company/companymain');
		$this->load->model('review/reviewmain');
	
		$ctSD = 0;
		$ctSL = 0;
		
		$data['grades'] = NULL;
		if($grades = $this->reviewmain->getAllGrades()) {
			foreach($grades as $grade) {
				$data['grades'][$grade['grade']]['result'] = $grade['result'];
				$data['grades'][$grade['grade']]['total']	= 0;
			}
		}		
		ksort($data['grades']);
	
		if($companies = $this->ObjToArray($this->general_model->get_byFilter('company_main', array('ID'), array('is_delete'=>0)))) {
			foreach($companies as &$company) {
				$status = $this->reviewmain->getReviewStatus($company['ID']);
				if($status == "SEDANG DIPROSES") $ctSD++;
				
				if($status == "SELESAI") {
					$ctSL++;
				
					$grade = $this->reviewmain->getScoreItem($company['ID'], 'GRADE');
					
					if(isset($data['grades'][$grade])) {
						$data['grades'][$grade]['total'] += 1;
					} else {
						$data['grades'][$grade]['total'] = 1;
					}
				}
			}
		}
		
		
		// DATA APPLICATION AND REVIEW
		$data['numApply'] = $this->homedashboard->getTotalApplications();
		$data['isComp'] = $ctSL;
		$data['inProg'] = $ctSD;
		
		// DATA COMPANY BY STATE
		$data['compByState']['TOTAL'] = $this->homedashboard->getTotalApplications();
		$data['compByState']['ALL'] = $this->homedashboard->getCompByStateAll();
		$data['compByState']['TOP5'] = $this->homedashboard->getCompByStateTopFive();		
		$data['mapael'] = $this->homedashboard->getCompByStateAll(TRUE);
		
		//echo "<pre>";
		//$this->reviewmain->getMarkDist();
		//echo "</pre>";
		
		// DATA FOR MARK DISTRIBUTION BY GRADES
		$chartist['label'] = NULL;
		$chartist['series'] = NULL;
		if($grades = $this->reviewmain->getMarkDist()) {
			$x = 0;
			foreach($grades as $g=>$d) {
				$chartist['label'][$x] = $d['min']." - ".$d['max']." (".$g.")";
				
				$series = NULL;				
				for($i = 0; $i < $x; $i++) $series[$i] = NULL;				
				$series[$i] = $d['total'];				
				for($j = ($i+1); $j < count($grades); $j++) $series[$j] = NULL;
				
				$chartist['series'][$x] = $series;
				$x++;
			}
		}
		$data['chartist'] = $chartist;
		
		// TOP 10 PER METHOD
		$data['topcomp'] = $this->homedashboard->getTopCompPerMethod();
		
		/*
		// LIST OF COMPANIES
		$numReview = $this->ObjToArray($this->general_model->get_byFilter('_ref_review', array('COUNT(*) CT'), array('is_delete'=>0)),true);
		$numReview = ($numReview['CT']) ? $numReview['CT'] : 0;
		// ALL COMPANIES
		$data['companies']['all'] = $this->ObjToArray($this->general_model->get_byFilter('company_main', array('*'), array('is_delete'=>0)));
		if($data['companies']['all']) {
			foreach($data['companies']['all'] as $key=>&$value) {
				$numAns = $this->ObjToArray($this->general_model->get_byFilter('review', array('COUNT(*) CT'), array('companyID'=>$value['ID'])));
				$numBs = $this->ObjToArray($this->general_model->get_byFilter('review', array('COUNT(*) CT'), array('companyID'=>$value['ID'],'status'=>'BELUM SELESAI')));
				if($numAns < $numReview) {
					$value['status'] = 'SEDANG DIPROSES';
				} else {
					$value['status'] = ($numBs) ? 'SEDANG DIPROSES' : 'SELESAI';
				}
			}
		}
		// IN PROGRESS COMPANIES
		$data['companies']['apply'] = $this->homedashboard->getInProgress(NULL, TRUE);
		if($data['companies']['apply']) {
			foreach($data['companies']['apply'] as $key=>&$value) {
				$numAns = $this->ObjToArray($this->general_model->get_byFilter('review', array('COUNT(*) CT'), array('companyID'=>$value['ID'])));
				$numBs = $this->ObjToArray($this->general_model->get_byFilter('review', array('COUNT(*) CT'), array('companyID'=>$value['ID'],'status'=>'BELUM SELESAI')));
				if($numAns < $numReview) {
					$value['status'] = 'SEDANG DIPROSES';
				} else {
					$value['status'] = ($numBs) ? 'SEDANG DIPROSES' : 'SELESAI';
				}
			}
		}
		// COMPLYING COMPANIES
		$data['companies']['comp'] = $this->homedashboard->getCompliance(NULL, TRUE);
		if($data['companies']['comp']) {
			foreach($data['companies']['comp'] as $key=>&$value) {
				$numAns = $this->ObjToArray($this->general_model->get_byFilter('review', array('COUNT(*) CT'), array('companyID'=>$value['ID'])));
				$numBs = $this->ObjToArray($this->general_model->get_byFilter('review', array('COUNT(*) CT'), array('companyID'=>$value['ID'],'status'=>'BELUM SELESAI')));
				if($numAns < $numReview) {
					$value['status'] = 'SEDANG DIPROSES';
				} else {
					$value['status'] = ($numBs) ? 'SEDANG DIPROSES' : 'SELESAI';
				}
			}
		}
		// NON COMPLYING COMPANIES
		$data['companies']['nonComp'] = $this->homedashboard->getNonCompliance(NULL, TRUE);
		if($data['companies']['nonComp']) {
			foreach($data['companies']['nonComp'] as $key=>&$value) {
				$numAns = $this->ObjToArray($this->general_model->get_byFilter('review', array('COUNT(*) CT'), array('companyID'=>$value['ID'])));
				$numBs = $this->ObjToArray($this->general_model->get_byFilter('review', array('COUNT(*) CT'), array('companyID'=>$value['ID'],'status'=>'BELUM SELESAI')));
				if($numAns < $numReview) {
					$value['status'] = 'SEDANG DIPROSES';
				} else {
					$value['status'] = ($numBs) ? 'SEDANG DIPROSES' : 'SELESAI';
				}
			}
		}
		
		//echo "<pre>";
		//print_r($data['companies']['apply']);
		//echo "</pre>";
		*/
		
		// LIST OF STATES
		$data['states'] = $this->general_model->get_all('_ref_state');
		
		
		
		$this->layouts->view('home', array(), $data);	
	}
	
	public function openmodal() {
		$type = (isset($_POST['type']) && $_POST['type']) ? strtoupper($_POST['type']) : NULL;
		
		$this->load->model('home/homedashboard');		
		$this->load->model('company/companymain');
		$this->load->model('review/reviewmain');		
		
		$grades = NULL;
		if($grds = $this->reviewmain->getAllGrades()) {
			foreach($grds as $grade) {
				$grades[$grade['grade']] = $grade['result'];
			}
		}		
		if($grades) ksort($grades);
		
		$data = NULL;
		if($type) {
			switch($type) {
				case 'APPLY' :
					$data['companies'] = $this->homedashboard->getTotalApplications(FALSE);
					break;
				case 'COMPLETED' :
				case 'PROCESSING' :
					$data['companies'] = NULL;
					if($companies = $this->homedashboard->getTotalApplications(FALSE)) {
						$c = 0;
						foreach($companies as $company) {
							$status = $this->reviewmain->getReviewStatus($company['ID']);
							$grade = $this->reviewmain->getScoreItem($company['ID'], 'GRADE');
							if(($type == 'COMPLETED' && $status == "SELESAI") || ($type == 'PROCESSING' && $status == "SEDANG DIPROSES")) {
								$company['result'] = (isset($grades[$grade])) ? $grades[$grade] : NULL;								
								$data['companies'][$c++] = $company;
							}
						}
					}
					break;
				case 'GRADEA':
				case 'GRADEB':
				case 'GRADEC':
				case 'GRADED':
					$data['companies'] = NULL;
					if($companies = $this->homedashboard->getTotalApplications(FALSE)) {
						$c = 0;
						foreach($companies as $company) {
							$grade = $this->reviewmain->getScoreItem($company['ID'], 'GRADE');
							
							if($type == 'GRADE'.(strtoupper($grade))) {						
								$data['companies'][$c++] = $company;
							}
						}
					}
					break;
				default:
					break;
			}
		}
		$data['type'] = $type;
		
		$this->load->view('home/openmodal',$data);
	}
	
	/**
	* @title : OBJTOARRAY
	* @description : CONVERT FROM STDCLASS OBJECT TO ARRAY
	* @author : ZUL ZOLKAFFLY
	*	@create : 15/08/2016 (ZUL ZOLKAFFLY)
	*/
	private function ObjToArray($obj,$single=false) {
		return (!empty($obj)) ? json_decode(json_encode(($single)?$obj[0]:$obj),true) : NULL;
	}
	
	
}

























?>
