<?php
$pembuat = intval($ditetapkanoleh);

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

$url = base_url("resources/template/penetapan/pidana_umum/".$filename.".rtf");

$Template = file_get_contents($url);

$nomorperkara = array();
$nomorsurat = array();
$tanggalsurat = array();
foreach ($dataperkara ->result() as $row) {
	$nomorperkara = $row->nomor_perkara;
	$nomorsurat = $row->nomor_surat;
	$tanggalsurat = $this->templatehelper->convertKeTglIndo($row->tanggal_surat);
}

$Template = str_replace('#nomorperkara#',''.$nomorperkara.'',$Template);
$Template = str_replace("#nosurat#",''.$nomorsurat.'',$Template);
$Template = str_replace("#tglsurat#",''.$tanggalsurat.'',$Template);

$namapn = array();
$namaketuapn = array();
$alamatpn = array();
$namawakilpn=array();

foreach ($datapengadilan->result() as $row) {
	$namapn = $this->templatehelper->standradnaming($row->NamaPN);
	$namaketuapn = $this->templatehelper->standradnaming($row->KetuaPNNama);
	$namawakilpn = $this->templatehelper->standradnaming($row->WakilKetuaPNNama);
	$namakota =str_replace("Pengadilan Negeri ",'',$namapn);
}

$Template = str_replace("#namapn#",''.$namapn.'',$Template);
$Template = str_replace('#namakota#',''.$namakota.'',$Template);
if ($pembuat==1) {
	$Template = str_replace('#atasnama#','',$Template);
	$Template = str_replace('#ketuapn#',''.$namaketuapn.'',$Template);
} else {
	$Template = str_replace('#atasnama#','a.n ',$Template);
	$Template = str_replace('#ketuapn#',''.$namawakilpn.'',$Template);
}

$hakimketua = '';
$penetapan = '';
$tanggalpenetapanhakim = '';
$majelis = true;
foreach ($datapenetapanhakim->result() as $row) {
	if($row->jabatan_hakim_id==1){
		$penetapan .=$this->templatehelper->standradnaming($row->nama_gelar)." \\tab ".$row->jabatan_hakim_nama.";\\par".PHP_EOL;
	}elseif($row->jabatan_hakim_id==2){
		$penetapan .=$this->templatehelper->standradnaming($row->nama_gelar)." \\tab ".$row->jabatan_hakim_nama.";\\par".PHP_EOL;
	}elseif($row->jabatan_hakim_id==3){
		$majelis = false;
		$penetapan .=$this->templatehelper->standradnaming($row->nama_gelar)." \\tab Hakim;\\par".PHP_EOL;
	}
	$tanggalpenetapanhakim = $this->templatehelper->convertKeTglIndo($row->tanggal_penetapan);


}
if($majelis){
	$Template = str_replace('#majelis#','Majelis',$Template);
}else{
	$Template = str_replace('#majelis#','',$Template);
} 
if(empty($hakimketua)){
	$hakimketua = '........';
}

if(empty($tanggalpenetapanhakim)){
	$tanggalpenetapanhakim = '........';
}

$Template = str_replace("#penetapan_hakim#",''.$penetapan.'',$Template);
$Template = str_replace('#tanggalpenetapanhakim#',''.$tanggalpenetapanhakim.'',$Template);
$Template = str_replace("#hakimketua#",''.$hakimketua.'',$Template);

$counter = 1;
$pihak2='';$namaterdakwa='';
if (!empty($dataterdakwa->num_rows)){   
    foreach ($dataterdakwa->result() as $row){
        $pihak2 .= 'Nama lengkap \tab : '.$this->templatehelper->standradnaming(strtolower($row->nama)).'\\par ';
        $pihak2 .= 'Tempat lahir \tab : '.$row->tempat_lahir.'\\par ';
        $pihak2 .= 'Umur/Tanggal lahir \tab : '.$this->tanggalhelper->getSelisihTahun($row->tanggal_lahir,date('Y-m-d')).'/ '.$this->templatehelper->convertKeTglIndo($row->tanggal_lahir).'\\par ';
        if ($row->jenis_kelamin=='L') {
            $pihak2 .= 'Jenis kelamin \tab : '.'Laki-laki'.'\\par ';
        } else {
            $pihak2 .= 'Jenis kelamin \tab : '.'Perempuan'.'\\par ';
        }
     
        $pihak2 .= 'Kebangsaan \tab : '.$row->warga_negara.'\\par ';
        $pihak2 .= 'Tempat tinggal \tab : '.$row->alamat.'\\par ';
        $pihak2 .= 'Agama \tab : '.$row->agama_nama.'\\par ';
        $pihak2 .= 'Pekerjaan \tab : '.$row->pekerjaan.'\\par \\par ';
        $namaterdakwa .= $this->templatehelper->standradnaming(strtolower($row->nama));

        $Template=str_replace('#namaterdakwa#',$namaterdakwa,$Template);
        $Template = str_replace('#terdakwa#',$pihak2,$Template);
        $counter++;
    }
}

if($counter>2){
	$Template = str_replace('#jenisterdakwa#','Para',$Template);
	$Template = str_replace('#terdakwa/paraterdakwa#',$namaterdakwa.' dan kawan-kawan',$Template);
}else{
	$Template = str_replace('#jenisterdakwa#','',$Template);
	$Template = str_replace('#terdakwa/paraterdakwa#',$namaterdakwa,$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;
?>