<?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 t1.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`,
			if(t3.template is null,'',t3.template) as template,
			t4.masa_kerja,
			t4.idjeniscuti,
			t4.keperluan,
			t4.waktu,
			t4.alamat_cuti as alamatcuti,
			t4.telp_cuti as telpcuti,
			t4.tanggal_mulai,
			t4.tanggal_selesai,
			t5.nama as namapemohon,
			t5.nip as nippemohon,
			t6.nama as jabatanpemohon,
			t7.nama as namawaktu
		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_cuti_pegawai as t4 on t1.id=t4.idsurat
		inner join tb_pegawai as t5 on t4.idpegawai=t5.id
		left join tb_master_jabatan as t6 on t5.idjabatanstruk=t6.id
		inner join tb_kodewaktu as t7 on t4.idkodewaktu=t7.id
		where t1.id=? ".$wh;
	$rst=$db->loadAssoc($query,$datapush);
	
	if(isset($rst['idpegawai'])){
		if($rst['idpegawai']==$_SESSION['profile']['idpegawai'] && $_SESSION['profile']['pegawai']==1 && !isset($_GET['prev'])){
			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['masakerjapemohon']=$rst['masa_kerja']." Tahun";
	
	for($i=0;$i<6;$i++){
		$rst['centang'.($i+1)]="";
		if(($i+1)==$rst['idjeniscuti']) {
			ob_start();
			?>
			<div align="center" style="position: relative;">
				<div style="width: 28px;">
					<img style="width: 100%" src="<?php echo NPAGE.'plugins/plg_cetak/css/centangtr.png';?>" />
				</div>
			</div>
			<?php
			$rst['centang'.($i+1)]=ob_get_clean();
		}
	}
	$rst['selama']=$rst['waktu']." ".$rst['namawaktu'];
	$rst['tglmulaicuti']=$db->fulldate($rst['tanggal_mulai']);
	$rst['tglsampaicuti']=$db->fulldate($rst['tanggal_selesai']);
	
	$query="select t1.id,
			t1.idpegawaidis,
			t1.modejabatan,
			t1.idjabatan,
			t1.keterangan,
			t1.idstatus,
			t1.tanggal_close ,
			t2.nama as namapegawai,
			t2.nip as nippegawai
		from tb_suratbdk_disposisi as t1
		inner join tb_pegawai as t2 on t1.idpegawaidis=t2.id
		where t1.idsurat=? order by t1.id asc ";
	$rsdis=$db->loadAll($query,array($idsurat));
	
	$datadis="";
	if(sizeof($rsdis)<=0){
		ob_start();
		?>
		<table class="tabeldata" style="margin-top: 10px;">
			<tr>
				<td colspan="5" style="text-transform: uppercase">Vii. pertimbangan atasan Langsung **</td>
			</tr>
			<tr>
				<td width="24%">DISETUJUI</td>
				<td width="24%">PERUBAHAN ****</td>
				<td width="24%">DITANGGUHKAN ****</td>
				<td width="24%">TIDAK DISETUJUI ****</td>
			</tr>
			<tr>
				<td valign="top" height="50px"></td>
				<td valign="top"></td>
				<td valign="top"></td>
				<td valign="top"></td>
			</tr>
		</table>
		<div style="clear: both"></div>
		<div align="center" style="float: right; margin-top: 10px;">
			<div>Atasan Langsung, </div>
			<div style="height: 40px;"></div>
			<div>(...............................)</div>
			<div></div>
		</div>
		<br />
		<table class="tabeldata" style="margin-top: 10px;">
			<tr>
				<td colspan="5" style="text-transform: uppercase">Viii. Keputusan Pejabat yang Berwenang Memberikan Cuti **</td>
			</tr>
			<tr>
				<td width="24%">DISETUJUI</td>
				<td width="24%">PERUBAHAN ****</td>
				<td width="24%">DITANGGUHKAN ****</td>
				<td width="24%">TIDAK DISETUJUI ****</td>
			</tr>
			<tr>
				<td valign="top" height="50px"></td>
				<td valign="top"></td>
				<td valign="top"></td>
				<td valign="top"></td>
			</tr>
		</table>
		<div style="clear: both"></div>
		<div align="center" style="float: right; margin-top: 10px;">
			<div>Pejabat berwenang, </div>
			<div style="height: 45px;"></div>
			<div>(...............................)</div>
			<div></div>
		</div>
		<?php
		$datadis=ob_get_clean();
	}else{
		$datadis="";
		foreach ($rsdis as $obd) {
			$namajabatan="";
			if($obd['modejabatan']==1){
				$query="select * from tb_master_jabatan where id=? ";
				$rsjab=$db->loadAssoc($query,array($obd['idjabatan']));
				if(isset($rsjab['id'])) $namajabatan=$rsjab['nama'];
			}else if($obd['modejabatan']==2){
				$query="select * from tb_master_jabatan_fungsional where id=? ";
				$rsjab=$db->loadAssoc($query,array($obd['idjabatan']));
				if(isset($rsjab['id'])) $namajabatan=$rsjab['nama'];
			}
			$tanggalstatus=$db->fulldate($obd['tanggal_close']);
			
			$datastatus=array(
				"28"=>"",
				"22"=>"",
				"26"=>"",
				"29"=>"",
			);
			
			$jabatandis="";
			$pegawaidis=$obd['namapegawai'];
			$nipdis="";
			if(strlen($obd['nippegawai'])>2) $nipdis="NIP. ".$obd['nippegawai'];
			ob_start();
			?>
			<div align="center" style="box-sizing: border-box;position: relative;">
				<div style="width:28px;height:30px;margin-top: -13px; ">
					<img style="width: 100%" src="<?php echo NPAGE.'plugins/plg_cetak/css/centangtr.png';?>" />
				</div>
				<div style="position: absolute;width: 100%;height: 100%;left: 0px;top: 25px;text-align: center;">
					<?php echo $obd['keterangan'];?>
				</div>
			</div>
			<?php
			$datastatus[$obd['idstatus']]=ob_get_clean();
			ob_start();
			?>
			<table class="tabeldata" style="margin-top: 10px;">
				<tr>
					<td colspan="5" style="text-transform: uppercase">Vii. pertimbangan <?php echo $namajabatan;?> **</td>
				</tr>
				<tr>
					<td width="24%">DISETUJUI</td>
					<td width="24%">PERUBAHAN ****</td>
					<td width="24%">DITANGGUHKAN ****</td>
					<td width="24%">TIDAK DISETUJUI ****</td>
				</tr>
				<tr>
					<td valign="top" height="50px"><?php echo $datastatus['28'];?></td>
					<td valign="top"><?php echo $datastatus['22'];?></td>
					<td valign="top"><?php echo $datastatus['26'];?></td>
					<td valign="top"><?php echo $datastatus['29'];?></td>
				</tr>
			</table>
			<div style="clear: both"></div>
			<div align="center" style="float: right; margin-top: 10px;">
				<div><?php echo $tanggalstatus;?>, </div>
				<div><?php echo $namajabatan;?>, </div>
				<div style="height: 40px;"></div>
				<div>(<?php echo $pegawaidis;?>)</div>
				<div><?php echo $nipdis;?></div>
			</div>
			<?php
			$obdis=ob_get_clean();
			$datadis.=$obdis;
		}
	}
	
	$rst['datastatus']=$datadis;
	
	$newline="";
	if(isset($rst['template'])) $newline=$rst['template'];
	
	$obj=(object) array("data"=>$newline);
	echo $db->renderdata($obj,$rst);
}
?>