<?php
if($this->session->userdata('is_logged_in')===FALSE){
			redirect('login');
}

//DODON

/*$idperkara=$this->encrypt->decode(base64_decode($enc));

$idperkara = intval($idperkara);

if(!is_numeric($idperkara) OR !intval($idperkara)>0){
    show_404();
	exit();
}*/

$url = base_url("resources/template/berita_acara/pidana_umum/".$nama_file.".rtf");
$judulTemplate = preg_replace('/\s/','',$judul_cetak);
//echo "halo : $judulTemplate";
$Template = file_get_contents($url);

//==================================================================================//

function in_multiarray($sval, $arr){
	$ret = false;
	foreach ($arr as $value) {
		if (is_array($value)){
			$ret = $ret | in_multiarray($sval, $value);
		}else{
			if ($value == $sval){
				return true;
			}
		}
	}
	return $ret;
}

$dataPerkara = $data_template;
$terdakwa = array();
$penahanan = array();
$i=0;
foreach ($dataPerkara->result() as $row) {
    if (!in_multiarray($row->namaTerdakwa, $terdakwa)){
    	$terdakwaItem = array();
    	$terdakwaItem['pihakIdTerdakwa'] = $row->pihakIdTerdakwa;
    	$terdakwaItem["namaTerdakwa"] = $this->templatehelper->standradnaming($row->namaTerdakwa);
    	$terdakwaItem["alamatTerdakwa"] = $this->templatehelper->standradnaming($row->alamatTerdakwa);
    	$terdakwaItem["agamaTerdakwa"] = $row->agamaTerdakwa;
    	$terdakwaItem["kelaminTerdakwa"] = $row->kelaminTerdakwa;
    	$terdakwaItem["negaraTerdakwa"] = $row->negaraTerdakwa;
    	$terdakwaItem["pendidikanTerdakwa"] = $row->pendidikanTerdakwa;
    	$terdakwaItem["tanggalLahirTerdakwa"] = $this->templatehelper->convertKeTglIndo($row->tanggalLahirTerdakwa);
    	$terdakwaItem["tempatLahirTerdakwa"] = $this->templatehelper->standradnaming($row->tempatLahirTerdakwa);
    	#$terdakwaItem['umurTerdakwa'] = $row->umurTerdakwa;
    	$terdakwaItem['pekerjaanTerdakwa'] = $this->templatehelper->standradnaming($row->pekerjaanTerdakwa);
    	$terdakwaItem['umurTerdakwa'] = $this->tanggalhelper->getSelisihTahun($terdakwaItem["tanggalLahirTerdakwa"],date('Y-m-d'));
    	$terdakwa[] = $terdakwaItem;

    	
    }

    		$penahananItem = array();
    		$penahananItem['penahananPihakId'] = $row->pihak_id;
    		$penahananItem["jenis_penahanan_id"] = $row->jenis_penahanan_id;
    		$penahananItem["jenis_tahanan_id"] = $row->jenis_tahanan_id;
    		$penahananItem["tanggal_surat"] = $row->tanggal_surat;
    		$penahananItem["nomor_surat"] = $row->nomor_surat;
    		$penahananItem["mulai"] = $this->templatehelper->convertKeTglIndo($row->mulai);
    		$penahananItem["sampai"] = $this->templatehelper->convertKeTglIndo($row->sampai);
    		$penahananItem["lama_penahanan"] = $row->lama_penahanan;
    		$penahananItem["statusPenahanan"] = $row->statusPenahanan;
    		$penahananItem["jenisPenahanan"] = $row->jenisPenahanan;
    		$penahananItem["jenisTahanan"] = $row->jenisTahanan;
    		$penahanan[$i] = $penahananItem;
    		$i++;

    $namaTerdakwa = $this->templatehelper->standradnaming($row->namaTerdakwa);
	$nomorPerkara = $row->nomorPerkara;
	$tanggalPenetapanHakim = $this->templatehelper->convertKeTglIndo($row->tanggalPenetapanHakim);
	$tanggalPendaftaran = $this->templatehelper->convertKeTglIndo($row->tanggalPendaftaran);
	$dakwaan = $this->templatehelper->cleanHtmlTag($row->dakwaan);
	$namaPN =$this->templatehelper->standradnaming($row->namaPN);
	$alamatPN = $this->templatehelper->standradnaming($row->alamatPN);
	$zonaWaktu = $this->templatehelper->standradnaming($row->zonaWaktu);
	$kota     =$this->templatehelper->namaKota($row->namaPN);
	$tanggalSurat  = $this->templatehelper->convertKeTglIndo($row->tanggalSurat);
	$tanggalSidangNow = $this->templatehelper->convertKeTglIndo($row->tanggalSidang1);
	$hariSidangNow = $this->templatehelper->getHari($row->tanggalSidang1);
	$jamSidangNow = $row->jamSidang1;
	$tanggalSidangNext = $this->templatehelper->convertKeTglIndo($row->tanggalSidang2);
	$hariSidangNext = $this->templatehelper->getHari($row->tanggalSidang2);
	$jamSidangNext = $row->jamSidang2;
	$zonaWaktu = $row->zonaWaktu;
	$tanggalSidangNext = $this->templatehelper->convertKeTglIndo($row->tanggalSidang2);
	$hariSidangNext = $this->templatehelper->getHari($row->tanggalSidang2);
	$jamSidangNext = $row->jamSidang2;
	$agendaSidangNext = $row->agendaSidang2;
	$amarPutusan = $this->templatehelper->cleanHtmlTag($row->amarPutusan);
	$hariPutusan = $this->templatehelper->getHari($row->tanggalPutusan);
	$tanggalPutusan = $this->templatehelper->convertKeTglIndo($row->tanggalPutusan);
	$namaPansek = $this->templatehelper->standradnaming($row->namaPansek);
	$namaHakimTT   =$this->templatehelper->standradnaming($row->namaHakim1);
	$paniteraTT	= $this->templatehelper->standradnaming($row->namaPanitera1);
	$namaJaksa = $this->templatehelper->standradnaming($row->namaJaksa)."\\tab Sebagai Jaksa;\\par ";
	$namaJPU = $this->templatehelper->standradnaming($row->namaJaksa);
	$nomorSurat = $row->nomorSurat;
}



