<?php
if (!isset($_SESSION)) {
  session_start();
}
require_once("Connections/constants.php");
include("function/function.php");
$action = $_POST['act'];
list($facilityTypeNumber, $distance1, $facility_name1) = explode("@@", $_POST['id']);

$apikey = $_SESSION['apikey'];
$username = $_SESSION['facility_name'];
$password = $_SESSION['password'];
$providerId = $_SESSION['provider_id'];

  $GETONLINEAPI = array(
    'METHOD' => urlencode('ONLINEAPI'),
    'APIKEY' => $apikey,
    'USERNAME' => $username,
    'PASSWORD' => $password,
    'PROVIDERID' => $providerId,

  );
  $get_saved_record = APICall($GETONLINEAPI);
  $get_saved_record_json = json_decode($get_saved_record,"true");
  $savedRecord = $get_saved_record_json['RESULT'];
  foreach ($savedRecord as $savedRecords){
    $method = $savedRecords['method'];
    $api_key = $savedRecords['api_key'];
    $guid = $savedRecords['guid'];
    $user = $savedRecords['user'];
    $passcode = $savedRecords['passcode'];
  }

  //////////////// get single record ////////////////
if ($action == "searchFacility"){
    $fields = array(
        'method' => urlencode($method),
        'api_key' => urlencode($api_key),
        'guid' => urlencode($guid),
        'user' => urlencode($user),
        'passcode' => urlencode($passcode),
        'provider_id' => urlencode($facilityTypeNumber),
    );

    $resultSearch = onlineAPI($fields);
    $decodeResultSearch = json_decode($resultSearch,"true");
  }

  if ($action == "provider_details") {
       $fields = array(
        'method' => urlencode('GET_PROVIDER_DETAIL'),
        'api_key' => urlencode($api_key),
        'guid' => urlencode($guid),
        'user' => urlencode($user),
        'passcode' => urlencode($passcode),
        'provider_id' => urlencode($facilityTypeNumber),
    );

       //print_r(json_encode($fields));

    $resultSearch = onlineAPI($fields);
    $decodeResultSearch = json_decode($resultSearch,"true");
    $facility_name = urlencode($decodeResultSearch['facility_name']);
    $facility_location = $decodeResultSearch['facility_location'];
    $facility_type = $decodeResultSearch['facility_type'];
    $facility_image = $decodeResultSearch['facility_photo'];
    $facility_tel_number = $decodeResultSearch['facility_tel_number'];
    $facility_tel_number_2 = $decodeResultSearch['facility_tel_number_2'];
    $facility_region = $decodeResultSearch['facility_region'];
    $facility_email = $decodeResultSearch['facility_email'];
    $area = $decodeResultSearch['area'];

    $latitude = $decodeResultSearch['latitude'];
    $longitude = $decodeResultSearch['longitude'];
    
  }

 // print_r($resultSearch);

 

$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=$facility_name&key=AIzaSyBp00jF_PxWbtR0eMu2K1Fc4NeIr2silkU";
$response = file_get_contents($url);
$response1 = json_decode($response, "true");
$result = $response1['results'];
foreach ($result as $result1) {
 $place_id = $result1['place_id'];
 $formatted_address = $result1['formatted_address'];
}


$urlOpeningHours = "https://maps.googleapis.com/maps/api/place/details/json?place_id=$place_id&fields=opening_hours&key=AIzaSyBp00jF_PxWbtR0eMu2K1Fc4NeIr2silkU";
$responseOpening = file_get_contents($urlOpeningHours);
$response1Opening = json_decode($responseOpening, "true");
$opening_hours = $response1Opening['result'];

//var_dump($response1['results']);

