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

require_once("Connections/constants.php");
include("function/function.php");
$apikey = $_SESSION['apikey'];
$username = $_SESSION['facility_name'];
$password = $_SESSION['password'];
$providerId = $_SESSION['provider_id'];
$member_no = $_POST['id'];
$fullname = $_POST['idd2'];
$action = $_POST['act'];

$GET_VISIT_HISTORY = array(
    'METHOD' => urlencode('GET_VISIT_HISTORY_DRUGS'),
    'APIKEY' => $apikey,
    'USERNAME' => $username,
    'PASSWORD' => $password,
    'PROVIDERID' => $providerId,
    'MEMBER_NO' => $member_no,

);
$get_saved_record = onlineJohnAPI($GET_VISIT_HISTORY);
$get_saved_record_json = json_decode($get_saved_record,"true");
$savedRecord = $get_saved_record_json['RESULT'];


$GET_VISIT_HISTORY_LOCAL = array(
    'METHOD' => urlencode('GET_VISIT_HISTORY_DRUGS_LOCAL'),
    'APIKEY' => $apikey,
    'USERNAME' => $username,
    'PASSWORD' => $password,
    'PROVIDERID' => $providerId,
    'MEMBER_NO' => $member_no,

);
$get_saved_record_local = APICall($GET_VISIT_HISTORY_LOCAL);
$get_saved_record_json_local = json_decode($get_saved_record_local,"true");
$savedRecord_local = $get_saved_record_json_local['RESULT'];
//var_dump($get_saved_record_local);

?>
<div class="kt-portlet__body">
 <div class="row">
     <div class="col-6"></div>

     <div class="col-6" style="text-align:right">
         <a href="javascript:void(0)" class="btn btn-brand btn-elevate btn-icon-sm" data-toggle="modal" data-target="#kt_modal_7" onclick="getstep2('add_new_drug', '<?php echo "1" ?>','main-content')">
             <i class="la la-plus"></i>
             New Record
         </a>
         <br>
     </div>
     &nbsp;&nbsp;&nbsp;

 </div>
    <table id="example" class="display" style="width:100%">
        <thead>
        <tr>
            <th>No</th>
            <th>Img</th>
            <th>Name</th>
            <th style="width:35%">Facility</th>
            <th style="width:30%">Description</th>
            <th style="width:20%">Status</th>
            <th style="width:25%">Date</th>
            <th>Action</th>
        </tr>
        </thead>
        <tbody>
        <?php
        $count = 0;
        if ($get_saved_record_json['STATCODE'] == "200") {
        foreach ($savedRecord as $savedRecords){
        $date_of_consultation = $savedRecords['date_of_consultation'];
        $process_claim_no = $savedRecords['process_claim_no'];
        $type_of_visit = $savedRecords['type_of_visit'];
        $attending_officer = $savedRecords['attending_officer'];
        $totalClaim = $savedRecords['totalClaim'];
        $provider_id = $savedRecords['provider_id'];
        $item = $savedRecords['item'];
        $noimage = BASE_URL . 'assets/images/hmsplace.png';
        $count ++;


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

            //print_r($savedRecord);



            $fields = array(
                'method' => urlencode('GET_PROVIDER_DETAIL'),
                'api_key' => urlencode($api_key),
                'guid' => urlencode($guid),
                'user' => urlencode($user),
                'passcode' => urlencode($passcode),
                'provider_id' => urlencode($provider_id),
            );

            //print_r(json_encode($fields));

            $resultSearch = onlineAPI($fields);
            $decodeResultSearch = json_decode($resultSearch,"true");
            $facility_name = $decodeResultSearch['facility_name'];


        ?>
        <tr>
            <td><?php echo $count ?></td>
            <td><img src="<?php echo $noimage ?>"  style="width:40px; height: 40px;border-radius: 20px "/></td>
            <td><?php echo strtoupper($item) ?></td>
            <td><span class="kt-badge  kt-badge-- kt-badge--inline kt-badge--pill"><?php echo strtoupper($facility_name) ?></span></td>
            <td>N/A</td>
            <td><span class="kt-badge  kt-badge--warning kt-badge--inline kt-badge--pill" style="color:white; height: 25px; font-size: 9px; text-align: center;">RX INSURANCE</span></td>
            <td><?php echo strtoupper(date("jS F Y ", strtotime($date_of_consultation))) ?></td>
            <td></td>
        </tr>



        <?php
        }
        }
        if ($get_saved_record_json_local['STATCODE'] == "200") {
        foreach ($savedRecord_local as $savedRecords_local){
            $medication_id = $savedRecords_local['id'];
        $date_of_consultation_local = $savedRecords_local['date_time'];
        $item_name = $savedRecords_local['item'];
        $description = $savedRecords_local['description'];
        $facility_image1 = $savedRecords_local['image'];
        $facility_name1 = $savedRecords_local['pharmacy'];
        $facility_image = "assets/images/pharmacy/".$facility_image1;

        $count = $count+1;
        ?>

        <tr>
            <td><?php echo $count ?></td>
            <td>
                <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:40px; object-fit: cover; border-radius: 40px; width: 40px'>
                            </a>
                        </li>
                    </ul>
                </div>
                </td>
            <td><?php echo strtoupper($item_name) ?></td>
            <td><span class="kt-badge  kt-badge-- kt-badge--inline kt-badge--pill" style=""><?php echo strtoupper($facility_name1 = $facility_name1 != '' ? $facility_name1 : "N/A"  ) ?></span></td>
            <td><?php echo strtoupper( $description = $description != '' ? $description : 'N/A') ?></td>
            <td>
                <span class="kt-badge  kt-badge--success kt-badge--inline kt-badge--pill" style="color:white; height: 25px; font-size: 9px; text-align: center;"><?php echo strtoupper("Self Purchased"  ) ?>
                    
                </span>

            </td>
            <td><?php echo strtoupper(date("jS F Y ", strtotime($date_of_consultation_local))) ?></td>
            <td>
                <span class="" data-toggle="modal" data-target="#kt_modal_7_edit" onclick="getstep2('edit_medication', '<?php echo $medication_id ?>','main_content_medication')"><i class="fa fa-pen" style="color: #ffd652; cursor: pointer"></i></span>
                &nbsp; | &nbsp;
                <span><i class="flaticon-delete" style="color: #FF416C; cursor: pointer" onclick="getstepDelete('deleteMedication','<?php echo $medication_id ?>','')"></i></span>
            </td>
        </tr>

        <?php } } ?>

        </tbody>
    </table>
