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

foreach ($template->result() as $template){
    $filename=$template->kdtemplate;
    $namafile=str_replace(" ","",$template->namatemplate);
}


$url = base_url("resources/template/penetapan/pidana_militer/".$filename.".rtf");
/*$jenisGugatan = intval($jenisGugatan); 
if(!is_numeric($jenisGugatan)){
    show_404();
    exit();
}*/

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

$dataPenetapan = $dataTemplate;
$terdakwa = array();
foreach ($dataPenetapan->result() as $row) {
    if (!in_multiarray($row->namat, $terdakwa)){
    	$terdakwaItem = array();
    	$terdakwaItem["nama"] = $row->namat;
    	$terdakwaItem["alamat"] = $row->alamatt;
    	$terdakwaItem["urutant"] = $row->urutanT;
    	$terdakwaItem["tlt"] = $row->tlt;
    	$terdakwaItem["tgllhrt"] = $row->tgllhrt;
    	$terdakwaItem["jkt"] = $row->jkt;
    	$terdakwaItem["agama"] = $row->agamat;
    	$terdakwaItem["alamatt"] = $row->alamatt;
    	$terdakwaItem["nrp"] = $row->nrp;
    	$terdakwaItem["jabatant"] = $row->jabatant;
    	$terdakwaItem["pangkatt"] = $row->pangkat;
    	$terdakwaItem["kesatuan"] = $row->kesatuan;
    	$terdakwa[] = $terdakwaItem;
    }

	$nomorperkara=$row->nomor_perkara;
	$urutan=$row->urutan_hakim;
	$jabatan=$row->jabatan_hakim_nama;
	$namaPejabat=$this->templatehelper->standradnaming($row->KetuaPNNama);
	$tanggalPendaftaran=$this->templatehelper->convertKeTglIndo($row->tanggal_pendaftaran);
	$tanggalSurat=$this->templatehelper->convertKeTglIndo($row->tgl_surat_dakwaan);
	$tglPenetapanharisidang=$this->templatehelper->convertKeTglIndo($row->penetapanharisidang);
	$nomorSurat=$row->nomor_surat;
	$tgl_pen_hakim=$this->templatehelper->convertKeTglIndo($row->penetapan_majelis_hakim);
	$tgl_pen_pp=$this->templatehelper->convertKeTglIndo($row->tgl_pen_pp);
	$kode_pn=$row->kode_pn;
	$nomurUrutPerkara = $row->nomor_urut_perkara;
	$matra=$row->matra;
	if($matra==1){
		$matra1='AD';
	}elseif ($matra==2) {
		$matra1='AL';
	}else{
		$matra1='AU';
	}
	$nomorSplit = explode("/",$nomorperkara);
	$noreg = $nomorSplit['0'];
	$blnSplit = $nomorSplit['3'];
	$thnSplit = $nomorSplit['4'];
	$nama_pengadilan=$row->namaPengadilan;
	$nama_kota=$this->templatehelper->namaKota($row->namaPengadilan);
	$tanggal_skeppera = $this->templatehelper->convertKeTglIndo($row->tanggal_skeppera);
	$pejabat_skeppera = $row->pejabat_skeppera;	
	$nomor_skeppera = $row->nomor_skeppera;
	$tanggal_surat_dakwaan = $this->templatehelper->convertKeTglIndo($row->tanggal_surat_dakwaan);
	$nomor_surat_dakwaan = $row->nomor_surat_dakwaan;
	$nama_kadilmil = $row->KetuaPNNama;
	$Spengadilan = explode(" ",$row->pengadilan);
	$nmPengadilan = $Spengadilan['0'].' '.$Spengadilan['1'];
	$kotaPengadilan = $Spengadilan['3'];
	$kode_pn1 = str_replace("PM.", "", $kode_pn);
	$ditetapkanoleh = $row->ditetapkanoleh;
	$jabatan_ttd = $row->jabatan_ttd;
	$nrp_ditetapkan = $row->nrp;
	$pangkat_ditetapkan = $row->pangkat;
	$zona_waktu = $row->zona_waktu;
	$pangkat_korp_ketua=$row->PangkatKetua; 
	$nrp_kadilmil=$row->KetuaPNNip;
	$berkas_dari= $row->berkas_dari;
	$nama_pt = $row->namaPT; 

}
foreach ($infopk->result() as $val) {
	$nomorPkTmp = explode("-",$val->permohonan_pk);
	$blnSplitPk = $nomorPkTmp['1'];
	$thnSplitPk = $nomorPkTmp['0'];
	$nomorAktaPk = $noreg.'/'.$kode_pn.'/'.$matra1.'/'.$blnSplitPk.'/'.$thnSplitPk;
	$tglPermohonanPk = $val->permohonan_pk;
	$penetapan_pemeriksaan_pk = $val->penetapan_pemeriksaan_pk;
	$pemeriksaan_pk = $val->pemeriksaan_pk;
}

