<?php
if($this->session->userdata('is_logged_in')===FALSE){
            redirect('login');
}
$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 = '..................';
$nomorsurat = '....................';
$tanggalsurat = '...................';
foreach ($dataperkara ->result() as $row) {
	$nomorperkara = $row->nomor_perkara;
	$nomorsurat = (!empty($row->nomor_surat))? $row->nomor_surat:'....................';
	$tanggalsurat = (!empty($row->tanggal_surat))? $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;
$dkk='';

foreach ($dataterdakwa->result() as $row){
	if($counter==1){
		if(empty($row->nama)){
			$row->nama = '......'; 
		}

		$namaterdakwa = '\listid1'.$this->templatehelper->standradnaming($row->nama).$dkk;

		if(empty($row->tempat_lahir)){
			$row->tempat_lahir = '......'; 
		}

		$tempatlahir = '\listid1'.$row->tempat_lahir;	
		if(empty($row->tanggal_lahir) OR $row->tanggal_lahir==NULL){
			$tgllahir="................";
			$umur=".............";
		}else{
			$tgllahir = '\listid1'.' '.$this->templatehelper->convertKeTglIndo($row->tanggal_lahir);
			$umur = $this->tanggalhelper->getSelisihTahun($row->tanggal_lahir,date('Y-m-d'));
		}
		

		if(empty($row->warga_negara)){
			$row->warga_negara = '......'; 
		}

		$kebangsaan = '\listid1'.$this->templatehelper->standradnaming($row->warga_negara);

		if(empty($row->alamat)){
			$row->alamat = '......'; 
		}
		$alamat = '\listid1'.$row->alamat;

		if(empty($row->agama_nama)){
			$row->agama_nama = '......'; 
		}
		$agama = '\listid1'.$this->templatehelper->standradnaming($row->agama_nama);
		
		if(empty($row->pekerjaan)){
			$row->pekerjaan = '......'; 
		}

		$pekerjaan = '\listid1'.$this->templatehelper->standradnaming($row->pekerjaan);
		
		if ($row->jenis_kelamin=='P') {
			$sex = 'Perempuan';
		} else{
			$sex = 'Laki-laki';
		}
	}
	$counter++;
}

$namaKejaksaan =  ((!empty($namaKejaksaan)) ? $this->templatehelper->standradnaming($namaKejaksaan) : 'Kejaksaan ...........' );
$Template = str_replace('#namaterdakwa#',''.$namaterdakwa.'',$Template);
$Template = str_replace('#tempatlahir#',''.$tempatlahir.'',$Template);
$Template = str_replace('#umur#',''.$umur.'',$Template);
$Template = str_replace('#tgllahir#',''.$tgllahir.'',$Template);
$Template = str_replace('#kebangsaan#',''.$kebangsaan.'',$Template);
$Template = str_replace('#alamat#',''.$alamat.'',$Template);
$Template = str_replace('#agama#',''.$agama.'',$Template);
$Template = str_replace('#pekerjaan#',''.$pekerjaan.'',$Template);
$Template = str_replace('#jeniskelamin#',''.$sex.'',$Template);
$Template = str_replace("#namaKejaksaan#",''.$namaKejaksaan.'',$Template);

if($counter>2){
	$Template = str_replace('#jenisterdakwa#','Para',$Template);
	$Template = str_replace('#dkk#','Dkk',$Template);
}else{
	$Template = str_replace('#jenisterdakwa#','',$Template);
	$Template = str_replace('#dkk#','',$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;
?>