<?php
if (!isset($_SESSION)) {
  session_start();
}

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

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

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

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

 //print_r($get_saved_record_json['STATCODE']);
  //print_r(json_encode($savedRecord12));

  //var_dump(json_encode($GETDRUGTYPE));


}

?>


<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['STATCODE'] != '200'){
          echo $get_saved_record_json['STATMSG'];
        }else{
          foreach ($savedRecord12 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" style="font-weight: 600; width: 100%; height: 60px;">
                      <?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('GETALLDRUGSSEARCH'),
                          'APIKEY' => $apikey,
                          'USERNAME' => $username,
                          'PASSWORD' => $password,
                          'PROVIDERID' => $providerId,
                          'TYPEOFDRUG' => $typeofdrug,
                          'drugtype1' => $drugtype,
                          'drugname' => $drugname,
                          '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($savedRecord_drug.'ds');

                        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">GENERIC</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;">
            <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>

<script type="text/javascript">
  $(document).ready(function(){
  
    $(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>