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

$atasPermin = intval($atasPermin);
if(!is_numeric($atasPermin)){
    show_404();
    exit();
}
$jabatanJs = intval($jabatanJS);
if(!is_numeric($jabatanJs)){
    show_404();
    exit();
}

$tglPanggil = $tglPanggil;
$namasaksi = $namasaksi;
$pekerjaansaksi = $pekerjaansaksi;
$alamatsaksi = $alamatsaksi;

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

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

$Template = file_get_contents($url);

function getDataPengacara($data_pengacara_penggugat,$idpihak){
    $str = ', dalam hal ini memberikan kuasa kepada ';
    $addAnd = false;
    foreach ($data_pengacara_penggugat->result() as $row){
        $parties = explode(',', $row->pihak_id);
        if(in_array($idpihak, $parties)){
	       if($addAnd){
                $str .= '  dan ';
	       }
            $str .= $row->nama.', '.$row->pekerjaan.', beralamat di '.$row->alamat.' berdasarkan Surat Kuasa Khusus tanggal \highlight7#mohon-di isi tanggal#\highlight0 ';
            $addAnd = true;
        }
    }
    if($addAnd){
        return $str;
    }else{
        return '';
    }
}
//-------Data PN--------------------------------------//
$dataperkara = $data_perkara;
$nomorperkara = '';
$nomorsurat = '';
$tanggalsurat = '';
$tanggal_pendaftaran='';
foreach ($dataperkara->result() as $row) {
    $nomorperkara = $row->nomor_perkara;
    $nomorsurat = $row->nomor_surat;
    $tanggalsurat = $this->templatehelper->convertKeTglIndo($row->tanggal_surat);
    $tanggal_pendaftaran=$this->templatehelper->convertKeTglIndo($row->tanggal_pendaftaran);
}

if(empty($nomorperkara)){ $nomorperkara = '........'; }
if(empty($nomorsurat)){ $nomorsurat = '........'; }
if(empty($tanggalsurat)){ $tanggalsurat = '........'; }
if(empty($tanggal_pendaftaran)){ $tanggal_pendaftaran = '........'; }
$Template = str_replace('#nomor_perkara#',''.$nomorperkara.'',$Template);
$Template = str_replace('#tgl_surat#',''.$tanggalsurat.'',$Template);
$Template = str_replace('#tgl_pendaftaran#',''.$tanggal_pendaftaran.'',$Template);

$Template = str_replace('#hari_panggil#',''.$this->templatehelper->getHari($tglPanggil).'',$Template);
$Template = str_replace('#tgl_panggil#',''.$this->templatehelper->convertKeTglIndo($tglPanggil).'',$Template);

$datapengadilan = $data_pn;
$namapn = '';
$alamatpn = '';
$zona = '';
foreach ($datapengadilan->result() as $row) {
    $namapn = $this->templatehelper->standradnaming($row->NamaPN);
    $zona = $row->ZonaWaktu;
    $alamatpn = $row->AlamatPN;
}

