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

$jenisAkta=intval($jenisAkta);
if(!is_numeric($jenisAkta)){
    show_404();
    exit();
}
$penandatangan=$this->encrypt->decode(base64_decode($penandatangan));
$penandatangan=intval($penandatangan);
if(!is_numeric($penandatangan)){
    show_404();
    exit();
}

foreach ($template->result() as $template){
    $filename=$template->kdtemplate;
    $namafile=str_replace(" ","",$template->namatemplate);
}
$url = base_url("resources/template/akta/perdata_tun/".$filename.".rtf");
$Template = file_get_contents($url);
//--------------MULAI--------------------------------------------//
if(!empty($dataPN)){
    foreach ($dataPN->result() as $row) {
        $namapn = $this->templatehelper->standradnaming($row->NamaPN);
        $alamatpn = $this->templatehelper->standradnaming($row->AlamatPN);
        $namapt = $this->templatehelper->standradnaming($row->NamaPT);
        $pansek = $this->templatehelper->standradnaming($row->Panses);
        $wapan = $this->templatehelper->standradnaming($row->Wapan);
    }
}
$Template = str_replace('#namapn#',''.$namapn.'',$Template);
$Template = str_replace('#alamatpn#',''.$alamatpn.'',$Template);
$Template = str_replace('#namapt#',''.$namapt.'',$Template);
if($penandatangan==68){    
        $Template = str_replace('#pansek#',''.$pansek.'',$Template);
        $Template = str_replace('#jabatan#','Panitera',$Template);
}else if($penandatangan==70){
        $Template = str_replace('#pansek#',''.$wapan.'',$Template);
        $Template = str_replace('#jabatan#','a.n. Panitera',$Template);
}
#DATA PERKARA
if(!empty($dataPerkara)){
    foreach ($dataPerkara->result() as $row) {
        $nomorperkara = $row->nomor_perkara;        
    }
}
$Template = str_replace('#nomorperkara#',''.$nomorperkara.'',$Template);
#DATA PERKARA PUTUSAN
if(!empty($tanggalPutus)){
    $tglpts = $tanggalPutus->result();
    $tglputusan = $this->templatehelper->convertKeTglIndo($tglpts[0]->tanggal_putusan);     
    $amarpts = $this->templatehelper->cleanHtmlTag($tglpts[0]->amar_putusan);
}
$Template = str_replace('#tglputusan#',''.$tglputusan.'',$Template);
$Template = str_replace('#amarpts#',''.$amarpts.'',$Template);
if($jenisAkta==1 OR $jenisAkta==2 OR $jenisAkta==3 OR $jenisAkta==4 OR $jenisAkta==5 OR $jenisAkta==6){
    $Template = str_replace('#haripermohonanbanding#',''.$this->templatehelper->getHari($tglpermohonan).'',$Template);
    $Template = str_replace('#tglpermohonanbanding#',''.$this->templatehelper->convertKeTglIndo($tglpermohonan).'',$Template);    
}else{
    $Template = str_replace('#tglsurat#',''.$this->templatehelper->convertKeTglIndo($tglsurat).'',$Template);    
    $Template = str_replace('#nomorsurat#',''.$nomorsurat.'',$Template);
    $Template = str_replace('#haricabut#',''.$this->templatehelper->getHari($tglcabut).'',$Template);
    $Template = str_replace('#tglcabut#',''.$this->templatehelper->convertKeTglIndo($tglcabut).'',$Template);
}
#DATAPEMBANDING
if($jenisAkta==1 OR $jenisAkta==3 OR $jenisAkta==5 OR $jenisAkta==7 OR $jenisAkta==9 OR $jenisAkta==11){
    if(!empty($infopembanding)){
        $infoPembanding = $infopembanding->result();        
        $namaPembanding = $this->templatehelper->standradnaming($infoPembanding[0]->pemohon_nama);    
        $tempattinggal = $infoPembanding[0]->alamat;
        $pekerjaan = $infoPembanding[0]->pekerjaan;
        $pihakasal = $infoPembanding[0]->pihak_asal_text;
    }    
    $Template = str_replace('#namaPembanding#',''.$namaPembanding.'',$Template);
    $Template = str_replace('#kewarganegaraan#','Indonesia',$Template);
    $Template = str_replace('#tempattinggal#',''.$tempattinggal.'',$Template);
    $Template = str_replace('#pekerjaan#',''.$pekerjaan.'',$Template);
    $Template = str_replace('#pihakasal#',''.$pihakasal.'',$Template);
}

