<?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/putusan/perdata_umum/".$filename.".rtf");
$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;
}

$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->namapengacarap;
    	$pengacaraTergugat[] = $pengacaraPitem;
    }

	$namaPengadilan =$this->templatehelper->standradnaming($row->namapengadilan);
	$kota     =$this->templatehelper->namaKota($row->namapengadilan);
    $alamatPN =$row->alamatPN;
    $namaketua     =$this->templatehelper->namaKota($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);
	$tanggalPendaftaran   =$this->templatehelper->convertKeTglIndo($row->tanggal_pendaftaran);
	$tanggalSurat   =$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);
    $panggilan =$row->panggilan;
	$totBiaya =$row->totbiaya;
    $sita =$row->sita;
	$ps =$row->ps;
	$pnbp =$row->pnbp;
	$atk =$row->atk;
	$hhk =$row->hhk;
    $redaksi = $row->redaksi;
    $materai = $row->materai;
    $sum = $row->totbiaya;
	}

    $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)?(' \b '.$i.'\b0 .'):("")).' '.$naskah.' \\par '.PHP_EOL.(($i<$cPenggugat)?("#loopingparties#"):(""));
		$Template = str_replace("#loopingparties#",''.$brnaskah.'',$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)?(' \b ' .$i.'\b0 .'):("")).' '.$naskah.' \\par '.PHP_EOL.(($i<$cTergugat)?("#loopingparties2#"):(""));
		$Template = str_replace("#loopingparties2#",''.$brnaskah.'',$Template);	
		$i++;
}

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

$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#','',$Template);
}

$counter = 1;
foreach ($datapihak3 as $row){
    $pihak3 = '\listid1'.$this->templatehelper->standradnaming($row['nama']);
    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++; 
}

/*$counter=1;
foreach ($data_turutTergugat->result() as $row) {
    $pihak4 = '\listid1'.$this->templatehelper->standradnaming($row->namat);
    $pihak4 .= ' '.$row->statustt;
    $Template = str_replace('#pihak4#',''.$pihak4.'',$Template);
    $counter++; 
}*/

$cTurutTergugat = count($data_turutTergugat);
$ii = count($tergugat)+1;
foreach ($data_turutTergugat->result() as $row) {
    $naskah =" \b ".$this->templatehelper->standradnaming($row->namat).", \b0 ".$row->statustt."\b  Turut Tergugat ".$this->templatehelper->converToAngkaRomawi($row->urutant)."\b0";
    $brnaskah = (($cTurutTergugat > 1)?(' \b ' .$ii.' \b0 .'):("")).' '.$naskah.' \\par '.PHP_EOL.(($i<$cTurutTergugat)?("#pihak4#"):(""));
    $Template = str_replace("#pihak4#",''.$brnaskah.'',$Template); 
    $ii++;
}

$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);
}
if(!empty($tanggalPendaftaran)){
    $tglDaftar=$tanggalPendaftaran;
}else{
    $tglDaftar='.................';
}

if(!empty($nama_mediator)){
    $namaMediator = $nama_mediator;
}else{
    $namaMediator = '................';
}

if(!empty($profesi)){
    $statusMediator = $profesi;
}else{
    $statusMediator = '................';
}

if(!empty($tgl_mediasi)){
    $tglMediasi = $tgl_mediasi;
}else{
    $tglMediasi = '................';
}


$Template = str_replace("#:#",''.':'.'',$Template);
$Template = str_replace("#tgl_pendaftaran#",''.$tglDaftar.'',$Template);
$Template = str_replace("#tgl_surat#",''.$tanggalSurat.'',$Template);
$Template = str_replace("#nama_pn#",''.$namaPengadilan.'',$Template);
$Template = str_replace("#alamat_pn#",''.$alamatPN.'',$Template);
$Template = str_replace("#namaketuapn#",''.$namaketua.'',$Template);
$Template = str_replace("#perkara.nomor_perkara#",''.$nomorperkara.'',$Template);
$Template = str_replace("#mediator_nama#",''.$namaMediator.'',$Template);
$Template = str_replace("#status_mediator#",''.$statusMediator.'',$Template);
$Template = str_replace("#tgl_mediasi#",''.$tglMediasi.'',$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("#tgl_sidang#",''.$tanggal_putusan.'',$Template);
$Template = str_replace("#hari_sidang#",''.$hari_putusan.'',$Template);
$Template = str_replace("#nama_pp#",''.$nama_pp.'',$Template);
$Template = str_replace("#biaya_proses#",''.number_format($atk, 2, ',', '.').'',$Template);
$Template = str_replace("#materai#",''.number_format($materai, 2, ',', '.').'',$Template);
$Template = str_replace("#redaksi#",''.number_format($redaksi, 2, ',', '.').'',$Template);
$Template = str_replace("#biaya_atk#",''.number_format($atk, 2, ',', '.').'',$Template);
$Template = str_replace("#biaya_pnbp#",''.number_format($pnbp, 2, ',', '.').'',$Template);
$Template = str_replace("#biaya_panggilan#",''.number_format($panggilan, 2, ',', '.').'',$Template);
$Template = str_replace("#biaya_sita#",''.number_format($sita, 2, ',', '.').'',$Template);
$Template = str_replace("#biaya_pemeriksaan#",''.number_format($ps, 2, ',', '.').'',$Template);
$Template = str_replace("#total_jumlah#",''.number_format($sum, 2, ',', '.').'',$Template);
$Template = str_replace("#jumlah_terbilang#",''.$this->templatehelper->Terbilang(intval($sum)).'',$Template);
$Template = str_replace("#tot_jumlah#",''.number_format($sum, 2, ',', '.').'',$Template);
$Template = str_replace("#totterbilang#",''.$this->templatehelper->Terbilang(intval($sum)).'',$Template);
$Template = str_replace("#jawaban_tergugat#",'...................',$Template);
$Template = str_replace("#temu_kuasa#",'...................',$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);
    $Template = str_replace("#hakim_ketua#",''.$hakim_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);
    $Template = str_replace("#hakim_ketua#",''.$hakim_ketua.'',$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\"");
header("Content-length: ".strlen($Template));
echo $Template;
?>