$cekTerdakwa = count($terdakwa);
if($cekTerdakwa==1){
	$jenisterdakwa = "Terdakwa";
} else {
	$jenisterdakwa = "Para Terdakwa";
}

foreach ($terdakwa as $row) {
	$pihaklist = $this->templatehelper->standradnaming($row['namaTerdakwa']).'\\line ';
}
$i = 1;
$cTerdakwa = count($terdakwa);
if($cTerdakwa==1){
	$statusJumlahTerdakwa = "";
} else {
	$statusJumlahTerdakwa = " ,dkk";
}
foreach ($terdakwa as $row) {
	$pihak  = '\b Terdakwa '.$i.'\b0\\par ';
	$pihak .= '\tab Nama lengkap \tab : '.$this->templatehelper->standradnaming($row['namaTerdakwa']).'\\par ';
	$pihak .= '\tab Tempat lahir \tab : '.$this->templatehelper->standradnaming($row['tempatLahirTerdakwa']).'\\par ';
	$pihak .= '\tab Umur/Tanggal lahir \tab : '.$row['umurTerdakwa'].' / '.$row['tanggalLahirTerdakwa'].'\\par ';
	$pihak .= '\tab Jenis kelamin \tab : '.$row['kelaminTerdakwa'].'\\par ';
	$pihak .= '\tab Kebangsaan \tab : '.$row['negaraTerdakwa'].'\\par ';
	$pihak .= '\tab Tempat tinggal \tab : '.$this->templatehelper->standradnaming($row['alamatTerdakwa']).'\\par ';
	$pihak .= '\tab Agama \tab : '.$row['agamaTerdakwa'].'\\par ';
	$pihak .= '\tab Pekerjaan \tab : '.$this->templatehelper->standradnaming($row['pekerjaanTerdakwa']).'\\par \\par ';
	if(($nama_file=='PID.S_14_200_312')||($nama_file=='PID_14_200_301')){
		$pihak .= "Terdakwa / Para Terdakwa ditangkap pada tanggal ..... berdasarkan surat perintah penangkapan Nomor ..... tanggal ..... ; \\par ";
		$pihak .= "Terdakwa / Para Terdakwa ditahan dalam tahanan ..... oleh : \\par ";
		$a=0;
		foreach ($penahanan as $row) {$a++;
			$pihak .= $a.'.'.$row['jenisPenahanan'].' sejak Tanggal '.$row['mulai'].' sampai dengan Tanggal '.$row['sampai'].'\\line ';
		}

		$Template = str_replace('#keteranganph#',''.$naskahPH.'',$Template);

	}
	
	$loopPihak = (($cTerdakwa > 1)?($i):("")).' \tab '.$pihak.' \\par '.PHP_EOL.(($i<$cTerdakwa)?("#loopingterdakwa#"):(""));
	$Template = str_replace('#loopingterdakwa#',''.$loopPihak.'',$Template);
	$i++;
}





