<?php
include "config/conn.php";
$sql="SELECT shift_list.*, m_lokasi.nama_lokasi,m_lokasi.id_bbws,m_lokasi.nama_alat FROM shift_list
LEFT JOIN m_lokasi ON m_lokasi.nama_lokasi = CONVERT(shift_list.dev_name USING utf8)
WHERE m_lokasi.id_bbws = '".$_SESSION['id_bbws']."'";
$query=mysql_query($sql);

if(isset($_POST)){
	if($_POST['actiontype'] == 'edit'){
		mysql_query("UPDATE shift_list SET 
						a1_max = ".$_POST['edit-a1max'].", 
						a2_max = ".$_POST['edit-a2max'].",
						levelType = ".$_POST['edit-tipealat'].", 
						enabled = ".$_POST['edit-status']." 
					WHERE MD5(dev_name) = '".$_POST['edit-id']."'");	
		echo '<script type="text/javascript">document.location.href="'.BASEURL.'home.php?page='.$_GET['page'].'&stat=success";</script>';	
	
	}else if($_POST['actiontype'] == 'add'){
		mysql_query("INSERT INTO shift_list (dev_name, a1_max, a2_max, levelType, enabled) VALUES 
						('".$_POST['new-tipepos']."',
						'".$_POST['new-a1max']."',
						'".$_POST['new-a2max']."',
						".$_POST['new-tipealat'].",
						".$_POST['new-status'].")");
		echo '<script type="text/javascript">document.location.href="'.BASEURL.'home.php?page='.$_GET['page'].'&stat=success";</script>';
	}
}
?>
					<link rel="stylesheet" href="https://cdn.datatables.net/1.11.0/css/dataTables.bootstrap5.min.css">
					<!--//Page Toolbar//-->
                    <div class="toolbar pt-4 px-4 px-lg-8">
                        <div class="position-relative container-fluid px-0">
                            <div class="row align-items-center position-relative">
                                <div class="col-md-7">
                                    <h3 class="mb-2">Data Kalibrasi Tinggi Muka Air</h3>

                                    <!--nav aria-label="breadcrumb">
                                        <ol class="breadcrumb mb-0">
                                            <li class="breadcrumb-item"><a href="index.html">Home</a></li>
                                            <li class="breadcrumb-item active">UI Components</li>
                                            <li class="breadcrumb-item active">Feather Icons</li>
                                        </ol>
                                    </nav-->
                                </div>
                                <div class="col-md-5 text-md-end">
                                  <div class="d-flex justify-content-sm-end align-items-center">
                                    <div class="d-flex align-items-center">
                                      <button type="button" class="btn btn-info mb-2 me-2" data-bs-toggle="modal" data-bs-target="#newform"><i data-feather="plus"></i>&nbsp; Tambah Data Kalibrasi</button>
                                    </div>
                                  </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!--//Page Toolbar End//-->

                    <!--//Page content//-->
                    <div class="content pt-4 px-4 px-lg-8 d-flex flex-column-fluid">
                        <div class="container-fluid px-0">
                            <div class="row">
                                <div class="col-12 mb-4">
                                    <div class="row">
                                        <div class="col-12 mb-4">
                                            <div class="card">
                                                <div class="table-responsive">
					                                <table id="datatable" class="table mt-0 table-striped table-card table-nowrap">
					                                    <thead class="text-uppercase small text-muted">
					                                        <tr>
					                                            <th class="mc">Nama POS</th>
					                                            <th class="mc">Tipe</th>
					                                            <th class="mc">A1 Max</th>
					                                            <th class="mc">A2 Max</th>
					                                            <th class="mc">Status</th>
					                                            <th class="mc">Aksi</th>
					                                        </tr>
					                                    </thead>
					                                    <tbody>
				                                    	<?php
					                                    while($row=mysql_fetch_array($query)){
					                                    	$namaLokasi = ucwords(strtolower($row['nama_alat']));
					                                    	$devMd5 = md5($row['dev_name']);

					                                    	if($row['levelType'] == '0'){
					                                    		$levelType = 'Ultrasonic';
					                                    	}else{
					                                    		$levelType = 'Preasure';					                                    		
					                                    	}

					                                    	if($row['enabled'] == '0'){
					                                    		$status = 'InActive';
					                                    		$statusDisplay = '<span class="badge bg-tint-secondary text-secondary">'.$status.'</span>';
					                                    	}else{
					                                    		$status = 'Active';					                                    		
					                                    		$statusDisplay = '<span class="badge bg-tint-success text-success">'.$status.'</span>';
					                                    	}

					                                    ?>
					                                        <tr>
					                                            <td><?php echo $namaLokasi;?></td>
					                                            <td class="tc"><?php echo $levelType;?></td>
					                                            <td class="tr"><?php echo number_format($row['a1_max'],2,',','.');?></td>
					                                            <td class="tr"><?php echo number_format($row['a2_max'],2,',','.');?></td>
					                                            <td class="tc"><?php echo $statusDisplay; ?></td>
					                                            <td class="tc">
					                                            	<i class="text-warning" data-feather="edit" data-bs-toggle="modal" data-bs-target="#editform" style="cursor: pointer;" onclick="todetail('<?php echo $devMd5; ?>')"></i>
					                                            </td>
					                                        </tr>
					                                    <?php  
					                                	}
					                                    ?>
					                                    </tbody>
					                                </table>
					                            </div>
					                            <!-- New Modal -->
					                            <div class="modal fade" id="newform" data-bs-backdrop="static"
	                                                data-bs-keyboard="false" tabindex="-1"
	                                                aria-labelledby="newformLabel" aria-hidden="true">
	                                                <div class="modal-dialog modal-lg">
	                                                    <div class="modal-content">
	                                                    	<form id="form-add" action="<?php echo BASEURL; ?>home.php?page=kalibrasi" method="POST">
	                                                    		<input type="hidden" name="actiontype" id="actiontype" value="add" />
	                                                    		<input type="hidden" name="new-id" id="new-id" value="" />
	                                                        <div class="modal-header">
	                                                            <h5 class="modal-title" id="editformLabel">Tambah Data Kalibrasi Tinggi Muka Air</h5>
	                                                            <button type="button" class="btn-close"
	                                                                data-bs-dismiss="modal" aria-label="Close"></button>
	                                                        </div>
	                                                        <div class="modal-body">
	                                                            <div class="row">
	                                                            	<div class="col-6">
	                                                            		<div class="row">
	                                                            			<div class="col-7 mb-3">
	                                                            				<?php
																                $sql="SELECT id_lokasi,nama_lokasi,nama_alat,id_merk,id_tipe,CURDATE(),ReceivedDate,DATEDIFF(current_date(),ReceivedDate) AS hr,icon FROM m_lokasi WHERE 1=1 AND  status_lokasi=1 AND id_bbws=".$_SESSION['id_bbws']." AND id_tipe LIKE '%PDA%' ORDER BY nama_alat ASC ";

																                $queryLok = mysql_query($sql);
																                ?>
	                                                            				<label for="new-namapos" class="form-label">Nama POS</label>
	                                                            				<select class="form-select" aria-label="" id="new-tipepos" name="new-tipepos">
																                <?php
																                while ($rowLok=mysql_fetch_array($queryLok)){
																                ?>
																                	<option value="<?php echo $rowLok['nama_lokasi']; ?>"><?php echo ucwords($rowLok['nama_alat']); ?></option>
																                <?php
																                }
																                ?>
	                                                            				</select>
	                                                            			</div>
	                                                            			<div class="col-5 mb-3">
	                                                            				<label for="new-tipealat" class="form-label">Tipe</label>
                                                  								<select class="form-select" aria-label="" id="new-tipealat" name="new-tipealat">
									                                                <option value="0">Ultrasonic</option>
									                                                <option value="1">Preasure</option>
									                                            </select>
	                                                            			</div>
	                                                            			<!--div class="col-12 mb-3">
	                                                            				<label for="new-bbws" class="form-label">Balai Besar Wilayah Sungai</label>
                                                  								<input type="text" class="form-control" id="new-bbws" name="new-bbws" value="Citanduy" disabled>
	                                                            			</div-->
	                                                            		</div>
	                                                            	</div>
	                                                            	<div class="col-6">
	                                                            		<div class="row">
																			<div class="col-4 mb-3">
	                                                            				<label for="new-a1max" class="form-label">A1 Max</label>
                                                  								<input type="text" class="form-control" id="new-a1max" name="new-a1max" style="text-align: right;">
	                                                            			</div>
	                                                            			<div class="col-4 mb-3">
	                                                            				<label for="new-a2max" class="form-label">A2 Max</label>
                                                  								<input type="text" class="form-control" id="new-a2max" name="new-a2max" style="text-align: right;">
	                                                            			</div>
	                                                            			<div class="col-4 mb-3">
	                                                            				<label for="new-status" class="form-label">Status</label>
                                                  								<select class="form-select" aria-label="" id="new-status" name="new-status">
									                                                <option value="0">InActive</option>
									                                                <option value="1">Active</option>
									                                            </select>
	                                                            			</div>
	                                                            		</div>
	                                                            	</div>
	                                                            </div>
	                                                        </div>
	                                                        <div class="modal-footer">
	                                                            <button type="button" class="btn btn-secondary"
	                                                                data-bs-dismiss="modal">Close</button>
	                                                            <button type="submit"
	                                                                class="btn btn-info">Save</button>
	                                                        </div>
	                                                    	</form>
	                                                    </div>
	                                                </div>
	                                            </div>
					                            <!-- Edit Modal -->
	                                            <div class="modal fade" id="editform" data-bs-backdrop="static"
	                                                data-bs-keyboard="false" tabindex="-1"
	                                                aria-labelledby="editformLabel" aria-hidden="true">
	                                                <div class="modal-dialog modal-lg">
	                                                    <div class="modal-content">
	                                                    	<form id="form-edit" action="<?php echo BASEURL; ?>home.php?page=kalibrasi" method="POST">
	                                                    		<input type="hidden" name="actiontype" id="actiontype" value="edit" />
	                                                    		<input type="hidden" name="edit-id" id="edit-id" value="" />
	                                                        <div class="modal-header">
	                                                            <h5 class="modal-title" id="editformLabel">Ubah Data Kalibrasi Tinggi Muka Air</h5>
	                                                            <button type="button" class="btn-close"
	                                                                data-bs-dismiss="modal" aria-label="Close"></button>
	                                                        </div>
	                                                        <div class="modal-body">
	                                                            <div class="row">
	                                                            	<div class="col-6">
	                                                            		<div class="row">
	                                                            			<div class="col-7 mb-3">
	                                                            				<?php
																                $sql="SELECT id_lokasi,nama_lokasi,nama_alat,id_merk,id_tipe,CURDATE(),ReceivedDate,DATEDIFF(current_date(),ReceivedDate) AS hr,icon FROM m_lokasi WHERE 1=1 AND  status_lokasi=1 AND id_bbws=".$_SESSION['id_bbws']." AND id_tipe LIKE '%PDA%' ORDER BY nama_alat ASC ";

																                $queryLok = mysql_query($sql);
																                ?>
	                                                            				<label for="edit-namapos" class="form-label">Nama POS</label>
	                                                            				<select class="form-select" aria-label="" id="edit-tipepos" name="edit-tipepos" readonly="readonly">
																                <?php
																                while ($rowLok=mysql_fetch_array($queryLok)){
																                ?>
																                	<option value="<?php echo $rowLok['nama_lokasi']; ?>"><?php echo ucwords($rowLok['nama_alat']); ?></option>
																                <?php
																                }
																                ?>
	                                                            				</select>
	                                                            			</div>
	                                                            			<div class="col-5 mb-3">
	                                                            				<label for="edit-tipealat" class="form-label">Tipe</label>
                                                  								<select class="form-select" aria-label="" id="edit-tipealat" name="edit-tipealat">
									                                                <option value="0">Ultrasonic</option>
									                                                <option value="1">Preasure</option>
									                                            </select>
	                                                            			</div>
	                                                            			<!--div class="col-12 mb-3">
	                                                            				<label for="edit-bbws" class="form-label">Balai Besar Wilayah Sungai</label>
                                                  								<input type="text" class="form-control" id="edit-bbws" name="edit-bbws" value="Citanduy" disabled>
	                                                            			</div-->
	                                                            		</div>
	                                                            	</div>
	                                                            	<div class="col-6">
	                                                            		<div class="row">
																			<div class="col-4 mb-3">
	                                                            				<label for="edit-a1max" class="form-label">A1 Max</label>
                                                  								<input type="text" class="form-control" id="edit-a1max" name="edit-a1max" style="text-align: right;">
	                                                            			</div>
	                                                            			<div class="col-4 mb-3">
	                                                            				<label for="edit-a2max" class="form-label">A2 Max</label>
                                                  								<input type="text" class="form-control" id="edit-a2max" name="edit-a2max" style="text-align: right;">
	                                                            			</div>
	                                                            			<div class="col-4 mb-3">
	                                                            				<label for="edit-status" class="form-label">Status</label>
                                                  								<select class="form-select" aria-label="" id="edit-status" name="edit-status">
									                                                <option value="0">InActive</option>
									                                                <option value="1">Active</option>
									                                            </select>
	                                                            			</div>
	                                                            		</div>
	                                                            	</div>
	                                                            </div>
	                                                        </div>
	                                                        <div class="modal-footer">
	                                                            <button type="button" class="btn btn-secondary"
	                                                                data-bs-dismiss="modal">Close</button>
	                                                            <button type="submit"
	                                                                class="btn btn-info">Save</button>
	                                                        </div>
	                                                    	</form>
	                                                    </div>
	                                                </div>
	                                            </div>
                                            </div>
                                        </div>
                                    </div>
                                    

                                </div>
                            </div>
                        </div>
                    </div>   

                    <button type="button" class="btn btn-primary" id="liveToastBtn" style="display: none;">Show live toast</button>

                    <div class="position-fixed start-50 top-0 translate-middle-x p-3" style="z-index: 1080">
                      <div id="liveToast" class="toast bg-success text-white border-0 shadow-lg" role="alert" aria-live="assertive" aria-atomic="true">
                        <div class="d-flex">
                            <div class="toast-body">
                              Data berhasil di-update
                            </div>
                            <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
                          </div>
                      </div>
                    </div>     
