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


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

$Template = file_get_contents($url);

$tgl_hari_ini=date("d/m/Y");
$Template = str_replace("#TGL_HARI_INI#",$this->templatehelper->convertKeTglIndo($datapenetapansidang->row(0)->penetapan_hari_sidang),$Template);
$Template = str_replace("#HARI_INI#",$this->templatehelper->gethari($tgl_hari_ini),$Template);

$Template = str_replace("#NOMOR_PERKARA#",$dataperkara->row(0)->nomor_perkara,$Template);
$Template = str_replace("#JENIS_PERKARA#",$dataperkara->row(0)->nama,$Template);
$Template = str_replace("#TGL_SEKARANG#",$this->templatehelper->convertKeTglIndo(date('Y-m-d')),$Template);
$Template = str_replace("#TGL_DAFTAR#",$this->templatehelper->convertKeTglIndo($dataperkara->row(0)->tanggal_pendaftaran),$Template);
$Template = str_replace("#TGL_PMH#",$this->templatehelper->convertKeTglIndo($datapenetapanhakim->row(0)->tanggal_penetapan),$Template);
$Template = str_replace("#TGL_PHS#",$this->templatehelper->convertKeTglIndo($datapenetapansidang->row(0)->penetapan_hari_sidang),$Template);

$Template = str_replace("#JABATAN#",$jabatan,$Template);
$Template = str_replace("#NAMA_JURUSITA#",$nama_jurusita,$Template);
$Template = str_replace("#NAMA_SATKER#",ucwords(strtolower($datapengadilan->row(0)->NamaPN)),$Template);
$Template = str_replace("#NAMA_PANITERA#",ucwords(strtolower($datapengadilan->row(0)->PansekPNNama)),$Template);
$Template = str_replace("#ALAMAT_SATKER#",$datapengadilan->row(0)->AlamatPN,$Template);
$Template = str_replace("#KETUA_PA#",$datapengadilan->row(0)->KetuaPNNama,$Template);
$Template = str_replace("#RUANG_SIDANG#",$listSidang->row(0)->ruangan,$Template);
$Template = str_replace("#TGL_SIDANG#", $this->templatehelper->convertKeTglIndo($listSidang->row(0)->tanggal_sidang) ,$Template);
$Template = str_replace("#HARI_SIDANG#", $this->templatehelper->gethari($listSidang->row(0)->tanggal_sidang),$Template);
$Template = str_replace("#JAM_SIDANG#",substr($listSidang->row(0)->jam_sidang,0,5)." ".$datapengadilan->row(0)->ZonaWaktu,$Template);

$Template = str_replace("#KOTA_SATKER#", $this->templatehelper->namaKota(ucwords(strtolower($datapengadilan->row(0)->NamaPN))) ,$Template);

if (!empty($dataputusan->num_rows)){   
    $Template = str_replace("#TGL_PUTUS#",$this->templatehelper->convertKeTglIndo($dataputusan->row(0)->tanggal_putusan),$Template);
}

$i=0;
$temp_pihak='';
$temp_kuasa='';
$pihak_array='';
foreach ($dataPihak1->result() as $row) {
    if($i>0){
        $pihak_array = str_replace("#ke#",' '.$this->templatehelper->converToAngkaRomawi($i), $pihak_array);
    }
    if($id_pihak==$row->id_pihak){
        if($temp_pihak!=$row->id_pihak){
            $temp_pihak=$row->id_pihak;
                       
            if($row->id_kuasa!=''){
                if($i>0){$pihak_array= str_replace("#DKK#",'', $pihak_array);}
                $pihak_array.= $row->nama_kuasa.'#DKK#, alamat '.$row->alamat_kuasa.' kuasa dari ';
            }
            $pihak_array.=$row->nama.', umur '.$row->umur.' tahun, agama '.$row->agama.
                    ', pekerjaan '.$row->pekerjaan.', Pendidikan '.$row->pendidikan.
                    ', alamat '.$row->alamat.' sebagai #SEBAGAI_P##ke#;#par#';
            $array_pihak=$i;
        }
    }
    $i++;
        
}
if($i>1){
    $pihak_array = str_replace("#ke#",' '.$this->templatehelper->converToAngkaRomawi($i), $pihak_array);
    $pihak_array = str_replace("#par#",' \\par ', $pihak_array);
}else{
    $pihak_array = str_replace("#ke#",'', $pihak_array);
    $pihak_array = str_replace("#par#",'', $pihak_array);
}
$pihak_array = str_replace("#DKK#",'', $pihak_array);
$Template = str_replace("#loopingpenggugat#",$pihak_array,$Template);

