<?php
if($this->session->userdata('is_logged_in')===FALSE){
            redirect('login');
}
//$segment = $this->uri->segment_array();
//$idperkara = intval($this->encrypt->decode(base64_decode($segment[3],TRUE)));
//$idperkara=$this->encrypt->decode(base64_decode($enc));
// $jenisTemplate = intval($jenisTemplate);
// if(!is_numeric($jenisTemplate)){
//     show_404();
//     exit();
// }


//PDT_15_210_28   value='1'>Putusan PKPU Konter Pailit                                               
//PDT_15_210_29   value='2'>Putusan PKPU Menjadi Pailit                                              
//PDT_15_210_30   value='3'>Putusan PKPU Tetap - Kabul                                               
//PDT_15_210_31   value='4'>Putusan PKPU Yang Diajukan Debitor - Kabul
//PDT_15_210_32   value='5'>Putusan PKPU Yang Diajukan Debitor - Tolak
//PDT_15_210_33   value='6'>Putusan PKPU Yang Diajukan Kreditor - Kabul
//PDT_15_210_33   value='7'>Putusan Actio Pauliana 
//PDT_15_210_33   value='8'>Putusan Homologasi 
//PDT_15_210_33   value='9'>Putusan Rehabilitasi

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

$url = base_url("resources/template/putusan/perdata_khusus/".$filename.".rtf");

$Template = file_get_contents($url);
//=============================================roby ======================================================

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

$dataPutusan = $data_template;
$tergugat = array();
$penggugat = array();
$hakim = array();
$pengacaraPenggugat = array();
$pengacaraTergugat = array();
$datapihak3 = array();

