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

$url = base_url("resources/template/penetapan/perdata_agama/PDT_15_13_121_1.rtf");//Dilangsungin aja lah :D

$Template = file_get_contents($url);


foreach ($dataperkara ->result() as $row) {
	$nomorperkara = $row->nomor_perkara;
	$Template = str_replace("#NOMOR_PERKARA#",$row->nomor_perkara,$Template);
	$Template = str_replace("#JENIS_PERKARA#",$dataperkara->row(0)->nama,$Template);
	$Template = str_replace("#TGL_DAFTAR#",$this->templatehelper->convertKeTglIndo($dataperkara->row(0)->tanggal_pendaftaran),$Template);
}

foreach ($datapenetapanhakim ->result() as $row) {
	$Template = str_replace("#TGL_PMH#",$this->templatehelper->convertKeTglIndo($datapenetapanhakim->row(0)->tanggal_penetapan),$Template);
	$Template = str_replace("#KETUA_MAJELIS#",$datapenetapanhakim->row(0)->nama_gelar,$Template);
}

foreach ($datamediasi ->result() as $row) {
	$Template = str_replace("#TGL_PENUNJUKKAN_MEDIATOR#",$this->templatehelper->convertKeTglIndo($row->penetapan_penunjukan_mediator),$Template);
	$Template = str_replace("#NAMA_MEDIATOR#",$this->templatehelper->convertKeTglIndo($row->nama_mediator),$Template);
}

	$Template = str_replace("#NAMA_SATKER#",ucwords(strtolower($datapengadilan->row(0)->NamaPN)),$Template);
	$Template = str_replace("#KOTA_SATKER#",str_replace("Pengadilan Agama ","",ucwords(strtolower($datapengadilan->row(0)->NamaPN))),$Template);
	$Template = str_replace("#NAMA_PANITERA#",ucwords(strtolower($datapengadilan->row(0)->PansekPNNama)),$Template);

/*
$namakota = $datapengadilan->row(0)->NamaPN;
if ($pembuat==1) {
	$Template = str_replace('#JABATAN#','Panitera',$Template);
	$Template = str_replace('#atasnama#','',$Template);
	$Template = str_replace("#NAMA#",$datapengadilan->row(0)->PansekPNNama,$Template);
} else {
	$Template = str_replace('#JABATAN#','Panitera',$Template);
	$Template = str_replace('#atasnama#','a.n ',$Template);
	$Template = str_replace("#NAMA#",$datapengadilan->row(0)->WaPanNama,$Template);
}
foreach ($datapaniterapengganti ->result() as $row) {
		//$tanggalpenetapanjurusita = $this->templatehelper->convertKeTglIndo($row->tanggal_surat);
	$Template = str_replace("#tanggalpenetapanjurusita#",$this->templatehelper->convertKeTglIndo($row->tanggal_penetapan),$Template);
}
foreach ($datapenetapanhakim ->result() as $row) {
	//$tanggalpenetapanhakim = $this->templatehelper->convertKeTglIndo($row->tanggal_penetapan);
	$Template = str_replace("#tanggalpenetapanhakim#",$this->templatehelper->convertKeTglIndo($row->tanggal_penetapan),$Template);
}
$nomorperkara = $dataperkara->row(0)->nomor_perkara;
$Template = str_replace("#nama_pp#",$datapaniterapengganti->row(0)->nama_gelar,$Template);
$Template = str_replace("#nomorperkara#",$dataperkara->row(0)->nomor_perkara,$Template);
//$Template = str_replace("#namakota#",ucwords(strtolower($datapengadilan->row(0)->NamaPN)),$Template);
//$Template = str_replace("#tanggalpenetapanjurusita#",''.$tanggalpenetapanjurusita.'',$Template);
foreach ($datapengadilan->result() as $row) {
	$namapn = $this->templatehelper->standradnaming($row->NamaPN);
	$namaketuapn = $this->templatehelper->standradnaming($row->KetuaPNNama);
	$namawakilpn = $this->templatehelper->standradnaming($row->WakilKetuaPNNama);
	$namakota =str_replace("Pengadilan Agama ",'',$namapn);
}
$Template = str_replace('#namakota#',''.$namakota.'',$Template);
$Template = str_replace('#namapa#',''.$namapn.'',$Template);
*/

//Pihaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaak
$counter = 1;
$pihak1 = '';
$pihak1_id='';
$arr_pihak1 = array();
$numbering = true;
if($dataPihak1->num_rows >1){
    for ($i=0; $i < $dataPihak1->num_rows; $i++) { 
        $text = 'penggugat'.($i+1);
        if($i==$dataPihak1->num_rows-1){
            $Template = str_replace('#loopingpenggugat#','\b#'.$text.'#\b0',$Template);
        }else{
            $Template = str_replace('#loopingpenggugat#','\b#'.$text.'#\b0\\par'.PHP_EOL.'#loopingpenggugat#',$Template);
        }
    }   
}else{
    $numbering = false;
    $Template = str_replace('#loopingpenggugat#','#penggugat#',$Template);
}

if (!empty($dataPihak1->num_rows)){   
    foreach ($dataPihak1->result() as $row){
        $pihak1_id=$row->pihak_id;
        $arr_pihak1[]=$row->id;
        if ($dataPihak1->num_rows==1) {
            $tab='';
            $penggugat = '';
        }else{
            $tab='\tab';
            $penggugat = '\b0\\par ';
        }
        
        $pihak1 = $penggugat;
        $pihak1 .= ''.' 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_pihak1# #penggugatke#\b0';

        if($numbering==true){
            $Template = str_replace('##jenis_pihak1#'.$counter.'#',$pihak1,$Template);
            $Template = str_replace('#penggugat#',$counter,$Template);
            $Template = str_replace('#penggugat/parapenggugat#','Para \b#jenis_pihak1#\b0',$Template);
        }else{
           $Template = str_replace('#penggugat#',$pihak1,$Template);
           $Template = str_replace('#penggugatke#','',$Template);
           $Template = str_replace('#penggugat/parapenggugat#','Untuk selanjutnya disebut sebagai \b#jenis_pihak1#\b0',$Template);
        }
        $counter++;    
    }
}


$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);
		$Template = str_replace("#SEBAGAI_P#","Pemohon",$Template);
		$Template = str_replace("#SEBAGAI_T#","Termohon",$Template);
    }else{
        $Template = str_replace("#jenis_pihak1#",''." Penggugat ".'',$Template);
        $Template = str_replace("#jenis_pihak2#",''." Tergugat ".'',$Template);
		$Template = str_replace("#SEBAGAI_P#","Penggugat",$Template);
		$Template = str_replace("#SEBAGAI_T#","Tergugat",$Template);
    }
}else{
    $Template = str_replace("#GUGATAN_PERMOHONAN# ",''." Permohonan ".'',$Template);   
    $Template = str_replace("#jenis_pihak1#",''." Pemohon ".'',$Template);
    $Template = str_replace("#jenis_pihak2#",''." Termohon ".'',$Template);
}

$Template = str_replace("#NAMA_P#",$dataPihak1->row(0)->nama,$Template);
$Template = str_replace("#NAMA_T#",$dataPihak2->row(0)->nama,$Template);
//Pihaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaak

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=\"Penunjukkan Mediator - $nomorperkara.rtf\"");
echo $Template;
?>