<?php
	
	if(!isset($_SESSION['login_user'])){
		header('location: ../../../login.php'); // Mengarahkan ke Home Page
	}

	if(isset($_SESSION['user']) AND $_SESSION['user'] <> 'TRUE')
	{
		?>
		  <div class="alert alert-danger alert-dismissible" id="succsess-alert">
	        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
	        <h4><i class="icon fa fa-ban"></i> Alert!</h4>
	        Dilarang mengakses file ini.
	      </div>
		<?php
	}
	else{
	//link buat paging
	$linkaksi = 'med.php?mod=user';

	if(isset($_GET['act']))
	{
		$act = $_GET['act'];
		$linkaksi .= '&act='.$act;
	}
	else
	{
		$act = '';
	}

	$aksi = 'mod/data_master/user/act_user.php';

	?>
	<?php
	switch ($act) {
		case 'form':
			if(!empty($_GET['id']))
			{
				$act = "$aksi?mod=user&act=edit&idmx=".$_GET['idmx'];
				$query = mysqli_query($conn,"SELECT * FROM users WHERE user_id = '$_GET[id]'");
				$temukan = mysqli_num_rows($query);
				if($temukan > 0)
				{
					$c = mysqli_fetch_assoc($query);
					
					$ko = $c['id_identitas'];
					$tko = explode(",", $ko);
					 $jmko = count($tko);
				}
				else
				{
					header("location:med.php?mod=user&idmx=".$_GET['idmx']);
				}

			}
			else
			{
				$act = "$aksi?mod=user&act=simpan&idmx=".$_GET['idmx'];
			}
			?>
			<style>
			    select[readonly].select2+.select2-container {
                  pointer-events: none;
                  touch-action: none;
                }
                select[readonly]+.select2-container .select2-selection--single{
                    background-color: #e9ecef;
                }
			</style>
			<div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
				<div class="breadcrumb-title pe-3">Data Master</div>
				<div class="ps-3">
					<nav aria-label="breadcrumb">
						<ol class="breadcrumb mb-0 p-0">
							<li class="breadcrumb-item"><a href="javascript:;"><i class="bx bx-home-alt"></i></a>
							</li>
							<li class="breadcrumb-item active" aria-current="page">Form Data Users</li>
						</ol>
					</nav>
				</div>
			</div>
            <?php
	        flash('example_message');
	        ?>
            <div class="row">
                <div class="col-lg-12">
                    <div class="card ">
                        <div class="card-header bg-primary text-center">
                            <h4 class="m-b-0 text-white">Form Data Users</h4>
                        </div>

				    	<form class="form-horizontal" role="form" method='POST' action='<?php echo $act ?>' enctype="multipart/form-data">
					    	<div class="card-body">
					    		<input type="hidden" name="id" value="<?php echo isset($c['user_id']) ? $c['user_id'] : '' ;?>">
			            	<div class="row mb-3">
			                        <label for="inputEmail1" class="col-lg-2 col-sm-2 col-form-label">Perumahan</label>
			                        <div class="col-lg-10">
			                            <select multiple="multiple" name="toko[]" style="width:100%" class="select2" data-placeholder="Select Perumahan" <?php echo isset($_GET['st']) ? 'readonly' : '' ;?>>
			                                <?php
			                                    $sql_kondisi = mysqli_query($conn,"SELECT * FROM tb_identitas WHERE status='Aktif'");
			                                    $i=0;
			                                    while($k = mysqli_fetch_assoc($sql_kondisi))
			                                    {		
			                                      if($tko[$i] == $k['id'])
			                                      {
			                                        echo"<option value='$k[id]' selected>$k[nama]</option>";  
			                                      }
			                                      else
			                                      {
			                                        echo"<option value='$k[id]'>$k[nama]</option>";
			                                      }     	                                                  
			                                     $i++;
			                                    }
			                                ?>
			                            </select>
			                        </div>
			                    </div>
			                    <div class="row mb-3">
		                            <label for="inputEmail1" class="col-lg-2 col-sm-2 col-form-label">Username</label>
		                            <div class="col-lg-10">
		                                <input type="text" name="username" class="form-control " value="<?php echo isset($c['username']) ? $c['username'] : '' ;?>" required>
		                            </div>
		                        </div>

			                    <div class="row mb-3">
		                            <label for="inputEmail1" class="col-lg-2 col-sm-2 col-form-label">Password</label>
		                            <div class="col-lg-10">
		                                <input type="password" name="password" class="form-control " value="" >
		                            </div>
		                        </div>
			                    <div class="row mb-3">
		                            <label for="inputEmail1" class="col-lg-2 col-sm-2 col-form-label">Repeat Password</label>
		                            <div class="col-lg-10">
		                                <input type="password" name="rpassword" class="form-control" value="" >
		                            </div>
		                        </div>
			                    <div class="row mb-3">
		                          <label class="col-lg-2 col-sm-2 col-form-label">Group</label>
		                          <div class="col-lg-10">
		                              <select name="level" class="form-control select2" style="width: 100%" <?php echo isset($_GET['st']) ? 'readonly' : '' ;?>>
		                              	  <option value="<?php echo isset($c['level']) ? $c['level'] : 'Level' ;?>"><?php echo isset($c['level']) ? group($c['level']) : 'Level' ;?></option>
		                              	  <?php
		                              		$sql_kondisi = mysqli_query($conn,"SELECT * FROM `tb_group` WHERE `status` = 'Aktif'");
		                                    while($k = mysqli_fetch_assoc($sql_kondisi))
		                                    {		                                                 
		                                      if(isset($c['level']) && $k['kode_group'] == $c['level'])
		                                      {
		                                        echo"<option value='$k[kode_group]' selected>$k[nama]</option>";  
		                                      }
		                                      else
		                                      {
		                                        echo"<option value='$k[kode_group]'>$k[nama]</option>";
		                                      }
		                                      
		                                    }
		                                    ?>
		                              </select>
		                          	</div>
		                      	</div>
			                    <div class="row mb-3">
		                          <label class="col-lg-2 col-sm-2 col-form-label">Akun</label>
		                          <div class="col-lg-10">
		                              <select class="form-control select2" name="nama_lengkap" class="form-control round-input" style="width: 100%" <?php echo isset($_GET['st']) ? 'readonly' : '' ;?>>
		                              	<option>Pilih Akun</option>
		                                  <?php
												$sqlgrp = mysqli_query($conn,"SELECT * FROM `tb_group` WHERE `kode_group` = '$c[level]'");
		                                    	$group = mysqli_fetch_assoc($sqlgrp);
		                                    	
		                                    		$sql_akun = mysqli_query($conn,"SELECT `pegawai_id` as id,`pegawai_nama` as nama FROM `pegawai`");
		                                    	
		                                        while($k = mysqli_fetch_assoc($sql_akun))
		                                        {
		                                          if(isset($c['nama_lengkap']) && $k['id'] == $c['nama_lengkap'])
		                                          {
		                                            echo"<option value='$k[id]' selected>$k[nama]</option>";  
		                                          }
		                                          else
		                                          {
		                                            echo"<option value='$k[id]'>$k[nama]</option>";
		                                          }
		                                          
		                                        }
		                                    ?>	       
		                              </select>
		                          </div>
		                     	 </div>
			                    <div class="row mb-3">
		                            <label for="inputEmail1" class="col-lg-2 col-sm-2 col-form-label">Email</label>
		                            <div class="col-lg-10">
		                                <input type="text" name="email" class="form-control " value="<?php echo isset($c['email']) ? $c['email'] : '' ;?>" required>
		                            </div>
		                        </div>
					                            	
					                            	
			                    <div class="row mb-3">
	                                <label class="col-form-label col-md-2">No Tlp/hp</label>
	                                <div class="col-md-10">
	                                    
		                                <input type="text" name="no_telp" class="form-control phone" value="<?php echo isset($c['no_telp']) ? $c['no_telp'] : '' ;?>" required>
	                                </div>
	                            </div>
			                    <div class="row mb-3">
	                                <label for="inputEmail1" class="col-lg-2 col-sm-2 col-form-label">Foto</label>
	                                <div class="col-lg-10">
	                                	<input type="file" name="file" accept="image/jpeg" id="input-file-ttd" class="dropify" data-default-file="<?php echo isset($c['foto_user']) ? 'assets/images/img_user/'.$c['foto_user'] : '' ;?>" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>/>

	                                    
	                                </div>
	                            </div>
	                            <input type="hidden" name="file1" value="<?php echo $c['foto_user'] ?>" />
			                    <div class="row mb-3">
	                              <label class="col-lg-2 col-sm-2 col-form-label">Aktif</label>
	                              <div class="col-lg-10">
	                                  <select name="aktif" class="form-control" style="width: 100%">
	                                  	  <option value="<?php echo isset($c['aktif']) ? $c['aktif'] : 'Pilih Aktif' ;?>"><?php echo isset($c['aktif']) ? $c['aktif'] : 'Pilih Aktif' ;?></option>
	                                      <option value="Ya">Ya</option>
	                                      <option value="Tidak">Tidak</option>
	                                  </select>
	                              	</div>
	                          	</div>
				    	</div>
				    	<hr>
                            <div class="d-flex align-items-center justify-content-between">
					         	<button type="submit" name="submit" value="simpan" class="btn btn-success"><i class='fa fa-save'></i> Simpan</button>
								<button type="button" name="submit" onclick="history.back()" class="btn btn-dark"><i class='fa fa-rotate-left'></i> Kembali</button>
					        </div>
					        
				            <script type="text/javascript">
				            	function group(){
						   			var group = $("#idgroup").val();
							      	var jenis = 'group';
						          	$.ajax({
						          		type: 'GET',
						              	url: "mod/data_master/user/get_group.php",
						              	data: {group: group, jenis: jenis},
						              	cache: false,
						              	success: function(msg){
						                  $("#idakun").html(msg);
						                  //alert(kelompok);
						                }
						            });
						        };
				            	
				            </script>
					    </form>
                    </div>
                </div>
            </div>
				
			<?php
		break;

		default :
			?>
			  <!--breadcrumb-->
              <div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
                <div class="breadcrumb-title pe-3">Data Master</div>
                <div class="ps-3">
                  <nav aria-label="breadcrumb">
                    <ol class="breadcrumb mb-0 p-0">
                      <li class="breadcrumb-item"><a href="javascript:;"><i class="bx bx-home-alt"></i></a>
                      </li>
                      <li class="breadcrumb-item active" aria-current="page">Data Users</li>
                    </ol>
                  </nav>
                </div>
                <div class="ms-auto">
                    <?php
							$qmn = "SELECT * FROM tb_group_menu WHERE `kode_group` = '$level_check' AND id_menu='$_GET[idmx]'";
							$sqqmn = mysqli_query($conn,$qmn);	
							$mn = mysqli_fetch_assoc($sqqmn);
							if ($mn['insert']=="Aktif") {
							?>	
							<a href="med.php?mod=user&act=form&idmx=<?php echo $mn['id_menu'] ?>">
                            <button class="btn btn-primary px-4"><i class="bi bi-plus-lg me-2"></i>Add User</button>
							</a>
						<?php }?>
                </div>
              </div>
              <!--end breadcrumb-->
            <?php
	        flash('example_message');
	        ?>
        
              <div class="card mt-4">
                <div class="card-body">
                    <div class="table-responsive">
                        <table id="example" class="table table-striped table-hover align-middle">
				        			<thead>
				                        <tr>
											<th class="text-center">No</th>
											<th class="text-center">Nama</th>
											<th class="text-center">Username</th>
											<th class="text-center">Store</th>
											<th class="text-center">Email</th>
											<th class="text-center">No Telp</th>
											<th class="text-center">Level</th>
											<th class="text-center">Aktif</th>
		                                    <th class="text-center">Aksi</th>
										</tr>
				                    </thead>
				                    <tbody>
			                            <?php
											$query = "SELECT * FROM `users`";
											$sql_kul = mysqli_query($conn,$query);	
											$x=1;
											while ($m = mysqli_fetch_assoc($sql_kul)) {
												$ko = $m['id_identitas'];
												$tko = explode(",", $ko);
												$jmko = count($tko);									
												
												$sql_akun = mysqli_query($conn,"SELECT `pegawai_id` as id,`pegawai_nama` as nama FROM `pegawai` WHERE pegawai_id = '$m[nama_lengkap]'");
												$g = mysqli_fetch_assoc($sql_akun);
									    		$nama = $g['nama'];
												
										?>
			                            <tr class="">
			                                <td align="center"><?php echo $x ?></td>
			                                <td align="center"><?php echo $nama?></td>
			                                <td align="center"><?php echo $m['username']?></td>		                                    
			                                <td align="center">
			                                	<?php
			                                	for ($i=0; $i < $jmko; $i++) {
			                                			echo nama_toko($tko[$i]). ", ";
			                                		  
			                                	}
			                                	?>
			                                </td>
			                                <td align="center"><?php echo $m['email']?></td>
			                                <td align="center"><?php echo $m['no_telp']?></td>
			                                <td align="center"><?php echo group($m['level'])?></td>
			                                <td align="center">
			                                	<?php 
			                                	if ($m['aktif']=='Ya') {
			                                	?>
			                    					<span class="lable-table bg-success-subtle text-success rounded border border-success-subtle font-text2 fw-bold">Active<i class="bi bi-check2 ms-2"></i></span>
			                                	<?php
			                                	}else{
			                                	?>
			                    					<span class="lable-table bg-danger-subtle text-danger rounded border border-danger-subtle font-text2 fw-bold">Inactive<i class="bi bi-x-lg ms-2"></i></span>
			                                	<?php
			                                	}

			                                	?>
			                                		
			                                </td>
											<td align="center">
											   <div class="btn-group">
			                                   <!-- <a href="user_data.php?hal=user_tampil&id=<?php echo $data['0']?>"><button class="btn btn-success btn-sm"> <i class="fa fa-search"></i></button></a>							
			                               --> 
			                               <?php
											if ($mn['update']=="Aktif") {
											?>					
						                    	<a href="med.php?mod=user&act=form&id=<?php echo $m['user_id'] ?>&idmx=<?php echo $mn['id_menu'] ?>" class="btn btn-outline-success btn-sm" title="Edit Data">
						                    	<i class="material-icons-outlined">edit</i>
						                    	</a>
											<?php
											} 											
						                    ?>
										<?php
										if ($mn['delete']=="Aktif") {
										?>		
											<button type="button" class="btn btn-outline-danger btn-sm btn-hapus" data-url="<?php echo $aksi; ?>?mod=user&act=hapus&id=<?php echo $m['user_id']; ?>&idmx=<?php echo $mn['id_menu']; ?>" title="Hapus Data"><i class="material-icons-outlined">close</i></button>
										<?php
										} 											
						                    ?>
			                                    
												

											 </div>
											</td>
			                            </tr>
										
			                            <?php
												 $x++;
											 }
											?>
			                        </tbody>
							<tfoot>
								<tr>
									<th class="text-center">No</th>
									<th class="text-center">Nama</th>
									<th class="text-center">Username</th>
									<th class="text-center">Store</th>
									<th class="text-center">Email</th>
									<th class="text-center">No Telp</th>
									<th class="text-center">Level</th>
									<th class="text-center">Aktif</th>
									<?php if($mn['update']=="Aktif" OR $mn['view']=="Aktif" OR $mn['delete']=="Aktif"){?>
                  					<th class="text-center">Aksi</th>
                  					<?php } ?>
								</tr>
							</tfoot>
						</table>
						</div>
                </div>
              </div>
			<?php
		break;
	}

	}
?>
<script>
	$(function() {
        $("#file").change(function() {
            $("#message").empty(); // To remove the previous error message
            var file = this.files[0];
            var imagefile = file.type;
            var match= ["image/jpeg","image/png","image/jpg"];
            if(!((imagefile==match[0]) || (imagefile==match[1]) || (imagefile==match[2])))
            {
                $('#previewing').attr('src','noimage.png');
                $("#message").html("<p id='error'>Mohon Pilih File dengan benar</p>"+"<h4>Catatan</h4>"+"<span id='error_message'>Only jpeg, jpg and png Images type allowed</span>");
                return false;
            }else{
                var reader = new FileReader();
                reader.onload = imageIsLoaded;
                reader.readAsDataURL(this.files[0]);
            }
        });
    });
    function imageIsLoaded(e) {
        $("#file").css("color","#FFFFFF");
        $('#image_preview').css("display", "block");
        $('#previewing').attr('src', e.target.result);
        $('#previewing').attr('width', '250px');
        $('#previewing').attr('height', '230px');
    };													
function myFunction() {
  var msg;
	msg= "Apakah Anda Yakin Akan Menghapus Data ? " ;
	var agree=confirm(msg);
	if (agree)
	return true ;
	else
	return false ;
}
</script>