foreach ($dataPutusan->result() as $row) {
    if (!in_multiarray($row->namaP, $penggugat)){
    	$penggugatItem = array();
    	$penggugatItem["nama"] = $row->namaP;
    	$penggugatItem["alamat"] = $row->alamatP;
    	$penggugatItem["statusP"] = $row->statusP;
    	$penggugatItem["urutanP"] = $row->urutanP;
    	$penggugat[] = $penggugatItem;
    }
    if (!in_multiarray($row->namaT, $tergugat)){
    	$tergugatItem = array();
    	$tergugatItem["nama"] = $row->namaT;
    	$tergugatItem["alamat"] = $row->alamatT;
    	$tergugatItem["statusT"] = $row->statusT;
    	$tergugatItem["urutanT"] = $row->urutanT;
    	$tergugat[] = $tergugatItem;
    }
    if (!in_multiarray($row->nama_hakim, $hakim)){
    	$hakimItem = array();
    	$hakimItem["urutan_hakim"] = $row->urutan_hakim;
    	$hakimItem["jabatan_hakim_id"] = $row->jabatan_hakim_id;
    	$hakimItem["nama"] = $row->nama_hakim;
    	$hakimItem["jabatan_hakim_nama"] = $row->jabatan_hakim_nama;
    	$hakim[] = $hakimItem;
    }

    if (!in_multiarray($row->nama, $datapihak3)){
        $pihak3Item = array();
        $pihak3Item["urutan"] = $row->urutan;
        $pihak3Item["nama"] = $row->nama;
        $pihak3Item["alamat"] = $row->alamat;
        $pihak3Item["jenis_pihak_id"] = $row->jenis_pihak_id;
        $datapihak3[] = $pihak3Item;
    }

    if (!in_multiarray($row->namaPengacaraP, $pengacaraPenggugat)){
    	$pengacaraPitem = array();
    	$pengacaraPitem["nama"] = $row->namaPengacaraP;
    	$pengacaraPenggugat[] = $pengacaraPitem;
    }
    if (!in_multiarray($row->namaPengacaraT, $pengacaraTergugat)){
    	$pengacaraPitem = array();
    	$pengacaraPitem["nama"] = $row->namaPengacaraT;
    	$pengacaraTergugat[] = $pengacaraPitem;
    }

	$namaPengadilan =$this->templatehelper->standradnaming($row->namaPengadilan);
	$kota     =$row->namaPengadilan;
    $namaketua     =$row->namaketua;
	$nomorperkara =$row->nomor_perkara;
	$nama_mediator =$this->templatehelper->standradnaming($row->nama_mediator);
	$profesi =$row->profesi;
	$tgl_mediasi =$this->templatehelper->convertKeTglIndo($row->keputusan_mediasi);
	$nama_pp =$this->templatehelper->standradnaming($row->nama_pp);
    $jurusita_nama =$this->templatehelper->standradnaming($row->jurusita_nama);
	$tanggalPendaftaran   =$this->templatehelper->convertKeTglIndo($row->tanggal_pendaftaran);
	$tgl_surat_gugatan   =$this->templatehelper->convertKeTglIndo($row->tgl_gugatan);
	$nomorSurat   =$this->templatehelper->convertKeTglIndo($row->nomor_surat);
	$tglPenHakim   =$this->templatehelper->convertKeTglIndo($row->penetapan_majelis_hakim);
	$nama_hakim =$this->templatehelper->standradnaming($row->nama_hakim);
	$tglPenPP   =$this->templatehelper->convertKeTglIndo($row->penetapan_panitera_pengganti);
	$tanggal_putusan   =$this->templatehelper->convertKeTglIndo($row->tanggal_putusan);
	$hari_putusan   =$this->templatehelper->getHari($row->tanggal_putusan);
	$amar_putusan   =$this->templatehelper->cleanHtmlTag($row->amar_putusan);
	$totBiaya =$this->templatehelper->convertKeTglIndo($row->totBiaya);
	$panggilan =$this->templatehelper->convertKeTglIndo($row->panggilan);
	$sita =$row->sita;
	$ps =$row->ps;
	$materai =$row->materai;
	$pnbp =$row->pnbp;
	$atk =$row->atk;
	$hhk =$row->hhk;
}
    $namaPihak1='';
	$cPenggugat = count($penggugat);
	$i = 1;
	foreach ($penggugat as $item) {
		$naskah ="\b ".$this->templatehelper->standradnaming($item["nama"])." , \b0 ".$item["statusP"]."\b  Penggugat ".$this->templatehelper->converToAngkaRomawi($item["urutanP"])."\b0 ;";
		$brnaskah = (($cPenggugat > 1)?($i):("")).' \tab '.$naskah.' \\par '.PHP_EOL.(($i<$cPenggugat)?("#loopingparties#"):(""));
		$Template = str_replace("#loopingparties#",''.$brnaskah.'',$Template);
        $namapihak1= $this->templatehelper->standradnaming($item["nama"]);
        $Template = str_replace("#nama_pihak1#",''.$namapihak1.'',$Template);
		$i++;	
	}

	$cTergugat = count($tergugat);
	$i = 1;
	foreach ($tergugat as $item) {
		$naskah ="\b ".$this->templatehelper->standradnaming($item["nama"])." , \b0 ".$item["statusT"]."\b  Tergugat ".$this->templatehelper->converToAngkaRomawi($item["urutanT"])."\b0 ;";
		$brnaskah = (($cTergugat > 1)?($i):("")).' \tab '.$naskah.' \\par '.PHP_EOL.(($i<$cTergugat)?("#loopingparties2#"):(""));
		$Template = str_replace("#loopingparties2#",''.$brnaskah.'',$Template);	
		$i++;
    }
    if (count($tergugat)>1){
        $Template = str_replace("#:#",''.":".'',$Template); 
    } else {
        $Template = str_replace("#:#",''." ".'',$Template); 
    }

