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

class Home extends MX_Controller {

	 
	function __construct()
	{
		parent::__construct();
		$this->load->model('mo_admin','ma');
	}
	
	public function index()
	{
		$StatusLogin=$this->session->userdata('sStatus');
		if(!$StatusLogin || $StatusLogin == 0) {
			$this->BelumLogin();
		} else {
			$this->TelahLogin();
		}
	}
	
	public function BelumLogin()
	{
		$this->load->view('vw_login');
	}
	
	public function ProsesLogin()
	{
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		
		$Login = $this->ma->Auth($username,$password);
		//echo print_r($cekLogin);
		if(!$Login)
		{
			echo json_encode(array('msg'=>'User/Password Salah!!!')); 
		}
		else
		{
			$sLogin = array(
				'sId' => $Login->f_user_id,
				'sEmail' => $Login->f_user_email,
				'sName' => $Login->f_user_name,
				'sLevel' => $Login->f_user_level,
				'sRoleId' => $Login->f_user_role,
				'sStatus' => $Login->f_user_active,
				'validated' => true,
			);
			$this->session->set_userdata($sLogin);
			echo json_encode(array('msg'=>'sukses')); 
		}
	}
	
	public function TelahLogin()
	{
		$data['app_title']	= 'Dashboard';
		$data['app_desc']	= 'ARS';
		$data['content']	= 'dashboard';
		
		$this->load->view('webadmin/main',$data);
	}
	
	function ProsesLogout()
	{
			$sLogin = array(
				'sId' => '',
				'sName' => '',
				'sPassword' => '',
				'sRoleId' => '',
				'sRoleName' => '',
				'sLevel' => '',
				'sLimitStatus' => '',
				'sEmpId' => '',
				'sBranchId' => '',
				'sStatus' => false,
				'validated' => false,
			);

		$this->session->unset_userdata($sLogin);
		$this->session->sess_destroy();
		redirect( base_url(),'refresh');
	}
	
	function registrasi()
	{
		$this->auth->check_isvalidated();
		$this->load->library('datatables');
		
		$this->datatables->select('',false);
		$this->datatables->where('a.f_training_schedule_status', 'Open');
        
		$this->datatables->join('t_training_room as e','a.f_training_room_id=e.f_training_room_id','Left');    
		$this->datatables->join('t_employee as d','a.f_training_schedule_trainer=d.f_emp_id','Left');
	    $this->datatables->join('t_training_category as b','a.f_training_category_id=b.f_training_category_id','Left');                

		$this->datatables->from('t_training_schedule as a');
		
		echo $this->datatables->generate();
	}	
	
