<?php
session_start();
$ibbws = $_GET['ibbws'];

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

//$sql = "SELECT * FROM m_lokasi WHERE 1=1 AND status_lokasi=1 AND lat IS NOT NULL AND lat <> '' AND lat <> '0' ";
$sql = "SELECT * FROM m_lokasi WHERE 1=1 AND lat IS NOT NULL AND lat <> '' AND lat <> '0' ";
//$sql .= " AND id_bbws='$ibbws'";
$sql .= " ORDER BY nama_lokasi DESC";
$query_case = mysql_query($sql);

$icoURL = BASEURL.'../images/maps/';

$result = new stdCLass();
$i = 0;
while($row_case = mysql_fetch_array($query_case)){
	$result->data[$i] = new stdCLass();
	$result->data[$i]->lat = $row_case['lat'];
	$result->data[$i]->long = $row_case['lng'];
	$result->data[$i]->last_update = $row_case['SamplingDate'].' '.$row_case['SamplingTime'];
	$result->data[$i]->nama = strtoupper(substr($row_case['id_tipe'], 0, 3)).' '.strtoupper($row_case['nama_alat']);

	if($row_case['foto_lokasi'] == null){
		$result->data[$i]->image = 'http://v2.sdatelemetry.com/cctv/default_cctv.png';
	}else{
		$result->data[$i]->image = 'http://v2.sdatelemetry.com/gambarpos/'.$row_case['foto_lokasi'];
		if ( !file_exists( $result->data[$i]->image )) {
			$result->data[$i]->image = 'http://v2.sdatelemetry.com/cctv/default_cctv.png';
		}
	}

	if(($row_case['id_tipe'] == 'PDA') || $row_case['id_tipe'] == 'PDA2'){
		if($row_case['WLevel'] >= $row_case['siaga1']){
			//$result->data[$i]->icon = $icoURL.'marker_wlevel_danger_32.png';
			$result->data[$i]->icon = 'marker_wlevel_danger_32.png';
			$result->data[$i]->color = 'd50000';
			$result->data[$i]->siaga = 'Siaga 1';
		}else if($row_case['WLevel'] >= $row_case['siaga2']){
			//$result->data[$i]->icon = $icoURL.'marker_wlevel_warning_32.png';
			$result->data[$i]->icon = 'marker_wlevel_warning_32.png';
			$result->data[$i]->color = 'ffab00';
			$result->data[$i]->siaga = 'Siaga 2';
		}else if($row_case['WLevel'] >= $row_case['siaga3']){
			//$result->data[$i]->icon = $icoURL.'marker_wlevel_info_32.png';
			$result->data[$i]->icon = 'marker_wlevel_info_32.png';
			$result->data[$i]->color = '0091ea';
			$result->data[$i]->siaga = 'Siaga 3';
		}else if($row_case['WLevel'] >= $row_case['siaga4']){
			//$result->data[$i]->icon = $icoURL.'marker_wlevel_success_32.png';
			$result->data[$i]->icon = 'marker_wlevel_success_32.png';
			$result->data[$i]->color = '64dd17';
			$result->data[$i]->siaga = 'Siaga 4';
		}else{
			//$result->data[$i]->icon = $icoURL.'marker_wlevel_success_32.png';
			$result->data[$i]->icon = 'marker_wlevel_success_32.png';
			$result->data[$i]->color = '64dd17';
			$result->data[$i]->siaga = 'Normal';
		}
		$result->data[$i]->measure_num = number_format($row_case['WLevel'],0,',','.');
		$result->data[$i]->measure_uom = 'cm';
	}else if(($row_case['id_tipe'] == 'PCH') || ($row_case['id_tipe'] == 'PCH2')){
		if($row_case['Rain'] >= $row_case['siaga1']){
			//$result->data[$i]->icon = $icoURL.'marker_rain_danger_32.png';
			$result->data[$i]->icon = 'marker_rain_danger_32.png';
			$result->data[$i]->color = 'd50000';
			$result->data[$i]->siaga = 'Siaga 1';
		}else if($row_case['Rain'] >= $row_case['siaga2']){
			//$result->data[$i]->icon = $icoURL.'marker_rain_warning_32.png';
			$result->data[$i]->icon = 'marker_rain_warning_32.png';
			$result->data[$i]->color = 'ffab00';
			$result->data[$i]->siaga = 'Siaga 2';
		}else if($row_case['Rain'] >= $row_case['siaga3']){
			//$result->data[$i]->icon = $icoURL.'marker_rain_info_32.png';
			$result->data[$i]->icon = 'marker_rain_info_32.png';
			$result->data[$i]->color = '0091ea';
			$result->data[$i]->siaga = 'Siaga 3';
		}else if($row_case['Rain'] >= $row_case['siaga4']){
			//$result->data[$i]->icon = $icoURL.'marker_rain_success_32.png';
			$result->data[$i]->icon = 'marker_rain_success_32.png';
			$result->data[$i]->color = '64dd17';
			$result->data[$i]->siaga = 'Siaga 4';
		}else{
			//$result->data[$i]->icon = $icoURL.'marker_rain_success_32.png';
			$result->data[$i]->icon = 'marker_rain_success_32.png';
			$result->data[$i]->color = '64dd17';
			$result->data[$i]->siaga = 'Normal';
		}
		$result->data[$i]->measure_num = number_format($row_case['Rain'],0,',','.');
		$result->data[$i]->measure_uom = 'mm';
	}else{
		//$result->data[$i]->icon = $icoURL.'marker_unknown_32.png';
		$result->data[$i]->icon = 'marker_unknown_32.png';
			$result->data[$i]->color = '607d8b';
			$result->data[$i]->siaga = 'Unknown';

	}
	$i++;
}
echo json_encode($result);

?>