<?php

if (!isset($_SESSION)) {
  session_start();
}

require_once("Connections/constants.php");
include("function/function.php");
$action = $_POST['act'];

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

if ($action == "searchByDrug") {
  $GETDRUGTYPE = array(
    'METHOD' => urlencode('DRUGTYPE1'),
    'APIKEY' => $apikey,
    'USERNAME' => $username,
    'PASSWORD' => $password,
    'PROVIDERID' => $providerId,
    'member_plan' => $member_plan,

  );
  $get_saved_record_all = APICall($GETDRUGTYPE);
  $get_saved_record_json_all = json_decode($get_saved_record_all,"true");
  $savedRecord_all = $get_saved_record_json_all['RESULT'];

  $GETDRUGTYPE = array(
    'METHOD' => urlencode('DRUGTYPE'),
    'APIKEY' => $apikey,
    'USERNAME' => $username,
    'PASSWORD' => $password,
    'PROVIDERID' => $providerId,
    'member_plan' => $member_plan,

  );
  $get_saved_record = APICall($GETDRUGTYPE);
  $get_saved_record_json = json_decode($get_saved_record,"true");
  $savedRecord = $get_saved_record_json['RESULT'];
}

//var_dump($get_saved_record);


?>
<style type="text/css">
  .list_item {
    /*background-color: #F1F1F1;*/
    -webkit-box-shadow: 0 1px 1px 0 rgba(0,0,0,.1);
    box-shadow: 0 1px 1px 0 rgba(0,0,0,.1);
    margin: 5px 15px 2px;
    padding: 2px;
    font-size: 14px;
    line-height: 1.5;
  }
  .show_more_main {
    margin: 15px 25px;
  }
  .show_more {
    background-color: #5867dd;
/*    background-image: -webkit-linear-gradient(top,#fcfcfc 0,#f8f8f8 100%);
background-image: linear-gradient(top,#fcfcfc 0,#f8f8f8 100%);*/
border: 1px solid;
border-color: #5867dd;
color: #ffffff;
font-size: 12px;
outline: 0;
}
.show_more {
  cursor: pointer;
  display: block;
  padding: 10px;
  text-align: center;
  font-weight:bold;
  float: right;
}
.loding {
  background-color: #e9e9e9;
  border: 1px solid;
  border-color: #c6c6c6;
  color: #333;
  font-size: 12px;
  display: block;
  text-align: center;
  padding: 10px 0;
  outline: 0;
  font-weight:bold;
}
.loding_txt {
  background-image: url(loading.gif);
  background-position: left;
  background-repeat: no-repeat;
  border: 0;
  display: inline-block;
  height: 16px;
  padding-left: 20px;
}

.btn-label-warning {
  font-weight: 600 !important; 
  width: 100% !important;
  height: 60px !important;
}

.sticky {
  position: fixed !important;
  top: 0 !important;
  width: 100 !important%
}

.sticky + .content {
  padding-top: 102px !important;
}

.maindiv::-webkit-scrollbar {
  display: none;
}
</style>

<link rel="stylesheet" type="text/css" href="<?php echo BASE_URL ?>assets/css/custom_style.css">



<form action="" method="post" id="add_member" enctype="multipart/form-data">
 <div class="kt-portlet">
  <div class="kt-portlet__head">
   <div class="kt-portlet__head-label">
    <h3 class="kt-portlet__head-title" id="adding_user">
     Search for a Drugs
   </h3>
 </div>
 <div class="kt-portlet__head-label">
  <div class="row">
    <div class="col-4">
      <h3 class="kt-portlet__head-title" id="adding_user">
       <select class="form-control" name="name_type" id="name_type" style="width: 100%;">
         <option value="">- Please select type -</option>
         <?php 
         if(is_null($savedRecord_all || $savedRecord_all == "")){

         }else{
          foreach ($savedRecord_all as $savedRecords_all){
            $typeofdrug_all = $savedRecords_all['typeofdrug'];

            ?>

            <option value="<?php echo $typeofdrug_all ?>"><?php echo $typeofdrug_all ?></option>
            <?php 
          }
        }
        ?>
      </select>
    </h3>
  </div>
  <div class="col-4">
    <h3 class="kt-portlet__head-title" id="adding_user">
      <input type="text" name="drugname" class="form-control" style="width: 100%" placeholder="Enter drug name" />    
    </h3>
  </div>
  <div class="col-4">
    <h3 class="kt-portlet__head-title" id="adding_user">
      <button class="btn btn-primary" onclick="getstep2('searchByDrugType','',''); return false;">Search</button>   
    </h3>
  </div>
</div>  
</div>
</div>