#Hakim
$penetapan = '';
$hakim_anggotas = '';
$daftar_majelis = '';
$data_hakim = $hakim;
$majelis = true;
$i=1;$j=1;
foreach ($data_hakim as $row) {
    $tgl_penetapan_hakim = $tglPenHakim;
	if($row['jabatan_hakim_id']==1){
		$penetapan .='kami, '.$this->templatehelper->standradnaming($row['nama']).", sebagai ".$row['jabatan_hakim_nama']." ";
        $hakim_ketua = $this->templatehelper->standradnaming($row['nama']);
        $daftar_majelis .= $this->templatehelper->standradnaming($row['nama']).' \tab  Hakim Ketua;\\par ';
	}elseif($row['jabatan_hakim_id']==2){
        if ($i === count($data_hakim)){
            $penetapan .= 'dan ';
            $hakim_anggotas .= $this->templatehelper->standradnaming($row['nama']).'';
        }else{
            $penetapan .= ", ";
            if ($j==1 && $i>1){
                $hakim_anggotas .= $this->templatehelper->standradnaming($row['nama']).'\tab '.$hakim_ketua.'\\par \\par \\par ';
            }else{
                $hakim_anggotas .= $this->templatehelper->standradnaming($row['nama']).'\\par \\par \\par ';
            }
            
        }
		$penetapan .=$this->templatehelper->standradnaming($row['nama']).' ';
        $daftar_majelis .= $this->templatehelper->standradnaming($row['nama']).' \tab  Hakim Anggota;\\par ';
        $j++;
	}elseif($row['jabatan_hakim_id']==3){
		$majelis = false;
		$penetapan .=$this->templatehelper->standradnaming($row['nama']).", sebagai Hakim,";
        $daftar_majelis .= $this->templatehelper->standradnaming($row['nama']).' \tab Hakim;\\par ';
        $hakim_ketua = $this->templatehelper->standradnaming($row['nama']);
	}
    $i++;
}

$pengacarapihak3 = array();
$datapihak3 = $datapihak3;
//$datapengacarapihak1 = getDataPengacaraPenggugat($idperkara,1);

#foreach ($datapengacarapihak1 as $row){

#}
$numbering = true;
if(count($datapihak3)>1){
    for ($i=0; $i < count($datapihak3); $i++) { 
        $text = 'pihak'.($i+1);
        if($i==count($datapihak3)-1){
            $Template = str_replace('#loopingparties3#','\b#'.$text.'#\b0,#'.$text.'info#',$Template);
        }else{
            $Template = str_replace('#loopingparties3#','\b#'.$text.'#\b0,#'.$text.'info#\\par'.PHP_EOL.'#loopingparties3#',$Template);
        }
    }   
}else{
    $numbering = false;
    //$Template = str_replace('#loopingparties3#','#pihak3#,#pihak3info#',$Template);
    $Template = str_replace('#loopingparties3#','',$Template);
}

$counter = 1;
foreach ($datapihak3 as $row){
    $pihak3 = '\listid1'.$this->templatehelper->standradnaming($row['nama']);
    #$pengacara = getDataPengacara($datapengacarapihak1,$row['pihak_id']);
    if ($row['jenis_pihak_id']==1){
        $pihak3info = ' bertempat tinggal di '.$row['alamat'].'  selanjutnya disebut sebagai Penggugat Intervensi '.$this->templatehelper->converToAngkaRomawi($row['urutan']).';';
    }else{
        $pihak3info = ' berkedudukan di '.$row['alamat'].'  selanjutnya disebut sebagai Penggugat Intervensi '.$this->templatehelper->converToAngkaRomawi($row['urutan']).';';
    }
    
    $pihak3info = str_replace('  ',' ',$pihak3info);
    if($numbering==true){
        $Template = str_replace('#pihak'.$counter.'#','\b '.$counter.'. '.$pihak3.'\b0',$Template);
    }else{
        $Template = str_replace('#pihak'.$counter.'#','\b'.$pihak3.'\b0',$Template);
    }
    
    $Template = str_replace('#pihak'.$counter.'info#',''.$pihak3info.'',$Template);
    $counter++; 
}


$jMajelis = count($hakim);
if($jMajelis>1){
		$Template = str_replace('#majelis1#','Ketua dengan dihadiri oleh para Hakim Anggota',$Template);
		$Template = str_replace('#hakim/majelishakim#','Majelis Hakim',$Template);
}else{
	$Template = str_replace('#majelis1#','Ketua',$Template);
	$Template = str_replace('#hakim/majelishakim#','Hakim',$Template);
}
foreach ($pengacaraPenggugat as $val) {
 $namapengacara_p=$val['nama'];
}

