<?php
include "config/conn.php";

$bulan[1] = 'Januari';
$bulan[2] = 'Februari';
$bulan[3] = 'Maret';
$bulan[4] = 'April';
$bulan[5] = 'Mei';
$bulan[6] = 'Juni';
$bulan[7] = 'Juli';
$bulan[8] = 'Agustus';
$bulan[9] = 'September';
$bulan[10] = 'Oktober';
$bulan[11] = 'November';
$bulan[12] = 'Desember';

if($_POST['cari']=="Cari") {
	$bulanini=$_POST['tgl'];
	$namalokasi=$_POST['location'];
} else { 
	if(isset($_GET['bulan'])){
		$bulanini=$_GET['bulan'];
		$namalokasi=$_GET['idloc'];

	}else{
		$bulanini=date('m');
		$namalokasi=$_GET['idloc'];
	}
//	$tgls = date('Y-m-d',strtotime('-1 day',strtotime($tglf))) ;
}
$sqlinfo=mysql_query("select * from m_lokasi where nama_lokasi='".$namalokasi."'");
$rowinfo=mysql_fetch_array($sqlinfo);
?>
					<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">Detail Laporan Curah Hujan Manual </h3>
                                </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">
                                    	<?php
						                if($_SESSION['id_bbws']==8) { $idbbws="IN (8,21)";} else { $idbbws="IN (".$_SESSION['id_bbws'].")";}
										$sqllocation=mysql_query("SELECT a.nama_alat,a.nama_lokasi,b.nama_bbws
																	FROM
																	m_lokasi AS a
																	INNER JOIN m_bbws AS b ON a.id_bbws = b.id_bbws
																	WHERE 1=1 AND a.id_bbws ".$idbbws." AND a.status_lokasi=1 AND a.id_tipe='PCH'");
						                ?>
                        				<select class="form-select" aria-label="" id="location" name="location" style="margin-top:-5px; margin-right: 15px;" onchange="refreshPage()">
						                <?php
						                $i=0;
										while($rowlocation=mysql_fetch_array($sqllocation)){
										$i++;
						                ?>
						                	<option value="<?php echo $rowlocation['nama_lokasi'];?>" <?php if($rowlocation['nama_lokasi']==$_POST['location'] || $_GET['idloc']==$rowlocation['nama_lokasi']){ echo 'selected="selected"';}?>><?php echo strtoupper($rowlocation['nama_alat'])." - <strong>".$rowlocation['nama_bbws']."</strong>";?></option>
						                <?php
						                }
						                ?>
                        				</select>
                        				<select class="form-select" name="tgl" id="tgl" style="margin-top:-5px; margin-right: 15px; width: 180px;" onchange="refreshPage()">
                        				<?php for($j=1; $j<=12; $j++){?>
        									<option value="<?php echo $j; ?>" <?php if($j==$_POST['tgl'] || $_GET['bulan']==$j){ echo 'selected="selected"';}?>><?php echo $bulan[$j].' '.date('Y'); ?></option>
										<?php }?>
                        				</select>
                                       <a href="includes/downloadDataTMAmanual.php?pos=<?php echo $_GET['idloc'];?>&tipe=PCH&bln=<?php echo $bulanini;?>"><button type="button" class="btn btn-success mb-2 me-2" target="_blank">Export</button></a>
                                    </div>
                                  </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <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-3 mb-4">
                                	<div class="card">
                                    	<div class="card-body">
                                    		<div class="row">
                                    			<div class="col-12 mb-3">
                                    				<label class="form-label" style="font-size: 0.9em; margin: 0px; color: #666666;">Nama Lokasi</label>
                      								<span style="clear:both; display:block; font-size: 1.2em;"><?php echo $rowinfo['nama_alat']; ?></span>
                                    			</div>
                                    			<!--div class="col-12 mb-3">
                                    				<label class="form-label" style="font-size: 0.9em; margin: 0px; color: #666666;">Nomor Register</label>
                      								<span style="clear:both; display:block; font-size: 1.2em;"><?php echo $rowinfo['no_hp']; ?></span>
                                    			</div-->
                                    			<div class="col-12 mb-3">
                                    				<label class="form-label" style="font-size: 0.9em; margin: 0px; color: #666666;">Alamat</label>
                      								<span style="clear:both; display:block; font-size: 1.2em;"><?php echo $rowinfo['lokasi_alamat']; ?></span>
                                    			</div>
                                    			<div class="col-12 mb-3">
                                    				<label class="form-label" style="font-size: 0.9em; margin: 0px; color: #666666;">Petugas</label>
                      								<span style="clear:both; display:block; font-size: 1.2em;"><?php echo $rowinfo['nama_pengamat']; ?></span>
                                    			</div>
                                    			<div class="col-12 mb-3">
                                    				<label class="form-label" style="font-size: 0.9em; margin: 0px; color: #666666;">Tahun</label>
                      								<span style="clear:both; display:block; font-size: 1.2em;"><?php echo date('Y'); ?></span>
                                    			</div>
                                    			<div class="col-12 mb-3">
                                    				<label class="form-label" style="font-size: 0.9em; margin: 0px; color: #666666;">Bulan</label>
                      								<span style="clear:both; display:block; font-size: 1.2em;"><?php echo $bulanini; ?></span>
                                    			</div>
                                    		</div>
                                    	</div>
                                    </div>
                                </div>
                                <div class="col-9 mb-4">
                                    <div class="card">
                                    	<div class="card-body">
                                    		<div class="table-responsive">
				                                <table id="datatable" class="table mt-0 table-striped table-card table-nowrap" style=" border-radius: 10px;">
				                                    <thead class="text-uppercase small text-muted" style="background-color: #475569;">
				                                        <tr>
				                                            <th class="mc" rowspan="2" style="color:#ffffff;">Tanggal</th>
				                                            <th class="mc" colspan="2" style="color:#ffffff;">Jam Pencatatan</th>
				                                            <th class="mc" rowspan="2" style="color:#ffffff;">Keterangan</th>
				                                        </tr>
				                                        <tr>
				                                        	<th class="mc" colspan="2" style="color:#ffffff;">07:00</th>
				                                        </tr>
				                                    </thead>
				                                    <tbody>
				                                    	<?php

$i=0;
//while($rch=mysql_fetch_array($query)){
for($i=1; $i<=31; $i++){
//$i++;
	if($i > 9){
									$ii = $i;
								}else{
									$ii = '0'.$i;
								}

								$samplingDate = date('Y')."-".$bulanini."-".$ii;

$sql="SELECT Rain FROM ".$namalokasi." WHERE 1=1 AND ML=50 AND ReceivedDate='".date('Y')."-".$bulanini."-".$i."'";
$query=mysql_query($sql);
$rch=mysql_fetch_array($query);

$sqlDetail7 =  mysql_query("SELECT * FROM m_lokasi_detail WHERE nama_lokasi = '".$namalokasi."' AND sampling_date = '".$samplingDate."' AND sampling_time = '07:00:00'");
$numDetail7 = mysql_num_rows($sqlDetail7);
$rowDetail7 = mysql_fetch_array($sqlDetail7);

//if($i<date('d')) {
	if($rch['Rain']=='0') { $hjn="-"; } else { $hjn=number_format($rch['Rain'],1)." mm";}
//} else { $hjn="";}
?>
	<tr>
    <td class="mc" style="padding: 0.5rem 0.75rem;" ><?php echo $i; ?></td>
    <td class="mr" style="padding: 0.5rem 0.75rem; width: 100px; border-left: 1px #bbbbbb solid;" ><?php echo $rch['Rain'];?></td>
    <td class="mc" style="padding: 0.5rem 0.75rem 0.5rem 0rem; width: 50px;">
		<?php 
  		//echo $numDetail17;
  		if($numDetail7 > 0){
  			if(file_exists('cdn/manual/'.$rowDetail7['manual_foto'])){
				$img7 = CDN.'manual/'.$rowDetail7['manual_foto'];
			}else{
				$img7 = CDN2.'manual/'.$rowDetail7['manual_foto'];
			}
			echo '<i class="text-primary" data-feather="image" title="Lihat Foto Lokasi POS '.strtoupper($rowinfo['nama_alat']).'" data-bs-toggle="modal" data-bs-target="#displayimage" style="cursor: pointer;" onClick="toDisplayImg(\''.strtoupper($rowinfo['nama_alat']).'\',\''.$img7.'\',\'Tanggal '.$samplingDate.' Jam 07:00 \')"></i>';
         }
  		?>
  	</td>
    <td style="padding: 0.5rem 0.75rem; border-left: 1px #bbbbbb solid;"></td>
	</tr>
<?php } ?>
				                                    </tbody>
				                                </table>
				                            </div>
                                    	</div>
                                    </div>
                                </div>
                            </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 Pengukuran </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://v2.sdatelemetry.com/cdn/manual/sampling_manual.jpg" id="display-img" style="width: 100%;">
	                                                            	</div>
	                                                            </div>
	                                                        </div>
	                                                    </div>
	                                                </div>
	                                            </div>