<?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/penetapan/pidana_umum/".$filename.".rtf");
$judul = "";

$Template = file_get_contents($url);

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

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

$Template = str_replace("#nomorperkara#",''.$nomorperkara.'',$Template);
$Template = str_replace("#nosurat#",''.$nomorsurat.'',$Template);
$Template = str_replace("#tglsurat#",''.$this->templatehelper->convertKeTglIndo($tanggalsurat).'',$Template);

$namapn = '';
$namakota = '';

foreach ($datapengadilan->result() as $row) {
	$namapn = $this->templatehelper->standradnaming($row->NamaPN);
	$namakota =str_replace("Pengadilan Negeri ",'',$this->templatehelper->standradnaming($namapn));
	$zona_waktu = trim($row->ZonaWaktu);
}

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

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

$Template = str_replace("#namapn#",''.$namapn.'',$Template);
$Template = str_replace('#namakota#',''.$namakota.'',$Template);

$hakimketua = '';
$tanggalpenetapanhakim = '';

$majelis = true;
foreach ($datapenetapanhakim->result() as $row) {

	if($row->jabatan_hakim_id==1){
		$hakimketua =$row->nama_gelar;
	}elseif($row->jabatan_hakim_id==3){
		$majelis = false;
		$hakimketua =$row->nama_gelar;
	}
	$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("#tanggalpenetapanhakim#",''.$tanggalpenetapanhakim.'',$Template);
$Template = str_replace("#hakimketua#",''.$hakimketua.'',$Template);

$harisidang='';

foreach ($datapenetapansidang->result() as $row) {
	$tglsidang = $this->templatehelper->convertKeTglIndo($row->sidang_pertama);
	$tglpenetapansidang = $this->templatehelper->convertKeTglIndo($row->penetapan_hari_sidang);
	$jamsidang = substr($row->jam_sidang, 0,5);
	$harisidang = $this->templatehelper->getHari($row->sidang_pertama);
}

if(empty($tglsidang)){
	$tglsidang = '........';
}
if(empty($tglpenetapansidang)){
	$tglpenetapansidang = '........';
}
if(empty($jamsidang)){
	$jamsidang = '........';
}
$Template = str_replace("#tglsidang#",''.$tglsidang.'',$Template);
$Template = str_replace("#tglpenetapansidang#",''.$tglpenetapansidang.'',$Template);
$Template = str_replace("#jamsidang#",''.$jamsidang.' '.$zona_waktu,$Template);
$Template = str_replace("#harisidang#",''.$harisidang.'',$Template);

$namaterdakwa='';
$dkk='';
$counter = 1;
$dkk='';

foreach ($dataterdakwa->result() as $row)
{
    if($dataterdakwa->num_rows=="1"){
        if(empty($row->nama)){
            $nama = '......'; 
        }else{
            $nama=$row->nama;
        }
        $namaterdakwa = '\listid1 '.$this->templatehelper->standradnaming($nama);
    }

    if($dataterdakwa->num_rows > "1"){
        $Template = str_replace('#namaterdakwa#',$this->templatehelper->standradnaming($nama=$row->nama).', dan kawan-kawan',$Template);
    }else{
        $Template = str_replace('#namaterdakwa#',$this->templatehelper->standradnaming($nama=$row->nama),$Template);
    }
}

if($dataterdakwa->num_rows>0){
	$Template = str_replace('#menghadapkan/menghadirkan#','menghadapkan',$Template);
}else{
	$Template = str_replace('#menghadapkan/menghadirkan#','menghadirkan',$Template);
}


$namaKejaksaan =  ((!empty($namaKejaksaan)) ? $this->templatehelper->standradnaming($namaKejaksaan) : 'Kejaksaan ...........' );
$Template = str_replace("#namaKejaksaan#",''.$namaKejaksaan.'',$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;
?>