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

	if(isset($_SESSION['pegawai']) AND $_SESSION['pegawai'] <> '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=pegawai';

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

	$aksi = 'mod/data_master/pegawai/act_pegawai.php';

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

			}
			else
			{
				$act = "$aksi?mod=pegawai&act=simpan&idmx=".$_GET['idmx'];
			}
			?>
            <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 <?php echo ucwords($title) ?></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 <?php echo ucwords($title) ?></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['pegawai_id']) ? $c['pegawai_id'] : '' ;?>">

			                    <div class="row mb-3">
			                        <label for="inputEmail1" class="col-lg-2 col-sm-2 control-label">Nama Pegawai</label>
			                        <div class="col-lg-10">
			                            <input type="text" name="pegawai_nama" class="form-control " value="<?php echo isset($c['pegawai_nama']) ? $c['pegawai_nama'] : '' ;?>" required <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
			                        </div>
			                    </div>

			                    <div class="row mb-3">
			                        <label for="inputEmail1" class="col-lg-2 col-sm-2 control-label">No KTP</label>
			                        <div class="col-lg-10">
			                            <input type="text" name="pegawai_nomor" class="form-control " value="<?php echo isset($c['pegawai_nomor']) ? $c['pegawai_nomor'] : '' ;?>" required <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
			                        </div>
			                    </div>
			                    <div class="row mb-3">
			                        <label for="inputEmail1" class="col-lg-2 col-sm-2 control-label">Jabatan</label>
			                        <div class="col-lg-10">
			                            <select name="pegawai_jabatan" class="form-select select2" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
                                        	<?php
                                        		$sql_kondisi = mysqli_query($conn,"SELECT * FROM tb_jabatan WHERE status= 'active' ORDER BY nama asc ");
                                        		
                                                while($k = mysqli_fetch_assoc($sql_kondisi))
                                                {		
                                                  if(isset($c['pegawai_jabatan']) && $k['id'] == $c['pegawai_jabatan'])
            			                            {
            			                              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 class="col-lg-2 col-sm-2 control-label">Jenis Kelamin</label>
			                      <div class="col-lg-10">
			                          <select name="pegawai_gender" class="form-select" style="width: 100%" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
			                          	  <option value="<?php echo isset($c['pegawai_gender']) ? $c['pegawai_gender'] : 'Jenis Kelamin' ;?>"><?php echo isset($c['pegawai_gender']) ? $c['pegawai_gender'] : 'Jenis Kelamin' ;?></option>
			                              <option value="Laki - Laki">Laki - Laki</option>
			                              <option value="Perempuan">Perempuan</option>
			                          </select>
			                      	</div>
			                  	</div>
			                  	<div class="row mb-3">
			                      <label class="col-lg-2 col-sm-2 control-label">Agama</label>
			                      <div class="col-lg-10">
			                          <select name="pegawai_agama" class="form-select" style="width: 100%" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
			                          	  <option value="<?php echo isset($c['pegawai_agama']) ? $c['pegawai_agama'] : 'Agama' ;?>"><?php echo isset($c['pegawai_agama']) ? $c['pegawai_agama'] : 'Agama' ;?></option>
			                              	<option value="Islam">Islam</option>
																			<option value="Kristen">Kristen</option>
																			<option value="Katholik">Katholik</option>
																			<option value="Hindu">Hindu</option>
																			<option value="Budha">Budha</option>
			                          </select>
			                      	</div>
			                  	</div>
						                                  	
			                    <div class="row mb-3">
			                        <label class="control-label col-md-2">Alamat</label>
			                        <div class="col-md-10">
			                            <textarea class="form-control" name="pegawai_alamat" rows="5" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>><?php echo isset($c['pegawai_alamat']) ? $c['pegawai_alamat'] : '' ;?></textarea>
			                        </div>
			                    </div>
			                    <div class="row mb-3">
			                        <label class="control-label col-md-2">Keterangan</label>
			                        <div class="col-md-10">
			                            <textarea class="form-control" name="pegawai_keterangan" rows="5" <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>><?php echo isset($c['pegawai_keterangan']) ? $c['pegawai_keterangan'] : '' ;?></textarea>
			                        </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>
					    	</div>
				    	</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 <?php echo ucwords($title) ?></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=pegawai&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 Product</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">Jabatan</th>
									<th class="text-center">Jenis Kelamin</th>
									<th class="text-center">Agama</th>
									<th class="text-center">Alamat</th>
									<th class="text-center">Keterangan</th>
									
									<?php if($mn['update']=="Aktif" OR $mn['view']=="Aktif" OR $mn['delete']=="Aktif"){?>
                  					<th class="text-center">Aksi</th>
                  					<?php } ?>
								</tr>
		                  	</thead>
		                  	<tbody>
                			<?php
								$query = "SELECT * FROM `pegawai`";
								$sql_kul = mysqli_query($conn,$query);	
								$i=1;
								while ($m = mysqli_fetch_assoc($sql_kul)) {			
									
							?>
		                        <tr class="">
		                            <td align="center"><?php echo $i ?></td>
		                            <td><?php echo("<b>".strtoupper($m['pegawai_nama'])."</b><br/>".$m['pegawai_nomor']); ?></td>
				                    <td align="center"><?php echo jabatan($m['pegawai_jabatan'])?></td>
				                    <td align="center"><?php echo $m['pegawai_gender']?></td>
				                    <td align="center"><?php echo $m['pegawai_agama']?></td>
				                    <td><?php echo $m['pegawai_alamat']?></td>
				                    <td><?php echo $m['pegawai_keterangan']?></td>
		                            
									<?php if($mn['update']=="Aktif" OR $mn['view']=="Aktif" OR $mn['delete']=="Aktif"){?>
									<td align="center">
    	   								<div class="btn-group" role="group" aria-label="Aksi">
    	   								    <?php if ($mn['view'] == "Aktif") { ?>					
                                                <a href="<?php echo $linkaksi ?>&act=form&view=1&id=<?php echo $m['pegawai_id'] ?>&idmx=<?php echo $_GET['idmx'] ?>" 
                                                   class="btn btn-outline-info btn-sm" 
                                                   title="Edit Data">
                                                    <i class="material-icons-outlined">remove_red_eye</i>
                                                </a>
                                            <?php } ?>
                                            <?php if ($mn['update'] == "Aktif") { ?>					
                                                <a href="med.php?mod=pegawai&act=form&id=<?php echo $m['pegawai_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=pegawai&act=hapus&id=<?php echo $m['pegawai_id']; ?>&idmx=<?php echo $mn['id_menu']; ?>" 
                                                        title="Hapus Data">
                                                    <i class="material-icons-outlined">close</i>
                                                </button>
                                            <?php } ?>
                                        </div>
                                
									</td>
									<?php } ?>
                				</tr>
			
                				<?php
									 $i++;
								 }
								?>
            				</tbody>
							<tfoot>
								<tr>
									<th class="text-center">No</th>
									
									<th class="text-center">Nama</th>
									<th class="text-center">Jabatan</th>
									<th class="text-center">Jenis Kelamin</th>
									<th class="text-center">Agama</th>
									<th class="text-center">Alamat</th>
									<th class="text-center">Keterangan</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;
	}

	}
?>