#DATAKUASAPENGGUGAT
if($jenisAkta==2 OR $jenisAkta==4 OR $jenisAkta==6 OR $jenisAkta==8 OR $jenisAkta==10 OR $jenisAkta==12){
    if(!empty($infoKuasaPihak)){
        $infokuasaP = $infoKuasaPihak->result();            
        $namakuasa = $this->templatehelper->standradnaming($infokuasaP[0]->nama);    
        $alamatkuasa = $infokuasaP[0]->alamat;
        $pekerjaankuasa = $infokuasaP[0]->pekerjaan;
        $tglkuasa = $this->templatehelper->convertKeTglIndo($infokuasaP[0]->tglKuasa);
        $namapihakdiwakili = $this->templatehelper->standradnaming($infokuasaP[0]->pihakdiwakili);
    }    
    $Template = str_replace('#namakuasa#',''.$namakuasa.'',$Template);
    $Template = str_replace('#alamatkuasa#',''.$alamatkuasa.'',$Template);
    $Template = str_replace('#pekerjaankuasa#',''.$pekerjaankuasa.'',$Template);
    $Template = str_replace('#kewarganegaraan#','Indonesia',$Template);
    $Template = str_replace('#tglkuasa#',''.$tglkuasa.'',$Template);
    $Template = str_replace('#namapihakdiwakili#',''.$namapihakdiwakili.'',$Template);
}

#looping pembanding
$dataPihakPembanding = "";
$i = 0;
if(!empty($infoallpembanding)){
        foreach ($infoallpembanding->result() as $rows) { 
            if(($rows->jenis_pihak_id)==1){                       
                $dataPihakPembanding .= " \b ".$this->templatehelper->standradnaming($rows->nama).", \b0 Warga Negara Indonesia, ";
                $dataPihakPembanding .= "Tempat Tinggal di ".$rows->alamat.", Pekerjaan ".$rows->pekerjaan.", ";                                       
                $dataPihakPembanding .= "untuk selanjutnya disebut sebagai \b ".$rows->pihak_asal_text."/Pembanding \b0 ; \\par ";                                                    
            }elseif(($rows->jenis_pihak_id)==2 OR ($rows->jenis_pihak_id)==3 OR ($rows->jenis_pihak_id)==4 ){          
                $dataPihakPembanding .= " \b ".$this->templatehelper->standradnaming($rows->nama).",";
                $dataPihakPembanding .= " Tempat Kedudukan di ".$rows->alamat.", ";                                       
                $dataPihakPembanding .= "untuk selanjutnya disebut sebagai \b ".$rows->pihak_asal_text."/Pembanding \b0 ; \\par ";
            }
            $i++;
        }
}
$Template = str_replace('#pihak_pembanding#',''.$dataPihakPembanding.'',$Template);
#looping terbanding
$dataPihakTerbanding = "";
$i = 0;
if(!empty($infoallterbanding)){
        foreach ($infoallterbanding->result() as $rows) { 
            if(($rows->jenis_pihak_id)==1){                       
                $dataPihakTerbanding .= " \b ".$this->templatehelper->standradnaming($rows->nama).", \b0 Warga Negara Indonesia, ";
                $dataPihakTerbanding .= "Tempat Tinggal di ".$rows->alamat.", Pekerjaan ".$rows->pekerjaan.", ";                                       
                $dataPihakTerbanding .= "untuk selanjutnya disebut sebagai \b ".$rows->pihak_asal_text."/".$rows->status_pihak_text." \b0 ; \\par ";                                                    
            }elseif(($rows->jenis_pihak_id)==2 OR ($rows->jenis_pihak_id)==3 OR ($rows->jenis_pihak_id)==4 ){          
                $dataPihakTerbanding .= " \b ".$this->templatehelper->standradnaming($rows->nama).",";
                $dataPihakTerbanding .= " Tempat Kedudukan di ".$rows->alamat.", ";                                       
                $dataPihakTerbanding .= "untuk selanjutnya disebut sebagai \b ".$rows->pihak_asal_text."/".$rows->status_pihak_text." \b0 ; \\par ";
            }
            $i++;
        }
}
$Template = str_replace('#pihak_terbanding#',''.$dataPihakTerbanding.'',$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\"");
echo $Template;

?>