$i=1;
foreach ($dataPutBef->result() as $val) {
	$tahapan = $val->tahapan;
	$nomorPerkara = $val->nomorPerkara;
	$tglPutus = $val->tglPutus;
	$infoPut = $i.'. \tab Putusan '.$tahapan.' '.$nomorPerkara.'  '.$this->templatehelper->convertKeTglIndo($tglPutus).' \par ';
	$Template = str_replace("#infoPut#",''.$infoPut.'',$Template);	
	$i++;
}

/*
$pk_infoMajelisHakim->row(0)->nama;
print_r($pk_infoMajelisHakim->row(0)->nama);
exit();
*/
$varHakim = $pk_infoMajelisHakim->result();
if(count($varHakim)==3){
	$hakimKetuapk = $varHakim[0]->nama.' '.$varHakim[0]->pangkat.' NRP '.$varHakim[0]->nip;	
	$hakimAnggota1pk = $varHakim[1]->nama.' '.$varHakim[1]->pangkat.' NRP '.$varHakim[1]->nip;
	$hakimAnggota2pk = $varHakim[2]->nama.' '.$varHakim[2]->pangkat.' NRP '.$varHakim[2]->nip;
	$namaHakimKetua = $varHakim[0]->nama;
	$pangkatHakimKetua = $varHakim[0]->pangkat;
	$nrpHakimketua = $varHakim[0]->nip;
}else{
	$hakimKetuapk = '';	
	$hakimAnggota1pk = '';
	$hakimAnggota2pk = '';
	$namaHakimKetua = '';
	$pangkatHakimKetua = '';
	$nrpHakimketua = '';
}
$Template = str_replace("#hakimKetuapk#",''.$hakimKetuapk.'',$Template);	
$Template = str_replace("#hakimAnggota1pk#",''.$hakimAnggota1pk.'',$Template);	
$Template = str_replace("#hakimAnggota2pk#",''.$hakimAnggota2pk.'',$Template);	
$Template = str_replace("#namaHakimKetua#",''.$namaHakimKetua.'',$Template);
$Template = str_replace("#pangkatHakimKetua#",''.$pangkatHakimKetua.'',$Template);
$Template = str_replace("#nrpHakimketua#",''.$nrpHakimketua.'',$Template);


$cterdakwa = count($terdakwa);
if($cterdakwa>1){
	$cTer='para terdakwa';
}else{
	$cTer='terdakwa';
}

$i = 1;
$terdakwaList='';
foreach ($terdakwa as $item) {
	if($cterdakwa>1){
		$naskah  = "Terdakwa ".$this->templatehelper->converToAngkaRomawi($item["urutant"])."\b0 ;"." \\par ";
		$terdakwaList = ','.$this->templatehelper->standradnaming($item["nama"]).' '.$item["pangkatt"].'  NRP '.$item["nrp"];
	}else{
		$naskah = "\\par ";
		$terdakwaList = $this->templatehelper->standradnaming($item["nama"]).' '.$item["pangkatt"].'  NRP '.$item["nrp"];
	}

	$naskah .= 'Nama lengkap \tab : '.$this->templatehelper->standradnaming($item["nama"]).'\\par ';
	$naskah .= 'Pangkat/NRP \tab : '.$item["nrp"].'\\par ';
	$naskah .= 'Jabatan \tab : '.$item["jabatant"].'\\par ';
	$naskah .= 'Kesatuan \tab : '.$item["kesatuan"].'\\par ';
	$naskah .= 'Tempat, tanggal lahir \tab : '.$item["tlt"].','.$this->templatehelper->convertKeTglIndo($item["tgllhrt"]).'\\par ';
	$naskah .= 'Jenis kelamin \tab : '.$item["jkt"].'\\par ';
	$naskah .= 'Kewarganegaraan \tab : '.'Indonesia'.'\\par ';
	if(!empty($item["agama"])){
		$agm=$item["agama"];
	}else{
		$agm='Belum diisi';
	}
	$naskah .= 'Agama \tab : '.$agm.'\\par ';
	$naskah .= 'Tempat tinggal \tab : '.$item["alamatt"].'\\par ';
	$brnaskah = $naskah.' \\par '.PHP_EOL.(($i<$cterdakwa)?("#terdakwaParties#"):(""));
	$Template = str_replace("#terdakwaParties#",''.$brnaskah.'',$Template);	
	$i++;
}
$Template = str_replace('#terdakwaList#',''.$terdakwaList.'',$Template);

