<?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))) ;
}
$thnterakhir = date('Y',strtotime('-2 year',strtotime($tglf))) ;
$sql="SELECT 
		b.ka_id,
		a.nama_das,
		b.ka_nilai_index,
		m_lokasi.nama_alat,
		b.ka_tanggal_sampling,
		b.ka_nilai_index AS nilaiindex
		FROM
		m_das AS a
		INNER JOIN m_ka AS b ON a.id_das = b.id_das
		INNER JOIN m_lokasi ON m_lokasi.id_lokasi = b.ka_nama_lokasi
		WHERE 1 = 1 AND b.id_bbws=".$_SESSION['id_bbws']." ";
if($_POST['location']!="" && $_POST['tahun']!="") { 
$sql.=" AND a.ka_nama_lokasi=".$_POST['location']." AND a.ka_tanggal_sampling LIKE '%".$_POST['tahun']."%'";
}

$query=mysql_query($sql);
if($_POST['simpan']=="Simpan"){
		$id_ka=strip_tags($_POST['id_ka']);
		$idas=strip_tags($_POST['idas']);
		$ilokasi=strip_tags($_POST['id_lokasi']);
		$tgl_sampling=strip_tags($_POST['tgl_sampling']);		
		$nilai_index=trim($_POST['nilai_index']);
		$id_bbws=trim($_POST['id_bbws']);

		$file=trim($_FILES['filepublikasi']['name']);

		$fileNamepublikasi = "publikasi_ka".$ilokasi."_".$file;
		$fileNamePerbaikan = "perbaikan_ka".$_FILES['fileperbaikan']['name'];
	
		if($file=="") {?><script>alert('Maaf file harus di lampirkan!');</script><?php } else { 
		
		if ($id_ka=="") {
		   	// INSERT m_lokasi
			$sqlinsertmpublikasi="INSERT INTO m_ka values('','$id_bbws','$idas','$ilokasi','','$tgl_sampling','','','','$nilai_index','','$fileNamepublikasi',now())";
		    $query=mysql_query($sqlinsertmpublikasi);
			if($query){
				move_uploaded_file($_FILES['filepublikasi']['tmp_name'], "file/publikasi/".$fileNamepublikasi);
				echo "<script>alert('Input hasil publikasi berhasil disimpan!');history.go(-1);</script>";
			}
		} else {
		$sql="UPDATE t_publikasi SET gpa='$gpa', sensor='$sensor',solarcell='$solarcell',solarcharger='$solarcharger',baterai='$baterai',lainlain='$lainlain',tindakan='$tindakan',hasil_tindakan='$hasil_tindakan',keterangan='$keterangan',tgl_tindakan='$tgl_tindakan',status='$status',tindakan_sensor='$tindakan_sensor',tindakan_solarcell='$tindakan_solarcell',tindakan_solarcharger='$tindakan_solarcharger',tindakan_baterai='$tindakan_baterai',file_perbaikan='$fileNamePerbaikan'
		WHERE id_publikasi='$id_publikasi'";			
		$query=mysql_query($sql);
			if($query){
				move_uploaded_file($_FILES['fileperbaikan']['tmp_name'], "file/perbaikan/".$_FILES['fileperbaikan']['name']);
				echo"<script>alert('Update data publikasi berhasil !');history.go(-1);</script>";
			}
		}
		} //akhir alert
}

