<?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/putusan/perdata_tun/".$filename.".rtf");

$Template = file_get_contents($url);

//-------Data PN--------------------------------------//
$dataperkara = $dataumum;
    foreach ($dataperkara->result() as $row) {
            $nomorperkara = $row->nomor_perkara; 
            $tgl_putusan = $this->templatehelper->convertKeTglIndo($row->tanggal_putusan);
            $hari_putusan = $this->templatehelper->getHari($row->tanggal_putusan);               
            $amar = $this->templatehelper->cleanHtmlTag($row->amar_putusan);            
            $nama_panitera = $this->templatehelper->standradnaming($row->panitera_nama);                       
}

$datapengadilan = $datapn;
if(!empty($datapengadilan)){
    if($datapengadilan->num_rows>0){
        foreach ($datapengadilan->result() as $row) {
            $namapn = $this->templatehelper->standradnaming($row->NamaPN);   
            $alamatpn = $row->AlamatPN;
        }
    }
    
}

if ($jenisTemplate==1){
    if(!empty($datapenggugat)){
        if($datapenggugat->num_rows>0){
            foreach ($datapenggugat->result() as $row) {
                $tgl_pemberitahuan = $this->templatehelper->convertKeTglIndo($row->tanggal_pemberitahuan_putusan);
                $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);
            }
        }
    }

}else if ($jenisTemplate==2){

        if(!empty($datatergugat)){
        if($datatergugat->num_rows>0){
            foreach ($datatergugat->result() as $row) {
                $tgl_pemberitahuan = $this->templatehelper->convertKeTglIndo($row->tanggal_pemberitahuan_putusan);
                $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 = '........'; }
if(!empty($tgl_pemberitahuan)){
    $hari_pemberitahuan = $this->templatehelper->getHari($row->tanggal_pemberitahuan_putusan);    
}else{
    $hari_pemberitahuan = '........';
}
if(empty($tgl_pemberitahuan)){ $tgl_pemberitahuan = '........'; }

$Template = str_replace('#nomor_perkara#',''.$nomorperkara.'',$Template);
$Template = str_replace('#hari_pemberitahuan#',''.$hari_pemberitahuan.'',$Template);
$Template = str_replace('#tgl_pemberitahuan#',''.$tgl_pemberitahuan.'',$Template);
$Template = str_replace('#hari_putusan#',''.$hari_putusan.'',$Template);
$Template = str_replace('#tgl_putusan#',''.$tgl_putusan.'',$Template);
$Template = str_replace('#amar#',''.$amar.'',$Template);
$Template = str_replace('#nama_panitera#',''.$nama_panitera.'',$Template);
$Template = str_replace('#nama_pn#',''.$namapn.'',$Template);
$Template = str_replace('#nama#',''.$nama.'',$Template);
$Template = str_replace('#urutan#',''.$urutan.'',$Template);
$Template = str_replace('#kewarganegaraan#',''.$kewarganegaraan.'',$Template);
$Template = str_replace('#alamat#',''.$alamat.'',$Template);
$Template = str_replace('#pekerjaan#',''.$pekerjaan.'',$Template);

//--------Data Versus Penggugat---------------//
$datapenggugat = $datapihakpenggugat;
$numbering = true;
if($datapenggugat->num_rows>1){
	$jmlPenggugat=$datapenggugat->num_rows;
    for ($i=0; $i < $jmlPenggugat; $i++) { 
        $text = 'pihak'.($i+1);
        if($i==$jmlPenggugat-1){
	$Template = str_replace("#loopingparties#",'\b#'.$text.'#\b0,#'.$text.'info#',$Template);
        }else{
	$Template = str_replace("#loopingparties#",'\b#'.$text.'#\b0,#'.$text.'info#\\par'.PHP_EOL.'#loopingparties#',$Template);
        }
    }	
}else{
    $numbering = false;
    $Template = str_replace("#loopingparties#",'#pihak1#,#pihak1info#',$Template);
}

$counter = 1;
$urutan = '';
foreach ($datapenggugat->result() as $row){
    $pihak1 = '\listid1'.$this->templatehelper->standradnaming($row->nama);    
    if($numbering){
       $urutan = ' '.$this->templatehelper->converToAngkaRomawi($row->urutan);
    }
    if ($row->info==1){
        $pihak1info = " kewarganegaraan ".$row->warga_negara." pekerjaan ".$this->templatehelper->standradnaming($row->pekerjaan)." bertempat tinggal di ".$this->templatehelper->standradnaming($row->alamat)." selanjutnya disebut sebagai PENGGUGAT ".$urutan.";";
    }else{
        $pihak1info = " kewarganegaraan ".$row->warga_negara." pekerjaan ".$this->templatehelper->standradnaming($row->pekerjaan)." berkedudukan di ".$this->templatehelper->standradnaming($row->alamat)."  selanjutnya disebut sebagai PENGGUGAT ".$urutan.";";
    }    
    $pihak1info = str_replace('  ',' ',$pihak1info);
    if($numbering==true){
        $Template = str_replace('#pihak'.$counter.'#','\b '.$counter.'. '.'\tab '.$pihak1.'\b0',$Template);
    }else{
        $Template = str_replace('#pihak'.$counter.'#','\b'.$pihak1.'\b0',$Template);
    }
	
    $Template = str_replace('#pihak'.$counter.'info#',''.$pihak1info.'',$Template);
    $counter++;	
}
//-------------Data Versus Tergugat--------------------------//
$datatergugat = $datapihaktergugat;
$numbering = true;
if($datatergugat->num_rows>1){	
	$jmlTergugat=$datatergugat->num_rows;
    for ($i=0; $i < $jmlTergugat; $i++) { 
        $text = 'tergugat'.($i+1);
        if($i==$jmlTergugat-1){
            $Template = str_replace('#loopingparties2#','\b#'.$text.'#\b0,#'.$text.'info#',$Template);
        }else{
            $Template = str_replace('#loopingparties2#','\b#'.$text.'#\b0,#'.$text.'info#\\par'.PHP_EOL.'#loopingparties2#',$Template);
        }		
    }	
}else{
    $numbering = false;
    $Template = str_replace('#loopingparties2#','#tergugat1#,#tergugat1info#',$Template);
}
$counter = 1;
$urutan ='';
foreach ($datatergugat->result() as $row){
    $pihak2 = '\listid1'.$this->templatehelper->standradnaming($row->nama);
    if($numbering){
        $urutan = ' '.$this->templatehelper->converToAngkaRomawi($row->urutan);
    }
    if ($row->info==1){
        $pihak2info = " kewarganegaraan ".$row->warga_negara." pekerjaan ".$this->templatehelper->standradnaming($row->pekerjaan)." bertempat tinggal di ".$this->templatehelper->standradnaming($row->alamat)." selanjutnya disebut sebagai TERGUGAT ".$urutan.";";
    }else{
        $pihak2info = " kewarganegaraan ".$row->warga_negara." pekerjaan ".$this->templatehelper->standradnaming($row->pekerjaan)." berkedudukan di ".$this->templatehelper->standradnaming($row->alamat)."  selanjutnya disebut sebagai TERGUGAT ".$urutan.";";
    }    
    $pihak2info = str_replace('  ',' ',$pihak2info);
    if($numbering==true){
        $Template = str_replace('#tergugat'.$counter.'#','\b '.$counter.'. '.'\tab '.$pihak2.'\b0',$Template);
        $Template = str_replace('#:#',':',$Template);
    }else{
        $Template = str_replace('#tergugat'.$counter.'#','\b'.$pihak2.'\b0',$Template);
        $Template = str_replace('#:#','',$Template);
    }
    $Template = str_replace('#tergugat'.$counter.'info#',''.$pihak2info.'',$Template);
    $counter++;
}

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;
?>