if(!empty($tgl_penetapan)){
	$tgl_penetapan = $tgl_penetapan;
	$Template = str_replace("#tgl_penetapan#",''.$tgl_penetapan.'',$Template);
}

if (empty($tanggalSurat)){ $tanggalSurat="#Isi Dulu Tanggal Surat Pada Aplikasi#"; }
$Template = str_replace("#nomor#",''.$noreg.'',$Template);
$Template = str_replace("#terdakwa#",''.$cTer.'',$Template);
$Template = str_replace("#namaPengadilan#",''.$nama_pengadilan.'',$Template);
$Template = str_replace("#nomor_register#",''.$nomorperkara.'',$Template);
$Template = str_replace("#tanggal_register#",''.$tanggalPendaftaran.'',$Template);
$Template = str_replace("#tgl_pen_hakim#",''.$tgl_pen_hakim.'',$Template);
$Template = str_replace("#tgl_pen_pp#",''.$tgl_pen_pp.'',$Template);
$Template = str_replace("#tgl_tapsid#",''.$tglPenetapanharisidang.'',$Template);
$Template = str_replace("#tanggalSurat#",''.$tanggalSurat.'',$Template);
$Template = str_replace("#kode_pn#",''.$kode_pn.'',$Template);
$Template = str_replace("#kode_pn1#",''.$kode_pn1.'',$Template);
$Template = str_replace("#matra#",''.$matra1.'',$Template);
$Template = str_replace("#nomor#",''.$nomurUrutPerkara.'',$Template);
$Template = str_replace("#bulan#",''.$blnSplit.'',$Template);
$Template = str_replace("#tahun#",''.$thnSplit.'',$Template);
$Template = str_replace("#kode_pn#",''.$kode_pn.'',$Template);
$Template = str_replace("#skeppera#",''.$pejabat_skeppera.'',$Template);
$Template = str_replace("#nomor_keppera#",''.$nomor_skeppera.'',$Template);
$Template = str_replace("#tanggal_keppera#",''.$tanggal_skeppera.'',$Template);
$Template = str_replace("#nomor_dakwaan#",''.$nomor_surat_dakwaan.'',$Template);
$Template = str_replace("#tanggal_dakwaan#",''.$tanggal_surat_dakwaan.'',$Template);
$Template = str_replace("#nama_kadilmil#",''.$nama_kadilmil.'',$Template);
$Template = str_replace("#kota_pn#",''.ucwords(strtolower($kotaPengadilan)).'',$Template);
$Template = str_replace("#kota_pn1#",''.$kotaPengadilan.'',$Template);
$Template = str_replace("#nomor_akta_pk#",''.$nomorAktaPk.'',$Template);
$Template = str_replace("#tanggal_pk#",''.$this->templatehelper->convertKeTglIndo($tglPermohonanPk).'',$Template);
$Template = str_replace("#tglPenetapan#",''.$this->templatehelper->convertKeTglIndo($penetapan_pemeriksaan_pk).'',$Template);
$Template = str_replace("#tgl_pemeriksaan_pk#",''.$this->templatehelper->convertKeTglIndo($pemeriksaan_pk).'',$Template);
$Template = str_replace("#hari_pemeriksaan_pk#",''.$this->templatehelper->gethari($pemeriksaan_pk).'',$Template);


$Template = str_replace("#ditetapkanoleh#",''.$ditetapkanoleh.'',$Template);
$Template = str_replace("#jabatan_ttd#",''.$jabatan_ttd.'',$Template);
$Template = str_replace("#pangkat_ditetapkan#",''.$pangkat_ditetapkan.'',$Template);
$Template = str_replace("#nrp_ditetapkan#",''.$nrp_ditetapkan.'',$Template);
$Template = str_replace("#zona_waktu#",''.$zona_waktu.'',$Template);
$Template = str_replace("#pangkat_korp#",''.$pangkat_korp_ketua.'',$Template);
$Template = str_replace("#nrp_kadilmil#",''.$nrp_kadilmil.'',$Template);
$Template = str_replace("#berkas_dari#",''.$berkas_dari.'',$Template);
$Template = str_replace("#nama_pt#",''.ucwords(strtolower($nama_pt)).'',$Template);

header("Content-Type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control:  must-revalidate, post-check=0, pre-check=0");
header("Content-disposition: attachment; filename=\"".$namafile."-".$nomorperkara.".rtf\"");
echo $Template;
?>