if($_GET['act']=="del"){
//echo "sdfsfs";
$SQLDEL=mysql_query("DELETE FROM m_ka WHERE ka_id='".$_GET['kaid']."'");
echo "<script>alert('Data berhasil dihapus !');</script>";
header('location:?page=pubinputkualitasair');
}
?>
					<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 Publikasi Kualitas 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 Kualitas Air</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">No</th>
					                                            <th class="mc">Lokasi Sampling</th>
					                                            <th class="mc">Tanggal Sampling</th>
					                                            <th class="mc">Index Rata-Rata</th>
					                                            <th class="mc">File</th>
					                                            <th class="mc">Tingkat Pencemaran</th>
					                                            <th class="mc">Aksi</th>
					                                        </tr>
					                                    </thead>
					                                    <tbody>
				                                    	<?php
				                                    	$no = 1;
					                                    while($row=mysql_fetch_array($query)){
					                                    	$namaAlat = ucwords(strtolower($row['nama_alat']));
					                                    	$tipePos = substr($row['id_tipe'], 0, 3);
					                                    	$urlFotoLokasi = BASEURL.'file/publikasi/'.$row['ka_file'];
					                                    ?>
					                                        <tr>
					                                            <td class="tc"><?php echo $no;?></td>
					                                            <td><?php echo $namaAlat;?></td>
					                                            <td class="tc"><?php echo $row['ka_tanggal_sampling'];?></td>
					                                            <td class="tr"><?php echo $row['ka_nilai_index'];?></td>
					                                            <td class="tc">
					                                            	<a href="<?php echo $urlFotoLokasi; ?>" title="Lihat Dokumen Publikasi Kualitas Air <?php echo $row['nama_publikasi'];?>" data-bs-toggle="tooltip" data-bs-placement="left" target="_blank"><i class="text-info" data-feather="file-text"style="cursor: pointer;"></i></a>
					                                            </td>
					                                            <td class="tr"></td>
					                                            <td class="tc">
					                                            	<i class="text-warning" data-feather="edit" data-bs-toggle="modal" data-bs-target="#editform" style="cursor: pointer;"></i> &nbsp;&nbsp;
					                                            	<i class="text-danger" data-feather="delete" data-bs-toggle="modal" data-bs-target="#removeform" style="cursor: pointer;"></i>
					                                            </td>
					                                        </tr>
					                                    <?php  
					                                    	$no++;
					                                	}
					                                    ?>
					                                    </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">
	                                                        <div class="modal-header">
	                                                            <h5 class="modal-title" id="editformLabel">Tambah Data Publikasi KualitasAir</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-namapublikasi" class="form-label">Nama Publikasi Kualitas Air</label>
                                                  								<input type="text" class="form-control" id="new-namapublikasi" name="new-namapublikasi">
	                                                            			</div>
	                                                            			<div class="col-5 mb-3">
	                                                            				<label for="new-tanggalpublikasi" class="form-label">Tanggal Publikasi</label>
                                                  								<input type="text" class="form-control" id="new-tanggalpublikasi" name="new-tanggalpublikasi" style="text-align: right;">
	                                                            			</div>
	                                                            			<div class="col-7 mb-3">
	                                                            				<label for="new-uploadpublikasi" class="form-label">Upload Dokumen</label>
                                                  								<input class="form-control" type="file" id="new-uploadpublikasi" name="new-uploadpublikasi">
	                                                            			</div>
	                                                            		</div>
	                                                            	</div>
	                                                            	<div class="col-6">
	                                                            		<div class="row">
	                                                            			<div class="col-12 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']." 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-namapos" name="new-namapos">
																                <?php
																                while ($rowLok=mysql_fetch_array($queryLok)){
																                ?>
																                	<option value="<?php echo $rowLok['id_lokasi']; ?>"><?php echo $rowLok['id_tipe'].' '.ucwords($rowLok['nama_lokasi']); ?></option>
																                <?php
																                }
																                ?>
	                                                            				</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="button"
	                                                                class="btn btn-info">Save</button>
	                                                        </div>
	                                                    </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">
	                                                        <div class="modal-header">
	                                                            <h5 class="modal-title" id="editformLabel">Ubah Data Publikasi Kualitas 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-12 mb-3">
	                                                            				<label for="edit-namapublikasi" class="form-label">Nama Publikasi Kualitas Air</label>
                                                  								<input type="text" class="form-control" id="edit-namapublikasi" name="edit-namapublikasi">
	                                                            			</div>
	                                                            			<div class="col-5 mb-3">
	                                                            				<label for="edit-tanggalpublikasi" class="form-label">Tanggal Publikasi</label>
                                                  								<input type="text" class="form-control" id="edit-tanggalpublikasi" name="edit-tanggalpublikasi" style="text-align: right;">
	                                                            			</div>
	                                                            			<div class="col-7 mb-3">
	                                                            				<label for="edit-curveb" class="form-label">Upload Dokumen</label>
                                                  								<input class="form-control" type="file" id="edit-uploadpublikasi" name="edit-uploadpublikasi">
	                                                            			</div>
	                                                            		</div>
	                                                            	</div>
	                                                            	<div class="col-6">
	                                                            		<div class="row">
	                                                            			<div class="col-12 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']." 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-namapos" name="edit-namapos">
																                <?php
																                while ($rowLok=mysql_fetch_array($queryLok)){
																                ?>
																                	<option value="<?php echo $rowLok['id_lokasi']; ?>"><?php echo $rowLok['id_tipe'].' '.ucwords($rowLok['nama_lokasi']); ?></option>
																                <?php
																                }
																                ?>
	                                                            				</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="button"
	                                                                class="btn btn-info">Save</button>
	                                                        </div>
	                                                    </div>
	                                                </div>
	                                            </div>
	                                            <!-- Remove Modal -->
	                                            <div class="modal fade" id="removeform" data-bs-backdrop="static"
	                                                data-bs-keyboard="false" tabindex="-1"
	                                                aria-labelledby="removeformLabel" aria-hidden="true">
	                                                <div class="modal-dialog modal-lg">
	                                                    <div class="modal-content">
	                                                        <div class="modal-header">
	                                                            <h5 class="modal-title" id="removeformLabel">Hapus Data Publikasi Kualitas 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-12 mb-3">
	                                                            				<label for="edit-namapublikasi" class="form-label">Nama Publikasi Kualitas Air</label>
                                                  								<input type="text" class="form-control" id="edit-namapublikasi" name="edit-namapublikasi">
	                                                            			</div>
	                                                            			<div class="col-5 mb-3">
	                                                            				<label for="edit-tanggalpublikasi" class="form-label">Tanggal Publikasi</label>
                                                  								<input type="text" class="form-control" id="edit-tanggalpublikasi" name="edit-tanggalpublikasi" style="text-align: right;">
	                                                            			</div>
	                                                            		</div>
	                                                            	</div>
	                                                            	<div class="col-6">
	                                                            		<div class="row">
	                                                            			<div class="col-12 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']." 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-namapos" name="edit-namapos">
																                <?php
																                while ($rowLok=mysql_fetch_array($queryLok)){
																                ?>
																                	<option value="<?php echo $rowLok['id_lokasi']; ?>"><?php echo $rowLok['id_tipe'].' '.ucwords($rowLok['nama_lokasi']); ?></option>
																                <?php
																                }
																                ?>
	                                                            				</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="button"
	                                                                class="btn btn-danger">Hapus</button>
	                                                        </div>
	                                                    </div>
	                                                </div>
	                                            </div>
                                            </div>
                                        </div>
                                    </div>
                                    

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