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

$jenisTemplate = intval($jenisTemplate);
if(!is_numeric($jenisTemplate)){
    show_404();
    exit();
}

$jursitLabel = intval($jursit_id_label);
if(!is_numeric($jursitLabel)){
    show_404();
    exit();
}

$url = "";
$judul = "";

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


$url = base_url("resources/template/relaas/perdata_agama/".$filename.".rtf");

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


	foreach ($dataPenggugat->result() as $row) {
             $namaPengadilan     =$this->templatehelper->standradnaming($row->namaPengadilan);
             	
             $nomorperkara   =$row->noPerkara;
	         $jurusita = $this->templatehelper->standradnaming($row->jurusita);
             $namaPihak  =$row->namaPihak;
             $pekerjaanPihak = $row->pekerjaanPihak;
             $alamatPihak =$row->alamatPihak;
             $tglPutusan     =$this->templatehelper->convertKeTglIndo($row->tglPutusan);
             $amarPutusan    =$this->templatehelper->cleanHtmlTag($row->amarPutusan);
             $namaPihakP   =$row->namaPihakP;
             $namaPihakT   =$row->namaPihakT;

 }
if(empty($nomorperkara)){
	$nomorperkara = '........';
}
if(empty($jurusita)){
	$jurusita = '........';
}

if ($jursitLabel==1) {
	$jursitLabelx = 'Jurusita';
	} else 
	$jursitLabelx = 'Jurusita Penggati';



$Template = str_replace("#namapa#",''.$namaPengadilan.'',$Template);
$Template = str_replace("#nomorperkara#",''.$nomorperkara.'',$Template);
$Template = str_replace("#namajurusita#",''.$jurusita.'',$Template);
$Template = str_replace("#namaPihak#",''.$namaPihak.'',$Template);
$Template = str_replace("#pekerjaanPihak#",''.$pekerjaanPihak.'',$Template);
$Template = str_replace("#alamatPihak#",''.$alamatPihak.'',$Template);
$Template = str_replace("#tglputus#",''.$tglPutusan.'',$Template);
$Template = str_replace("#namaPihakP#",''.$namaPihakP.'',$Template);
$Template = str_replace("#nama_t#",''.$namaPihakT.'',$Template);
$Template = str_replace("#amar#",''.$amarPutusan.'',$Template);
$Template = str_replace("#jursitLabel#",''.$jursitLabelx.'',$Template);




$counter = 1;
$pihak2 = '';
$pihak2_id='';
$arr_pihak2 = array();
$numbering = true;
if($dataPihak2->num_rows >1){
    for ($i=0; $i < $dataPihak2->num_rows; $i++) { 
        $text = 'tergugat'.($i+1);
        if($i==$dataPihak2->num_rows-1){
            $Template = str_replace('#loopingtergugat#','\b#'.$text.'#\b0',$Template);
        }else{
            $Template = str_replace('#loopingtergugat#','\b#'.$text.'#\b0\\par'.PHP_EOL.'#loopingtergugat#',$Template);
        }
    }   
}else{
    $numbering = false;
    $Template = str_replace('#loopingtergugat#','#tergugat#',$Template);
}

if (!empty($dataPihak2->num_rows)){   
    foreach ($dataPihak2->result() as $row){
        $pihak2_id=$row->pihak_id;
        $arr_pihak2[]=$row->id;
        if ($dataPihak2->num_rows==1) {
            $tab='';
            $tergugat = '';
        }else{
            $tab='\tab';
            $tergugat = '\b0\\par ';
        }
        
        $pihak2 = $tergugat;
        $pihak2 .= ''.' Nama '.$this->templatehelper->standradnaming($row->nama).', Umur' . ' tahun,' . ' agama '.
        $row->agama.
        ', pekerjaan '.$row->pekerjaan.', pendidikan  '.$row->pendidikan.', tempat kediaman di '.$row->alamat.', sebagai \b#jenis_pihak2# #tergugatke#\b0';

        if($numbering==true){
            $Template = str_replace('#tergugat'.$counter.'#',$pihak2,$Template);
            $Template = str_replace('#tergugatke#',$counter,$Template);
            $Template = str_replace('#tergugat/paratergugat#','Para \b#jenis_pihak2#\b0',$Template);
        }else{
           $Template = str_replace('#tergugat#',$pihak2,$Template);
           $Template = str_replace('#tergugatke#','',$Template);
           $Template = str_replace('#tergugat/paratergugat#','Untuk selanjutnya disebut sebagai \b#jenis_pihak2#\b0',$Template);
        }
        $counter++;    
    }
}

$idperkara=$this->encrypt->decode(base64_decode($enc));
$idalurperkara = $this->tanggalhelper->getIDAlurPerkara($idperkara);
$jenisPerkaraID=$this->tanggalhelper->getIDJenisPerkara($idperkara);

if ($idalurperkara==15) {
    $Template = str_replace("#GUGATAN_PERMOHONAN#",''." Gugatan ".'',$Template);
    if ($jenisPerkaraID==346) {
        $Template = str_replace("#jenis_pihak1#",''." Pemohon ".'',$Template);
        $Template = str_replace("#jenis_pihak2#",''." Termohon ".'',$Template);
    }else{
        $Template = str_replace("#jenis_pihak1#",''." Penggugat ".'',$Template);
        $Template = str_replace("#jenis_pihak2#",''." Tergugat ".'',$Template);
    }
}else{
    $Template = str_replace("#GUGATAN_PERMOHONAN# ",''." Permohonan ".'',$Template);   
    $Template = str_replace("#jenis_pihak#",''." Pemohon ".'',$Template);
    $Template = str_replace("#jenis_pihak2#",''." Termohon ".'',$Template);
}



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