<?php
$FtoM['Jan'] = '01';
$FtoM['Feb'] = '02';
$FtoM['Mar'] = '03';
$FtoM['Apr'] = '04';
$FtoM['May'] = '05';
$FtoM['Jun'] = '06';
$FtoM['Jul'] = '07';
$FtoM['Aug'] = '08';
$FtoM['Sep'] = '09';
$FtoM['Oct'] = '10';
$FtoM['Nov'] = '11';
$FtoM['Dec'] = '12';
//05Jun22-07Jun22
//0123456789
if(isset($_GET['rangedate'])) {
    $tglf = ''.substr($_GET['rangedate'], 19, 4).'-'.substr($_GET['rangedate'], 16, 2).'-'.substr($_GET['rangedate'], 13, 2);
    $tgls = ''.substr($_GET['rangedate'], 6, 4).'-'.substr($_GET['rangedate'], 3, 2).'-'.substr($_GET['rangedate'], 0, 2);
    $filterRangeDate = $_GET['rangedate'];
} else { 
    $tglf = date('Y-m-d');
    $tgls = date('Y-m-d',strtotime('-1 day',strtotime($tglf))) ;
    $filterRangeDate = date('d/m/Y',strtotime('-1 day',strtotime($tglf))).' - '.date('d/m/Y');
} //var_dump($filterRangeDate);

if(isset($_GET['type'])){
    if($_GET['type']=='minutely'){   
        $type='minutely';
    }else if($_GET['type']=='hourly'){   
        $type='hourly';
    }else if($_GET['type']=='daily'){
        $type='daily';
    }else{
        $type='hourly';
    }
}else{
    $type='hourly';
}

$queryLast=mysql_query("SELECT * FROM ".$_GET['pos']." WHERE 1=1 ORDER BY Id DESC LIMIT 1");
$rowLast=mysql_fetch_array($queryLast);

$queryInfo=mysql_query("SELECT * FROM m_lokasi WHERE nama_lokasi='".$_GET['pos']."'");
$rowInfo=mysql_fetch_array($queryInfo); //var_dump($rowInfo);

