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

class perkara_task_list extends CI_Controller {

	function index(){
		
		$functions = $this->nativesession->get_flash_session('function');
		

		if($this->session->userdata('is_logged_in')==FALSE){
			redirect('login');
		}
		$enc = $this->session->userdata('userid');
		$keyword = $this->nativesession->get_flash_session('keyword');
		$this->nativesession->set_flash_session('keyword', $keyword);
		if(empty($keyword))
			$this->nativesession->set_flash_session('url_requested', base_url(uri_string()));
		$idalurperkara = $this->encrypt->decode(base64_decode($this->session->userdata('userid')));
		if($functions == 'sort'){
			$col = $this->nativesession->get_flash_session('col');
			$keyword = $this->nativesession->get_flash_session('keyword');
			$this->sort($col,$enc,$keyword);
		}elseif($functions == 'type'){
			$this->session->set_userdata('asc_desc','DESC');
			$this->session->set_userdata('total_rows',-1);
			$this->showData($enc,$idalurperkara);
		}elseif($functions == 'page'){
			$page = $this->nativesession->get_flash_session('page');
			$keyword = $this->nativesession->get_flash_session('keyword');
			$column_sorted = $this->nativesession->get_flash_session('col');
			$this->showData($enc,$idalurperkara,$column_sorted,$page,$keyword);
		}elseif($functions == 'search'){
			$keyword = $this->nativesession->get_flash_session('keyword');
			$this->session->set_userdata('keyword',$keyword);
			$page = $this->nativesession->get_flash_session('page');
			$this->session->set_userdata('total_rows',-1);
			$this->showData($enc,$idalurperkara,2,$page,$keyword);
		}else{
			$this->session->set_userdata('asc_desc','DESC');
			$this->session->set_userdata('total_rows',-1);
			$this->showData($enc);
		}
	}

	function sort($col=null,$enc=null,$keyword=null){
		$enc = $this->session->userdata('userid');
		if($this->session->userdata('asc_desc')){
			if($this->session->userdata('asc_desc')=="DESC"){
				$this->session->set_userdata('asc_desc','ASC');
			}else{
				$this->session->set_userdata('asc_desc','DESC');
			}
		}else{
			$this->session->set_userdata('asc_desc','ASC');
		}
		$idalurperkara = $this->encrypt->decode(base64_decode($enc));
		$this->showData($enc,$idalurperkara,$col,0,$keyword);
	}


	function showData($enc=0,$idalurperkara='',$col=2,$start_page=0,$keyword=''){

		$asc_desc = $this->session->userdata('asc_desc');
		$this->load->model('perkara/perkara');
		if($start_page>0){
			$begin_limit = ($start_page-1)*50;
		}else{
			$begin_limit = 0;
			$start_page = 1;
		}
		
		$query2 = $this->perkara->getDetilUser($enc);
		if(!empty($query2->hakim_id)){
			$where="((majelis_hakim_id IS NOT NULL AND CONCAT(',', majelis_hakim_id, ',') LIKE '%,".$query2->hakim_id.",%' AND '".$query2->hakim_id."' <> '')
					OR (CONCAT(',', majelis_hakim_id_keberatan, ',') LIKE '%,".$query2->hakim_id.",%' AND '".$query2->hakim_id."' <> '')
					OR (CONCAT(',', majelis_hakim_id_verzet, ',') LIKE '%,".$query2->hakim_id.",%' AND '".$query2->hakim_id."' <> ''))";
		}elseif (!empty($query2->panitera_id)) {
			$where="((panitera_pengganti_id IS NOT NULL AND CONCAT(',', panitera_pengganti_id, ',') LIKE '%,".$query2->panitera_id.",%' AND '".$query2->panitera_id."' <> '')
					OR (CONCAT(',', panitera_pengganti_id_keberatan, ',') LIKE '%,".$query2->panitera_id.",%' AND '".$query2->panitera_id."' <> '')
					OR (CONCAT(',', panitera_pengganti_id_verzet, ',') LIKE '%,".$query2->panitera_id.",%' AND '".$query2->panitera_id."' <> ''))";
		}elseif (!empty($query2->jurusita_id)) {
			$where="((jurusita_id IS NOT NULL AND CONCAT(',', jurusita_id, ',') LIKE '%,".$query2->jurusita_id.",%' AND '".$query2->jurusita_id."' <> '')
					OR (CONCAT(',', jurusita_id_keberatan, ',') LIKE '%,".$query2->jurusita_id.",%' AND '".$query2->jurusita_id."' <> '')
					OR (CONCAT(',', jurusita_id_verzet, ',') LIKE '%,".$query2->jurusita_id.",%' AND '".$query2->jurusita_id."' <> ''))";
		}
		
		$query = $this->perkara->getPerkaraTaskList($enc,$idalurperkara,$col,$asc_desc,$begin_limit,$keyword, $where);
		$data['list_perkara'] = $query;

		if($this->session->userdata('total_rows')){
			if($this->session->userdata('total_rows')==-1){
				$data['total_rows'] = $this->perkara->fecth_data_num($idalurperkara,$keyword);
				$this->session->set_userdata('total_rows',$data['total_rows']);
			}else{
				$data['total_rows'] = $this->session->userdata('total_rows');
			}
		}else{
			$data['total_rows'] = $this->perkara->fecth_data_num($idalurperkara,$keyword);
			$this->session->set_userdata('total_rows',$data['total_rows']);
		}
		
		$data['page_active'] = $begin_limit;
		$data['page_title'] = 'DAFTAR PERKARA YANG DITANGANI';
		
		$data['main_body'] = 'perkara_list/perkara_task_list';
		$data['enc'] ='';
		$data['idalurperkara'] = $idalurperkara;
		
		$data['page_number'] = $start_page;
		$data['page_url'] = 'perkara_yang_ditangani';
		
		$data['keyword'] = $keyword;
		$data['column_sorted'] = $col;
		$this->load->vars($data);
		$this->load->view('header');
		$this->load->view('body/body');
	}
}