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

//echo $action;

switch ($facilityTypeNumber) {
  case "1":
  $facilityType = 'All';
  break;
  case "2":
  $facilityType = 'Pharmacy';
  break;
  case "3":
  $facilityType = 'Hospital';
  break;
  case "4":
  $facilityType = 'Diagnostics';
  break;
  case "5":
  $facilityType = 'Optical';
  break;
  case "6":
  $facilityType = 'Dental';
  break;
  case "7":
  $facilityType = 'Specialist';
  break;
  case "8":
  $facilityType = 'Clinic';
  break;

  default:
  $facilityType = 'Facility type unknown';
}
//////////// local API KEY ////////////
$apikey = $_SESSION['apikey'];
$username = $_SESSION['facility_name'];
$password = $_SESSION['password'];
$providerId = $_SESSION['provider_id'];
$member_no = $_SESSION['member_no']; 

$long = $_POST['long'];
$lat = $_POST['lat'];


$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'];

 ////////////////// RETURN API DETAILS FOR ONLINE QUERY /////////////////

if ($get_saved_record_json['STATCODE'] != "200") {
    # code...
}else{
  foreach ($savedRecord as $savedRecords){
    $method = $savedRecords['method'];
    $api_key = $savedRecords['api_key'];
    $guid = $savedRecords['guid'];
    $user = $savedRecords['user'];
    $passcode = $savedRecords['passcode'];
  }
}


include_once("search_form.php");


if ($action == "all_providers" || $action == "hospital" ||  $action == "laboratory" ||  $action == "pharmacy" ||  $action == "optical" ||  $action == "dental" ||  $action == "specialist" ||  $action == "clinic"){

//////////////////////// RETURN RESULTS FROM ONLINE //////////////////
  $fields = array(
    'method' => urlencode($method),
    'api_key' => urlencode($api_key),
    'guid' => urlencode($guid),
    'user' => urlencode($user),
    'passcode' => urlencode($passcode),
    'facility_type' => urlencode($facilityType),
    'user_cordinate' => $lat.','.$long,
    'region' => $region,
    'city' => $city,
    'area' => $area,
    'member_no' => $member_no,

  );

  $result = onlineAPI($fields);
  $decodeResult = json_decode($result,"true");
    //print_r(json_encode($fields));
}

//////////////// TextBox search ////////////////
if ($action == "searchFacility"){
  $facilityType = $_POST['facility_type_hide'];
  list($long, $lat) = explode("@@",$_POST['idd2']);
  $region = $_POST['region'];
  $city = $_POST['city'];
  $area = $_POST['area'];

  $fields = array(
    'method' => urlencode($method),
    'api_key' => urlencode($api_key),
    'guid' => urlencode($guid),
    'user' => urlencode($user),
    'passcode' => urlencode($passcode),
    'facility_type' => urlencode($facilityType),
    'user_cordinate' => $lat.','.$long,
    'region' => $region,
    'city' => $city,
    'area' => $area,
    'member_no' => $member_no,

  );

  $resultSearch = onlineAPI($fields);
  $decodeResult = json_decode($resultSearch,"true");
    //print_r(json_encode($fields));
}



