<?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']['pegawai']==1 && !isset($_GET['prev'])){
		$wh.="and t5.idpegawai=? ";
		array_push($datapush,$_SESSION['profile']['idpegawai']);
	}
	$query="select t1.`id`,
			t1.`tanggal_surat`,
			t1.`idpegawai`,
			t1.`iduser`,
			t1.`idkategori`,
			t1.`idjenissurat`,
			t1.`datasurat`,
			t1.`iddisposisi`,
			t1.`keterangan`,
			t1.`status`,
			t1.`statusdisposisi`,
			t1.`statusdisposisi`,
			t1.nomor_panjang as nomorsurat,
			if(t3.template is null,'',t3.template) as template,
			t4.id as idsurattugas,
			t4.`lokasi`,
			t4.tugas,
			t4.tanggal_mulai,
			t4.tanggal_selesai
		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
		left join tb_surat_tugas as t4 on t1.id=t4.idsurat
		left join tb_surat_tugas_pegawai as t5 on t4.id=t5.idsurattugas
		where t1.id=? ".$wh;
	$rst=$db->loadAssoc($query,$datapush);
	
	if(!isset($_GET['prev'])){
		if(isset($rst['iduser'])){
			if($rst['iduser']==$_SESSION['profile']['iduser']){
				if($rst['status']!=28 || $rst['statusdisposisi']!=1) exit;
			}
		}
	}
	
	
	$rst['modekertas']="portrait";
	$rst['lebarkertas']="100%";
	$rst['fontjudul1']="18px";
	$rst['fontjudul2']="16px";
	$rst['defaultfont']="14px";
	$rst['judulfont']="16px";
	$rst['fontjudul3']="14px";
	$rst['ukuranlogo']="width:70px;height:75px;";
	$rst['lebarkolom']="180px";
	$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['datatanggalsurat']=$db->fulldate($rst['tanggal_surat']);
	
	$rst['harimulaitugas']=$db->gethari($rst['tanggal_mulai']);
	$rst['harisampaitugas']=$db->gethari($rst['tanggal_selesai']);
	
	$rst['tglmulaitugas']=$db->fulldate($rst['tanggal_mulai']);
	$rst['tglsampaitugas']=$db->fulldate($rst['tanggal_selesai']);
	

	$query="select catatan from tb_surat_tugas_menimbang where idsurattugas=? ";
	$rsm=$db->loadAll($query,array($rst['idsurattugas']));
	
	ob_start();
	foreach ($rsm as $obm) {
		?>
		<li><?php echo $obm['catatan'];?></li>
		<?php
	}
	$datamenimbang=ob_get_clean();
	$rst['datamenimbang']=$datamenimbang;
	
	$query="select catatan from tb_surat_tugas_dasar where idsurattugas=? ";
	$rsd=$db->loadAll($query,array($rst['idsurattugas']));
	
	ob_start();
	foreach ($rsd as $obd) {
		?>
		<li><?php echo $obd['catatan'];?></li>
		<?php
	}
	$datadasar=ob_get_clean();
	$rst['datadasar']=$datadasar;
	
	$query="select t2.nama,t2.nip,
			 t3.nama as golongan,
			 t4.nama as jabatan
			from tb_surat_tugas_pegawai as t1
		inner join tb_pegawai as t2 on t1.idpegawai=t2.id
		left join tb_master_golongan as t3 on t2.idgolongan=t3.id
		left join tb_master_jabatan as t4 on t2.idjabatanstruk=t4.id
		where t1.idsurattugas=? ";
	$rspegawai=$db->loadAll($query,array($rst['idsurattugas']));
	
	ob_start();
	?>
	<table class="tabeldata">
		<tr>
			<th>No</th>
			<th>Nama</th>
			<th>Nip</th>
			<th>Pangkat/golongan</th>
			<th>Jabatan</th>
			<th>Tugas</th>
		</tr>
	<?php
	$nomor=1;
	foreach ($rspegawai as $obp) {
		?>
		<tr align="center">
			<td><?php echo $nomor;?></td>
			<td><b><?php echo $obp['nama'];?></b></td>
			<td><?php echo $obp['nip'];?></td>
			<td><?php echo $obp['golongan'];?></td>
			<td><?php echo $obp['jabatan'];?></td>
			<td><?php echo $rst['tugas'];?></td>
		</tr>
		<?php
	}
	?>
	</table>
	<?php
	$datapegawai=ob_get_clean();
	$rst['datapegawai']=$datapegawai;
	$newline="";
	if(isset($rst['template'])) $newline=$rst['template'];
	
	$obj=(object) array("data"=>$newline);
	echo $db->renderdata($obj,$rst);
}
?>