<?php
session_start();
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_lokasi INNER JOIN t_publikasi_curahhujan ON t_publikasi_curahhujan.id_lokasi=m_lokasi.id_lokasi WHERE 1=1 AND m_lokasi.id_bbws=".$_SESSION['id_bbws']."";
$query=mysql_query($sql);

if($_POST['simpan']=="Simpan"){
		$id_publikasi=strip_tags($_POST['id_publikasi']);
		$peserta_publikasi=strip_tags($_POST['peserta_publikasi']);
		$tgl_publikasi=$_POST['tgl_publikasi']."-12-31";
		$ibbws=trim($_POST['ibbws']);
		$id_lokasi=strip_tags($_POST['id_lokasi']);
		$gpa=strip_tags($_POST['gpa']);
		$note_gpa=strip_tags($_POST['note_gpa']);
		$sensor=strip_tags($_POST['sensor']);
		$note_sensor=strip_tags($_POST['note_sensor']);
		$solarcell=strip_tags($_POST['solarcell']);
		$note_solarcell=strip_tags($_POST['note_solarcell']);
		$solarcharger=strip_tags($_POST['solarcharger']);
		$note_solarcharger=strip_tags($_POST['note_solarcharger']);
		$baterai=strip_tags($_POST['baterai']);
		$note_baterai=strip_tags($_POST['note_baterai']);
		$lainlain=strip_tags($_POST['lainlain']);
		$note_lainlain=strip_tags($_POST['note_lainlain']);
		$tgl_tindakan=strip_tags($_POST['tgl_tindakan']);
		$tindakan=strip_tags($_POST['tindakan']);
		$hasil_tindakan=strip_tags($_POST['hasil_tindakan']);
		$keterangan=strip_tags($_POST['keterangan']);
		$status=strip_tags($_POST['status']);
		$tindakan_sensor=strip_tags($_POST['tindakan_sensor']);
		$tindakan_solarcell=strip_tags($_POST['tindakan_solarcell']);
		$tindakan_solarcharger=strip_tags($_POST['tindakan_solarcharger']);
		$tindakan_baterai=strip_tags($_POST['tindakan_baterai']);
		$fileNamepublikasi = "publikasi_curahhujan".$id_lokasi."_".$_FILES['filepublikasi']['name'];
		$fileNamePerbaikan = "perbaikan_".$_FILES['fileperbaikan']['name'];

		if ($id_publikasi=="") {
		   	// INSERT m_lokasi
			$sqlinsertmpublikasi="INSERT INTO t_publikasi_curahhujan values('','','$ibbws','$id_lokasi','$tgl_publikasi','$peserta_publikasi','$gpa','$note_gpa','$sensor','$note_sensor','$solarcell','$note_solarcell','$solarcharger','$note_solarcharger','$baterai','$note_baterai','$lainlain','$note_lainlain','$tindakan','$note_tindakan','','','',now(),'Open','','','','','$fileNamepublikasi','')";
		    $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_curahhujan 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>";
			}
		}
}
?>
<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 Curah Hujan </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 Publikasi Curah Hujan</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</th>
					                                            <th class="mc">Jenis</th>
					                                            <th class="mc">Tanggal Publikasi</th>
					                                            <th class="mc">File</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['file_publikasi'];
					                                    ?>
					                                        <tr>
					                                            <td class="tc"><?php echo $no;?></td>
					                                            <td><?php echo $namaAlat; ?></td>
					                                            <td class="tc"><?php echo $row['id_tipe'];?></td>
					                                            <td class="tc"><?php echo date('d/m/Y',strtotime($row['tgl_publikasi']));?></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="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 Curah Hujan</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 Curah Hujan</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']." AND id_tipe = 'PCH' 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 Curah Hujan</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 Curah Hujan</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']." AND id_tipe = 'PCH' 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 Curah Hujan</h5>
	                                                            <button type="button" class="btn-close"
	                                                                data-bs-dismiss="modal" aria-label="Close"></button>
	                                                        </div>
	                                                        <div class="modal-body">
	                                                            
	                                                        </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>        