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

$jenisAkta=intval($jenisAkta);
if(!is_numeric($jenisAkta)){
    show_404();
    exit();
}
$penandatangan=$this->encrypt->decode(base64_decode($penandatangan));
$penandatangan=intval($penandatangan);
if(!is_numeric($penandatangan)){
    show_404();
    exit();
}
$tglBanding=$tglBanding;
$bandingMelalui=intval($bandingMelalui);
if(!is_numeric($bandingMelalui)){
    show_404();
    exit();
}
foreach ($template->result() as $template)
{
    $filename=$template->kdtemplate;
    $namafile=str_replace(" ","",$template->namatemplate);
}
$url = base_url("resources/template/akta/pidana_umum/".$filename.".rtf");
$Template = file_get_contents($url);
//--------------MULAI--------------------------------------------//
function cetakPihak(&$strbuf, $tmp_label, $arrDef, $hasilFetch, $cetakLabel = true){
    $tblcnt = 1;
    $jmlphk = count($hasilFetch);
    foreach($hasilFetch as $baris){
        $idxKolom = 1;
        foreach($arrDef as $kolomTabel => $label){
            $counter = (($jmlphk > 1)?($tblcnt.'.'):(''));
            $strbuf = str_replace($tmp_label,
                    (($idxKolom == 1)?($counter):('')).
                        ' \tab '.(($cetakLabel)?$label:('')).(($cetakLabel)?(' \tab : \tab '):('')).$baris[$kolomTabel].'; \\par'.
                        PHP_EOL.
                        //$tmp_label,
                        (($idxKolom < count($arrDef)) ? 
                            ($tmp_label) 
                            :  
                            (($tblcnt<$jmlphk)?($tmp_label):('')) 
                        ),
                    $strbuf);
            $idxKolom++;
        }
        $tblcnt++;
    }
}
//-------------Nomor Perkara----------------------//
$data_perkara = $dataPerkara;
$nomorperkara = '';
foreach ($data_perkara->result() as $row) {
        $nomorperkara = $row->nomor_perkara;
    }
if(empty($nomorperkara)){ $nomorperkara = '........'; }
$akta="Akta ";
$nomorperkarabaru=substr_replace($nomorperkara, $akta, 
    strpos($nomorperkara,'/')+1, 
    0);
$Template = str_replace("#nomorperkarabaru#",''.$nomorperkarabaru.'',$Template);
$Template = str_replace("#nomorperkara#",''.$nomorperkara.'',$Template);
//---------------Tanggal/Hari Banding-------------//
$tglpermohonan = $tglBanding;
if(empty($tglpermohonan)){
    $tglpermohonan = '..................';
    $haripermohonan = '.................';
}else{
    $haripermohonan = $this->templatehelper->getHari($tglpermohonan);
}
$Template = str_replace("#tglpermohonan#",''.$this->templatehelper->convertKeTglIndo($tglpermohonan).'',$Template);
$Template = str_replace("#haripermohonan#",''.$haripermohonan.'',$Template);

$Template = str_replace('#terdakwajudul#','TERDAKWA',$Template);
$Template = str_replace('#jmlterdakwa#','Terdakwa',$Template);

