<?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 * FROM m_das WHERE id_bbws=".$_SESSION['id_bbws']."";
$query=mysql_query($sql);

if(isset($_POST)){
	if($_POST['actiontype'] == 'edit'){
		if($_POST['edit-luasdas'] == ''){ $luasdas = 0; }else{ $luasdas = intval($_POST['edit-luasdas']); }
		mysql_query("UPDATE m_das SET 
						kode_das = '".$_POST['edit-kodedas']."',
						nama_sungai = '".$_POST['edit-namasungai']."', 
						luas_daerah_pengaliran = ".$luasdas." 
					WHERE id_das = '".$_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_das (id_bbws, kode_das, nama_das, nama_sungai, luas_daerah_pengaliran) VALUES 
						(".$_SESSION['id_bbws'].",
						'".$_POST['new-kodedas']."',
						'".$_POST['new-namadas']."',
						'".$_POST['new-namasungai']."',
						".$_POST['new-luasdas'].")");
		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 Daerah Aliran Sungai </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 DAS</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 DAS</th>
					                                            <th class="mc">Kode DAS</th>
					                                            <th class="mc">Nama Sungai</th>
					                                            <th class="mc">Luas Daerah<br/>(m<sup>2</sup>)</th>
					                                            <th class="mc">Aksi</th>
					                                        </tr>
					                                    </thead>
					                                    <tbody>
				                                    	<?php
					                                    while($row=mysql_fetch_array($query)){
					                                    	$id = $row['id_das'];
					                                    ?>
					                                        <tr>
					                                            <td class="tl"><?php echo ucwords(strtolower($row['nama_das'])); ?></td>
					                                            <td class="tc"><?php echo $row['kode_das']; ?></td>
					                                            <td class="tl"><?php echo $row['nama_sungai']; ?></td>
					                                            <td class="tr"><?php echo number_format($row['luas_daerah_pengaliran'],0,',','.'); ?></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 $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 Daerah Aliran Sungai</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-12 mb-3">
	                                                            				<label for="new-namadas" class="form-label">Nama DAS</label>
                                                  								<input type="text" class="form-control" id="new-namadas" name="new-namadas">
	                                                            			</div>
	                                                            			<div class="col-12 mb-3">
	                                                            				<label for="new-namasungai" class="form-label">Nama Sungai</label>
                                                  								<input type="text" class="form-control" id="new-namasungai" name="new-namasungai">
	                                                            			</div>
	                                                            		</div>
	                                                            	</div>
	                                                            	<div class="col-6">
	                                                            		<div class="row">
	                                                            			<div class="col-6 mb-3">
	                                                            				<label for="new-kodedas" class="form-label">Kode DAS</label>
                                                  								<input type="text" class="form-control" id="new-kodedas" name="new-kodedas">
	                                                            			</div>
	                                                            			<div class="col-6 mb-3">
	                                                            				<label for="new-luasdas" class="form-label">Luas Darah Aliran</label>
                                                  								<input type="text" class="form-control" id="new-luasdas" name="new-luasdas" 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=mandas" 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 Daerah Aliran Sungai</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-12 mb-3">
	                                                            				<label for="edit-namadas" class="form-label">Nama DAS</label>
                                                  								<input type="text" class="form-control" id="edit-namadas" name="edit-namadas">
	                                                            			</div>
	                                                            			<div class="col-12 mb-3">
	                                                            				<label for="edit-namasungai" class="form-label">Nama Sungai</label>
                                                  								<input type="text" class="form-control" id="edit-namasungai" name="edit-namasungai">
	                                                            			</div>
	                                                            		</div>
	                                                            	</div>
	                                                            	<div class="col-6">
	                                                            		<div class="row">
	                                                            			<div class="col-6 mb-3">
	                                                            				<label for="edit-kodedas" class="form-label">Kode DAS</label>
                                                  								<input type="text" class="form-control" id="edit-kodedas" name="edit-kodedas">
	                                                            			</div>
	                                                            			<div class="col-6 mb-3">
	                                                            				<label for="edit-luasdas" class="form-label">Luas Darah Aliran</label>
                                                  								<input type="text" class="form-control" id="edit-luasdas" name="edit-luasdas" 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>     
