<?php
if(count(get_included_files()) ==1) exit("Direct access not permitted.");
require_once('include/global.php');
if (!isset($_SESSION)) session_start();

if(isset($_GET['idsurat'])){
	$idsurat=$_GET['idsurat'];
	$db=pDatabase::getInstance();
	
	$wh="";
	$datapush=array($idsurat);
	if($_SESSION['profile']['level']<=10){
		$wh.="and t4.iduser=? ";
		array_push($datapush,$_SESSION['profile']['id']);
	}
	$query="select t1.`id`,
			t1.`tanggal_surat`,
			t1.tanggal_close,
			t1.`idpegawai`,
			t1.`iduser`,
			t1.`idkategori`,
			t1.`idjenissurat`,
			t1.idkategorijenis,
			t1.`datasurat`,
			t1.`iddisposisi`,
			t1.`keterangan`,
			t1.`status`,
			t1.`statusdisposisi`,
			t1.nomor_panjang as nomorsurat,
			t2.nama as jenissurat,
			if(t3.template is null,'',t3.template) as template,
			t4.nama_sekolah,
			t4.id as idsuratpkl,
			t4.nomor_surat_pengajuan as nomorsuratpengajuan,
			t5.nama as sifatsurat,
			if(t6.propinsi is null,'',t6.propinsi) as propinsi_sekolah,
			if(t7.kota is null,'',t7.kota) as kota_sekolah,
			t8.nama as namastatus
		from tb_suratbdk as t1
		inner join tb_master_jenissurat as t2 on t1.idjenissurat=t2.id	and t1.idkategori=t2.idkategori
		left join tb_master_template_cetak as t3 on t2.idtemplate=t3.id
		inner join tb_pengajuan_pkl as t4 on t1.id=t4.idsurat
		left join tb_master_kategorijenissurat as t5 on t1.idkategorijenis=t5.id
		left join tb_propinsi as t6 on t4.idpropinsi=t6.id
		left join tb_kota as t7 on t4.idkota=t7.id
		inner join tb_master_status as t8 on t1.status=t8.id
		where t1.id=? ".$wh;
	$rst=$db->loadAssoc($query,$datapush);
	
	if($_SESSION['profile']['level']<=10){
		if(($rst['status']!=68 && $rst['status']!=69) || $rst['statusdisposisi']!=1) exit;
	}
	
	
	$rst['modekertas']="portrait";
	$rst['lebarkertas']="100%";
	$rst['fontjudul1']="18px";
	$rst['fontjudul2']="16px";
	$rst['defaultfont']="15px";
	$rst['judulfont']="16px";
	$rst['fontjudul3']="16px";
	$rst['ukuranlogo']="width:70px;height:75px;";
	$rst['lebarkolom']="160px";
	$rst['jarakatas']="15px;";
	$rst['jarakisi']="25px;";
	
	$query="select * from tb_setting where id=3";
	$rsst=$db->loadAssoc($query);
	if(isset($rsst['id'])){
		$arrsett=json_decode($rsst['setting'],true);
		$rst['dataalamat']=$arrsett['alamat'];
		$rst['datatelp']=$arrsett['telp'];
		$rst['dataemail']=$arrsett['email'];
	}
	
	if(isset($arrsett['idlogo'])){
		$query="select * from tb_dokumen where id=?";
		$rsdok=$db->loadAssoc($query,array($arrsett['idlogo']));
		if(isset($rsdok['id'])){
			$rst['logo']=NPAGE.$rsdok['folder'].$rsdok['nama_baru'];
		}
	}
	
	$rst['tanggalsurat']=$db->fulldate($rst['tanggal_surat']);
	$rst['tanggalclose']=$db->fulldate($rst['tanggal_close']);
	
	$namakepala="().........................)";
	$nipkepala="";
	$query="select * from tb_pegawai where status=1 and idjabatanstruk=1";
	$rspeg=$db->loadAssoc($query);
	if(isset($rspeg['id'])){
		$namakepala=$rspeg['nama'];
		$nipkepala="NIP. ".$rspeg['nip'];
	}
	$rst['namakepala']=$namakepala;
	$rst['nipkepala']=$nipkepala;
	
	$query="select * from tb_pengajuan_pkl_peserta where idsuratpkl=? ";
	$rsps=$db->loadAll($query,array($rst['idsuratpkl']));
	$datapeserta="";
	if(sizeof($rsps)>0){
		ob_start();
		?>
		<table class="tabelpeserta">
			<tr>
				<td align="left">No</th>
				<td align="left">Nama</th>
				<td align="left">NIM/NIS</th>
			</tr>
		<?php
		$nomorurut=1;
		foreach ($rsps as $ops) {
			?>
			<tr>
				<td width="40px"><?php echo $nomorurut;?></td>
				<td width="120px"><?php echo $ops['nopelajar'];?></td>
				<td><?php echo $ops['nama'];?></td>
			</tr>
			<?php
			$nomorurut=$nomorurut+1;
		}
		?>
		</table>
		<?php
	}
	$datapeserta=ob_get_clean();
	$rst['datapeserta']=$datapeserta;
	
	$newline="";
	if(isset($rst['template'])) $newline=$rst['template'];
	
	$obj=(object) array("data"=>$newline);
	echo $db->renderdata($obj,$rst);
}
?>