$i=0;
$temp_pihak='';
$temp_kuasa='';
$pihak_array='';
foreach ($dataPihak2->result() as $row) {
    if($i>0){
        $pihak_array = str_replace("#ke#",' '.$this->templatehelper->converToAngkaRomawi($i), $pihak_array);
    } 
    if($id_pihak==$row->id_pihak){
        if($temp_pihak!=$row->id_pihak){

            $temp_pihak=$row->id_pihak;
                        
            if($row->id_kuasa!=''){
                if($i>0){$pihak_array= str_replace("#DKK#",'', $pihak_array);}
                $pihak_array.= $row->nama_kuasa.'#DKK#, alamat '.$row->alamat_kuasa.' kuasa dari ';
            }
            $pihak_array.=$row->nama.', umur '.$row->umur.' tahun, agama '.$row->agama.
                    ', pekerjaan '.$row->pekerjaan.', Pendidikan '.$row->pendidikan.
                    ', alamat '.$row->alamat.' sebagai #SEBAGAI_T##ke#;#par#';
            $array_pihak=$i;
            
        }
    }
    $i++;
}
if($i>1){
    $pihak_array = str_replace("#ke#",' '.$this->templatehelper->converToAngkaRomawi($i), $pihak_array);
    $pihak_array = str_replace("#par#",' \\par ', $pihak_array);
}else{
    $pihak_array = str_replace("#ke#",'', $pihak_array);
    $pihak_array = str_replace("#par#",'', $pihak_array);
}
$pihak_array = str_replace("#DKK#",'', $pihak_array);
$Template = str_replace("#loopingtergugat#",$pihak_array,$Template);

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


if ($idalurperkara==15) {
    if ($jenisPerkaraID==346) {
        $Template = str_replace("#GUGATAN_PERMOHONAN#",''."Gugatan".'',$Template);
        $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("#GUGATAN_PERMOHONAN#",''."Gugatan".'',$Template);
        $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("#SEBAGAI_P#","Pemohon",$Template);
}
    
//if ($jenisPerkaraID==360) {//Khusus Isbat Nikah //Dika Yang Edit ID=360
    $Template = str_replace("#NAMA_P1#",$dataPihak1->row(0)->nama,$Template);
    $Template = str_replace("#NAMA_P2#",$dataPihak1->row(1)->nama,$Template);
//}
if($dataPihak1->row())
    {
        $nama_p = $dataPihak1->row($array_pihak)->nama;
        if(empty($dataPihak1->row($array_pihak)->id_kuasa)){
            $Template = str_replace("#NAMA_P_TT#",$nama_p,$Template);    
        }else{
            $Template = str_replace("#NAMA_P_TT#",$dataPihak1->row($array_pihak)->nama_kuasa,$Template);    
        }
        if($dataPihak1->num_rows>1){
            $nama_p .= ' Dkk';
        }
        $Template = str_replace("#NAMA_P#",$nama_p,$Template);
    }
if($dataPihak2->row())
    {
        $nama_t = $dataPihak2->row($array_pihak)->nama;
         if(empty($dataPihak2->row($array_pihak)->id_kuasa)){
            $Template = str_replace("#NAMA_T_TT#",$nama_t,$Template);    
        }else{
            $Template = str_replace("#NAMA_T_TT#",$dataPihak2->row($array_pihak)->nama_kuasa,$Template);    
        }
        if($dataPihak2->num_rows>1){
            $nama_t .= ' Dkk';
        }
    $Template = str_replace("#NAMA_T#",$nama_t,$Template);
    }
//Pihaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaak

//Function ROMAWIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
function Romawi($n){
    $hasil = "";
    $iromawi = array("","I","II","III","IV","V","VI","VII","VIII","IX","X",20=>"XX",30=>"XXX",40=>"XL",50=>"L",
    60=>"LX",70=>"LXX",80=>"LXXX",90=>"XC",100=>"C",200=>"CC",300=>"CCC",400=>"CD",500=>"D",600=>"DC",700=>"DCC",
    800=>"DCCC",900=>"CM",1000=>"M",2000=>"MM",3000=>"MMM");
    if(array_key_exists($n,$iromawi)){
    $hasil = $iromawi[$n];
    }elseif($n >= 11 && $n <= 99){
    $i = $n % 10;
    $hasil = $iromawi[$n-$i] . Romawi($n % 10);
    }elseif($n >= 101 && $n <= 999){
    $i = $n % 100;
    $hasil = $iromawi[$n-$i] . Romawi($n % 100);
    }else{
    $i = $n % 1000;
    $hasil = $iromawi[$n-$i] . Romawi($n % 1000);
    }
    return $hasil;
}
$Template = str_replace("#TGL_CETAK#",$this->templatehelper->convertKeTglIndo(date('Y-m-d')),$Template);
$Template = str_replace("#HARI_CETAK#",$this->templatehelper->gethari(date('Y-m-d')),$Template);
//Function ROMAWIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
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=\"$blangko_relaas-".$dataperkara->row(0)->nomor_perkara.".rtf\"");
echo $Template;
?>