<?php
 session_start();
 include("../../php_crud/my_class.php");
 $obj = new my_class();
 extract($_GET);
//  print_r($_GET);
 $district_no = $_GET['district_info'];
 // echo $id ;         
//   exit();
//district start

// select GW_STAT.WELL_ID,GW_STAT.OLD_ID,GW_STAT.MANUAL_LAT,GW_STAT.MANUAL_LON,GW_STAT.SITE,
// GW_STAT.SUBDIVISION_NAME,GW_STAT.DISTRICT_NAME,GW_STAT.UPZL_NAME,ST_STATUS.LASTRECEIVE_DATE,ST_STATUS.LASTRECEIVE_DATA,ST_STATUS.STATION_STATUS
                            
// from HYDROLOGY_STATIONS_CURRENT_STATUS ST_STATUS LEFT JOIN GW_STATION_AUTO_MANUAL  GW_STAT ON GW_STAT.WELL_ID=ST_STATUS.STATION_ID
// where ST_STATUS.ST_DATA_TYPE='GroundWaterDaily' AND ',' || GW_STAT. WELL_DATA_TYPE || ',' LIKE '%,' || '1' ||',%' AND  DISTRICT_NO='47'
// AND GW_STAT.DATA_READ_TYPE IN ('MANUAL','BOTH') AND GW_STAT.MANUAL_LAT IS NOT NULL AND GW_STAT.MANUAL_LON IS NOT NULL ORDER BY GW_STAT.WELL_ID ASC
// if($id=="" && $district_no!=""){
//   //echo "line 12<br/>";
//   $where_cond = " ST_STATUS.ST_DATA_TYPE='GroundWaterDaily' AND ',' || GW_STAT. WELL_DATA_TYPE || ',' LIKE '%,' || '$id' ||',%' AND  DISTRICT_NO='$district_no'
//   AND GW_STAT.DATA_READ_TYPE IN ('MANUAL','BOTH') AND GW_STAT.MANUAL_LAT IS NOT NULL AND GW_STAT.MANUAL_LON IS NOT NULL ORDER BY GW_STAT.WELL_ID ASC";
// } else 
if ($id!="" && $district_no!=""){
  //echo "line 16<br/>";
  $where_cond = "',' || GW_STAT. WELL_DATA_TYPE || ',' LIKE '%,' || '$id' ||',%' AND  DISTRICT_NO='$district_no'
  AND GW_STAT.DATA_READ_TYPE IN ('MANUAL','BOTH') AND GW_STAT.MANUAL_LAT IS NOT NULL AND GW_STAT.MANUAL_LON IS NOT NULL ORDER BY GW_STAT.WELL_ID ASC";
}
else if($id!="" && $district_no ==""){
  //echo "line 21<br/>";
  $where_cond = "',' || GW_STAT. WELL_DATA_TYPE || ',' LIKE '%,' || '$id' ||',%' AND GW_STAT.DATA_READ_TYPE IN ('MANUAL','BOTH') AND GW_STAT.MANUAL_LAT IS NOT NULL AND 
GW_STAT.MANUAL_LON IS NOT NULL ORDER BY GW_STAT.WELL_ID ASC";
}else{
  //echo "line 25<br/>";
  //default cond
  // $where_cond = "',' || GW_STAT. WELL_DATA_TYPE || ',' LIKE '%,' || '1' ||',%' AND GW_STAT.DATA_READ_TYPE IN ('MANUAL','BOTH') AND GW_STAT.MANUAL_LAT IS NOT NULL AND GW_STAT.MANUAL_LON IS NOT NULL ORDER BY GW_STAT.WELL_ID ASC";
  $where_cond = "GW_STAT.DATA_READ_TYPE IN ('MANUAL','BOTH') AND GW_STAT.MANUAL_LAT IS NOT NULL AND GW_STAT.MANUAL_LON IS NOT NULL ORDER BY GW_STAT.WELL_ID ASC";
}
//echo $where_cond;
foreach($obj->View_colmn_By_Cond("HYDROLOGY_STATIONS_CURRENT_STATUS ST_STATUS LEFT JOIN GW_STATION_AUTO_MANUAL  GW_STAT ON GW_STAT.WELL_ID=ST_STATUS.STATION_ID","GW_STAT.WELL_ID,GW_STAT.OLD_ID,GW_STAT.MANUAL_LAT,GW_STAT.MANUAL_LON,GW_STAT.SITE,
GW_STAT.SUBDIVISION_NAME,GW_STAT.DISTRICT_NAME,GW_STAT.UPZL_NAME,ST_STATUS.LASTRECEIVE_DATE,ST_STATUS.LASTRECEIVE_DATA,ST_STATUS.STATION_STATUS",$where_cond) as $gw_daily_district){
	//print_r($gw_daily_district);
  $gw_daily_well_id      = $gw_daily_district['WELL_ID'];
  $gw_daily_site         = $gw_daily_district['SITE'];
  $gw_daily_old_id       = $gw_daily_district['OLD_ID'];
  $gw_daily_dist         = $gw_daily_district['DISTRICT_NAME'];
  $gw_daily_subdiv       = $gw_daily_district['SUBDIVISION_NAME'];
  $gw_daily_upzl         = $gw_daily_district['UPZL_NAME'];
  $gw_daily_latitude     = $gw_daily_district['MANUAL_LAT'];
  $gw_daily_longitude    = $gw_daily_district['MANUAL_LON'];
  //fetch last data receive date with value gwd start
  // $gw_daily_tbl_name           = 'HYDROLOGY.GWT_DILY_TBL@HYDRO10';
  // $gw_daily_stat_name          = $gw_daily_well_id ;
  // $gw_daily_date_col           = 'DT';
  // $gw_daily_val_col            = 'WATER_TBL';
  // $gw_daily_station_col        = 'WELL_ID';
  // $gw_daily_last_date_val      = $obj->show_last_read_date_with_val($gw_daily_tbl_name ,$gw_daily_stat_name,$gw_daily_date_col,$gw_daily_val_col,$gw_daily_station_col);
  // $gw_daily_last_read_date     = $gw_daily_last_date_val['READ_DATE'];
  // $gw_daily_last_read_val      = $gw_daily_last_date_val['READ_VAL'];
  $gw_daily_station_status     = $gw_daily_district['STATION_STATUS'];
  $gw_daily_last_read_date     = $gw_daily_district['LASTRECEIVE_DATE'];
  $gw_daily_last_read_val      = $gw_daily_district['LASTRECEIVE_DATA'];
  
  if ($gw_daily_station_status == 'ACTIVE') {
    $marker_icon = 'asset/custom/dashboard_active_map.png';
  } else {
    $marker_icon = 'asset/custom/dashboard_inactive_map.png';
  }
  
  //fetch last data receive date with value gwd end

  $gwd_well_id           = "";
  $gwd_site              = "";
  $gwd_old_id            = "";
  $gwd_latitude          = "";
  $gwd_longitude         = "";
  $gwd_last_read_date    = "";
  $gwd_last_read_val     = "";
  $gwd_dist              = "";
  $gwd_subdiv            = "";
  $gwd_upzl              = "";

  if($gw_daily_well_id   == ''){
    $gwd_well_id    = '-';
  }else{
    $gwd_well_id    = $gw_daily_well_id  ;
  }
  if($gw_daily_old_id   == ''){
    $gwd_old_id  = '-';
  }else{
    $gwd_old_id  = $gw_daily_old_id ;
  }
  if($gw_daily_site    == ''){
    $gwd_site   = '-';
  }else{
    $gwd_site   = $gw_daily_site  ;
  }
  if($gw_daily_latitude   == ''){
    $gwd_latitude   = '-';
  }else{
    $gwd_latitude   = $gw_daily_latitude  ;
  }
  if($gw_daily_longitude  == ''){
    $gwd_longitude = '-';
  }else{
    $gwd_longitude  = $gw_daily_longitude ;
  }
  if($gw_daily_last_read_val   == ''){
    $gwd_last_read_val  = '-';
  }else{
    $gwd_last_read_val = $gw_daily_last_read_val.'m' ;
  }
  if($gw_daily_last_read_date   == ''){
    $gwd_last_read_date = '-';
  }else{
    $gwd_last_read_date = $gw_daily_last_read_date  ;
  }
  if($gw_daily_dist  == ''){
    $gwd_dist  = '-';
  }else{
    $gwd_dist  = $gw_daily_dist  ;
  }
  if($gw_daily_subdiv   == ''){
    $gwd_subdiv  = '-';
  }else{
    $gwd_subdiv  = $gw_daily_subdiv ;
  }
  if($gw_daily_upzl  == ''){
    $gwd_upzl  = '-';
  }else{
    $gwd_upzl  = $gw_daily_upzl  ;
  }

  $gw_daily_districts[] = [
    'well_id'                 => $gwd_well_id,
    'old_id'                  => $gwd_old_id,
    'site'                    => $gwd_site,
    'lat'                     => $gwd_latitude,
    'lon'                     => $gwd_longitude,
    'last_read_date'          => $gwd_last_read_date,
    'last_read_val'           => $gwd_last_read_val,
    'district'                => $gwd_dist,
    'sub-division'            => $gwd_subdiv,
    'upzl'                    => $gwd_upzl,
    'gwd_icon'                => $marker_icon

  ];
  
 }
// print_r($gw_daily_districts);
?>
<!--map link start-->
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<script src = "module/ground_water_ex/js/leaflet-providers.js"></script>
<!--map link end-->
<style>
  /*map css start*/
          #map{
       	   width: 100%;
           height: 100vh;
    	   }
   	  .map-info-box {
       	   background-color: #fff;
           border: 1px solid #d3d3d3;
           width: 250px;
           /* height:300px; */
           padding: 10px;
           border-radius: 12px;
           z-index: 500;
           position: absolute;
           right: 45px;
           top: 165px;
           cursor: auto;
           display: none; /* Initially hide the info box */ 
           }
           .close {
           font-size: 25px;
           font-weight: 600;
           cursor: pointer;
           float: right;
           }
		  /*map css end*/