	function fnGrafikPie()
	{
		$this->load->model('responden/mo_responden','more');
		
		$subkategori	= $this->more->comboDataSubKategori();
		$result 		= $this->more->getDataResponden();

		// Lembar 1 ----------------------------------------------------
		$i=1; $data_pensiun = array(); $data_pensiun1 = array(); $total_pensiun =0; $total_pensiun1 =0;
		foreach($result as $a)
		{
			$h=0;$j=0; $modus = array();
			foreach($subkategori[1] as $b => $c)
			{
				$d = $a['f_survey']['subkategori'][$b];
				foreach($a['f_survey']['subkategori'][$b] as $e => $f)
				{
					if($e == 6 && $f['urut'] == $d[1]['urut'] || $e == 7 && $f['urut'] == $d[2]['urut'] || $e == 8 && $f['urut'] == $d[3]['urut'] || $e == 9 && $f['urut'] == $d[4]['urut'] || $e == 10 && $f['urut'] == $d[5]['urut'])
					{
						$hasil_akhir = 1;
					}
					else
					{
						$hasil_akhir = $f['jawab'];
					}
					$modus[$h] = $hasil_akhir;
					$j += $hasil_akhir;
					$h++;
				}
			}
			$data_pensiun[kesiapan($j)] += 1;
			$total_pensiun += 1;
			// Modus dan Hasilnya
			$values = array_count_values($modus);
			asort($values);
			$hmodus = array_search(max($values), $values); 
			$data_pensiun1[tipe_pensiun($hmodus)] += 1;
			$i++;
		}
	
		$pensiun = array(
			0 => 'Sangat Siap',
			1 => 'Siap',
			2 => 'Belum Cukup Siap',
			3 => 'Kurang Siap',
			4 => 'Tidak Siap',
			5 => 'Sangat Tidak Siap',
		);		

		$tipe_pensiun = array(
			1 => 'Berlibur',
			2 => 'Istirahat',
			3 => 'Aktif',
			4 => 'Terencana',
		);

		/* Tingkat Kesiapan Pensiun */
		$pie1 = array(
			"name" => 'Brands',
			"colorByPoint" => true,
		);		
		foreach($pensiun as $a => $b)
		{
			$st = ($data_pensiun[$b]) ? $data_pensiun[$b]:0;
			$p1 = ($st) ? round(($st*100)/$total_pensiun,2):0;
			//$total += $st;
			$pie1['data'][] = array(
				'name' => $b,
				'y' => $p1,
			);					
		}

		/* Tipe Kesiapan Pensiun */
		$pie2 = array(
			"name" => 'Brands',
			"colorByPoint" => true,
		);
		foreach($tipe_pensiun as $a => $b)
		{
			$st = ($data_pensiun1[$b]) ? $data_pensiun1[$b]:0;
			$p1 = ($st) ? round(($st*100)/$total_pensiun,2):0;
			$pie2['data'][] = array(
				'name' => $b,
				'y' => $p1,
			);
		}		

		// Lembar 2 ----------------------------------------------------
		$i=1; $data_cemas = array(); $data_cemas1 = array(); $total_cemas=0;$total_cemas1=0;
		foreach($result as $a)
		{
			$h=0;$j=0; $modus = array();
			foreach($subkategori[2] as $b => $c)
			{
				$d = $a['f_survey']['subkategori'][$b];
				foreach($a['f_survey']['subkategori'][$b] as $e => $f)
				{
					if($e == 16 && $f['urut'] == $d[11]['urut'] || $e == 17 && $f['urut'] == $d[12]['urut'] || $e == 18 && $f['urut'] == $d[13]['urut'] || $e == 19 && $f['urut'] == $d[14]['urut'] || $e == 20 && $f['urut'] == $d[15]['urut'])
					{
						$hasil_akhir = 1;
					}
					else
					{
						$hasil_akhir = $f['jawab'];
					}
					
					$cemas[$i][$hasil_akhir] .= $c['desc'].',';
					$modus[$h] = $hasil_akhir;
					$j += $hasil_akhir;
					$h++;
				}
			}
			
			// Total Kesiapan dan Hasilnya
			$data_cemas[kecemasan($j)] += 1;
			$total_cemas += 1;
			// Modus dan Hasilnya
			$values[$i] = array_count_values($modus);
			ksort($values[$i]);
			$hmodus = array_search(key($values[$i]), $values[$i]); 
			$hmodus1[$i] = array_search(key($values[$i]), $values[$i]); 
			$trim = trim($cemas[$i][key($values[$i])],',');
			$explode = explode(',',$trim);
			foreach($explode as $x => $y)
			{
				$data_cemas1[$y] +=1;
				$total_cemas1 += 1;
			}
			$i++;
		}

		$cemas = array(
			0 => array("range" => "< 20","name" => "Panik"),
			1 => array("range" => "21 - 23","name" => "Agak Panik"),
			2 => array("range" => "24 - 27","name" => "Kecemasan Sedang"),
			3 => array("range" => "28 - 31","name" => "Cukup Cemas"),
			4 => array("range" => "> 32","name" => "Kecemasan Ringan"),
		);
		
		$cemas1 = array(
			0 => array('val'=>'Hilang Pekerjaan','name'=>'Kehilangan Pekerajaan'),
			1 => array('val'=>'Hilang Keuangan','name'=>'Kehilangan Sumber Keuangan'),
			2 => array('val'=>'Hilang Rekan','name'=>'Kehilangan Rekan Kerja'),
			3 => array('val'=>'Hilang Kesehatan','name'=>'Menurunnya Tingkar Kesehatan'),
			4 => array('val'=>'Hilang Jabatan','name'=>'Kehilangan Jabatan'),
		);
		
		/* Tingkat Kecemasan Pensiun */
		$pie3 = array(
			"name" => 'Brands',
			"colorByPoint" => true,
		);
		foreach($cemas as $a => $b)
		{
			$st = ($data_cemas[$b['name']]) ? $data_cemas[$b['name']]:0;
			$p1 = ($st) ? round(($st*100)/$total_cemas,2):0;
			$pie3['data'][] = array(
				'name' => $b['name'],
				'y' => $p1,
			);
		}

		/* Proporsi Penyebab Kecemasan */
		$pie4 = array(
			"name" => 'Brands',
			"colorByPoint" => true,
		);
		foreach($cemas1 as $a => $b)
		{
			$st = ($data_cemas1[$b['val']]) ? $data_cemas1[$b['val']]:0;
			$p1 = ($st) ? round(($st*100)/$total_cemas1,2):0;
			$pie4['data'][] = array(
				'name' => $b['name'],
				'y' => $p1,
			);
		}
		
		$final = array('total_peserta'=>$total_pensiun,'pie1'=> $pie1,'pie2'=> $pie2,'pie3'=> $pie3,'pie4'=> $pie4);
		echo json_encode($final);
	}

}