foreach ($pengacaraTergugat as $val) {
 $namapengacara_t=$val['nama'];
}
if (!empty($namapengacara_p)) {
    $Template = str_replace("#pengacara_pihak1#",''.$namapengacara_p.'',$Template);
} else {
    $Template = str_replace("#pengacara_pihak1#",'................',$Template);
}

if (!empty($namapengacara_t)) {
    $Template = str_replace("#pengacara_pihak2#",''.$namapengacara_t.'',$Template);
} else {
    $Template = str_replace("#pengacara_pihak2#",'................',$Template);
}
$Template = str_replace("#tgl_pendaftaran#",''.$tanggalPendaftaran.'',$Template);
$Template = str_replace("#nama_pn#",str_replace("Pengadilan Negeri"," ",$namaPengadilan).'',$Template);
$Template = str_replace("#namaketuapn#",''.$namaketua.'',$Template);
$Template = str_replace("#perkara.nomor_perkara#",''.$nomorperkara.'',$Template);
$Template = str_replace("#mediator_nama#",''.$nama_mediator.'',$Template);
$Template = str_replace("#status_mediator#",''.$profesi.'',$Template);
$Template = str_replace("#tgl_mediasi#",''.$tgl_mediasi.'',$Template);
$Template = str_replace("#amar_putusan#",''.$amar_putusan.'',$Template);
$Template = str_replace("#tgl_penetapan_majelis#",''.$tglPenHakim.'',$Template);
$Template = str_replace("#tgl_putusan#",''.$tanggal_putusan.'',$Template);
$Template = str_replace("#hari_putusan#",''.$hari_putusan.'',$Template);
$Template = str_replace("#nama_pp#",''.$nama_pp.'',$Template);
$Template = str_replace("#nama_jurusita#",''.$jurusita_nama.'',$Template);
$Template = str_replace("#biaya_proses#",''.number_format($totBiaya).'',$Template);
$Template = str_replace("#biaya_pnbp#",''.number_format($pnbp).'',$Template);
$Template = str_replace("#biaya_panggilan#",''.number_format($panggilan).'',$Template);
$Template = str_replace("#biaya_sita#",''.number_format($sita).'',$Template);
$Template = str_replace("#biaya_pemeriksaan#",''.number_format($ps).'',$Template);
$Template = str_replace("#tgl_surat_gugatan#",''.$tgl_surat_gugatan.'',$Template);

$total_jumlah=6000+intval($totBiaya)+intval($pnbp)+intval($panggilan);

$Template = str_replace("#total_jumlah#",''.$total_jumlah.'',$Template);
$Template = str_replace("#jumlah_terbilang#",''.$total_jumlah.'',$Template);

if($majelis){
	$Template = str_replace('#majelis#',$penetapan.'masing-masing sebagai Hakim Anggota,',$Template);
    $Template = str_replace('#majelis1#','Ketua dengan dihadiri oleh para Hakim Anggota ',$Template);
    $Template = str_replace('#header_hakim_anggotas#','Hakim Anggota,\tab Hakim Ketua,',$Template);
    $Template = str_replace('#hakim_anggotas#',$hakim_anggotas,$Template);
    $Template = str_replace('#nama_pp_td#','Panitera Pengganti,\\par \\par \\par '.$nama_pp.' \\par ',$Template);
    $Template = str_replace('#daftar_majelis#',$daftar_majelis,$Template);
    $Template = str_replace('#hakim/majelishakim#','Majelis Hakim',$Template);
    $Template = str_replace('#ketua#',' Ketua',$Template);
}else{
	$Template = str_replace('#majelis#',$penetapan,$Template);
    $Template = str_replace('#majelis1#','',$Template);
    $Template = str_replace('#header_hakim_anggotas#','Panitera Pengganti,\tab Hakim Ketua,',$Template);
    $Template = str_replace('#hakim_anggotas#',$nama_pp.'\tab '.$hakim_ketua,$Template);
    $Template = str_replace('#nama_pp_td#','',$Template);
    $Template = str_replace('#hakim/majelishakim#','Hakim',$Template);
    $Template = str_replace('#ketua#','',$Template);
}

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