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

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

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

	$aksi = 'mod/sistem/act_setting_absensi.php';

	?>
	<?php
	switch ($act) {
	default:
			?>
			<div class="row page-titles ">
          <div class="col-md-5 align-self-center">
              <h4 class="text-themecolor">Data Setting Absensi</h4>
          </div>
          <div class="col-md-7 align-self-center text-end">
              <div class="d-flex justify-content-end align-items-center">
                  <ol class="breadcrumb justify-content-end">
                      <li class="breadcrumb-item"><a href="javascript:void(0)">Home</a></li>
                      <li class="breadcrumb-item active">Data Setting Absensi</li>
                  </ol>
                  <?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=setting_absensi&act=form&idmx=<?php echo $mn['id_menu'] ?>">
	                  	<button type="button" class="btn btn-info d-none d-lg-block m-l-15 text-white"><i class="fa fa-plus-circle"></i> Tambah </button>
									</a>
								<?php }?>
              </div>
          </div>
      </div>
      <!-- Row -->
      <?php
        flash('example_message');
       ?>
      <div class="row">
        <div class="col-lg-12">
            <div class="card ">
                <div class="card-header bg-info">
                    <h4 class="m-b-0 text-white">Data Setting Absensi</h4>
                </div>
                <div class="card-body">
                	<div class="table-responsive m-t-40">     
      							<table class="display nowrap table table-hover table-striped border" id="example23" cellspacing="0" width="100%">
      								<thead>
                        <tr>													
														<th class="text-center">No</th>
														<th class="text-center">Tempat</th>
														<th class="text-center">Jam Masuk</th>
														<th class="text-center">Jam Pulang</th>
														<th class="text-center">Jarak</th>
														<th class="text-center">Latitude</th>
														<th class="text-center">Longitude</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 `tb_setting_absensi`";
													$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 align="center"><?php echo $m['tempat']?></td>
				                      <td align="center"><?php echo $m['jam_masuk']?></td>
				                      <td align="center"><?php echo $m['jam_pulang']?></td>
				                      <td align="center"><?php echo $m['jarak']?></td>
				                      <td align="center"><?php echo $m['latitude']?></td>
				                      <td align="center"><?php echo $m['longitude']?></td>
															<?php if($mn['update']=="Aktif" OR $mn['view']=="Aktif" OR $mn['delete']=="Aktif"){?>
																<td align="center">
																	<div class="btn-group">
								                    				 <?php
																	if ($mn['view']=="Aktif") {
																	?>					
								        							<a href="<?php echo $linkaksi ?>&act=form&view=1&id=<?php echo $m['id'] ?>&idmx=<?php echo $_GET['idmx'] ?>"><button class="btn btn-info waves-effect waves-light"> <i class="fa fa-search"></i></button></a>	
																	<?php
																	} 											
										                			?>
										                			<?php
																	if ($mn['update']=="Aktif") {
																	?>	
								                          			<a href="med.php?mod=setting_absensi&act=form&id=<?php echo $m['id'] ?>&idmx=<?php echo $_GET['idmx'] ?>" ><button class="btn btn-success waves-effect waves-light"><i class="fa fa-edit"></i></button></a>
								                      				<?php
																	} 											
												                    ?>
																	<?php
																	if ($mn['delete']=="Aktif") {
																	?>		
																	<a href="<?php echo $aksi ?>?mod=setting_absensi&act=hapus&id=<?php echo $m['id'] ?>&idmx=<?php echo $_GET['idmx'] ?>"onclick="return confirm('Yakin ingin menghapus?');"><button class="btn btn-danger waves-effect waves-light"><i class="fa fa-times"></i></button> </a>
																	<?php
																	} 											
								               						?>	
																	</div>
																	</td>
																<?php }?>
                            </tr>
								
	                            <?php
														 $i++;
													 }
													?>
                        </tbody>
      							</table>
      						</div>
      					</div>
      			</div>
      	</div>
      </div>
			<?php
	break;		
	case 'form':
		if(!empty($_GET['id']))
			{
				$act = "$aksi?mod=setting_absensi&act=edit&idmx=".$_GET['idmx'];
				$query = mysqli_query($conn,"SELECT * FROM tb_setting_absensi WHERE id = '$_GET[id]'");
				$temukan = mysqli_num_rows($query);
				if($temukan > 0)
				{
					$c = mysqli_fetch_assoc($query);
				}
				else
				{
					header("location:med.php?mod=setting_absensi&idmx=".$_GET['idmx']);
				}

			}
			else
			{
				$act = "$aksi?mod=setting_absensi&act=simpan&idmx=".$_GET['idmx'];
			}

			?>

			<div class="row page-titles ">
            <div class="col-md-5 align-self-center">
                <h4 class="text-themecolor">Data Setting Absensi</h4>
            </div>
            <div class="col-md-7 align-self-center text-end">
                <div class="d-flex justify-content-end align-items-center">
                    <ol class="breadcrumb justify-content-end">
                        <li class="breadcrumb-item"><a href="javascript:void(0)">Home</a></li>
                        <li class="breadcrumb-item"><a href="med.php?mod=setting_absensi&idmx=<?php echo $_GET['idmx'] ?>">Data Setting Absensi</a></li>
                        <li class="breadcrumb-item active">Form</li>
                    </ol>
                </div>
            </div>
        </div>
        <?php
	        flash('example_message');
        ?>
        <div class="row">
          <div class="col-lg-12">
              <div class="card ">
                  <div class="card-header bg-info text-center">
                      <h4 class="m-b-0 text-white">Form Data Type</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['id']) ? $c['id'] : '' ;?>">
				                   
		               		<div class="row">
                  			<div class="col-xl-6 col-12">	
                  					<div class="form-group row">
                                <label for="example-time-input" class="col-lg-2 col-sm-2 col-form-label">Tempat</label>
                                <div class="col-sm-10">
																	<input class="form-control" name="tempat" type="text" value="<?php echo isset($c['tempat']) ? $c['tempat'] : '' ;?>" required <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
															  </div>
                            </div>	
				                   	<div class="form-group row">
                                <label for="example-time-input" class="col-lg-2 col-sm-2 col-form-label">Jam Masuk</label>
                                <div class="col-sm-10">
																	<input class="form-control" name="jam_masuk" type="time" id="example-time-input" value="<?php echo isset($c['jam_masuk']) ? $c['jam_masuk'] : '' ;?>" required <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
															  </div>
                            </div>
                            <div class="form-group row">
                                <label for="example-time-input2" class="col-lg-2 col-sm-2 col-form-label">Jam Pulang</label>
                                <div class="col-sm-10">
																	<input class="form-control" name="jam_pulang" type="time" id="example-time-input2" value="<?php echo isset($c['jam_pulang']) ? $c['jam_pulang'] : '' ;?>" required <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
															  </div>
                            </div>
				                    <div class="form-group row">
                                <label class="col-lg-2 col-sm-2 col-form-label">Jarak</label>
                                <div class="col-sm-10">
                                	<input type="text" name="jarak" class="form-control" value="<?php echo isset($c['jarak']) ? $c['jarak'] : '' ;?>" required <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
                                    
                                </div>
                            </div>

                            <div class="form-group row">
                                <label class="col-lg-2 col-sm-2 col-form-label">Latitude</label>
                                <div class="col-sm-10">
                                	<input type="text" id="latitude" name="latitude" class="form-control" value="<?php echo isset($c['latitude']) ? $c['latitude'] : '' ;?>" required <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
                                    
                                </div>
                            </div>

                            <div class="form-group row">
                                <label class="col-lg-2 col-sm-2 col-form-label">Longitude</label>
                                <div class="col-sm-10">
                                	<input type="text" id="longitude" name="longitude" class="form-control" value="<?php echo isset($c['longitude']) ? $c['longitude'] : '' ;?>" required <?php echo isset($_GET['view']) ? 'disabled' : '' ;?>>
                                    
                                </div>
                            </div>
				                </div>
				               	<div class="col-xl-6 col-12">
				               		<div class="form-group">
                                    <!--Display map-->
                                    <div id="peta" style="height:300px; width: 100%"></div>
	                                
                          </div>
				               	</div>
				              </div>
							    	</div>
							    	<?php
		                if (empty($_GET['view'])) {
		                ?>
							    	<div class="card-footer d-flex 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>
							      <?php
		                }  
		                ?>
						            
							    </form>
              </div>
          </div>
        </div>
					<?php
					if(!empty($_GET['id']))
					{	
             $latitude = $c['latitude'];
              $longitude = $c['longitude'];
              $radius = $c['jarak'];
              $name = $c['tempat'];
          ?>
            
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.8.0/leaflet.css" integrity="sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr/Fs0W40kiNHxM9vyTtQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
            <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.8.0/leaflet.js" integrity="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
            
            <script type="text/javascript">
                var mymap = L.map('peta').setView([<?php echo $latitude;?>, <?php echo $longitude;?>], 13);
                    L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoiaWxsYXNzaW5uZSIsImEiOiJjbDI2MzNlajMwMXhuM2lwMzFmeWR5dzRtIn0.Hy02CRumK06601ImTGAy0g', {
                      
                      attribution:'',
                      id: 'mapbox/streets-v11',
                      tileSize: 512,
                      zoomOffset:-1,
                      accessToken: 'pk.eyJ1IjoiYWRpZ3VuYXdhbnhkIiwiYSI6ImNrcWp2Yjg2cDA0ZjAydnJ1YjN0aDNnbm4ifQ.htvHCgSgN0UuV8hhZBfBfQ'
                    }).addTo(mymap);
                  L.marker([<?php echo $latitude;?>, <?php echo $longitude;?>]).addTo(mymap);
                  L.circle([<?php echo $latitude;?>, <?php echo $longitude;?>], <?php echo $radius;?>, {
                color: 'red',
                fillColor: '#f03',
                fillOpacity: 0.5
                }).addTo(mymap).bindPopup("<?php echo $name;?>").openPopup();
                  var popup = L.popup();
                  function onMapClick(e) {
                      
                    popup
                    .setLatLng(e.latlng)
                    .setContent(""+ e.latlng.toString())
                    .openOn(mymap);
                      var latitude = e.latlng.lat;
                      var longitude = e.latlng.lng;
                        document.getElementById("latitude").value = latitude;
                        document.getElementById("longitude").value = longitude;
                        console.log(latitude + " - " + longitude)
                }

                mymap.on('click', onMapClick);
         </script>
         <?php
       		}
         ?>
				</section>
			
				<?php
	break;
	}
	}
?>
<script>
													
function myFunction() {
  var msg;
	msg= "Apakah Anda Yakin Akan Menghapus Data ? " ;
	var agree=confirm(msg);
	if (agree)
	return true ;
	else
	return false ;
}
</script>