if ($response1Opening['status'] == "OK") {
foreach ($opening_hours as $opening_hours1 ) {
  $opening_hours1 = $opening_hours1['weekday_text'];
}
list($monday, $mondaytime) = explode(": ", $opening_hours1[0]);
list($tuesday, $tuesdaytime) = explode(": ", $opening_hours1[1]);
list($wednes, $wednestime) = explode(": ", $opening_hours1[2]);
list($thurs, $thurstime) = explode(": ", $opening_hours1[3]);
list($friday, $fridaytime) = explode(": ", $opening_hours1[4]);
list($satur, $saturtime) = explode(": ", $opening_hours1[5]);
list($sun, $suntime) = explode(": ", $opening_hours1[6]);

?>
<input type="hidden" name="latitude" id="latitude" value="<?php echo $latitude ?>"> 

<input type="hidden" name="longitude" id="longitude" value="<?php echo $longitude ?>"> 

  <div class="row">
   <div class="col-md-12">
    <h3 style="text-align: center; text-transform"></h3>

    <div class="row">
      <div class="col-xl-6 col-lg-12">

        <!--Begin::Portlet-->
        <div class="kt-portlet kt-portlet--height-fluid">

          <div class="kt-portlet__body">
            <div class="tab-content">

              <div class="tab-pane active" id="kt_widget3_tab2_content">

                <!--Begin::Timeline 3 -->

                <div class="kt-timeline-v3">
                  <div class="kt-timeline-v3__items">
                    <div class="kt-timeline-v3__item kt-timeline-v3__item--info">
                      <span class="kt-timeline-v3__item-time"><?php echo ucfirst($monday) ?></span>
                      <div class="kt-timeline-v3__item-desc">
                        <span class="kt-timeline-v3__item-text">
                         <?php 
                            echo $mondaytime;    
                          ?>
                       </span><br>
                       <span class="kt-timeline-v3__item-user-name">
                        <a href="#" class="kt-link kt-link--dark kt-timeline-v3__itek-link">
                        </a>
                      </span>
                    </div>
                  </div>
                  <div class="kt-timeline-v3__item kt-timeline-v3__item--warning">
                    <span class="kt-timeline-v3__item-time"><?php echo $tuesday ?></span>
                    <div class="kt-timeline-v3__item-desc">
                      <span class="kt-timeline-v3__item-text">
                       <?php echo $tuesdaytime ?>
                     </span><br>
                     <span class="kt-timeline-v3__item-user-name">
                      <a href="#" class="kt-link kt-link--dark kt-timeline-v3__itek-link">
                        
                      </a>
                    </span>
                  </div>
                </div>
                <div class="kt-timeline-v3__item kt-timeline-v3__item--brand">
                  <span class="kt-timeline-v3__item-time"><?php echo $wednes ?></span>
                  <div class="kt-timeline-v3__item-desc">
                    <span class="kt-timeline-v3__item-text">
                      <?php echo $wednestime ?>
                    </span><br>
                    <span class="kt-timeline-v3__item-user-name">
                      <a href="#" class="kt-link kt-link--dark kt-timeline-v3__itek-link">
                        
                      </a>
                    </span>
                  </div>
                </div>
                <div class="kt-timeline-v3__item kt-timeline-v3__item--success">
                  <span class="kt-timeline-v3__item-time"><?php echo $thurs ?></span>
                  <div class="kt-timeline-v3__item-desc">
                    <span class="kt-timeline-v3__item-text">
                     <?php echo $thurstime ?>
                    </span><br>
                    <span class="kt-timeline-v3__item-user-name">
                      <a href="#" class="kt-link kt-link--dark kt-timeline-v3__itek-link">
                        
                      </a>
                    </span>
                  </div>
                </div>
                <div class="kt-timeline-v3__item kt-timeline-v3__item--danger">
                  <span class="kt-timeline-v3__item-time"><?php echo $friday ?></span>
                  <div class="kt-timeline-v3__item-desc">
                    <span class="kt-timeline-v3__item-text">
                      <?php echo $fridaytime ?>
                    </span><br>
                    <span class="kt-timeline-v3__item-user-name">
                      <a href="#" class="kt-link kt-link--dark kt-timeline-v3__itek-link">
                        
                      </a>
                    </span>
                  </div>
                </div>
                <div class="kt-timeline-v3__item kt-timeline-v3__item--info">
                  <span class="kt-timeline-v3__item-time"><?php echo $satur ?></span>
                  <div class="kt-timeline-v3__item-desc">
                    <span class="kt-timeline-v3__item-text">
                      <?php echo $saturtime ?>
                    </span><br>
                    <span class="kt-timeline-v3__item-user-name">
                      <a href="#" class="kt-link kt-link--dark kt-timeline-v3__itek-link">
                        
                      </a>
                    </span>
                  </div>
                </div>
                <div class="kt-timeline-v3__item kt-timeline-v3__item--brand">
                  <span class="kt-timeline-v3__item-time"><?php echo $sun ?></span>
                  <div class="kt-timeline-v3__item-desc">
                    <span class="kt-timeline-v3__item-text">
                      <?php echo $suntime ?>
                    </span><br>
                    <span class="kt-timeline-v3__item-user-name">
                      <a href="#" class="kt-link kt-link--dark kt-timeline-v3__itek-link">
                        
                      </a>
                    </span>
                  </div>
                </div>
              </div>
            </div>

            <!--End::Timeline 3 -->
          </div>
        </div>
      </div>
    </div>

    <!--End::Portlet-->
  </div>
  <div class="col-xl-6 col-lg-12">


    <div class="kt-portlet kt-portlet--tab">
      <div class="kt-portlet__head">
        <div class="kt-portlet__head-label">
          <span class="kt-portlet__head-icon kt-hidden">
            <i class="la la-gear"></i>
          </span>
          <h3 class="kt-portlet__head-title">
           
          </h3>
        </div>
      </div>
      <div class="kt-portlet__body">
        
          <div id="mapNew" style="width: 100%; height: 300px">
            
          </div>

      </div>
    </div>
  </div>
</div>
</div>
</div>


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<div class="kt-portlet kt-portlet--height-fluid">
  <div class="row">
   <div class="col-md-12">
    <div class="row">
      <div class="col-md-4">
        <div class="mt-2 row">
         <div class="col-8" style="padding-left: 20px">
          <h5><span style="color: #5d78ff">Specialties</span></h5>
          <p>Unknown</p>
          <p>Unknown</p>
          <p>Unknown</p>
          <p>Unknown</p>
          <p>Unknown</p>
        </div>
      </div>
    </div>
    <div class="col-md-8">
      <div class="mt-2 row">
        <label for="gender" class="col-2 col-form-label"><h5><span style="color: #5d78ff">Address:</span></h5></label>
        <div class="col-8" style="text-align: right;">
          <p><?php echo $formatted_address ?></p>
          <p><?php echo $area ?></p>
          <p style="font-weight: bolder;"> 

            <span class="kt-widget5__number"><span class="kt-badge <?php if($distance1 <= 50){ echo 'kt-badge--success';}else if ($distance1 > 50 and $distance1 < 100 ){ echo 'kt-badge--warning';}else if ($distance1 > 100){ echo 'kt-badge--danger'; } ?>  kt-badge--inline kt-badge--pill"><?php echo ucwords(($distance1 == '' || is_null($distance1)) ? "Unknown" : $distance1.' '.'km' ) ?></span></span></p>
          
        </div>
      </div>
      <div class="row">
       <div class="col-5">
         <label for="gender" class="col-2 col-form-label"><h5><span style="color: #5d78ff">Phone:</span></h5></label>
       </div>

       <div class="col-5" style="text-align: right;">
        <a href="tel:<?php echo $facility_tel_number ?>">
        <?php echo $facility_tel_number ?>
        </a>
      </div>

      <div class="col-2"></div>
      
     </div>

     <div class="row">
       <div class="col-5">
         <label for="gender" class="col-2 col-form-label"><h5><span style="color: #5d78ff">Website:</span></h5></label>
       </div>

       <div class="col-5" style="text-align: right;"><span style="color: #5d78ff;">Unknown</span></div>
       <div class="col-2"></div>
     </div>
   </div>
 </div>
</div>
</div>
</div>

<script type="text/javascript">
function initMap(){
  const latitude = document.getElementById("latitude").value;
  const longitude = document.getElementById("longitude").value;
  //alert(longitude);
  const location = { lat: parseFloat(latitude), lng:  parseFloat(longitude) };
  //alert(uluru);
// The map, centered at Uluru
const map = new google.maps.Map(document.getElementById("mapNew"), {
  zoom: 17,
  center: location,
});
var marker = new google.maps.Marker({
  position: location,
  map: map
});
}
</script>

<script async
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBp00jF_PxWbtR0eMu2K1Fc4NeIr2silkU&callback=initMap">
</script>

<?php 
  }else{?>
    <div class="alert alert-solid-danger alert-bold" role="alert">
      <div class="alert-text">Details information not available!</div>
      </div>
  <?php 
}
 ?>