if(($bandingMelalui==1)||($bandingMelalui==10)) {    
    $Template = str_replace('#judul#','KALAPAS',$Template);
    $Template = str_replace('#tahanan#','Kepala Lembaga Pemasyarakatan',$Template);
}else if ($bandingMelalui==2) {
    $Template = str_replace('#judul#','KARUTAN',$Template);
    $Template = str_replace('#tahanan#','Rumah Tahanan Negara',$Template);
}
//---------------Data Pengadilan--------------------//
$datapengadilan = $dataPN;
$namapn = '';
$pansek = '';
foreach ($datapengadilan->result() as $row) {
    $namapn = $this->templatehelper->standradnaming($row->NamaPN);
    $namakota =str_replace("Pengadilan Negeri ",'',$namapn);
    $pansek = $this->templatehelper->standradnaming($row->Panses);   
    $wapan = $this->templatehelper->standradnaming($row->Wapan); 
}
if(empty($namapn)){ $namapn = '........'; }
if($penandatangan==68){    
    $Template = str_replace('#pansek#',''.$pansek.'',$Template);
    $Template = str_replace('#jabatan#','Panitera',$Template);
} else if($penandatangan==70){
    $Template = str_replace('#pansek#',''.$wapan.'',$Template);
    $Template = str_replace('#jabatan#','a.n. Panitera',$Template);
}
$Template = str_replace('#namapn#',''.$namapn.'',$Template);
$Template = str_replace('#namakota#',''.$namakota.'',$Template);
//-------------Data Tanggal Putusan-------------------//
$tanggalPutusan=$tanggalPutus;
foreach ($tanggalPutusan->result() as $row) {
    $tglputus=$this->templatehelper->convertKeTglIndo($row->tanggal_putusan);
}
$Template = str_replace('#tglputus#',''.$tglputus.'',$Template);
//-------------Data Terdakwa-------------------------//
if(($jenisAkta==1) || ($jenisAkta==2) || ($jenisAkta==5) || ($jenisAkta==6) || ($jenisAkta==9) || ($jenisAkta==10)){
    $loopingTerdakwa = $cetakDataTerdakwa;
    foreach ($loopingTerdakwa->result() as $row){
        if(empty($row->nama)){
            $row->nama = '......'; 
        }

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

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

        $tempatlahir = '\listid1 '.$this->templatehelper->standradnaming($row->tempat_lahir);

        if(empty($row->umur)){
            $tgllahir = '......'; 
        }else{
            $tgllahir = '\listid1 '.$this->templatehelper->convertKeTglIndo($row->tanggal_lahir);
        }

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

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

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

        if(empty($row->alamat)){
            $row->alamat = '......'; 
        }
        $alamat = '\listid1 '.$this->templatehelper->standradnaming($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=='L'){
            $row->jenis_kelamin = 'Laki - Laki';
        }elseif($row->jenis_kelamin=='P'){
            $row->jenis_kelamin = 'Perempuan';
        }
        $sex = '\listid1 '.$row->jenis_kelamin;
    }

    $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);
}
if(($jenisAkta==3)||($jenisAkta==7)||($jenisAkta==11)){
    $dataterdakwa = $cetakpartiesbanding;
    $tblcnt = 1;
    $dataterdakwa->num_rows>0;
    //$jmltdkw = count($dataterdakwa);
    $p = array();
    $gkolom = array(
        'nama' => 'Nama Lengkap',
        'tempat_lahir' => 'Tempat Lahir',
        'umur' => 'Umur / Tgl. Lahir',
        'jenis_kelamin' => 'Jenis Kelamin',
        'warga_negara' => 'Kebangsaan',
        'alamat' => 'Tempat Tinggal',
        'agama' => 'Agama',
        'pekerjaan' => 'Pekerjaan'
        );
    foreach ($dataterdakwa->result() as $row){
        $namaKuasa=$this->templatehelper->standradnaming($row->pemohonbanding);
        $tglkuasa=$this->templatehelper->convertKeTglIndo($row->tglKuasa);
        $nomorkuasa=$row->nomorKuasa;
        $inpass = array();

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

        $namaterdakwa = $this->templatehelper->standradnaming($row->nama);
        $inpass['nama'] = $namaterdakwa;

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

        $tempatlahir = $row->tempat_lahir;
        $inpass['tempat_lahir'] = $this->templatehelper->standradnaming($tempatlahir);

        if(empty($row->umur)){
            $tgllahir = '......'; 
        }else{
            $tgllahir = $this->templatehelper->convertKeTglIndo($row->tanggal_lahir);
        }

        if(empty($row->umur)){
            $row->umur = '......'; 
        }
        $umur = $row->umur;
        $inpass['umur'] = $umur.' Tahun / '.$tgllahir;

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

        $kebangsaan = $this->templatehelper->standradnaming($row->warga_negara);
        $inpass['warga_negara'] = $kebangsaan;

        if(empty($row->alamat)){
            $row['alamat'] = '......'; 
        }
        $alamat = $row->alamat;
        $inpass['alamat'] = $this->templatehelper->standradnaming($alamat);

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

        if($row->jenis_kelamin=='L'){
            $row->jenis_kelamin = 'Laki - Laki';
        }elseif($row->jenis_kelamin=='L'){
            $row->jenis_kelamin = 'Perempuan';
        }
        $sex = $row->jenis_kelamin;
        $inpass['jenis_kelamin'] = $sex;
        
        $tblcnt++;

        $p[] = $inpass;
    }
    cetakPihak($Template, '#terdakwaparties#', $gkolom, $p);
    if ($dataterdakwa->num_rows>1) {
        $Template = str_replace('#jmlterdakwa3#','Para Terdakwa',$Template);
    } else {
        $Template = str_replace('#jmlterdakwa3#','Terdakwa',$Template);
    }
    $Template = str_replace("#penasehat#",''.$namaKuasa.'',$Template);

    $tglMemoriBanding=$cetakDataBanding;
    foreach ($tglMemoriBanding->result() as $row) {
        $tglMemoriPenuntut=$this->templatehelper->convertKeTglIndo($row->tglMemori);
    }
    if(empty($tglMemoriPenuntut)){
        $tglMemoriPenuntut='.................';
    }
    $Template = str_replace("#tanggalmemori#",''.$tglMemoriPenuntut.'',$Template);
    if(empty($tglkuasa)){$tglkuasa='..........';}
    if(empty($nomorkuasa)){$nomorkuasa='..........';}
    $Template = str_replace('#tglkuasa#',''.$tglkuasa.'',$Template);
    $Template = str_replace('#nomorkuasa#',''.$nomorkuasa.'',$Template);
}
//--------------------Data JPU---------------//
if(($jenisAkta==4)||($jenisAkta==8)){
    $databanding = $cetakDataJPU;
    $jpu = '';
    $nip = '';
    $tglpermohonan ='';
    foreach ($databanding->result() as $row) {        
        $jpu = $this->templatehelper->standradnaming($row->pemohon_nama);        
        $nip = $row->NIP;
        $tglpermohonan = $this->templatehelper->convertKeTglIndo($row->permohonan_banding);
    }
    if(empty($tglpermohonan)){
        $tglpermohonan = '..................';
        $haripermohonan = '.................';
    }else{
        $haripermohonan = $this->templatehelper->getHari($tglpermohonan);
    }
    if(empty($jpu)){
        $jpu = '..................';
    }
    if(empty($nip)){
        $nip = '..........................';
    }
    $Template = str_replace("#tglpermohonan#",''.$tglpermohonan.'',$Template);
    $Template = str_replace("#haripermohonan#",''.$haripermohonan.'',$Template);
    $Template = str_replace("#nip#",''.$nip.'',$Template);
    $Template = str_replace("#jaksa#",''.$jpu.'',$Template);
}
if($jenisAkta==12){
    $databanding = $cetakDataPUKontra;
    $jpu = '';
    $nip = '';
    foreach ($databanding->result() as $row) {        
        $jpu = $this->templatehelper->standradnaming($row->pihak_nama);        
        $nip = $row->NIP;        
    }    
    if(empty($jpu)){
        $jpu = '..................';
    }
    if(empty($nip)){
        $nip = '..........................';
    }    
    $Template = str_replace("#nip#",''.$nip.'',$Template);
    $Template = str_replace("#jaksa#",''.$jpu.'',$Template);
    $tglMemoriBanding=$cetakDataBanding;
    foreach ($tglMemoriBanding->result() as $row) {
        $tglmemori=$this->templatehelper->convertKeTglIndo($row->tglMemori);
    }
    if(empty($tglmemori)){
        $tglmemori='.................';
    }
    $Template = str_replace("#tanggalmemori#",''.$tglmemori.'',$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;

?>