if($_GET['tpos']=="PDA"){ /*
    $sqlDetail = "SELECT DISTINCT ReceivedDate,jam_value AS ReceivedTime,UNIX_TIMESTAMP(CONCAT(ReceivedDate,'-',DATE_FORMAT(a.ReceivedTime,'%H:0'))) AS tgl,max(a.WLevel) AS WLevel, a.WLevel AS klWLevel,ML
                    FROM ".$_GET['pos']." AS a
                    INNER JOIN m_jam AS b ON DATE_FORMAT(b.jam_value,'%H:0') = DATE_FORMAT(a.ReceivedTime,'%H:0')
                    WHERE 1=1 AND a.WLevel!=0 AND a.WLevel>=1 AND a.ReceivedDate>='".$tgls."' AND a.ReceivedDate<='".$tglf."' GROUP BY ReceivedDate,jam_value
                    ORDER BY a.ReceivedDate DESC,b.jam_value DESC";
    $sqlDetail2 = "SELECT DISTINCT ReceivedDate,jam_value AS ReceivedTime,UNIX_TIMESTAMP(CONCAT(ReceivedDate,'-',DATE_FORMAT(a.ReceivedTime,'%H:0'))) AS tgl,max(a.WLevel) AS WLevel, a.WLevel AS klWLevel,ML
                    FROM ".$_GET['pos']." AS a
                    INNER JOIN m_jam AS b ON DATE_FORMAT(b.jam_value,'%H:0') = DATE_FORMAT(a.ReceivedTime,'%H:0')
                    WHERE 1=1 AND a.WLevel!=0 AND a.WLevel>=1 AND a.ReceivedDate>='".$tgls."' AND a.ReceivedDate<='".$tglf."' GROUP BY ReceivedDate,jam_value
                    ORDER BY a.ReceivedDate ASC,b.jam_value ASC"; //var_dump($sqlDetail2); */

    if($type == 'minutely'){
        $sqlDetail2 = "SELECT ReceivedDate, CONCAT(SUBSTRING(ReceivedTime,1,5),':00') AS ReceivedTime2, MAX(a.WLevel) AS WLevel_max, AVG(ML) AS ML_avg  
                        FROM ".$_GET['pos']." a
                        WHERE 1=1 AND a.WLevel >= 1 AND a.ReceivedDate BETWEEN '".$tgls."' AND '".$tglf."' 
                        GROUP BY ReceivedDate, SUBSTRING(ReceivedTime,1,5)
                        ORDER BY ReceivedDate DESC, SUBSTRING(ReceivedTime,1,5) DESC";
    }else if($type == 'hourly'){
        $sqlDetail2 = "SELECT ReceivedDate, CONCAT(SUBSTRING(ReceivedTime,1,2),':00:00') AS ReceivedTime2, MAX(a.WLevel) AS WLevel_max, AVG(ML) AS ML_avg  
                        FROM ".$_GET['pos']." a
                        WHERE 1=1 AND a.WLevel >= 1 AND a.ReceivedDate BETWEEN '".$tgls."' AND '".$tglf."' 
                        GROUP BY ReceivedDate, SUBSTRING(ReceivedTime,1,2)
                        ORDER BY ReceivedDate DESC, SUBSTRING(ReceivedTime,1,2) DESC";
    }else if($type == 'daily'){
        $sqlDetail2 = "SELECT ReceivedDate, '07:00:00' AS ReceivedTime2, MAX(a.WLevel) AS WLevel_max, AVG(ML) AS ML_avg  
                        FROM ".$_GET['pos']." a
                        WHERE 1=1 AND a.WLevel >= 1 AND a.ReceivedDate BETWEEN '".$tgls."' AND '".$tglf."' 
                        GROUP BY ReceivedDate
                        ORDER BY ReceivedDate DESC";
    } //echo $sqlDetail2;
    $sqlDetail = $sqlDetail2;
    $queryDetail2 = mysql_query(str_replace(' DESC', ' ASC', $sqlDetail2));
    $queryDetail3 = mysql_query($sqlDetail2);
} else { 
    /*
    $sqlDetail = "SELECT DISTINCT ReceivedDate,jam_value AS ReceivedTime,UNIX_TIMESTAMP(CONCAT(ReceivedDate,'-',DATE_FORMAT(a.ReceivedTime,'%H:0'))) AS tgl,SUM(Rain) Hujan
                    FROM ".$_GET['pos']." AS a
                    INNER JOIN m_jam AS b ON DATE_FORMAT(b.jam_value,'%H:0') = DATE_FORMAT(a.ReceivedTime,'%H:0')
                    WHERE 1=1 AND a.ReceivedDate>='".$tgls."' AND a.ReceivedDate<='".$tglf."' GROUP BY ReceivedDate,jam_value
                    ORDER BY a.ReceivedDate ASC,b.jam_value ASC"; */

    if($type == 'minutely'){
        $sqlDetail = "SELECT ReceivedDate, CONCAT(SUBSTRING(ReceivedTime,1,5),':00') AS ReceivedTime2, SUM(a.Rain) AS Rain_sum, AVG(ML) AS ML_avg  
                        FROM ".$_GET['pos']." a
                        WHERE 1=1 AND a.ReceivedDate BETWEEN '".$tgls."' AND '".$tglf."' 
                        GROUP BY ReceivedDate, SUBSTRING(ReceivedTime,1,5)
                        ORDER BY ReceivedDate DESC, SUBSTRING(ReceivedTime,1,5) DESC";
    }else if($type == 'hourly'){
        $sqlDetail = "SELECT ReceivedDate, CONCAT(SUBSTRING(ReceivedTime,1,2),':00:00') AS ReceivedTime2, SUM(a.Rain) AS Rain_sum, AVG(ML) AS ML_avg  
                        FROM ".$_GET['pos']." a
                        WHERE 1=1 AND a.ReceivedDate BETWEEN '".$tgls."' AND '".$tglf."' 
                        GROUP BY ReceivedDate, SUBSTRING(ReceivedTime,1,2)
                        ORDER BY ReceivedDate DESC, SUBSTRING(ReceivedTime,1,2) DESC";
    }else if($type == 'daily'){
        $sqlDetail = "SELECT ReceivedDate, '07:00:00' AS ReceivedTime2, SUM(a.Rain) AS Rain_sum, AVG(ML) AS ML_avg  
                        FROM ".$_GET['pos']." a
                        WHERE 1=1 AND a.ReceivedDate BETWEEN '".$tgls."' AND '".$tglf."' 
                        GROUP BY ReceivedDate
                        ORDER BY ReceivedDate DESC";
    }     
    $queryDetail2 = mysql_query(str_replace(' DESC', ' ASC', $sqlDetail)); //var_dump(mysql_num_rows($queryDetail2));
    $queryDetail3 = mysql_query($sqlDetail); //var_dump(mysql_num_rows($queryDetail3));
} 
//var_dump($sqlDetail);
$queryDetail = mysql_query($sqlDetail); //var_dump(mysql_num_rows($queryDetail));

