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

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

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

$Template = file_get_contents($url);
//-------Data PN--------------------------------------//
if($jenisTemplate==1 OR $jenisTemplate==2){
    $dataperkara = $dataumum;
        foreach ($dataperkara->result() as $row) {
                $nomorperkara = $row->nomor_perkara; 
                $tgl_penetapan_persiapan = $this->templatehelper->convertKeTglIndo($row->tanggal_penetapan_persiapan);
                $hari_penetapan = $this->templatehelper->getHari($row->tanggal_penetapan_persiapan);
                $tgl_panggilan = $this->templatehelper->convertKeTglIndo($row->tanggal_musyawarah);
                $hari_panggilan = $this->templatehelper->getHari($row->tanggal_musyawarah);               
                $agenda = $this->templatehelper->cleanHtmlTag($row->agenda_musyawarah);            
                $jam_panggilan = substr($row->waktu_musyawarah, 0, 5);
                $nama_panitera = $this->templatehelper->standradnaming($row->panitera_nama);                       
    }
    $Template = str_replace('#hari_pemberitahuan#',''.$hari_panggilan.'',$Template);
    $Template = str_replace('#tgl_pemberitahuan#',''.$tgl_panggilan.'',$Template);
    $Template = str_replace('#hari_penetapan#',''.$hari_penetapan.'',$Template);
    $Template = str_replace('#tgl_penetapan_persiapan#',''.$tgl_penetapan_persiapan.'',$Template);
    $Template = str_replace('#agenda#',''.$agenda.'',$Template);
    $Template = str_replace('#jam_panggilan#',''.$jam_panggilan.'',$Template);
    $Template = str_replace('#nama_panitera#',''.$nama_panitera.'',$Template);
}
if($jenisTemplate==3 OR $jenisTemplate==4){
    if(!empty($datadismissal)){
        if($datadismissal->num_rows>0){
            foreach ($datadismissal->result() as $row) {
                $nomorperkara = $row->nomor_perkara; 
                $tanggal_panggilan = $this->templatehelper->convertKeTglIndo($row->tanggal_panggilan);
                $hari_panggilan = $this->templatehelper->getHari($row->tanggal_panggilan);
                $jam_panggilan = substr($row->waktu, 0, 5);
            }
        }
    }
    $Template = str_replace('#tanggal_panggilan#',''.$tanggal_panggilan.'',$Template);
    $Template = str_replace('#hari_panggilan#',''.$hari_panggilan.'',$Template);
    $Template = str_replace('#jam_panggilan#',''.$jam_panggilan.'',$Template);
}
$datapengadilan = $datapn;
if(!empty($datapengadilan)){
    if($datapengadilan->num_rows>0){
        foreach ($datapengadilan->result() as $row) {
            $namapn = $this->templatehelper->standradnaming($row->NamaPN);
            $namakota = str_replace("Pengadilan Tata Usaha Negara ",'',$namapn);   
            $alamatpn = $row->AlamatPN;
            $pansek = $this->templatehelper->standradnaming($row->PansekPNNama);   
            $wapan = $this->templatehelper->standradnaming($row->WaPanNama);
            $ZonaWaktu = $row->ZonaWaktu;
        }
    }
}
if($jenisTemplate==3 OR $jenisTemplate==4){
    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);
    }
}

if ($jenisTemplate==1 OR $jenisTemplate==3){
    $jmlpihak = $datapihakpenggugat->num_rows;
    if(!empty($datapenggugat)){
        if($datapenggugat->num_rows>0){            
            foreach ($datapenggugat->result() as $row) {                        
                $nama = $this->templatehelper->standradnaming($row->namaP);
                $kewarganegaraan = $this->templatehelper->standradnaming($row->wargaP);
                $alamat = $this->templatehelper->standradnaming($row->alamatP);
                $pekerjaan = $this->templatehelper->standradnaming($row->pekerjaanP);
                $urutan = $this->templatehelper->converToAngkaRomawi($row->urutanP);
            }
        }
    }

}elseif($jenisTemplate==2 OR $jenisTemplate==4){
    $jmlpihak = $datapihaktergugat->num_rows;    
    if(!empty($datatergugat)){
            foreach ($datatergugat->result() as $row) {               
                $nama = $this->templatehelper->standradnaming($row->namaT);
                $kewarganegaraan = $this->templatehelper->standradnaming($row->wargaT);
                $alamat = $this->templatehelper->standradnaming($row->alamatT);
                $pekerjaan = $this->templatehelper->standradnaming($row->pekerjaanT);
                $urutan = $this->templatehelper->converToAngkaRomawi($row->urutan);
            }
    }
}
    
if(empty($namapn)){ $namapn = '........'; }
if(empty($alamatpn)){ $alamatpn = '........'; }
$jml='';
if($jmlpihak>1){
    $jml=$urutan;
}elseif($jmlpihak==1){
    $jml=='';
}

$nomorpkrbaru = str_replace('G', 'PEN-PP', $nomorperkara);

