<?php
include "config/conn.php";
if($_POST['btncari']=="Search") {
	$tglf=$_POST['tglf'];
	$tgls=$_POST['tgls'];
} else { 
	$tglf= date('Y-m-d');
	$tgls = date('Y-m-d',strtotime('-1 day',strtotime($tglf))) ;
}
$sql="SELECT
		a.id_debit,
		b.nama_lokasi,
		b.nama_alat,
		b.id_tipe,
		b.id_merk,
		a.tahun_debit,
		a.nilai_a,
		a.nilai_b,
		a.nilai_c,
		b.WLevel
		/*(FORMAT(a.nilai_c * POW((b.WLevel+a.nilai_a),a.nilai_c),2)/100) AS debit
		/*format(a.nilai_c * POW((((b.WLevel/100)+a.nilai_a),a.nilai_b),2)*/
		FROM
		m_debit AS a
		INNER JOIN m_lokasi AS b ON b.id_lokasi = a.id_lokasi WHERE 1=1 AND b.id_bbws=".$_SESSION['id_bbws']."";
$query=mysql_query($sql);

if(isset($_POST)){
	if($_POST['actiontype'] == 'edit'){
		mysql_query("UPDATE m_debit SET 
						tahun_debit = '".$_POST['edit-tahundebit']."',
						nilai_a = ".$_POST['edit-curvea'].", 
						nilai_b = ".$_POST['edit-curveb'].", 
						nilai_c = ".$_POST['edit-curvec']." 
					WHERE id_debit = ".$_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 m_debit (id_bbws, id_lokasi, tahun_debit, nilai_a, nilai_b, nilai_c) VALUES 
						(".$_SESSION['id_bbws'].",
						".$_POST['new-tipepos'].",
						".$_POST['new-tahundebit'].",
						".$_POST['new-curvea'].",
						".$_POST['new-curveb'].",
						".$_POST['new-curvec'].")");
		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 Monitoring Curve </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 Curve</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" rowspan="2">Nama POS</th>
					                                            <th class="mc" rowspan="2">Tipe POS</th>
					                                            <th class="mc" rowspan="2">Tahun</th>
					                                            <th class="mc" rowspan="2">TMA<br/>(m)</th>
					                                            <th class="mc" colspan="3">Nilai Curve</th>
					                                            <th class="mc" rowspan="2">Debit Air<br>(m<sup>3</sup>/s)</th>
					                                            <th class="mc" rowspan="2">Aksi</th>
					                                        </tr>
					                                        <tr>
					                                            <th class="mc">Nilai A</th>
					                                            <th class="mc">Nilai B</th>
					                                            <th class="mc">Nilai C</th>
					                                        </tr>
					                                    </thead>
					                                    <tbody>
				                                    	<?php
					                                    while($row=mysql_fetch_array($query)){
					                                    	$id = $row['id_debit'];

					                                    	$namaAlat = ucwords(strtolower($row['nama_alat']));
					                                    	$tipePos = substr($row['id_tipe'], 0, 3);
					                                    	if($row['tahun_debit'] == '0'){
					                                    		$tahun_debit = '2021';
					                                    	}else{
					                                    		$tahun_debit = $row['tahun_debit'];					                                    		
					                                    	}
					                                    ?>
					                                        <tr>
					                                            <td><?php echo $namaAlat;?></td>
					                                            <td class="tc"><?php echo $row['id_tipe'];?></td>
					                                            <td class="tc"><?php echo $tahun_debit;?></td>
					                                            <td class="tr"><?php echo number_format($row['WLevel']/100,2,',','.');?></td>
					                                            <td class="tr"><?php echo number_format($row['nilai_a'],4,',','.');?></td>
					                                            <td class="tr"><?php echo number_format($row['nilai_b'],4,',','.');?></td>
					                                            <td class="tr"><?php echo number_format($row['nilai_c'],4,',','.');?></td>
					                                            <td></td>
					                                            <td>
					                                            	<i class="text-warning" data-feather="edit" data-bs-toggle="modal" data-bs-target="#editform" style="cursor: pointer;" onclick="todetail('<?php echo $id; ?>')"></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=mandas" 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 Monitoring Debit</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-4 mb-3">
	                                                            				<label for="new-tahundebit" class="form-label">Tahun Debit</label>
                                                  								<select class="form-select" aria-label="" id="new-tahundebit" name="new-tahundebit">
									                                                <?php  
									                                                for ($i=intval(date('Y')); $i >= 2014; $i--) { 
									                                                ?>
									                                                <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
									                                                <?php
									                                                }
									                                                ?>
									                                            </select>
	                                                            			</div>
	                                                            			<div class="col-8 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['id_lokasi']; ?>"><?php echo ucwords($rowLok['nama_lokasi']); ?></option>
																                <?php
																                }
																                ?>
	                                                            				</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-curvea" class="form-label">Curve A</label>
                                                  								<input type="text" class="form-control" id="new-curvea" name="new-curvea" style="text-align: right;">
	                                                            			</div>
	                                                            			<div class="col-4 mb-3">
	                                                            				<label for="new-curveb" class="form-label">Curve B</label>
                                                  								<input type="text" class="form-control" id="new-curveb" name="new-curveb" style="text-align: right;">
	                                                            			</div>
	                                                            			<div class="col-4 mb-3">
	                                                            				<label for="new-curvec" class="form-label">Curve C</label>
                                                  								<input type="text" class="form-control" id="new-curvec" name="new-curvec" style="text-align: right;">
	                                                            			</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=manlengkung" 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 Monitoring Debit</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-4 mb-3">
	                                                            				<label for="edit-tahundebit" class="form-label">Tahun Debit</label>
                                                  								<select class="form-select" aria-label="" id="edit-tahundebit" name="edit-tahundebit">
									                                                <?php  
									                                                for ($i=intval(date('Y')); $i >= 2014; $i--) { 
									                                                ?>
									                                                <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
									                                                <?php
									                                                }
									                                                ?>
									                                            </select>
	                                                            			</div>
	                                                            			<div class="col-8 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 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">
																                <?php
																                while ($rowLok=mysql_fetch_array($queryLok)){
																                ?>
																                	<option value="<?php echo $rowLok['id_lokasi']; ?>"><?php echo ucwords($rowLok['nama_alat']); ?></option>
																                <?php
																                }
																                ?>
	                                                            				</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-curvea" class="form-label">Curve A</label>
                                                  								<input type="text" class="form-control" id="edit-curvea" name="edit-curvea" style="text-align: right;">
	                                                            			</div>
	                                                            			<div class="col-4 mb-3">
	                                                            				<label for="edit-curveb" class="form-label">Curve B</label>
                                                  								<input type="text" class="form-control" id="edit-curveb" name="edit-curveb" style="text-align: right;">
	                                                            			</div>
	                                                            			<div class="col-4 mb-3">
	                                                            				<label for="edit-curvec" class="form-label">Curve C</label>
                                                  								<input type="text" class="form-control" id="edit-curvec" name="edit-curvec" style="text-align: right;">
	                                                            			</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>     