<div id="main-content-drug">
  <div class="postList">
    <div class="kt-portlet__body" style="height:auto; overflow-y: auto;">
      <div class="row">
       <div class="col-md-12">
        <div class="row">
          <?php 
          if($get_saved_record_json_all['STATCODE'] != '200'){
          }else{
            foreach ($savedRecord as $savedRecords){
              $typeofdrug = $savedRecords['typeofdrug'];
              $recordId = $savedRecords['id'];

              ?>


              <div class="col-md-4 maindiv">
                <div class="kt-portlet__body kt-portlet__body--fit">
                  <div class="kt-widget kt-widget--user-profile-4">
                    <div class="kt-widget__body">
                      <a href="javascript:;" class="btn btn-label-warning" id="myHeader">
                        <?php echo strtoupper($typeofdrug) ?>
                      </a>

                      <div class="kt-form kt-form--label-left">
                        <div class="kt-portlet__body drugsearchhead">
                          <div class="form-group form-group-xs row">
                           <label class="col-12 col-form-label drugsearchheadlable">
                            <span class="col-9">NAME</span>
                            <span class="col-3" id="tyma">CLASS</span> 
                          </label>

                          <?php 
                          $GETALLDRUGS = array(
                            'METHOD' => urlencode('GETALLDRUGS'),
                            'APIKEY' => $apikey,
                            'USERNAME' => $username,
                            'PASSWORD' => $password,
                            'PROVIDERID' => $providerId,
                            'TYPEOFDRUG' => $typeofdrug,
                            'member_plan' => $member_plan,

                          );
                          $get_saved_record_drug = APICall($GETALLDRUGS);
                          $get_saved_record_drug_json = json_decode($get_saved_record_drug,"true");
                          $savedRecord_drug = $get_saved_record_drug_json['RESULT'];

                          //var_dump($get_saved_record_drug);

                          if(is_null($savedRecord_drug || $savedRecord_drug == "")){
                          }else{
                            foreach ($savedRecord_drug as $savedRecord_drugs){
                              $itemName = $savedRecord_drugs['item'];
                              $recordIdSub = $savedRecord_drugs['id'];
                              $drug_class = $savedRecord_drugs['drug_class'];
                              ?>


                              <label class="col-9 col-form-label" style="line-height: 1 !important;">
                                <?php echo $itemName; ?></label>
                                <label class="col-3 col-form-label" style="line-height: 1 !important; float: right;"> 
                                  <?php echo ($drug_class == '' || is_null($drug_class)) ? '-' : $drug_class ?></label>

                                  <?php 
                                }

                                ?>

                                
                                <div class="postList_item_<?php echo $recordIdSub ?>" style="width:100%; margin-left: 10px">
                                    

                                      
                                  <div class="show_more_sub" id="show_more_sub<?php echo $recordIdSub ?>">
                                  <span id="<?php echo $recordIdSub.'@@'.$typeofdrug.'@@'.$member_plan; ?>" class="show_more_more" title="Load more Drugs" style="color: #5578eb; cursor: pointer;">Show more</span>
                                  
                                </div>

                                    </div>
                                  
                                

                                <?php
                              }
                              ?>
                            </div>
                          </div>


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

                <?php
              }?>

              <div class="show_more_main" id="show_more_main<?php echo $recordId; ?>" style="float: right; display: none;">
                <span id="<?php echo $recordId; ?>" class="show_more" title="Load more Drugs">Show more</span>
                <span class="loding" style="display: none;"><!-- <span class="loding_txt">Loading...</span> --></span>
              </div>


            <?php }
            ?>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>


</div>
</form>




<!--begin::Modal-->
<!-- <div class="modal modal-stick-to-bottom fade" id="kt_modal_7" role="dialog" data-backdrop="true">
 <div class="modal-dialog modal-lg" role="document">
  <div class="modal-content">
   <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLabel">Search Filter</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">


    </div>

    
  </div>

</div>
</div>
</div> -->
<script type="text/javascript">
// When the user scrolls the page, execute myFunction
// window.onscroll = function() {
//   myFunction();
// };


// // Get the header
// //var header = document.getElementById("myHeader");

// var header = document.getElementsByClassName("btn-label-warning");

// // Get the offset position of the navbar
// var sticky = header.offsetTop;

// // Add the sticky class to the header when you reach its scroll position. Remove "sticky" when you leave the scroll position
// function myFunction() {
//   //alert('abasa');
//   if (window.pageYOffset > sticky) {
//     header.classList.add("sticky");
//   } else {
//     header.classList.remove("sticky");
//   }
// }


$(document).ready(function(){

  $(document).on('click','.show_more',function(){
    var ID = $(this).attr('id');
        //alert(ID);
        $('.show_more').hide();
        $('.loding').show();
        $.ajax({
          type:'POST',
          url:'search/load_more.php',
          data:'id='+ID,
          success:function(html){
            $('#show_more_main'+ID).remove();
            $('.postList').append(html);
          }
        });
      });

  $(document).on('click','.show_more_more',function(){
    var itemid = $(this).attr('id');
    var resp = itemid.split('@@');
    var ID = resp[0];
    var typeofdrug = resp[1];
    var member_plan = resp[2];
        //alert(ID);
        $('.show_more_more_'+ID).hide();
        $('.loding').show();
        $.ajax({
          type:'POST',
          url:'search/load_more_item.php',
          data:'id='+ID + '&typeofdrug='+typeofdrug + '&member_plan='+member_plan,
          success:function(html){

            $('#show_more_sub'+ID).remove();
            $('.postList_item_'+ID).append(html);
          }
        });
      });

});
</script>