</div>

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.24/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.24/datatables.min.js"></script>
<script>
    $(document).ready(function() {
        $('#example').DataTable( {
            dom: 'Bfrtip',
            buttons: [
                'copy', 'csv', 'excel', 'pdf', 'print'
            ]
        } );
    } );
</script>

<!--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">Add Medication</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">

                    <form name="" id="add_medication" method="post">
                        <div class="row">
                            <div class="col-4">
                                <div class="kt-avatar kt-avatar--outline kt-avatar--circle" id="kt_apps_user_add_avatar">
                                    <div class="kt-avatar__holder" style="background-image: url()">
                                        <?php
                                        if(empty($image)){?>
                                            <img alt='no_photo' width='110' height='40' src='<?php echo BASE_URL ?>assets/images/avatar-placeholder.png' style='text-align:center; border-radius: 80px; height:115px; object-fit: cover;' id='display'>
                                        <?php }else{?>
                                            <img alt='no_photo' width='110' height='40' src='BASE_URL assets/images/avatar-placeholder.png' style='text-align:center; border-radius: 80px; height:115px; object-fit: cover;' id='display'>
                                        <?php }
                                        ?>
                                    </div>
                                    <label class="kt-avatar__upload" data-toggle="kt-tooltip" title="" data-original-title="Change avatar">
                                        <i class="fa fa-pen"></i>
                                        <input type="file" name="imageUpload" id="imageUpload" aria-invalid="false" multiple accept=".jpg, .png, .gif, .jpeg" onchange="readURL(this);">
                                    </label>
                                    <span class="kt-avatar__cancel" data-toggle="kt-tooltip" title="" data-original-title="Cancel avatar">
					<i class="fa fa-times"></i>
					</span>
                                </div>
                            </div>

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

                            <div class="col-7">
                                <h3 class="kt-portlet__head-title" id="adding_user">
                                    <input type="text" name="drugname" class="form-control" style="width: 100%" placeholder="Enter drug name" id="drugname"/>
                                </h3>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-4">

                            </div>

                            <div class="col-7">
                                <h3 class="kt-portlet__head-title" id="adding_user">
                                    <input type="text" name="pharmacy_name" class="form-control" style="width: 100%" placeholder="Enter Pharmacy"  id="pharmacy_name"/>
                                </h3>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-4">

                            </div>

                            <div class="col-7">
                                <h3 class="kt-portlet__head-title" id="adding_user">
                                    <textarea class="form-control" cols="20" rows="5" name="medication_desceription"></textarea>
                                </h3>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-4">

                            </div>

                            <div class="col-7">
                                <div class="kt-portlet__foot">
                                    <div class="row">
                                        <div class="col kt-align-right">
                                            <button id="save_principal_member_btn" onclick="getstepInsert('add_medication');return false;" type="submit"
                                                    class="btn btn-success">Save</button>

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


                </div>


            </div>

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


<div class="modal modal-stick-to-bottom fade" id="kt_modal_7_edit" 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">Edit Medication</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close" onclick="closeModal()">
                </button>
            </div>
            <div class="modal-body" style="background-color:#fafafa">

                <div id="main_content_medication">

                </div>


            </div>

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


<script>
    $(document).ready(function(){
        //$('#lightgallery1').lightGallery();
        $('.lightgallery1').each(function(i, v) {
            $(v).lightGallery({
                thumbnail: false
            });
        });
    });

    function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#display')
                    .attr('src', e.target.result)
                    .width(110)
                    .height(115);

            };

            reader.readAsDataURL(input.files[0]);
        }
    }

    // function closeModal(){
    //     $("#kt_modal_7_edit").modal("hide");
    // }
</script>

<!--end::Modal-->