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

$Template = file_get_contents($url);

$nama_pn = '';
$alamatpn = '';

foreach ($datapengadilan->result() as $row) {
    $namapn = $this->templatehelper->standradnaming($row->NamaPN);
    $alamatpn = $row->AlamatPN;
    $namaketua=$this->templatehelper->standradnaming($row->KetuaPNNama);
    $pansek=$this->templatehelper->standradnaming($row->PansekPNNama);
    $wapan=$this->templatehelper->standradnaming($row->WaPanNama);
    $namakota = str_replace("Pengadilan Negeri ",'',$namapn);
}

if(empty($nama_pn)){
    $nama_pn = '........';
}
if(empty($alamatpn)){
    $alamatpn = '........';
}

foreach ($dataperkara->result() as $row) {
    $dakwaan = $row->dakwaan;
    $nomorperkara = $row->nomor_perkara;
}

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('#jenisterdakwa#','Para',$Template);
        $Template = str_replace('#terdakwa/paraterdakwa#',$this->templatehelper->standradnaming($nama=$row->nama).' dan kawan-kawan',$Template);
    }else{
        $Template = str_replace('#jenisterdakwa#','',$Template);
        $Template = str_replace('#terdakwa/paraterdakwa#',$this->templatehelper->standradnaming($nama=$row->nama),$Template);
    }
}
$hakimketua = '';
$hakimAnggota='';
$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);
}

foreach ($dataperkara->result() as $row) {
    $dakwaan = $row->dakwaan;
    $nomorperkara = $row->nomor_perkara;
}

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

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

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

$penetapan = '';
$tanggalpenetapan = '';
$namapp='';
foreach ($datapenetapanpanitera->result() as $row) {
    $penetapan .=$this->templatehelper->standradnaming($row->nama_gelar)."\\tab Panitera Pengganti;\\par".PHP_EOL;
    $tanggalpenetapan = $this->templatehelper->convertKeTglIndo($row->tanggal_penetapan);
    if ($namapp===$pansek) {
        $Template = str_replace('#pansek/pp#',''."Panitera".'',$Template);
    } else {
        $Template = str_replace('#pansek/pp#',''."Panitera Pengganti".'',$Template);
    }
}

if(empty($penetapan)){
    $penetapan = '........';
}
if(empty($tanggalpenetapan)){
    $tanggalpenetapan = '........';
}

if (empty($pansek)) {
    $pansek="..............";
}

if (empty($wapan)) {
    $wapan="..............";
}

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


$Template = str_replace('#tglpenetapanmajelishakim#',''.$tanggalpenetapanhakim.'',$Template);
$Template = str_replace('#nomorperkara#',''.$nomorperkara.'',$Template);
$Template = str_replace('#penetapanpanitera#',''.$penetapan.'',$Template);
$Template = str_replace('#tanggalpenetapan#',''.$tanggalpenetapan.'',$Template);

if ($ditetapkanoleh==1) {
    $Template = str_replace('#atasnama#','',$Template);
    $Template = str_replace('#pansek#',''.$pansek.'',$Template);
} else {
    $Template = str_replace('#atasnama#','a.n ',$Template);
    $Template = str_replace('#pansek#',''.$wapan.'',$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;
?> 