if(empty($namapn)){ $namapn = '........'; }
if(empty($zona)){ $zona = '........'; }
if(empty($alamatpn)){ $alamatpn = '........'; }
$Template = str_replace('#nama_pn#',''.$namapn.'',$Template);
$Template = str_replace('#alamat_pn#',''.$alamatpn.'',$Template);
$Template = str_replace('#waktu#',''.$zona.'',$Template);
//-------Data Atas Permin---------------------------------//
if($atasPermin==1){
    $lataspermin = "Penggugat";    
} else if ($atasPermin==2){
    $lataspermin = "Tergugat";
} else if ($atasPermin==3){
    $lataspermin = "Hakim";
}
$Template = str_replace('#ataspermin#',''.$lataspermin.'',$Template);
//--------Data Versus Penggugat---------------//
$pengacarapihak1 = array();
$datapenggugat = $data_penggugat;
$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);
    $pengacara = getDataPengacara($data_pengacara_penggugat,$row->pihak_id);    
    if($numbering){
       $urutan = ' '.$this->templatehelper->converToAngkaRomawi($row->urutan);
    }
    if ($row->info==1){
        $pihak1info = " bertempat tinggal di ".$this->templatehelper->standradnaming($row->alamat).$pengacara." selanjutnya disebut sebagai \t Penggugat ".$urutan.";";
    }else{
        $pihak1info = " berkedudukan di ".$this->templatehelper->standradnaming($row->alamat).$pengacara."  selanjutnya disebut sebagai \t 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--------------------------//
$pengacarapihak2 = array();
$datatergugat = $data_tergugat;
$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;
$pengacara = '';
$urutan ='';
foreach ($datatergugat->result() as $row){
    $pihak2 = '\listid1'.$this->templatehelper->standradnaming($row->nama);
    $pengacara = getDataPengacara($data_pengacara_penggugat,$row->pihak_id);
    if($numbering){
    	//$urutan = ' '.($row->urutan);
        $urutan = ' '.$this->templatehelper->converToAngkaRomawi($row->urutan);
    }
    if ($row->info==1){
        $pihak2info = " bertempat tinggal di ".$this->templatehelper->standradnaming($row->alamat).$pengacara." selanjutnya disebut sebagai \t Tergugat ".$urutan.";";
    }else{
        $pihak2info = " berkedudukan di ".$this->templatehelper->standradnaming($row->alamat).$pengacara."  selanjutnya disebut sebagai \t Tergugat ".$urutan.";";
    }
    //$pihak2info = " bertempat tinggal di ".$row['alamat'].$pengacara." selanjutnya disebut sebagai \t 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++;
}
//------Biaya Perkara----------------------------//
$biaya=0;
$biayaperkara = $listBiaya;
	    foreach($biayaperkara->result() as $row){
    	   $biaya += intval($row->jumlah);
        }
$pnbp=0;
$biayapnbp = $listPnbp;
    	foreach($biayapnbp->result() as $row){
        	$pnbp += intval($row->jumlah);
        }

$totalbiaya = $biaya+$pnbp;        
$Template = str_replace('#pnbp#',number_format(strval($pnbp),2,",","."),$Template);
$Template = str_replace('#total#',number_format(strval($totalbiaya),2,",","."),$Template);
$Template = str_replace('#totalterbilang#',''.$this->templatehelper->Terbilang($totalbiaya).' rupiah',$Template);
$Template = str_replace('#biaya#',number_format(strval($biaya),2,",","."),$Template);
$Template = str_replace('#biayaterbilang#',''.$this->templatehelper->Terbilang($biaya).' rupiah',$Template);
//----------Data Persidangan & Jurusita---------------------//
$datapersidangan = $listSidang;
foreach($datapersidangan->result() as $row){   
        $tglsidang = $this->templatehelper->convertKeTglIndo($row->tanggal_sidang);
        $harisidang = $this->templatehelper->getHari($row->tanggal_sidang);
    
    if(empty($row->jam_sidang)){
        $jamsidang = ".....";
    } else {
        $jamsidang = substr($row->jam_sidang, 0, 5);
    }
}

$Template = str_replace('#tanggalsidang#',''.$tglsidang.'',$Template);
$Template = str_replace('#harisidang#',''.$harisidang.'',$Template);
$Template = str_replace('#jamsidang#',''.$jamsidang.'',$Template);
//--------------Data Jurusita----------------//
$namajurusita=$listJsp;
foreach ($namajurusita->result() as $row) {
	$namajurusita = $this->templatehelper->standradnaming($row->jurusita_nama);
}
$Template = str_replace('#namajurusita#',''.$namajurusita.'',$Template);

if ($jabatanJs==1){
    $Template = str_replace('#jabatanjurusita#',''.'Jurusita'.'',$Template);
}else{
    $Template = str_replace('#jabatanjurusita#',''.'Jurusita Pengganti'.'',$Template);
}
//-------------Data Waris-----------------//
$Template = str_replace('#namasaksi#',''.$this->templatehelper->standradnaming($namasaksi).'',$Template);
$Template = str_replace('#pekerjaansaksi#',''.$this->templatehelper->standradnaming($pekerjaansaksi).'',$Template);
$Template = str_replace('#alamatsaksi#',''.$this->templatehelper->standradnaming($alamatsaksi).'',$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;
?>