</style>

<!--map start-->
<div class="col-sm-12">
    <div id="gwm-map" style="width: 100%; height: 650px"></div>
   
  <!--gwm district pointer start-->
  <!-- <div id="gwm-district" class="map-info-box">
	    <span class="close" onclick="closeGwmLabelMapInfoBox()">&times;</span> 
	    <div id="gwm-dis-pointer"></div>
 </div> -->
 <!--gwm district pointer end-->
</div>
 <!--map end-->

<script type="text/javascript">
  var gwdMapInfoBox;
  var activeMarker = null; // Keep track of the active marker

  window.onload = function(){
      show_ground_water_mapinfo(data_type); // Call your function when the window is loaded
  };

//////////////////////////////////////////////////
function updateMapWithSelectedValues(data_type) {
//alert(data_type);
    if (data_type === '1') {
      show_ground_water_mapinfo(1);
    } else if (data_type === '2') {
      show_ground_water_mapinfo(2);
    } else if (data_type === '3') {
      show_ground_water_mapinfo(3);
    } else {
       
    }
}
///////////////////////////////////////////////////
    //ground water daily start
  function show_ground_water_mapinfo(data_type){
  // alert(data_type);
 //  alert('324342');
var gwdmap = L.map('gwm-map').setView([23.8103, 90.4125], 7);
							// 	icon: gwdIcon
							// });
							// var gwdIcon = L.icon({
							// 	//iconUrl: 'http://www.hydrology.bwdb.gov.bd/map_icons/gw_daly_circle.png',
							// 	iconUrl: '../asset/custom/hydro_map_icon.png',
							// 	iconSize: [16, 16], // size of the icon		
							// });

							//Add the OpenStreetMap tile layer to the map
							L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
								// attribution: 'Map data © OpenStreetMap contributors',
								maxZoom: 18
							}).addTo(gwdmap);

							// Common coordinates for the markers

							// Add markers for each district with popups on hover
							var gw_daily_districts = <?php echo json_encode($gw_daily_districts); ?>;
							gwdMapInfoBox = document.getElementById('gwd-dis');
							var gwdpointer = document.getElementById('gwd-pointer');

							for (var i = 0; i < gw_daily_districts.length; i++) {
								(function(index) {
									var gwd_district = gw_daily_districts[index];
									var marker = L.marker([gwd_district.lat, gwd_district.lon], {
										icon: L.icon({
											iconUrl:gwd_district.gwd_icon,
											iconSize: [13, 13],
										}),


									}).addTo(gwdmap);
									//    marker.on('click', function(e) {
									//     // Display the modal when the marker is clicked
									//     displaygwdailyModal(gwd_district.name);    
									// });
									// Create and bind a popup to the marker
									// 	//    marker.bindPopup("<h4>"+gwd_district.name+"</h4><p>station name<h5>"+gwd_district.name+"</h5></p>", { autoPan: false });
									// 	marker.bindPopup(
									//     "<div style='background-color: #ffffff; color: #33334d;'>" +
									//     "<b style='font-size:15px;'>Ground Water Daily </b><br><b>Station Name: " +gwd_district.name+ "</b>" +
									//     "</div>",
									//     {
									//         autoPan: false
									//     }
									// );

									gwd_data = '<table class="custom-table">' +
										'<tr><td style = "background:#1e90ff; color:white;">Well Id</td><td style = "background:#1e90ff; color:white;" >'+gwd_district.well_id+'</td></tr>' +
										'<tr><td style = "background:#1e90ff; color:white;">Old Id</td><td style = "background:#1e90ff; color:white;" >'+gwd_district.old_id+'</td></tr>' +
										'<tr><td style = "background:#1e90ff; color:white;">District</td><td style = "background:#1e90ff; color:white;" >'+gwd_district.district+'</td></tr>' +
										'<tr><td style = "background:#1e90ff; color:white;">Upzila Name</td><td style = "background:#1e90ff; color:white;" >'+gwd_district.upzl+'</td></tr>' +
										//'<tr><td>Subdivision Name</td><td style = "background:#1e90ff;" >'+gwd_district.sub-division+'</td></tr>' +
									        '<tr><td style = "background:#1e90ff; color:white;">Latitude</td><td style = "background:#1e90ff; color:white;" >'+gwd_district.lat+'</td></tr>' +
										'<tr><td style = "background:#1e90ff; color:white;">Longitude</td><td style = "background:#1e90ff; color:white;" >'+gwd_district.lon+'</td></tr>' +
										'<tr><td style = "background:#1e90ff; color:white;">Site</td><td style = "background:#1e90ff; color:white;" >'+gwd_district.site+'</td></tr>' +
									        '<tr><td style = "background:#0c2461; color:white;">Last Receive Date</td><td style = "background:#0c2461; color:white;" >'+gwd_district.last_read_date+'</td></tr>' +
										'<tr><td style = "background:#0c2461; color:white;">Last Read Value</td><td style = "background:#0c2461; color:white;" >'+gwd_district.last_read_val+'</td></tr>' +
										'</table>';

									marker.bindPopup(gwd_data, {
										autoPan: false,
										closeButton: false, // To disable the close button on the popup
										autoClose: false, // To keep the popup open on mouseout
										closeOnClick: false, // To keep the popup open when clicking on it

									}).on('popupopen', function(event) {
										var popup = event.popup;
										// L.DomEvent.on(document.getElementById('gwd_gp_' + index), 'click', function() {
										// 	// Call the function to display the graph modal
										// 	displaygwdailyModal(gwd_district.well_id,gwd_district.old_id,gwd_district.district,gwd_district.upzl);
										// });
									});

									// Add event listener to display the popup on hover
									marker.on('mouseover', function(e) {
										marker.openPopup();
									});

								})(i);
							}
   }
   //ground water daily end
</script>
