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

class Perkara_tab_status_list_sidang_terakhir extends CI_Controller {

	function index(){
		$functions = $this->nativesession->get_flash_session('function');
		$enc = $this->nativesession->get_flash_session('enc');
		$tmp = $this->encrypt->decode(base64_decode($enc));
		$tmp1 = explode('/', $tmp);
		if(count($tmp1)!=3){
			show_404();
		}
		$this->nativesession->set_flash_session('idx', $tmp1[0]);
		$this->nativesession->set_flash_session('all', $tmp1[1]);
		$this->nativesession->set_flash_session('ids', $tmp1[2]);

		if($this->session->userdata('is_logged_in')==FALSE){
			redirect('dashboard');
		}

		$idalurperkara = $this->encrypt->decode(base64_decode($enc));
		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 ='';
			$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();
		}
	}

	function sort($col=null,$enc=null,$keyword=null){
		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('dashboard/data_dashboard');
		if($start_page>0){
			$begin_limit = ($start_page-1)*50;
		}else{
			$begin_limit = 0;
			$start_page = 1;
		}

        $idx =  $this->nativesession->get_flash_session('idx');
        $all =  $this->nativesession->get_flash_session('all');
        $ids =  $this->nativesession->get_flash_session('ids');
		$idpanitera = $this->session->userdata('panitera_id');
		$idhakim = $this->session->userdata('hakim_id');
		$idjurusita = $this->session->userdata('jurusita_id');

        $data['status_perkara'] =  $this->data_dashboard->getDetilProsesAlurPerkara($idx);
        $idproses = $data['status_perkara']->id;
        $tahapan_nama = $data['status_perkara']->tahapan_nama;
		$alur_perkara_id = $data['status_perkara']->alur_perkara_id;
		$proses_id = $data['status_perkara']->proses_id;
		$proses_nama= $data['status_perkara']->proses_nama;
		$proses_judul = 'Perkara Yang Belum '.$proses_nama;
		$batas_waktu =$data['status_perkara']->batas_waktu ;
		$satuan =$data['status_perkara']->satuan ;
		$nama_tabel_dari=$data['status_perkara']->nama_tabel_dari;
		$nama_kolom_dari=$data['status_perkara']->nama_kolom_dari;
		$nama_tabel_ke=$data['status_perkara']->nama_tabel_ke;
		$nama_kolom_ke=$data['status_perkara']->nama_kolom_ke;
		$tahapan_id = $data['status_perkara']->tahapan_id;
		if($all!='Y'){
			$addand = " AND (( perkara_penetapan.majelis_hakim_id is not null and 
				concat(',',perkara_penetapan.majelis_hakim_id,',') like '%,".$idhakim.",%'  and '".$idhakim."'<>'')
				or 
				(
				perkara_penetapan.panitera_pengganti_id is not null and 
				concat(',',perkara_penetapan.panitera_pengganti_id,',') like '%,".$idpanitera.",%' and '".$idpanitera."'<>'')
				or 
				(perkara_penetapan.jurusita_id is not null and 
				concat(',',perkara_penetapan.jurusita_id,',') like '%,".$idjurusita.",%' and '".$idjurusita."'<>''))";
			}else{
				$addand = "";
		}

		$check_sql2 = "select perkara.perkara_id as no_perkara from perkara ";
		if($nama_tabel_dari != 'perkara'){ 
			$check_sql2 .= " left outer join ".$nama_tabel_dari." on perkara.perkara_id=".$nama_tabel_dari.".perkara_id ";
		}if($nama_tabel_dari != $nama_tabel_ke ){ 
			$check_sql2 .= " left outer join ".$nama_tabel_ke." on ".$nama_tabel_dari.".perkara_id=".$nama_tabel_ke.".perkara_id ";
		}if($nama_tabel_ke!='perkara_penetapan'){ 
			$check_sql2 .= " left outer join perkara_penetapan on ".$nama_tabel_dari.".perkara_id=perkara_penetapan.perkara_id ";
		}
		$check_sql2 .= " where perkara.proses_terakhir_id != 211 AND perkara.alur_perkara_id='".$alur_perkara_id."' "
			." and ".$nama_tabel_dari.".".$nama_kolom_dari." is not null " 
			." and ".$nama_tabel_ke.".".$nama_kolom_ke." is null " ;		
		if($nama_tabel_dari =='perkara_banding'){
		   	$check_sql2 .= " AND perkara_banding.status_putusan_banding_text <> 'Dicabut' AND perkara.proses_terakhir_id != 391 ";
		}    	##cek hari
		if($ids==0){
			if($satuan==1){ 
				$check_sql2 .= " and GetHariKerja(".$nama_tabel_dari.".".$nama_kolom_dari.",'".date("Y-m-d")."') > $batas_waktu ".$addand." ";
			}else if($satuan==2){ 
				$check_sql2 .= " and DATEDIFF('".date("Y-m-d")."',".$nama_tabel_dari.".".$nama_kolom_dari.") > $batas_waktu ".$addand." ";
			}else if($satuan==3){ 
				$check_sql2 .= " and (DATEDIFF('".date("Y-m-d")."',".$nama_tabel_dari.".".$nama_kolom_dari.")/30) > $batas_waktu ".$addand." ";
			}
		}else{
			if($satuan==1){ 
				$check_sql2 .= " and GetHariKerja(".$nama_tabel_dari.".".$nama_kolom_dari.",'".date("Y-m-d")."') <= $batas_waktu ".$addand." ";
			}else if($satuan==2){ 
				$check_sql2 .= " and DATEDIFF('".date("Y-m-d")."',".$nama_tabel_dari.".".$nama_kolom_dari.") <= $batas_waktu ".$addand." ";
			}else if($satuan==3){ 
				$check_sql2 .= " and (DATEDIFF('".date("Y-m-d")."',".$nama_tabel_dari.".".$nama_kolom_dari.")/30) <= $batas_waktu ".$addand." ";
			}
		}
		if(($alur_perkara_id=='118' && $proses_id=='80') || ($alur_perkara_id=='118' && $proses_id=='210')){
			$check_sql2 .= " AND perkara.perkara_id NOT IN (SELECT perkara_id FROM perkara_diversi WHERE tgl_penetapan_kesepakatan_diversi <> '0000-00-00' AND tgl_minutasi <> '0000-00-00' AND hasil_diversi=1) ";
		}
		if($proses_id=='20' || $proses_id=='80'){
			$check_sql2 .= " AND perkara.perkara_id NOT IN (SELECT perkara_id FROM perkara_putusan WHERE tanggal_cabut IS NOT NULL) ";
		}
		if($proses_id=='20' && ($alur_perkara_id=='9' || $alur_perkara_id=='10' || $alur_perkara_id=='11')){
			$check_sql2 .= " AND perkara.perkara_id NOT IN (SELECT perkara_id FROM perkara_putusan WHERE tanggal_minutasi IS NOT NULL) ";
		}

		if($proses_id=='301' || $proses_id=='400'){
			$check_sql2 .= " AND perkara_banding.perkara_id NOT IN (SELECT perkara_id FROM perkara_banding_detil WHERE status_pihak_id <> '1' AND pemberitahuan_permohonan_banding IS NOT NULL) ";
		}
		if($proses_id=='440'){
			$check_sql2 .= " AND perkara_kasasi.minutasi_kasasi IS NULL AND perkara_kasasi.perkara_id NOT IN (SELECT perkara_id FROM perkara_kasasi_detil WHERE status_pihak_id = '1' AND tidak_memenuhi_syarat IS NOT NULL) ";
		}
		if($proses_id=='490'){
			$check_sql2 .= " AND perkara_kasasi.perkara_id NOT IN (SELECT perkara_id FROM perkara_kasasi WHERE status_kasasi_id = '492') ";
		}
		if($proses_id=='500'){
			$check_sql2 .= " AND perkara_kasasi.perkara_id NOT IN (SELECT perkara_id FROM perkara_kasasi_detil WHERE status_pihak_id <> '1' AND pemberitahuan_putusan_kasasi IS NOT NULL) ";
		}
		
		if($proses_id=='571'){
			$check_sql2 .= " AND perkara_pk.perkara_id NOT IN (SELECT perkara_id FROM perkara_pk_detil WHERE status_pihak_id <> '1' AND pemberitahuan_putusan_pk IS NOT NULL) ";
		}
		if($idproses=='1624' || $idproses=='1630' || $idproses=='1643'){
			$check_sql2 .= " AND perkara.perkara_id NOT IN (SELECT perkara_id FROM perkara_verzet WHERE tanggal_minutasi_verzet IS NOT NULL) ";
		}
		// echo $check_sql2;
      	$this->load->model('dashboard/data_dashboard','indikator');
		$result = $this->indikator->getTotal($check_sql2);
		$where="perkara_id IN (".$check_sql2.")";
		$leftJoin = ", (".$check_sql2.") as b";  
		$query = $this->data_dashboard->status_perkara_list($enc,$idalurperkara,$col,$asc_desc,$begin_limit,$keyword, $leftJoin);
		
		$data['list_perkara'] = $query;
		if($this->session->userdata('total_rows')){
			if($this->session->userdata('total_rows')==-1){
				$data['total_rows'] = $this->data_dashboard->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->data_dashboard->fecth_data_num($idalurperkara,$keyword);
			$this->session->set_userdata('total_rows',$data['total_rows']);
		}
		$data['page_active'] = $begin_limit;
		$data['page_title'] = 'DAFTAR STATUS PERKARA ';
		$data['main_body'] = 'perkara_list/perkara_tab_status_perkara_list';
		$data['enc'] = $enc;
		$data['idalurperkara'] = $idalurperkara;
		$data['page_number'] = $start_page;
		$data['page_url'] = 'perkara_tab_status';
		$data['keyword'] = $keyword;
		$data['column_sorted'] = $col;
		$this->load->vars($data);
		$this->load->view('header');
		$this->load->view('body/body');
	}
}