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

if(isset($_POST)){
	if($_POST['actiontype'] == 'edit'){ 
		mysql_query("UPDATE t_kegiatan SET 
						nama_kegiatan = '".$_POST['edit-namakegiatan']."',
						waktu = '".$_POST['edit-tanggalkegiatan']."', 
						kategori_kegiatan = ".$_POST['edit-tipekegiatan'].", 
						catatan = '".$_POST['edit-catatankegiatan']." '
					WHERE id_kegiatan = ".$_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'){
		$file = trim($_FILES['new-fotokegiatan']['name']);
		$fileNamepublikasi = "publikasi_ka_".$file;

		move_uploaded_file($_FILES['new-fotokegiatan']['tmp_name'], "file/kegiatan/".$fileNamepublikasi);

		mysql_query("INSERT INTO t_kegiatan (id_bbws, nama_kegiatan, file_kegiatan, waktu, kategori_kegiatan, catatan) VALUES 
						(".$_SESSION['id_bbws'].",
						'".$_POST['new-namakegiatan']."',
						'".$fileNamepublikasi."',
						'".$_POST['new-tanggalkegiatan']."',
						".$_POST['new-tipekegiatan'].",
						'".$_POST['new-catatankegiatan']."')"); 
		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">Foto Kegiatan Unit Hidrologi </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 Kegiatan</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">Nama Kegiatan</th>
					                                            <th class="mc">Kategori</th>
					                                            <th class="mc">Keterangan</th>
					                                            <th class="mc">Waktu</th>
					                                            <th class="mc">File</th>
					                                            <th class="mc">Aksi</th>
					                                        </tr>
					                                    </thead>
					                                    <tbody>
				                                    	<?php
				                                    	$no = 1;
					                                    while($row=mysql_fetch_array($query)){
					                                    	$id = $row['id_kegiatan'];
					                                    	if($row['kategori_kegiatan'] == '1'){
					                                    		$namaKategori = 'Pengukuran Debit';
					                                    	}else if($row['kategori_kegiatan'] == '2'){
					                                    		$namaKategori = 'Pemeliharaan Rutin';
					                                    	}else if($row['kategori_kegiatan'] == '3'){
					                                    		$namaKategori = 'Survey';
					                                    	}else if($row['kategori_kegiatan'] == '4'){
					                                    		$namaKategori = 'Pengukuran Kualitas Air';
					                                    	}

					                                    	if(strlen($row['nama_kegiatan']) > 40){
					                                    		$namaKegiatan = substr($row['nama_kegiatan'], 0, 40).'...';
					                                    	}else{
					                                    		$namaKegiatan = $row['nama_kegiatan'];
					                                    	}

					                                    	if(strlen($row['catatan']) > 40){
					                                    		$catatan = substr($row['catatan'], 0, 40).'...';
					                                    	}else{
					                                    		$catatan = $row['catatan'];
					                                    	}

					                                    	$urlFotoKegiatan = BASEURL.'file/kegiatan/'.$row['file_kegiatan'];
					                                    ?>
					                                        <tr>
					                                            <td class="tc"><?php echo $no;?></td>
					                                            <td class="tl"><span title="<?php echo $row['nama_kegiatan']; ?>"><?php echo $namaKegiatan;?></span></td>
					                                            <td class="tl"><?php echo $namaKategori;?></td>
					                                            <td class="tl"><span title="<?php echo $row['catatan']; ?>"><?php echo $catatan;?></span></td>
					                                            <td class="tc"><?php echo $row['waktu'];?></td>
					                                            <td class="tc">
					                                            	<?php if($row['file_kegiatan'] != ''){ ?>
					                                            		<i class="text-primary" data-feather="image" title="Lihat Foto Kegiatan <?php echo $row['nama_kegiatan']; ?>" data-bs-toggle="modal" data-bs-target="#displayimage" style="cursor: pointer;" onClick="toDisplayImg('<?php echo $namaKegiatan; ?>','<?php echo $urlFotoKegiatan; ?>')"></i>
					                                            	<?php } ?>
					                                            </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  
					                                    	$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">
	                                                    	<form id="form-add" action="<?php echo BASEURL; ?>home.php?page=dokkegiatan" method="POST" enctype="multipart/form-data">
	                                                    		<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 Kegiatan</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-namakegiatan" class="form-label">Nama Kegiatan</label>
                                                  								<input type="text" class="form-control" id="new-namakegiatan" name="new-namakegiatan">
	                                                            			</div>
	                                                            			<div class="col-7 mb-3">
	                                                            				<label for="new-tipekegiatan" class="form-label">Kategori Kegiatan</label>
                                                  								<select class="form-select" aria-label="" id="new-tipekegiatan" name="new-tipekegiatan">
									                                                <option value="1">Pengukuran Debit</option>
									                                                <option value="2">Pemeliharaan Rutin</option>
									                                                <option value="3">Survey</option>
									                                                <option value="4">Pengukuran Kualitas Air</option>
									                                            </select>
	                                                            			</div>
																			<div class="col-5 mb-3">
	                                                            				<label for="new-tanggalkegiatan" class="form-label">Tanggal</label>
                                                  								<input type="text" class="form-control" id="new-tanggalkegiatan" name="new-tanggalkegiatan" style="text-align: right;">
	                                                            			</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-12 mb-3">
	                                                            				<label for="new-catatankegiatan" class="form-label">Catatan/Deskripsi</label>
                                                  								<input type="text" class="form-control" id="new-catatankegiatan" name="new-catatankegiatan">
	                                                            			</div>
	                                                            			<div class="col-12 mb-3">
	                                                            				<label for="new-fotokegiatan" class="form-label">Unggah Foto Kegiatan</label>
                                                  								<input class="form-control" type="file" id="new-fotokegiatan" name="new-fotokegiatan">
	                                                            			</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=dokkegiatan" method="POST" enctype="multipart/form-data">
	                                                    		<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">Tambah Kegiatan</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-namakegiatan" class="form-label">Nama Kegiatan</label>
                                                  								<input type="text" class="form-control" id="edit-namakegiatan" name="edit-namakegiatan">
	                                                            			</div>
	                                                            			<div class="col-7 mb-3">
	                                                            				<label for="edit-tipekegiatan" class="form-label">Kategori Kegiatan</label>
                                                  								<select class="form-select" aria-label="" id="edit-tipekegiatan" name="edit-tipekegiatan">
									                                                <option value="1">Pengukuran Debit</option>
									                                                <option value="2">Pemeliharaan Rutin</option>
									                                                <option value="3">Survey</option>
									                                                <option value="4">Pengukuran Kualitas Air</option>
									                                            </select>
	                                                            			</div>
																			<div class="col-5 mb-3">
	                                                            				<label for="edit-tanggalkegiatan" class="form-label">Tanggal</label>
                                                  								<input type="text" class="form-control" id="edit-tanggalkegiatan" name="edit-tanggalkegiatan" style="text-align: right;">
	                                                            			</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-12 mb-3">
	                                                            				<label for="edit-catatankegiatan" class="form-label">Catatan/Deskripsi</label>
                                                  								<input type="text" class="form-control" id="edit-catatankegiatan" name="edit-catatankegiatan">
	                                                            			</div>
	                                                            			<div class="col-12 mb-3">
	                                                            				<label for="edit-fotokegiatan" class="form-label">Unggah Foto Kegiatan</label>
                                                  								<input class="form-control" type="file" id="edit-fotokegiatan" name="edit-fotokegiatan" disabled="disabled">
	                                                            			</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>
	                                            <!-- Display Image Modal -->
	                                            <div class="modal fade" id="displayimage" data-bs-backdrop="static"
	                                                data-bs-keyboard="false" tabindex="-1"
	                                                aria-labelledby="displayimageLabel" aria-hidden="true">
	                                                <div class="modal-dialog modal-lg">
	                                                    <div class="modal-content">
	                                                        <div class="modal-header">
	                                                            <h5 class="modal-title" id="displayimageLabel">Foto Kegiatan </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-12">
	                                                            		<img src="http://sdatelemetry.com/gambarpos/salawe.jpg" id="display-img" style="width: 100%;">
	                                                            	</div>
	                                                            </div>
	                                                        </div>
	                                                    </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>     