<?php
$idperkara = intval($idperkara);

if(!is_numeric($idperkara)){ show_404(); }

$url = base_url("resources/template/berita_acara/pidana_umum/".$nama_file.".rtf");
$judulTemplate = $judul_cetak;
$Template = file_get_contents($url);

function in_multiarray($sval, $arr){
	$ret = false;
	foreach ($arr as $value) {
		if (is_array($value)){
			$ret = $ret | in_multiarray($sval, $value);
		}else{
			if ($value == $sval){
				return true;
			}
		}
	}
	return $ret;
}

$pemohon = array();
$termohon = array();
$i=0;
foreach ($dataPerkara->result() as $row) {
    if (!in_multiarray($row->namaP, $pemohon)){
    	$pemohonItem = array();
    	$pemohonItem['namaPemohon'] = $this->templatehelper->standradnaming($row->namaP);
    	$pemohonItem["alamatPemohon"] = $this->templatehelper->standradnaming($row->alamatP);
    	$pemohonItem['statusPemohon'] = $this->templatehelper->standradnaming($row->statusP);
    	$pemohon[] = $pemohonItem;
    }

    if (!in_multiarray($row->namaT, $termohon)){
    	$termohonItem = array();
    	$termohonItem['namaTermohon'] = $this->templatehelper->standradnaming($row->namaT);
    	$termohonItem["alamatTermohon"] = $this->templatehelper->standradnaming($row->alamatT);
    	$termohonItem['statusTermohon'] = $this->templatehelper->standradnaming($row->statusT);
    	$termohon[] = $termohonItem;
    }

    $nomorPerkara = $row->nomorPerkara;
	$tanggalPenetapanHakim = $this->templatehelper->convertKeTglIndo($row->tanggalPenetapanHakim);
	$tanggalPendaftaran = $this->templatehelper->convertKeTglIndo($row->tanggalPendaftaran);
	$namaPN =$this->templatehelper->standradnaming($row->namaPN);
	$alamatPN = $this->templatehelper->standradnaming($row->alamatPN);
	$zonaWaktu = $this->templatehelper->standradnaming($row->zonaWaktu);
	$kota     =$this->templatehelper->namaKota($row->namaPN);
	$tanggalSurat  = $this->templatehelper->convertKeTglIndo($row->tanggalSurat);
	$tanggalSidangNow = $this->templatehelper->convertKeTglIndo($row->tanggalSidang1);
	$hariSidangNow = $this->templatehelper->getHari($row->tanggalSidang1);
	$jamSidangNow = $row->jamSidang1;
	$tanggalSidangNext = $this->templatehelper->convertKeTglIndo($row->tanggalSidang2);
	$hariSidangNext = $this->templatehelper->getHari($row->tanggalSidang2);
	$jamSidangNext = $row->jamSidang2;
	$zonaWaktu = $row->zonaWaktu;
	$tanggalSidangNext = $this->templatehelper->convertKeTglIndo($row->tanggalSidang2);
	$hariSidangNext = $this->templatehelper->getHari($row->tanggalSidang2);
	$jamSidangNext = $row->jamSidang2;
	$agendaSidangNext = $row->agendaSidang2;
	$amarPutusan = $this->templatehelper->cleanHtmlTag($row->amarPutusan);
	$hariPutusan = $this->templatehelper->getHari($row->tanggalPutusan);
	$tanggalPutusan = $this->templatehelper->convertKeTglIndo($row->tanggalPutusan);
	$namaHakimTT   =$this->templatehelper->standradnaming($row->namaHakim1);
	$paniteraTT	= $this->templatehelper->standradnaming($row->namaPanitera1);
	$namaPemohonList	= $this->templatehelper->standradnaming($row->namaP);
	$namaTermohonList	= $this->templatehelper->standradnaming($row->namaT);
}

$cekPemohon = count($pemohon);
if($cekPemohon==1){
	$jenisPemohon = "Pemohon";
} else {
	$jenisPemohon = "Para Pemohon";
}

$cekTermohon = count($termohon);
if($cekTermohon==1){
	$jenisTermohon = "Termohon";
} else {
	$jenisTermohon = "Para Termohon";
}

$i = 1;
foreach ($pemohon as $item) {
	$naskah ="\b ".$this->templatehelper->standradnaming($item["namaPemohon"])." , \b0 ".$item["statusPemohon"]."\b  Pemohon ".$this->templatehelper->converToAngkaRomawi($i)."\b0 ;";
	$brnaskah = (($cekPemohon > 1)?($i):("")).' \tab '.$naskah.' \\par '.PHP_EOL.(($i<$cekPemohon)?("#pemohonParties#"):(""));
	$Template = str_replace("#pemohonParties#",''.$brnaskah.'',$Template);	
	$i++;
}

$x = 1;
foreach ($termohon as $item) {
	$naskah ="\b ".$this->templatehelper->standradnaming($item["namaTermohon"])." , \b0 ".$item["statusTermohon"]."\b  Termohon ".$this->templatehelper->converToAngkaRomawi($x)."\b0 ;";
	$brnaskah = (($cekTermohon > 1)?($x):("")).' \tab '.$naskah.' \\par '.PHP_EOL.(($x<$cekTermohon)?("#termohonParties#"):(""));
	$Template = str_replace("#termohonParties#",''.$brnaskah.'',$Template);	
	$x++;
}

$Template = str_replace('#nomorperkara#',''.$nomorPerkara.'',$Template);
$Template = str_replace('#tanggalpendaftaran#',''.$tanggalPendaftaran.'',$Template);
$Template = str_replace('#namapn#',''.$namaPN.'',$Template);
$Template = str_replace('#alamatpn#',''.$alamatPN.'',$Template);
$Template = str_replace('#tanggalsurat#',''.$tanggalSurat.'',$Template);
$Template = str_replace('#harisidang#',''.$hariSidangNow.'',$Template);
$Template = str_replace('#tanggalsidang#',''.$tanggalSidangNow.'',$Template);
$Template = str_replace('#jamsidang#',''.$jamSidangNow.'',$Template);
$Template = str_replace('#zonawaktu#',''.$zonaWaktu.'',$Template);
$Template = str_replace('#paniteraTT#',''.$paniteraTT.'',$Template);
$Template = str_replace('#hakimTT#',''.$namaHakimTT.'',$Template);
$Template = str_replace('#tanggalsidangnew#',''.$tanggalSidangNext.'',$Template);
$Template = str_replace('#harisidangnew#',''.$hariSidangNext.'',$Template);
$Template = str_replace('#jamsidangnew#',''.$jamSidangNext.'',$Template);
$Template = str_replace('#agendasidangnew#',''.$agendaSidangNext.'',$Template);
$Template = str_replace('#amarputusan#',''.$amarPutusan.'',$Template);
$Template = str_replace('#tanggalputusan#',''.$tanggalPutusan.'',$Template);
$Template = str_replace('#hariputusan#',''.$hariPutusan.'',$Template);
$Template = str_replace('#penetapanhakim#',''.$tanggalPenetapanHakim.'',$Template);
$Template = str_replace('#jenisPemohon#',''.$jenisPemohon.'',$Template);
$Template = str_replace('#jenisTermohon#',''.$jenisTermohon.'',$Template);
$Template = str_replace('#pemohonList#',''.$namaPemohonList.'',$Template);
$Template = str_replace('#termohonList#',''.$namaTermohonList.'',$Template);
$Template = str_replace('#amarPutusan#',''.$amarPutusan.'',$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=\"".$judulTemplate."-".$nomorPerkara.".rtf\"");
header("Content-length: ".strlen($Template));
echo $Template;
?>