$count = 0;
$sorted_reviews = array();
if ($decodeResult == "" || is_null($decodeResult)){?>
  <div class="alert alert-solid-danger alert-bold" role="alert">
    <div class="alert-text"><?php echo "No Record Found, Please contact your IT administrator"; ?></div>
  </div>

<?php }else{
  foreach ($decodeResult as $decodeResults){
    $facility_name = $decodeResults['facility_name'];
    $provider_id = $decodeResults['provider_id'];
    $facility_type = $decodeResults['facility_type'];
    $facility_image = BASE_URL . 'assets/images/hmsplace.png'; //$decodeResults['facility_photo'];
    $facility_tel_number = $decodeResults['facility_tel_number'];
    $facility_tel_number_2 = $decodeResults['facility_tel_number_2'];
    $facility_region = $decodeResults['facility_region'];
    $facility_email = $decodeResults['facility_email'];
    $area = $decodeResults['area'];
    $distance1 = $decodeResults['distance'];
    $noimage = BASE_URL . 'assets/images/hmsplace.png';
    $count ++;
    ?>

    <style type="text/css">
      #kt_modal_details {
        background: rgba(0,0,0,0.7);
      }
    </style>
    <form>
      <input type="hidden" name="facility_type_hide" id="facility_type_hide" value="<?php echo $facilityType ?>">
    </form>
    <div id="hideCurrentResult">
      <div class="kt-portlet">
        <div class="kt-portlet__head">
          <div class="kt-portlet__head-label">
            <span class="kt-portlet__head-icon">
              <i class=""><?php echo $count; ?></i>
            </span>
            <h5 style="text-align: center;" data-toggle="modal" data-target="#kt_modal_details" onclick="getstep2('provider_details', '<?php echo $provider_id."@@".$distance1."@@".$facility_name ?>','main-content-details')"><span style="color: #5d78ff; cursor: pointer;">
              <?php echo strtoupper($facility_name) ?></span></h5>
            </div>
            <span style="padding-top:15px;"><?php echo strtoupper(($facilityType == 'All') ? $facility_type : $facilityType )  ?></span>
          </div>
          <div class="kt-portlet__body">
           <div class="row">
             <div class="col-md-12">



              <div class="tab-content">
                <div class="tab-pane active" id="kt_widget5_tab1_content" aria-expanded="true">
                  <div class="kt-widget5">
                    <div class="kt-widget5__item">
                      <div class="kt-widget5__content">
                        <div class="kt-widget5__pic">
                          <?php if (empty($facility_image) || is_null($facility_image)) {?>
                            <img class="kt-widget7__img" src="<?php if ($facility_image == "" || is_null($facility_image) ){echo $noimage;}else{ echo $facility_image; } ?>" alt="" width="110" height="100">
                          <?php }else{ ?>
                            <div class="demo-gallery" style="width: 100px">
                              <ul id="lightgallery" class="list-unstyled row lightgallery1">

                                <li class="col-xs-12 col-sm-12 col-md-12" data-responsive="<?php echo $facility_image ?> 375, <?php echo $facility_image ?> 480, <?php echo $facility_image ?> 800" data-src="<?php echo $facility_image ?>" data-sub-html="">
                                  <a href="">
                                    <img class="img-responsive" src="<?php echo $facility_image ?>" style='text-align:center; height:115px; object-fit: cover;'>
                                  </a>
                                </li>
                              </ul>
                            </div>
                          <?php } ?>



                        </div>
                        <div class="kt-widget5__section">
                          <a href="javascript:;" class="kt-widget5__title">

                           <?php echo ucwords(($facility_region == '' || is_null($facility_region)) ? "Unknown" : $facility_region ) ?>
                         </a>
                         <p class="kt-widget5__desc">
                          <?php echo ucwords(($area == '' || is_null($area)) ? "Unknown Area" : $area ) ?>
                        </p>
                        <div class="kt-widget5__info">
                          <span></span>
                          <span class="kt-font-info">
                            <a href="tel:<?php echo $facility_tel_number ?>">
                              <?php echo ucwords(($facility_tel_number == '' && $facility_tel_number_2 == '') ? "Unknown Contact" : $facility_tel_number.'/'.$facility_tel_number_2 ) ?>
                            </a>
                          </span>

                          <p class="kt-font-info">
                            <a href="email:<?php echo $facility_email ?>">
                              <?php echo ucwords(($facility_email == '' || is_null($facility_email)) ? "Unknown email" : $facility_email ) ?>
                            </a>

                          </p>


                        </div>
                      </div>
                    </div>
                    <div class="kt-widget5__content">
                      <div class="kt-widget5__stats">
                        <span class="kt-widget5__number">
                          <span style="height:24px;" 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>
                        <span class="kt-widget5__sales"></span>
                      </div>
                      <div class="kt-widget5__stats">
                        <span class="kt-widget5__number">Unknown</span>
                        <span class="kt-widget5__number">Unknown</span>
                        <span class="kt-widget5__number">Unknown</span>
                        <span class="kt-widget5__number">Unknown</span>
                        <span class="kt-widget5__number">Unknown</span>

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


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

<?php }
} ?>



<div class="modal modal-stick-to-bottom fade" id="kt_modal_details" role="dialog" data-backdrop="true" onclick="" >
 <div class="modal-dialog modal-lg" role="document">
  <div class="modal-content">
   <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLabel"> <button class="btn btn-primary" onclick="closeModal()">Back</button> <span id="facility_name"></span> </h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
    </button>
  </div>
  <div class="modal-body" style="background-color:#fafafa">

    <div id="main-content-details">


    </div>

    
  </div>

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

<?php 
if (checkInternet() == true) {?>
  <script type="text/javascript">
   $(document).ready(function(){
    //$('#lightgallery1').lightGallery();

    $('.lightgallery1').each(function(i, v) {
    $(v).lightGallery({
      thumbnail: false
    });
  });

    $('.search-box input[type="text"]').on("keyup input", function(){
         //alert('ddsd');
         var inputVal = $(this).val();
         var resultDropdown = $(this).siblings(".result");
         if(inputVal.length){
          $.get('<?php echo BASE_URL ?>'+"search/backend-search.php", {term: inputVal}).done(function(data){
           resultDropdown.show();
           resultDropdown.html(data);
         });
        } else{
          resultDropdown.hide();
          resultDropdown.empty();
        }
      });

        //Set search input value on click of result item
        $(document).on("click", ".result p", function(){
         $(this).parents(".search-box").find('input[type="text"]').val($(this).text());
            //$(this).parent(".result").empty();
            $(this).parent(".result").hide();
          });

        $('.search-box1 input[type="text"]').on("keyup input", function(){
         //alert('ddsd');
         var inputVal = $(this).val();
         var resultDropdown = $(this).siblings(".result1");
         if(inputVal.length){
          $.get('<?php echo BASE_URL ?>'+"search/backend-search.php", {term: inputVal}).done(function(data){
           resultDropdown.show();
           resultDropdown.html(data);
         });
        } else{
          resultDropdown.hide();
          resultDropdown.empty();
        }
      });
        //Set search input value on click of result item
        $(document).on("click", ".result1 p", function(){
         $(this).parents(".search-box1").find('input[type="text"]').val($(this).text());
            //$(this).parent(".result").empty();
            $(this).parent(".result1").hide();
          });

      });
    </script>
  <?php }else{
    echo "<script>alert('Please Check your internet connection')</script>";
  }

  ?>

  <script type="text/javascript">
    function closeModal(){
      $("#kt_modal_details").modal("hide");
    }
  </script>               