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

class Perkara_tab_dashboard_list 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);
		//echo $tmp;
		if(count($tmp1)!=4){
			show_404();
		}

		$this->nativesession->set_flash_session('colx', $tmp1[0]);
		$this->nativesession->set_flash_session('idperkarax', $tmp1[1]);
		$this->nativesession->set_flash_session('bulan_perkara', $tmp1[2]);
		$this->nativesession->set_flash_session('tahun_perkara', $tmp1[3]);

		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;
		}

		$colx =  $this->nativesession->get_flash_session('colx');
		$idperkarax =  $this->nativesession->get_flash_session('idperkarax');
		$bulan_perkara =  $this->nativesession->get_flash_session('bulan_perkara');
		$tahun_perkara =  $this->nativesession->get_flash_session('tahun_perkara');
		
		$mainurl = $colx.'/'.$idperkarax.'/'.$bulan_perkara.'/'.$tahun_perkara;
		$enc = base64_encode($this->encrypt->encode($mainurl));
		//echo $mainurl;

			if($tahun_perkara=='') $tahun_perkara= date("Y");
			if($bulan_perkara=='') $bulan_perkara= date("m");
			$blnlalu = intval($bulan_perkara - 1);
			$blndepan = intval($bulan_perkara + 1);
			if(strlen($blnlalu)==1) $blnlalu='0'.$blnlalu;
			if(strlen($bulan_perkara)==1) $bulan_perkara='0'.$bulan_perkara;
			if(strlen($blndepan)==1) $blndepan='0'.$blndepan;

			$bulanlalu = $tahun_perkara .'-'.$blnlalu;
			$tahunlalu = $tahun_perkara;
			$bulanini = $tahun_perkara .'-'.$bulan_perkara;
			$tahunini = $tahun_perkara;
			$bulandepan = $tahun_perkara.'-'.$blndepan;
			$tahundepan = $tahun_perkara;
			if($bulan_perkara==12)
			{ 
				$blndepan = 1;
				$tahundepan = intval($tahun_perkara+1);
				$bulandepan = $tahundepan.'-01';
			}
			if($bulan_perkara=='01')
			{ 
				$blnlalu = 12;
				$tahunlalu = intval($tahun_perkara-1);
				$bulanlalu = $tahunlalu.'-12';
			}	


		#roby
		$perkara_putusan='perkara_putusan';
		$check_sql2 = "SELECT p.perkara_id as perkara_id FROM ";
		if($colx==1 AND $idperkarax==$idperkarax){ ##belum putus
			$perkara='perkara';	
				if($idperkarax<>118){
					$addAnd = " ";
				}else{
					$addAnd = " AND p.perkara_id NOT IN (SELECT diversi.perkara_id FROM perkara_diversi AS diversi WHERE diversi.hasil_diversi = 1 
					  	AND tgl_minutasi IS NOT NULL AND dibuka_kembali=0
					  	AND (diversi.tgl_penetapan_kesepakatan_diversi != '0000-00-00' 
					    	OR LEFT(diversi.tgl_penetapan_kesepakatan_diversi,7) >= '".$bulanini."')) ";
				}
					$check_sql2 .=" ".$perkara."  AS p  LEFT OUTER JOIN ".$perkara_putusan." AS putus ON p.perkara_id = putus.perkara_id  
					WHERE p.alur_perkara_id = ".$idperkarax." 
				    AND LEFT(p.tanggal_pendaftaran, 7) < '".$bulanini."' 
				    AND (putus.tanggal_putusan IS NULL OR LEFT(putus.tanggal_putusan, 7) >= '".$bulanini."' ) 
				    AND '".$bulanini."'  <= CONCAT(YEAR(CURRENT_DATE),'-',RIGHT(CONCAT('0',CAST(MONTH(CURRENT_DATE) AS CHAR(2))),2))".$addAnd;
			}elseif ($colx==2 AND $idperkarax==$idperkarax) { ##masuk
				$perkara='perkara';	
				$check_sql2 .=" ".$perkara."  AS p WHERE p.alur_perkara_id = ".$idperkarax."
    				AND LEFT(p.tanggal_pendaftaran, 7) = '".$bulanini."' ";
			}elseif ($colx==3 AND $idperkarax==$idperkarax) { ##putus
				$perkara='perkara';	
				$check_sql2  .=" ".$perkara."  AS p LEFT OUTER JOIN perkara_putusan AS putus ON p.perkara_id = putus.perkara_id 
			  	WHERE p.alur_perkara_id = ".$idperkarax."
				    AND LEFT(p.tanggal_pendaftaran,7) <= '".$bulanini."'
				    AND LEFT(putus.tanggal_putusan,7) = '".$bulanini."'  ";
			}elseif ($colx==4 AND $idperkarax==$idperkarax) { ## sisa
				$perkara='perkara';	
					if($idperkarax<>118){
						$addAnd = " ";
					}else{
						$addAnd = " AND p.perkara_id NOT IN (SELECT diversi.perkara_id FROM perkara_diversi AS diversi WHERE diversi.hasil_diversi = 1 
									AND tgl_minutasi IS NOT NULL AND dibuka_kembali=0
									AND (diversi.tgl_penetapan_kesepakatan_diversi <> '0000-00-00' 
										OR LEFT(diversi.tgl_penetapan_kesepakatan_diversi,7) >= '".$bulanini."'))";
					}
					$check_sql2  .=" ".$perkara."  AS p LEFT OUTER JOIN perkara_putusan AS putus ON p.perkara_id = putus.perkara_id 
				  	WHERE p.alur_perkara_id = ".$idperkarax."
				    AND LEFT(p.tanggal_pendaftaran, 7) < '".$bulandepan."' 
				    AND (putus.tanggal_putusan IS NULL OR LEFT(putus.tanggal_putusan, 7) >= '".$bulandepan."'  ) 
				    AND '".$bulanini."'  <= '".$bulanini."' ".$addAnd;
			}elseif ($colx==5 AND $idperkarax==$idperkarax) { ##banding
				$perkara='perkara_banding';
				$check_sql2  ="SELECT  perkara_id as perkara_id from ".$perkara."  WHERE alur_perkara_id = ".$idperkarax."
    				AND LEFT(permohonan_banding, 7) = '".$bulanini."'  
    				AND '".$bulanini."'  <= '".$bulanini."'  ";
			}elseif ($colx==6 AND $idperkarax==$idperkarax) { ##kasasi
				$perkara='perkara_kasasi';
				$check_sql2  ="SELECT  perkara_id as perkara_id from ".$perkara."  WHERE alur_perkara_id = ".$idperkarax."
				    AND LEFT(permohonan_kasasi, 7) = '".$bulanini."' 
				    AND '".$bulanini."'  <= '".$bulanini."'  ";
			}elseif ($colx==7 AND $idperkarax==$idperkarax) { ##pk
				$perkara='perkara_pk';
				$check_sql2  ="SELECT  perkara_id as perkara_id from ".$perkara."  WHERE alur_perkara_id = ".$idperkarax."
    				AND LEFT(permohonan_pk, 7) = '".$bulanini."'  
    				AND '".$bulanini."'  <= '".$bulanini."'  ";
			}elseif ($colx==8 AND $idperkarax==$idperkarax) { ##eksekusi
				$perkara='perkara_eksekusi';
				$check_sql2  ="SELECT  perkara_id as perkara_id from ".$perkara."  WHERE alur_perkara_id = ".$idperkarax."
    				AND LEFT(permohonan_eksekusi, 7) = '".$bulanini."'  
    				AND '".$bulanini."'  <= '".$bulanini."'  ";
			}elseif ($colx==9 AND $idperkarax==$idperkarax) { ##grasi
				$perkara='perkara_grasi';
				$check_sql2  ="SELECT  perkara_id as perkara_id from ".$perkara."  WHERE alur_perkara_id = ".$idperkarax."
    				AND LEFT(permohonan_grasi, 7) = '".$bulanini."'  
    				AND '".$bulanini."'  <= '".$bulanini."'  ";
			}
		$result = $this->data_dashboard->getTotal($check_sql2);
		$where="perkara_id IN (".$check_sql2.")";
		$query = $this->data_dashboard->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->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 PERKARA INDIKATOR';
		$data['main_body'] = 'perkara_list/perkara_tab_status_list';
		$data['enc'] = $enc;
		$data['idalurperkara'] = $idalurperkara;
		$data['page_number'] = $start_page;
		$data['page_url'] = 'perkara_dashboard';
		$data['keyword'] = $keyword;
		$data['column_sorted'] = $col;
		$this->load->vars($data);
		$this->load->view('header');
		$this->load->view('body/body');
	}
}