$Template = str_replace('#nomorperkara#',''.$nomorPerkara.'',$Template);
$Template = str_replace('#namapn#',''.$namaPN.'',$Template);
$Template = str_replace('#alamatpn#',''.$alamatPN.'',$Template);
$Template = str_replace('#tanggalsidang#',''.$tanggalSidangNow.'',$Template);
$Template = str_replace('#jamsidang#',''.$jamSidangNow.'',$Template);
$Template = str_replace('#jenisterdakwa#',''.$jenisterdakwa.'',$Template);
$Template = str_replace('#namaterdakwalist#',''.$pihaklist.'',$Template);
$Template = str_replace('#hakim#',''.$this->templatehelper->standradnaming($hakim).'',$Template);
$Template = str_replace('#panitera#',''.$this->templatehelper->standradnaming($panitera).'',$Template);
$Template = str_replace('#amarputusan#',''.$amarPutusan.'',$Template);
$Template = str_replace('#tanggalputusan#',''.$tanggalPutusan.'',$Template);
$Template = str_replace('#harisidang#',''.$hariSidangNow.'',$Template);
$Template = str_replace('#paniteraTT#',''.$paniteraTT.'',$Template);
$Template = str_replace('#hakimTT#',''.$namaHakimTT.'',$Template);
$Template = str_replace('#namajpu#',''.$namaJaksa.'',$Template);
$Template = str_replace('#zonawaktu#',''.$zonaWaktu.'',$Template);
$Template = str_replace('#jenishakim#',''.$jenisHakim.'',$Template);
$Template = str_replace('#jenisterdakwa#',''.$jenisterdakwa.'',$Template);
$Template = str_replace('#jenispanggilan#','Menghadapkan',$Template);
$Template = str_replace('#tanggalsurat#',''.$tanggalSurat.'',$Template);
$Template = str_replace('#nomorsurat#',''.$nomorSurat.'',$Template);
$Template = str_replace('#tanggalsidangnew#',''.$tanggalSidangNext.'',$Template);
$Template = str_replace('#harisidangnew#',''.$hariSidangNext.'',$Template);
$Template = str_replace('#jamsidangnew#',''.$jamSidangNext.'',$Template);
$Template = str_replace('#agendasidangnew#',''.$agendaSidangNext.'',$Template);
$Template = str_replace('#namaterdakwa#',''.$namaTerdakwa.''.$statusJumlahTerdakwa.'',$Template);

header("Content-type: application/rtf");
header("Content-disposition: inline; filename=".$judulTemplate."-".$nomorPerkara.".rtf");
header("Content-length: ".strlen($Template));
echo $Template;
?>