if($_GET['tpos'] == 'PDA'){
    $iconStatus = 'trending-up';
    $iconUnit = 'm';
    $siaga['1'] = 'Siaga 1';
    $siaga['2'] = 'Siaga 2';
    $siaga['3'] = 'Siaga 3';
    $siaga['4'] = 'Siaga 4';
}else if($_GET['tpos'] == 'PCH'){
    $iconStatus = 'cloud-drizzle';
    $iconUnit = 'mm';
    $siaga['1'] = 'Sangat Lebat';
    $siaga['2'] = 'Lebat';
    $siaga['3'] = 'Sedang';
    $siaga['4'] = 'Cerah';
}else{
    $iconStatus = 'circle';
    $iconUnit = '';
}

?>
                    <link rel="stylesheet" href="<?php echo THEME_BASEURL; ?>assets/vendor/css/daterangepicker.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">POS <?php echo $_GET['tpos'].' '.strtoupper($rowInfo['nama_alat']);?> <!-- (<?php echo number_format($rowInfo['lat'],6,'.',',');?>, <?php echo number_format($rowInfo['lng'],6,'.',',');?>) --></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="btn-group btn-group-sm " style="margin-right: 20px;">
                                        <?php if($type == 'minutely'){ $activeType = 'active'; }else{  $activeType = ''; } ?>
                                        <button class="btn btn-outline-secondary <?php echo $activeType; ?>" onclick="refreshBy('minutely')">Minutely</button>
                                        <?php if($type == 'hourly'){ $activeType = 'active'; }else{  $activeType = ''; } ?>
                                        <button class="btn btn-outline-secondary <?php echo $activeType; ?>" onclick="refreshBy('hourly')">Hourly</button>
                                        <?php if($type == 'daily'){ $activeType = 'active'; }else{  $activeType = ''; } ?>
                                        <button class="btn btn-outline-secondary <?php echo $activeType; ?>" onclick="refreshBy('daily')">Daily</button>
                                    </div>
                                        <input type="hidden" name="type" id="type" value="<?php echo $type; ?>">
                                    <div class="d-flex align-items-center">
                                      <input type="text" class="form-control form-control-sm" name="daterange" id="daterange" style="width:200px !important; text-align: center;" value="<?php echo $filterRangeDate; ?>" />
                                    </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-9 mb-4">
                                    <div class="row">
                                        <div class="col-12 mb-4">
                                            <div class="card">
                                                <div class="card-body">
                                                    <?php if($_GET['pos'] == 'bdgkatulampa') { ?>
                                                    <div id="chart-pos-pdapch" style="height: 350px; margin-top:-5px;"></div>
                                                    <?php }else if($_GET['tpos'] == 'PDA') { ?>
                                                    <div id="chart-pos-pda" style="height: 350px; margin-top:-5px;"></div>
                                                    <?php }else if($_GET['tpos'] == 'PCH') { ?>
                                                    <div id="chart-pos-pch" style="height: 350px; margin-top:-5px;"></div>
                                                    <?php }else { ?>
                                                    <div id="chart-pos-none" style="height: 350px; margin-top:-5px;"></div>
                                                    <?php } ?>
                                                    <div class="row" style="padding-top: 20px; padding-left: 40px;">
                                                        <div class="col-3">
                                                            <div class="pe-4">
                                                                <small><?php echo $siaga['1']; ?></small>
                                                                <h5><i data-feather="<?php echo $iconStatus; ?>" class="text-danger" style="font-weight: bold;"></i> &nbsp;<?php echo number_format($rowInfo['siaga1']/100,0,',','.');?> <?php echo $iconUnit; ?></h5>
                                                            </div>
                                                        </div>
                                                        <div class="col-3">
                                                            <div class="pe-4">
                                                                <small><?php echo $siaga['2']; ?></small>
                                                                <h5><i data-feather="<?php echo $iconStatus; ?>" class="text-warning" style="font-weight: bold;"></i> &nbsp;<?php echo number_format($rowInfo['siaga2']/100,0,',','.');?> <?php echo $iconUnit; ?></h5>
                                                            </div>
                                                        </div>
                                                        <div class="col-3">
                                                            <div class="pe-4">
                                                                <small><?php echo $siaga['3']; ?></small>
                                                                <h5><i data-feather="<?php echo $iconStatus; ?>" class="text-primary" style="font-weight: bold;"></i> &nbsp;<?php echo number_format($rowInfo['siaga3']/100,0,',','.');?> <?php echo $iconUnit; ?></h5>
                                                            </div>
                                                        </div>
                                                        <div class="col-3">
                                                            <div class="pe-4">
                                                                <small><?php echo $siaga['4']; ?></small>
                                                                <h5><i data-feather="<?php echo $iconStatus; ?>" class="text-success" style="font-weight: bold;"></i> &nbsp;<?php echo number_format($rowInfo['siaga4']/100,0,',','.');?> <?php echo $iconUnit; ?></h5>
                                                            </div>                                                
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-6 mb-4">
                                            <div class="card">
                                                <div
                                                    class="card-header d-flex align-items-center justify-content-between">
                                                    <h5 class="mb-0 me-3"><i data-feather="archive" class="" style="font-size: 0.8rem;"></i>&nbsp; Information</h5>
                                                </div>
                                                <div class="card-body">
                                                    <div class="row">
                                                        <div class="col-6">
                                                            <div class="pe-4">
                                                                <small>ID Loc</small>
                                                                <h5>BJR02</h5>
                                                            </div>
                                                        </div>
                                                        <div class="col-6">
                                                            <div class="pe-4">
                                                                <small>Type</small>
                                                                <h5><?php echo $_GET['tpos'];?></h5>
                                                            </div>
                                                        </div>
                                                        <div class="col-12">
                                                            <div class="pe-4">
                                                                <small>Location Name</small>
                                                                <h5><?php echo strtoupper($rowInfo['nama_alat']); ?></h5>
                                                            </div>
                                                        </div>
                                                        <div class="col-12">
                                                            <div class="pe-4">
                                                                <small>Coordinate</small>
                                                                <h5><?php echo number_format($rowInfo['lat'],6,'.',',');?>, <?php echo number_format($rowInfo['lng'],6,'.',',');?></h5>
                                                            </div>
                                                        </div>
                                                        <?php
                                                        
                                                        ?>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-6 mb-4">
                                            <div class="card">
                                                <div
                                                    class="card-header d-flex align-items-center justify-content-between">
                                                    <h5 class="mb-0 me-3"><i data-feather="map-pin" class="" style="font-size: 0.8rem;"></i>&nbsp; Map Location</h5>
                                                </div>
                                                <div class="card-body" style="padding: 0px; overflow: hidden;">
                                                    <div id="googleMap" style="width:100%;height:300px;"></div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <!--div class="row">
                                        <div class="col-6 mb-4">
                                            <div class="card">
                                                <div
                                                    class="card-header d-flex align-items-center justify-content-between">
                                                    <h5 class="mb-0 me-3"><i data-feather="map-pin" class="" style="font-size: 0.8rem;"></i>&nbsp; Map Location</h5>
                                                </div>
                                                <div class="card-body">
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-6 mb-4">
                                            <div class="card">
                                                <div
                                                    class="card-header d-flex align-items-center justify-content-between">
                                                    <h5 class="mb-0 me-3"><i data-feather="battery-charging" class="" style="font-size: 0.8rem;"></i> &nbsp; Status Battery</h5>
                                                </div>
                                                <div class="card-body">
                                                    <div id="battery" style="height: 250px; margin-top:-5px;"></div>
                                                </div>
                                            </div>
                                        </div>
                                    </div-->

                                </div>
                                <div class="col-3 mb-4">
                                    <!--div class="card mb-4">
                                        <div class="card-body">
                                            
                                        </div>
                                    </div-->
                                    <div class="card">
                                        <!--Header-->
                                        <div class="d-flex align-items-center justify-content-between card-header">
                                          <h5 class="mb-0 me-3">Recent Activity</h5>
                                          <a href="<?php echo BASEURL; ?>includes/downloadDataTMA.php?pos=<?php echo $_GET['pos'];?>&tgls=<?php echo $tgls;?>&tglf=<?php echo $tglf;?>&tpos=<?php echo $_GET['tpos'];?>" class="btn btn-sm btn-gray">Download</a>
                                        </div>

                                        <!--Activity list-->
                                        <div class="list-group list-group-flush">
                                        <?php
                                        $i=0;
                                        while($rowDetail = mysql_fetch_array($queryDetail)){
                                            $i++;
                                            $wLevel = $rowDetail['WLevel_max'];
											$tmaok=$wLevel/100;
                                            //$rowDetail['WLevel'] += 380;
                                            if($_GET['tpos'] == 'PDA') {
                                                if($wLevel <= $rowInfo['siaga4']){
                                                    $rowDetail['iconLevel'] = '';
                                                    $rowDetail['iconStatusTxt'] = 'info';
                                                }else if($wLevel < $rowInfo['siaga3']){
                                                    $rowDetail['iconLevel'] = '4';
                                                    $rowDetail['iconStatusTxt'] = 'success';
                                                }else if($wLevel < $rowInfo['siaga2']){
                                                    $rowDetail['iconLevel'] = '3';
                                                    $rowDetail['iconStatusTxt'] = 'primary';
                                                }else if($wLevel < $rowInfo['siaga1']){
                                                    $rowDetail['iconLevel'] = '2';
                                                    $rowDetail['iconStatusTxt'] = 'warning';
                                                }else{
                                                    $rowDetail['iconLevel'] = '1';
                                                    $rowDetail['iconStatusTxt'] = 'danger';
                                                }
                                            } else if($_GET['tpos'] == 'PCH') { 
                                                if($rowDetail['Hujan'] <= $rowInfo['siaga4']){
                                                    $rowDetail['iconLevel'] = '';
                                                    $rowDetail['iconStatusTxt'] = 'info';
                                                }else if($rowDetail['Hujan'] < $rowInfo['siaga3']){
                                                    $rowDetail['iconLevel'] = '4';
                                                    $rowDetail['iconStatusTxt'] = 'success';
                                                }else if($rowDetail['Hujan'] < $rowInfo['siaga2']){
                                                    $rowDetail['iconLevel'] = '3';
                                                    $rowDetail['iconStatusTxt'] = 'primary';
                                                }else if($rowDetail['Hujan'] < $rowInfo['siaga1']){
                                                    $rowDetail['iconLevel'] = '2';
                                                    $rowDetail['iconStatusTxt'] = 'warning';
                                                }else{
                                                    $rowDetail['iconLevel'] = '1';
                                                    $rowDetail['iconStatusTxt'] = 'danger';
                                                }
                                            }
                                        ?>
                                            <a href="#!" class="list-group-item list-group-item-action px-3">
                                                <div class="d-flex align-items-center justify-content-between">
                                                <?php if($rowDetail['iconLevel'] != ''){ ?>
                                                  <div
                                                    class="size-30 rounded-circle bg-white text-<?php echo $rowDetail['iconStatusTxt']; ?> shadow fw-bolder lh-1 d-flex align-items-center justify-content-center flex-shrink-0 me-4" data-bs-original-title="Peringatan! Siaga <?php echo $rowDetail['iconLevel']; ?>" data-bs-placement="top" data-bs-toggle="tooltip">
                                                    <?php echo $rowDetail['iconLevel']; ?>
                                                  </div>
                                                <?php } ?>
                                                  <div class="flex-grow-1 pe-3 overflow-hidden">
                                                    <h6 class="mb-0 text-truncate"><?php echo date('H:i',strtotime($rowDetail['ReceivedTime2']));?></h6>
                                                    <small class="text-muted d-block text-truncate"><?php echo date('d/m/Y',strtotime($rowDetail['ReceivedDate']));?></small>
                                                  </div>
                                                  <div class="flex-shrink-0 text-<?php echo $rowDetail['iconStatusTxt']; ?> text-end">
                                                    <strong class="d-block lh-1">
                                                        <?php if($_GET['tpos']=="PDA") {
                                                            echo number_format($tmaok,2).' m';
                                                        } else { 
                                                            echo number_format($rowDetail['Rain_sum'],1).' mm'; }?>
                                                    </strong>
                                                    <small class="text-muted">Battery <?php echo number_format($rowDetail['ML_avg'],1,',','.');?> v</small>
                                                  </div>
                                                </div>
                                            </a>
                                        <?php 
                                        } 
                                        ?>
                                          
                                        </div>
                                      </div>
                                        
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>