$Template = str_replace('#nomor_perkara#',''.$nomorperkara.'',$Template);
$Template = str_replace('#nomorpkrbaru#',''.$nomorpkrbaru.'',$Template);
$Template = str_replace('#nama_pn#',''.$namapn.'',$Template);
$Template = str_replace('#namakota#',''.$namakota.'',$Template);
$Template = str_replace('#alamatpn#',''.$alamatpn.'',$Template);
$Template = str_replace('#nama#',''.$nama.'',$Template);
$Template = str_replace('#urutan#',''.$jml.'',$Template);
$Template = str_replace('#kewarganegaraan#',''.$kewarganegaraan.'',$Template);
$Template = str_replace('#alamat#',''.$alamat.'',$Template);
$Template = str_replace('#pekerjaan#',''.$pekerjaan.'',$Template);
$current_date = $this->templatehelper->convertKeTglIndo(date('Y-m-d'));
$Template = str_replace('#tanggal_sekarang#',$current_date,$Template);
$Template = str_replace('#ZonaWaktu#',$ZonaWaktu,$Template);

if($jenisTemplate==1 OR $jenisTemplate==2){
    $dataPihakP = "";
    $i = 0;
    $jmlpihak = $datapihakpenggugat->num_rows;
    if(!empty($datapihakpenggugat)){
            foreach ($datapihakpenggugat->result() as $rows) {              
                $urutan = $rows->urutan;
                $jenis_pihak = $rows->info;
                if ($jenis_pihak==1) {
                    $dataPihakP .= "Nama \t:\t".$rows->nama." \\par ";
                    $dataPihakP .= "kewarganegaraan \t:\t"."Indonesia"." \\par ";
                    $dataPihakP .= "Pekerjaan \t:\t".$rows->pekerjaan." \\par ";
                    $dataPihakP .= "Tempat Tinggal \t:\t".$rows->alamat." \\par ";                                       
                    
                }else if ($jenis_pihak==3) {
                    $dataPihakP .= "Nama \t:\t".$rows->nama." \\par ";
                    $dataPihakP .= "Alamat \t:\t".$rows->alamat." \\par ";    
                }          
                                                       
                $dataPihakP .= " \t \t sebagai \b PENGGUGAT ".($jmlpihak>1 ? $this->templatehelper->converToAngkaRomawi($rows->urutan) : '')." \b0 \\par ";                                    
                $i++;
            }
    }
    $Template = str_replace('#pihak_penggugat#',''.$dataPihakP.'',$Template);
}
if($jenisTemplate==3 OR $jenisTemplate==4){
    $dataPihakP = "";
    $i = 0;
    $jmlpihak = $datapihakpenggugat->num_rows;
    if(!empty($datapihakpenggugat)){
            foreach ($datapihakpenggugat->result() as $rows) {              
                $urutan = $rows->urutan;          
                $jenis_pihak = $rows->info;
                if ($jenis_pihak==1) {
                    $dataPihakP .= "Nama \t:\t".$rows->nama." \\par ";
                    $dataPihakP .= "kewarganegaraan \t:\t"."Indonesia"." \\par ";
                    $dataPihakP .= "Pekerjaan \t:\t".$rows->pekerjaan." \\par ";
                    $dataPihakP .= "Tempat Tinggal \t:\t".$rows->alamat." \\par ";                                       
                    
                }else if ($jenis_pihak==3) {
                    $dataPihakP .= "Nama \t:\t".$rows->nama." \\par ";
                    $dataPihakP .= "Alamat \t:\t".$rows->alamat." \\par ";    
                }          

                $dataPihakP .= " \t \t selaku Pihak \b PENGGUGAT ".($jmlpihak>1 ? $this->templatehelper->converToAngkaRomawi($rows->urutan) : '')." \b0 \\par ";                                    
                $i++;
            }
    }
    $Template = str_replace('#pihak_penggugat#',''.$dataPihakP.'',$Template);
}



if($jenisTemplate==3){
    $dataPihakT = "";
    $i = 0;
    $jmlpihak = $datapihaktergugat->num_rows;
    if(!empty($datapihaktergugat)){
            foreach ($datapihaktergugat->result() as $rows) {            
                $dataPihakT .= "Nama Jabatan \t:\t".$rows->nama." \\par ";
                $dataPihakT .= "Tempat Kedudukan \t:\t".$rows->alamat." \\par ";   
                $dataPihakT .= " \t \t selaku pihak \b TERGUGAT ".($jmlpihak>1 ? $this->templatehelper->converToAngkaRomawi($rows->urutan) : '')." \b0 \\par ";
                $i++;                                                   
            }
    }
    $Template = str_replace('#pihak_tergugat#',''.$dataPihakT.'',$Template);   
}

    $dataPihakT = "";
    $i = 0;
    $jmlpihak = $datapihaktergugat->num_rows;
    if(!empty($datapihaktergugat)){
            foreach ($datapihaktergugat->result() as $rows) {            
                $dataPihakT .= "Nama Jabatan \t:\t".$rows->nama." \\par ";
                $dataPihakT .= "Tempat Kedudukan \t:\t".$rows->alamat." \\par ";   
                $dataPihakT .= " \t \t sebagai \b TERGUGAT ".($jmlpihak>1 ? $this->templatehelper->converToAngkaRomawi($rows->urutan) : '')." \b0 \\par ";
                $i++;                                                   
            }
    }
    $Template = str_replace('#pihak_tergugat#',''.$dataPihakT.'',$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;
?>