<?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======================================================
$datatenggugat = $dataTemplate;
  

	foreach ($datatenggugat->result() as $row) {
             $namaPengadilan     =$this->templatehelper->standradnaming($row->namaPengadilan);
             	
             $nomorperkara   =$row->noPerkara;
             $tanggaldaftar   =$row->tgldaftar;
	         $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;
             $jenis_perkara   =$row->jenis_perkara;
             $umur_t   =$row->umur;
             $agama_t   =$row->agama;
             $pendidikan_t   =$row->pendidikan;
             $nama_p   =$row->namaPihakP;

 }

$data_p = $datapenggugat;


    foreach ($data_p->result() as $row) {
             $nama_P  =$row->nama;
             $pekerjaan_P = $row->pekerjaan;
             $alamat_P =$row->alamat;
             $umur_p   =$row->umur;
             $agama_p   =$row->agama;
             $pendidikan_p   =$row->pendidikan;
             

 }

 $data_t = $datatergugat;


    foreach ($data_t->result() as $row) {
             $nama_t  =$row->nama;
             $pekerjaan_t = $row->pekerjaan;
             $alamat_t =$row->alamat;
             $umur_t   =$row->umur;
             $agama_t   =$row->agama;
             $pendidikan_t   =$row->pendidikan;
             

 }


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

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

$Template = str_replace("Pengadilan Agama ",'',str_replace("#KOTA_PA#",ucwords(strtolower($namaPengadilan)),$Template));
$Template = str_replace("Mahkamah Syar'iyah ",'',str_replace("#KOTA_PA#",ucwords(strtolower($namaPengadilan)),$Template));

$Template = str_replace("#nama_pa#",''.ucwords(strtolower($namaPengadilan)).'',$Template);
$Template = str_replace("#nomor_perkara#",''.$nomorperkara.'',$Template);
//$Template = str_replace("#tgl_daftar#",''.$tanggaldaftar.'',$Template);
$Template = str_replace("#tgl_daftar#",$this->templatehelper->convertKeTglIndo($tanggaldaftar),$Template);


$Template = str_replace("#TGL_SEKARANG#",$this->templatehelper->convertKeTglIndo(date('Y-m-d')),$Template);
$Template = str_replace("#HARI_INI#",$this->templatehelper->gethari(date('Y-m-d')),$Template);

$Template = str_replace("#nama_jurusita#",''.$jurusita.'',$Template);
$Template = str_replace("#tgl_putus#",''.$tglPutusan.'',$Template);
$Template = str_replace("#namaPihakP#",''.$namaPihakP.'',$Template);
$Template = str_replace("#nama_t#",''.$namaPihakT.'',$Template);
$Template = str_replace("#amar#",''.$amarPutusan.'',$Template);
$Template = str_replace("#jursit_Label#",''.$jursitLabelx.'',$Template);
$Template = str_replace("#jenis_perkara#",''.$jenis_perkara.'',$Template);

$Template = str_replace("#nama_t#",''.$nama_t.'',$Template);
$Template = str_replace("#umur_t#",''.$umur_t.'',$Template);
$Template = str_replace("#agama_t#",''.$agama_t.'',$Template);
$Template = str_replace("#pendidikan_t#",''.$pendidikan_t.'',$Template);
$Template = str_replace("#pekerjaan_t#",''.$pekerjaan_t.'',$Template);
$Template = str_replace("#alamat_t#",''.$alamat_t.'',$Template);


$Template = str_replace("#nama_p#",''.$nama_p.'',$Template);
$Template = str_replace("#umur_p#",''.$umur_p.'',$Template);
$Template = str_replace("#agama_p#",''.$agama_p.'',$Template);
$Template = str_replace("#pendidikan_p#",''.$pendidikan_p.'',$Template);
$Template = str_replace("#pekerjaan_p#",''.$pekerjaan_P.'',$Template);
$Template = str_replace("#alamat_p#",''.$alamat_P.'',$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_pihak1#",''." 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;
?>