<?php
if(count(get_included_files()) ==1) exit("Direct access not permitted.");
if(!$statuslogin) exit;
$mod = basename(__FILE__, '.php');
$page->tbJs($mod);
$page->addPlugin(BS_JS."cetak");

if(isset($_GET['id'])){
	$page->addPlugin("upload");
	$page->addPlugin("pilihstatus");
	$page->addJs(BS_JS."reqcb.js");
	$id=strip_tags(stripslashes($_GET['id']));
	$page->setJudul("Input Data Surat Masuk Baru");
	$pengirim="";
	$keterangan="";
	$nomorsurat="";
	$tanggalsurat="";
	$tanggalterima="";
	$alamatpengirim="";
	$keperluan="";
	$tujuan="";
	
	$idkategori=0;
	$idjenissurat=0;
	$idkategorijenissurat=0;
	
	$namastatus="";
	$bg="";
	$warna="";
	
	$namalib="";
	$nomorsurat="";
	$editstatus=1;
	$statusdisposisi=0;
	$idsurat=0;
	$jenissurat="";
	$namalib="";
	
	if($id>0){
		$page->setJudul("Edit Data Surat Masuk");
		$whp="";
		$datapushp=array($_SESSION['profile']['level'],$_SESSION['profile']['level'],$id);
		
		$query="SELECT t1.`id`,
				t1.`tanggal_input`,
				t1.`tanggal_surat`,
				t1.`idkategori`,
				t1.`idjenissurat`,
				t1.idkategorijenissurat,
				t1.`idsurat`,
				t1.`iduser`,
				t1.`nomor`,
				t1.`tanggal_terima`,
				t1.`pengirim`,
				t1.`alamat_pengirim`,
				t1.`keperluan`,
				t1.`tujuan`,
				t1.`keterangan`,
				t1.`jumlah_dokumen`,
				t2.`jumlah_catatan`,
				t2.`status`,
				t2.statusdisposisi,
				t2.nomor_panjang as nomorsurat,
				t4.lib,
				t4.nama as jenissurat,
				a1.nama as namastatus,
				a1.bg,a1.warna,
				if(b1.id is null,0,1) as edit,
				if(b2.id is null,0,1) as hapus
			FROM tb_surat_masuk as t1
			inner join tb_suratbdk as t2 on t1.idsurat=t2.id
			inner join tb_master_kategorisurat as t3 on t1.idkategori=t3.id
			inner join tb_master_jenissurat as t4 on t1.idjenissurat=t4.id
			inner join tb_master_status as a1 on t2.status=a1.id
			left join tb_master_status_level as b1 on t2.status=b1.idstatus and b1.mode=0 and b1.idlevel=?
			left join tb_master_status_level as b2 on t2.status=b2.idstatus and b2.mode=1 and b2.idlevel=?
			WHERE t1.id=? ".$whp;
		$rs=$db->loadAssoc($query,$datapushp);
		if(isset($rs['id'])){
			$idsurat=$rs['idsurat'];
			$keterangan=$rs['keterangan'];
			$pengirim=$rs['pengirim'];
			$nomorsurat=$rs['nomor'];
			$tanggalsurat=$db->sql_date($rs['tanggal_surat']);
			$tanggalterima=$db->sql_date($rs['tanggal_terima']);
			$alamatpengirim=$rs['alamat_pengirim'];
			$keperluan=$rs['keperluan'];
			$tujuan=$rs['tujuan'];
			$idkategori=$rs['idkategori'];
			$idjenissurat=$rs['idjenissurat'];
			$idkategorijenissurat=$rs['idkategorijenissurat'];
			
			$editstatus=$rs['edit'];
			$statusdisposisi=$rs['statusdisposisi'];
			$namastatus=$rs['namastatus'];
			$bg=$rs['bg'];
			$warna=$rs['warna'];
			$namalib=$rs['lib'];
		}else{
			exit;
		}
	}
	$datahasil=array(
		"id"=>$id,
		"idjenissurat"=>$idjenissurat,
		"idkategorijenissurat"=>$idkategorijenissurat,
		"idkategori"=>$idkategori,
		"editstatus"=>$editstatus,
		"statusdisposisi"=>$statusdisposisi,
		"idsurat"=>$idsurat,
		"lib"=>$namalib,
	);
	$page->addLoad("suratmasuk.edit(".json_encode($datahasil,true).")");
	
	$disable="disabled='true'";
	$display="style='display:none;'";
	$classdis="class='input_dis'";
	if($editstatus==1){
		$disable="";
		$display="";
		$classdis="";
	}
	
	?>
	<div class="box_main_konten">
		<h3>DATA SURAT MASUK</h3>
		<div class="group_form">
			<title>Kategori Surat</title>
			<div class="col10">
				<select <?php echo $disable;?> id="sl_kategori" onchange="suratmasuk.katch()">
				<?php
				$query="select * from tb_master_kategorisurat where id=2 order by id desc";
				$rskat=$db->loadAll($query);
				foreach ($rskat as $obk) {
					?>
					<option value="<?php echo $obk['id'];?>"><?php echo $obk['nama'];?></option>
					<?php
				}
				?>
				</select>
			</div>
		</div>
		<div class="group_form">
			<title>Jenis Surat</title>
			<div class="col10">
				<select onchange="suratmasuk.getdisposisi()" <?php echo $disable;?> id="sl_jenis"></select>
			</div>
		</div>
		<div class="group_form">
			<title>Nomor Surat</title>
			<div class="col10">
				<input id="nomor" <?php echo $disable;?> value="<?php echo $nomorsurat;?>" />
			</div>
		</div>
		<div class="group_form">
			<title>Tanggal Surat</title>
			<div class="col10">
				<input style="width: 110px" id="tanggalsurat" <?php echo $disable;?> value="<?php echo $tanggalsurat;?>" />
			</div>
		</div>
		<div class="group_form">
			<title>Tanggal Terima</title>
			<div class="col10">
				<input style="width: 110px" id="tanggalterima" <?php echo $disable;?> value="<?php echo $tanggalterima;?>" />
			</div>
		</div>
		<div class="group_form">
			<title>Pengirim Surat</title>
			<div class="col10">
				<input id="pengirim" <?php echo $disable;?> value="<?php echo $pengirim;?>" />
			</div>
		</div>
		<div class="group_form">
			<title>Alamat Pengirim</title>
			<div class="col10">
				<input id="alamatpengirim" <?php echo $disable;?> value="<?php echo $alamatpengirim;?>" />
			</div>
		</div>
		<div class="group_form">
			<title>Tujuan/Kepada Surat</title>
			<div class="col10">
				<input id="tujuan" <?php echo $disable;?> value="<?php echo $tujuan;?>" />
			</div>
		</div>
		<div class="group_form">
			<title>Keperluan</title>
			<div class="col10">
				<input id="keperluan" <?php echo $disable;?> value="<?php echo $keperluan;?>" />
			</div>
		</div>
		<div class="group_form">
			<title>Keterangan tambahan (jika ada)</title>
			<div class="col10">
				<input id="keterangan" <?php echo $disable;?> value="<?php echo $keterangan;?>" />
			</div>
		</div>
		<div class="group_form">
			<title>Kategori Jenis Surat</title>
			<div class="col10">
				<select id="sl_kategorijenis" <?php echo $disable;?>>
					<?php
					$query="select * from tb_master_kategorijenissurat where status=1 order by id asc";
					$rsjk=$db->loadAll($query);
					foreach ($rsjk as $objk) {
						?>
						<option value="<?php echo $objk['id'];?>"><?php echo $objk['nama'];?></option>
						<?php
					}
					?>
				</select>
			</div>
		</div>
		<div style="clear: both;"></div>
	</div>
	<div class="box_main_konten">
		<h3>DOKUMEN SURAT</h3>
		<div id="grid_dokumen"></div>
		<?php
		if($editstatus==1){
			?>
			<div class="box_button_bawah" style="margin-top: 10px;">
				<span onclick="suratmasuk.inputdokumen()" class="bthijau medium">Upload Dokumen</span>&nbsp;&nbsp;
			</div>
			<?php
		}
		?>
	</div>
	<div id="loader_disposisi"></div>
	<div class="box_main_konten" id="boxdisposisi" style="display: none;">
		<h3>DISPOSISI</h3>
		<div id="grid_disposisi"></div>
		
	</div>
	<div class="box_main_konten">
		<h3>CATATAN SURAT</h3>
		<div id="grid_catatan"></div>
		<div style="clear: both;"></div>
	</div>
	<div class="box_button_bawah">
		<div id="loader_suratmasuk" class="error"></div>
		<span class="btmerah btncancel" onclick="suratmasuk.kembali()">Kembali</span>&nbsp;&nbsp;
		<?php
		if($editstatus==1){
			?>
			<span onclick="suratmasuk.simpan(0)" class="btbiru btnsave">Simpan draft</span>&nbsp;&nbsp;
			<span onclick="suratmasuk.simpan(1)" class="bthijau btnsave">Submit Surat Masuk</span>
			<?php
		}
		?>
	</div>
	
	<div id="bteditdokumen" style="display: none;">
		<span class="#('#{edit}'=='1' && '#{statusdisposisi}'=='0')?'icon_del':''#" onclick="suratmasuk.hapusdokumen(##{jumlah_tabel},##{baris_tabel},this)"></span>&nbsp;
	</div>
	<span id="btdokumen" class="dok_ya" onclick="suratmasuk.lihatdokumen(#{iddokumen})" style="display: none;"></span>
	<div id="box_upload_dok" style="display: none">
		<div>
			<div class="group_form">
				<title>Nama Dokumen</title>
				<div class="col10">
					<input id="namadokumen" />
				</div>
			</div>
			<div class="group_form">
				<title>Upload Dokumen</title>
				<div class="col10">
					<div id="upload_dokumen"></div>
				</div>
			</div>
			<div class="group_form">
				<title>&nbsp;</title>
				<div class="col10">
					<div class="box_button_bawah">
						<div id="loader_upload" class="error"></div>
						<div>
							<span onclick="suratmasuk.batalsimpan()" class="btbiru btnsave">Batal</span>&nbsp;&nbsp;
							<span onclick="suratmasuk.simpandokumen()" class="bthijau btnsave">Simpan</span>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div id="bteditnamadisposisi" style="display: none;">
		<span class="#('#{editstatus}'=='1')?'icon_del':''#" onclick="suratmasuk.hapusdisposisi(##{jumlah_tabel},##{baris_tabel},this)"></span>&nbsp;
	</div>
	<div id="bteditjabatan" style="display: none;">
		<span class="#('#{editstatus}'=='1')?'icon_del':''#" onclick="suratmasuk.hapusdisposisijabatan(##{jumlah_tabel},##{baris_tabel},this)"></span>&nbsp;
	</div>
	<div id="btjumlahjabatan" onclick="suratmasuk.bukajabatan(##{jumlah_tabel},##{baris_tabel},this)" style="display: none" class="bthijau medium">#{jumlah_jabatan}</div>
	<div id="btmodejabatan" style="display: none">#(#{modejabatan}=='1')?'Struktural':((#{modejabatan}=='2')?'Fungsional':'')#</div>
	<div id="btjabatan" style="display: none;">#(#{modejabatan}==1)?'#{jabatanstruk}':((#{modejabatan}=='2')?'#{jabatanfung}':'')#</div>
	<div id="btviewjab" onclick="suratmasuk.setviewjab(##{jumlah_tabel},##{baris_tabel},this)" style="display: none;" class="#('#{view}'=='1')?'mini_ok':'mini_dis'#"></div>
	<div id="bteditjab" onclick="suratmasuk.seteditjab(##{jumlah_tabel},##{baris_tabel},this)" style="display: none;" class="#('#{edit}'=='1')?'mini_ok':'mini_dis'#"></div>
	<div id="bthapusjab" onclick="suratmasuk.sethapusjab(##{jumlah_tabel},##{baris_tabel},this)" style="display: none;" class="#('#{hapus}'=='1')?'mini_ok':'mini_dis'#"></div>
	<div id="box_namadisposisi" style="display:none;padding:10px">
		<div style="padding: 10px;">
			<div class="group_form">
				<title>Urutan</title>
				<div class="col10">
					<input id="urutan" />
				</div>
			</div>
			<div class="group_form">
				<title>Nama Disposisi</title>
				<div class="col10">
					<input id="namadisposisi" />
				</div>
			</div>
			
			<div class="group_form">
				<title>&nbsp;</title>
				<div class="col10">
					<div class="box_button_bawah">
						<div class="err" id="loader_namadisposisi"></div>
						<span class="btmerah" onclick="suratmasuk.batalnamadatadis()">BATAL</span>&nbsp;&nbsp;
						<span class="bthijau" onclick="suratmasuk.simpannamadatadis()">SIMPAN</span>
					</div>
				</div>
			</div>
			<div style="clear: both;"></div>
		</div>
	</div>
	<div id="box_jabatandisposisi" style="display:none;padding:10px">
		<div style="padding: 10px;">
			<div class="group_form">
				<title>Mode Jabatan</title>
				<div class="col10">
					<select id="sl_modejabatan" onchange="suratmasuk.getjabatan(0)">
						<option value="1">Struktural</option>
						<option value="2">Fungsional</option>
					</select>
				</div>
			</div>
			<div class="group_form">
				<title id="ttlnamajabatan">Jabatan</title>
				<div class="col10">
					<select id="sl_jabatan"></select>
				</div>
			</div>
			<div class="group_form">
				<title>&nbsp;</title>
				<div class="col10">
					<div class="box_button_bawah">
						<div class="err" id="loader_jabatan"></div>
						<span class="btmerah" onclick="suratmasuk.bataldatadisjbt()">BATAL</span>&nbsp;&nbsp;
						<span class="bthijau" onclick="suratmasuk.inputdatadisjbt()">SIMPAN</span>
					</div>
				</div>
			</div>
			<div style="clear: both;"></div>
		</div>
	</div>
	<div id="btjumlahstatus" onclick="suratmasuk.bukastatus(##{jumlah_tabel},##{baris_tabel},this)" style="display: none" class="bthijau medium">#{jumlah_status}</div>
	<div id="bteditstatus" style="display: none;">
		<span class="#('#{editstatus}'=='1')?'icon_del':''#" onclick="suratmasuk.hapusstatusjabatan(##{jumlah_tabel},##{baris_tabel},this)"></span>&nbsp;
	</div>
	<?php
}else{
	$page->addLoad("suratmasuk.load()");
	$page->setJudul("Data Surat Masuk");
	?>
	<div class="box_button_atas">
		Jumlah baris: <select id="jumlah_hal" class="slkecil" onchange="suratmasuk.loade()">
			<option value="30">30</option>
			<option value="50">50</option>
			<option value="100">100</option>
		</select> &nbsp;
		Hal: <select id="hal_ke" class="slkecil" onchange="suratmasuk.load()"><option value="1">1</option></select>
		&nbsp;<input placeholder="Default: Nama Pengirim" onkeypress="suratmasuk.enter(event)" id="cari" class="in_cari" />
		&nbsp;&nbsp;<span id="btcari" class="btnone btncari" onclick="suratmasuk.loade()">Cari</span>
		&nbsp;&nbsp;<span class="btnone btncanggih" onclick="suratmasuk.caricanggih()">Cari Detail</span>
	</div>
	<div id="grid_suratmasuk"></div>
	<div id="loader_datasuratmasuk" class="error"></div>
	<div class="box_button_bawah">
		<span onclick="suratmasuk.buat(0)" class="bthijau btninput">Buat Surat Masuk Baru</span>
	</div>
	<div id="btedit" style="display:none;">
		<span class="#(#{hapus}==1)?'icon_del':''#" onclick="suratmasuk.hapus(#{id})"></span>
		<span class="#(#{edit}==1)?'icon_edit':'btlihat'#" onclick="suratmasuk.buat(#{id})"></span>
	</div>
	<span id="btstatus" style="display: none;cursor: pointer;background: #{bg}" class="medium">
		<span style="color: #{warna}">#{namastatus}</span>
	</span>
	<div id="btdetailsurat" style="display: none;">
		<div><b>#{pengirim}</b></div>
		<div>#{keperluan}</div>
	</div>
	<div id="btwaktu" style="display: none">
		<div>#{tanggal_mulai}</div>
		<div>s/d</div>
		<div>#{tanggal_selesai}</div>
	</div>
	<span id="btcatatan" onclick="suratmasuk.lihatcatatan(#{id},##{jumlah_tabel},##{baris_tabel},this)" style="display: none" class="datajumlah">#{jumlah_catatan}</span>
	<span id="btcetak" onclick="suratmasuk.initcetak(#{id},##{jumlah_tabel},##{baris_tabel},this)" class="#('#{status}'=='28' && '#{statusdisposisi}'=='1')?'btcetak':''#" style="display: none;"></span>
	<?php
}
?>
