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

class perkara_tab_status_penahanan_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);

		if(count($tmp1)!=3){
			show_404();
		}

		$this->nativesession->set_flash_session('colx', $tmp1[0]);
		$this->nativesession->set_flash_session('idperkarax', $tmp1[1]);
		$this->nativesession->set_flash_session('all', $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;
		}

		$jenis_pengadilan = $this->session->userdata('jenis_pengadilan');
		$idperkarax =  $this->nativesession->get_flash_session('idperkarax');
		$colx =  $this->nativesession->get_flash_session('colx');
		$all =  $this->nativesession->get_flash_session('all');
		$idpanitera = $this->session->userdata('panitera_id');
		$idhakim = $this->session->userdata('hakim_id');

		if(!empty($idhakim)){
			$userid=$idhakim;
		}else{
			$userid=$idpanitera;
		}

		if($colx==1){
			$check_sql2 = "SELECT n.pihak_id  
                          FROM penahanan_terdakwa n 
                          INNER JOIN (SELECT id, pihak_id, mulai, MAX(sampai) AS sampai
                            FROM penahanan_terdakwa WHERE  status_penahanan_id = 1
                            GROUP BY pihak_id) AS MAX  USING (pihak_id, sampai)
                          LEFT JOIN v_perkara AS p
                          ON p.perkara_id = n.perkara_id
                          WHERE p.alur_perkara_id=".$idperkarax." AND p.jenis_pengadilan = '".$jenis_pengadilan."'  AND p.tanggal_putusan IS NULL AND DATEDIFF(CURRENT_DATE,n.sampai) >=-7 AND DATEDIFF(CURRENT_DATE,n.sampai) <= 0 
                          AND n.perkara_id NOT IN (SELECT perkara_id FROM perkara_diversi WHERE tgl_penetapan_kesepakatan_diversi IS NOT NULL AND tgl_minutasi IS NOT NULL AND hasil_diversi=1) 
                           AND n.pihak_id NOT IN (SELECT b.pihak_id
                                          FROM    
                                          (SELECT pihak_id, MAX(id) AS id_pen
                                          FROM penahanan_terdakwa
                                          GROUP BY pihak_id ) AS a
                                          LEFT JOIN penahanan_terdakwa AS b ON a.pihak_id=b.pihak_id AND a.id_pen = b.id
                                          WHERE b.jenis_tahanan_id < 1 AND status_penahanan_id =2)
                          ORDER BY pihak_id DESC ";
		}else{
			$check_sql2 = "SELECT n.pihak_id 
						FROM
						  penahanan_terdakwa n 
						  INNER JOIN 
						    (SELECT id, pihak_id, mulai,  MAX(sampai) AS sampai 
						    FROM  penahanan_terdakwa WHERE status_penahanan_id = 1 
						    GROUP BY pihak_id) AS MAX USING (pihak_id, sampai) 
						  INNER JOIN v_perkara AS p 
						    ON p.perkara_id = n.perkara_id 
						WHERE DATEDIFF(CURRENT_DATE, n.sampai) > 0 
						  AND p.alur_perkara_id=".$idperkarax." AND p.jenis_pengadilan = '".$jenis_pengadilan."' 
						  AND p.tanggal_putusan IS NULL 
						  AND n.perkara_id NOT IN (SELECT perkara_id FROM perkara_diversi WHERE tgl_penetapan_kesepakatan_diversi IS NOT NULL AND tgl_minutasi IS NOT NULL AND hasil_diversi=1)
						   AND n.pihak_id NOT IN (SELECT b.pihak_id
                                          FROM    
                                          (SELECT pihak_id, MAX(id) AS id_pen
                                          FROM penahanan_terdakwa
                                          GROUP BY pihak_id ) AS a
                                          LEFT JOIN penahanan_terdakwa AS b ON a.pihak_id=b.pihak_id AND a.id_pen = b.id
                                          WHERE b.jenis_tahanan_id < 1 AND status_penahanan_id =2)
						ORDER BY pihak_id DESC ";
		}

		if($all!='N'){
			$check_sql2 = " ".$check_sql2." ";

		}else{
			if($colx==1){
					$check_sql2 = "SELECT a.pihak_id
                        FROM 
                        (SELECT n.pihak_id, n.perkara_id  
                          FROM penahanan_terdakwa n 
                          INNER JOIN (SELECT id, pihak_id, mulai, MAX(sampai) AS sampai
                            FROM penahanan_terdakwa WHERE  status_penahanan_id = 1
                            GROUP BY pihak_id) AS MAX  USING (pihak_id, sampai)
                          LEFT JOIN v_perkara AS p
                          ON p.perkara_id = n.perkara_id
                          WHERE p.alur_perkara_id=".$idperkarax." AND p.jenis_pengadilan = '".$jenis_pengadilan."'  AND p.tanggal_putusan IS NULL AND DATEDIFF(CURRENT_DATE,n.sampai) >=-7 AND DATEDIFF(CURRENT_DATE,n.sampai) <= 0 
                          AND n.perkara_id NOT IN (SELECT perkara_id FROM perkara_diversi WHERE tgl_penetapan_kesepakatan_diversi IS NOT NULL AND tgl_minutasi IS NOT NULL AND hasil_diversi=1) 
                           AND n.pihak_id NOT IN (SELECT b.pihak_id
                                          FROM    
                                          (SELECT pihak_id, MAX(id) AS id_pen
                                          FROM penahanan_terdakwa
                                          GROUP BY pihak_id ) AS a
                                          LEFT JOIN penahanan_terdakwa AS b ON a.pihak_id=b.pihak_id AND a.id_pen = b.id
                                          WHERE b.jenis_tahanan_id < 1 AND status_penahanan_id =2)
                          ORDER BY pihak_id DESC ) AS a,
                        perkara_penetapan AS b
                        WHERE a.perkara_id = b.perkara_id AND (( b.majelis_hakim_id IS NOT NULL AND 
                        CONCAT(',',b.majelis_hakim_id,',') LIKE '%,".$idhakim.",%'  AND '".$idhakim."' <> '')
                        OR 
                        (b.panitera_pengganti_id IS NOT NULL AND 
                        CONCAT(',',b.panitera_pengganti_id,',') LIKE '%,".$idpanitera.",%' AND '".$idpanitera."' <> ''))";
				}else{
					$check_sql2 = "SELECT a.pihak_id
                        FROM 
                        (SELECT n.pihak_id,n.perkara_id 
						FROM
						  penahanan_terdakwa n 
						  INNER JOIN 
						    (SELECT id, pihak_id, mulai,  MAX(sampai) AS sampai 
						    FROM  penahanan_terdakwa WHERE status_penahanan_id = 1 
						    GROUP BY pihak_id) AS MAX USING (pihak_id, sampai) 
						  INNER JOIN v_perkara AS p 
						    ON p.perkara_id = n.perkara_id 
						WHERE DATEDIFF(CURRENT_DATE, n.sampai) > 0 
						  AND p.alur_perkara_id=".$idperkarax." AND p.jenis_pengadilan = '".$jenis_pengadilan."' 
						  AND p.tanggal_putusan IS NULL 
						  AND n.perkara_id NOT IN (SELECT perkara_id FROM perkara_diversi WHERE tgl_penetapan_kesepakatan_diversi IS NOT NULL AND tgl_minutasi IS NOT NULL AND hasil_diversi=1)
						   AND n.pihak_id NOT IN (SELECT b.pihak_id
                                          FROM    
                                          (SELECT pihak_id, MAX(id) AS id_pen
                                          FROM penahanan_terdakwa
                                          GROUP BY pihak_id ) AS a
                                          LEFT JOIN penahanan_terdakwa AS b ON a.pihak_id=b.pihak_id AND a.id_pen = b.id
                                          WHERE b.jenis_tahanan_id < 1 AND status_penahanan_id =2)
						ORDER BY pihak_id DESC) AS a,
                        perkara_penetapan AS b
                        WHERE a.perkara_id = b.perkara_id AND (( b.majelis_hakim_id IS NOT NULL AND 
                        CONCAT(',',b.majelis_hakim_id,',') LIKE '%,".$idhakim.",%'  AND '".$idhakim."' <> '')
                        OR 
                        (b.panitera_pengganti_id IS NOT NULL AND 
                        CONCAT(',',b.panitera_pengganti_id,',') LIKE '%,".$idpanitera.",%' AND '".$idpanitera."' <> ''))";
					}
			}

		$result = $this->data_dashboard->getTotal($check_sql2);

		$where="pihak_id IN (".$check_sql2.")";

		$query = $this->data_dashboard->getPenahananListing($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 PENAHANAN ';
		$data['main_body'] = 'perkara_tab/penahanan_list_v';
		$data['enc'] = $enc;
		$data['idalurperkara'] = $idalurperkara;
		$data['page_number'] = $start_page;
		$data['page_url'] = 'perkara_tab_penahanan';
		$data['keyword'] = $keyword;
		$data['column_sorted'] = $col;
		$this->load->vars($data);
		$this->load->view('header');
		$this->load->view('body/body');
	}
}