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

if(isset($_GET['id'])){
	$page->addPlugin("pilihpegawai");
	$iduser=strip_tags(stripslashes($_GET['id']));
	$page->setJudul("Input/edit User login");
	$user="";
	$nama="";
	$level=0;
	
	$dis="";
	$namapegawai="";
	$nip="";
	$idpegawai=0;
	if($iduser>0){
		$dis="disabled=\"true\"";
		$page->setJudul("Edit User");
		$query="SELECT t1.id,t1.username,t1.level,t1.nama,
					t1.idpegawai,
					t2.nama as namalevel,
					if(t3.nama is null,'',t3.nama) as namapegawai,
					if(t3.nip is null,'',t3.nip) as nip
			 	FROM tb_username as t1
			 	inner join tb_level as t2 on t1.level=t2.id
			 	left join tb_pegawai as t3 on t1.idpegawai=t3.id
			WHERE t1.id=?";
		$rs=$db->loadAssoc($query,array($iduser));
		if(isset($rs['id'])){
			$user=$rs['username'];
			$nama=$rs['nama'];
			$level=$rs['level'];
			$namapegawai=$rs['namapegawai'];
			$nip=$rs['nip'];
			$idpegawai=$rs['idpegawai'];
		}else{
			exit();
		}
	}
	$ardata=array(
		"id"=>$iduser,
		"level"=>$level,
		"idpegawai"=>$idpegawai
	);
	$page->addLoad("usermanajemen.loadlevel(".json_encode($ardata,true).")");
	?>
	<div class="box_main_konten">
		<h3>USER LOGIN</h3>
		<div class="group_form">
			<title>Username</title>
			<div class="col10"><input <?php echo $dis;?> id="username" value="<?php echo $user;?>" /></div>
		</div>
		<div class="group_form">
			<title>Nama User</title>
			<div class="col10"><input id="nama" value="<?php echo $nama;?>"/></div>
		</div>
		<div class="group_form">
			<title>Level</title>
			<div class="col10">
				<select id="sl_level" onchange="usermanajemen.levelch()">
					<?php
					$qr="select * from tb_level where status=1";
					$rs=$db->getObj($qr);
					
					foreach ($rs as $obj) {
						?>
						<option pegawai="<?php echo $obj->pegawai;?>" value="<?php echo $obj->id;?>"><?php echo $obj->nama;?></option>
						<?php
					}
					?>
				</select>
			</div>
		</div>
		
		<div class="group_form" id="boxpass1">
			<title>Password</title>
			<div class="col10"><input type="password" id="pass1"/>
				
			</div>
		</div>
		<div class="group_form" id="boxpass2">
			<title>Ulang Password</title>
			<div class="col10"><input type="password" id="pass2"/>
				
			</div>
		</div>
		<div class="group_form" id="boxbtubahpass">
			<title> </title>
			<div class="col7">
				<span id="btubahpass" class="btmerah medium">Ubah Password</span> 
				<span id="btbatalubahpass" class="btmerah medium">Batal Ubah Password</span>
			</div>
		</div>
		<div style="clear: both;"></div>
	</div>
	<div class="box_main_konten" id="boxpegawai" style="display: none;">
		<h3>PEGAWAI</h3>
		<div class="group_form">
			<title>Nama Pegawai</title>
			<div class="col10">
				<input disabled="true" id="namapegawai" value="<?php echo $namapegawai;?>" />
				<div style="margin-top: 8px">
					<span class="bthijau medium" onclick="usermanajemen.pilihpegawai()">Pilih Pegawai</span>
				</div>
			</div>
		</div>
		<div class="group_form">
			<title>NIP</title>
			<div class="col10">
				<input disabled="true" id="nip" value="<?php echo $nip;?>" />
			</div>
		</div>
		<div style="clear: both;"></div>
	</div>
	<div>
		<div class="box_button_bawah" style="float: left">
			<div id="loader_simpan_user" class="error"></div>
			<span class="btmerah btncancel" onclick="usermanajemen.kembali()">Kembali</span>&nbsp;&nbsp;
			<span id="btbatalsimpan" style="display: none;" class="btkuning btncancel" onclick="usermanajemen.batalsimpan()">Reset</span>&nbsp;&nbsp;
			<span onclick="usermanajemen.simpan()" class="bthijau btnsave">Simpan</span>
		</div>
	</div>
	<?php
}else{
	$page->addLoad("usermanajemen.load()");
	$page->setJudul("Data user login");
	?>
	<div class="box_button_atas">
		Jumlah baris: <select id="jumlah_hal" class="slkecil" onchange="usermanajemen.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="usermanajemen.load()"><option value="1">1</option></select>
		&nbsp;<input placeholder="Default: Username" onkeypress="usermanajemen.enter(event)" id="cari" class="in_cari" />
		&nbsp;&nbsp;<span id="btcari" class="btnone btncari" onclick="usermanajemen.loade()">Cari</span>
		&nbsp;&nbsp<span class="btnone btncanggih" onclick="usermanajemen.caricanggih()">Cari Detail</span>
	</div>
	<div id="grid_usermanajemen"></div>
	<div id="loader_datauser" class="error"></div>
	<div class="box_button_bawah">
		<span class="bthijau btninput" onclick="usermanajemen.buat(0)">Buat user baru</span>
	</div>
	<div id="btedit" style="display: none;">
		<span class="icon_del" onclick="usermanajemen.hapus(#{id})"></span>&nbsp;
		<span class="icon_edit" style="margin-left: 3px;" onclick="usermanajemen.buat(#{id})"></span>
	</div>
	<div id="btdetailuser" style="display: none;">
		<div><b>Username</b></div>
		<div>#{username}</div>
		<div style="margin-top: 6px"><b>Nama User</b></div>
		<div>#{nama}</div>
		#(#{pegawai}==1)?'<div style=\"margin-top: 6px\"><b>Pegawai</b></div><div>#{namapegawai}</div>':''#
	</div>
	<?php
}
?>
<span id="btstatus" class="#(#{status}==1)?'mini_ok':'mini_dis'#" onclick="usermanajemen.status(#{id},##{jumlah_tabel},##{baris_tabel},this)"></span>
