<?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_ALLERGY = array(
    'METHOD' => urlencode('GET_VISIT_HISTORY_ALLERGY'),
    'APIKEY' => $apikey,
    'USERNAME' => $username,
    'PASSWORD' => $password,
    'PROVIDERID' => $providerId,
    'MEMBER_NO' => $member_no,

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


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

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


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

);
$get_saved_record_reaction = APICall($GET_REACTION_LIST);
$get_saved_record_json_reaction = json_decode($get_saved_record_reaction,"true");
$savedRecord_reaction = $get_saved_record_json_reaction['RESULT'];

//var_dump($savedRecord_reaction);

?>

<style>
    #bold_item{
        font-weight: 400;
        font-size:16px;
    }
</style>
<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_allergy123">
                <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>Name</th>
            <th style="width:25%">Date</th>
            <th>Action</th>
        </tr>
        </thead>
        <tbody>
        <?php
        $count = 0;
        if ($get_saved_record_json_local['STATCODE'] == "200") {
            foreach ($savedRecord_local as $savedRecords){
                $medication_id = $savedRecords['id'];
                $drug = $savedRecords['drug'];
                $food = $savedRecords['food'];
                $date_of_consultation = $savedRecords['date_time'];
                $reaction_status = $savedRecords['reaction_status'];
                $true_allergy_description = $savedRecords['true_allergy_description'];
                $adverse_effect_description = $savedRecords['adverse_effect_description'];
                $medication_medium = $savedRecords['medication_medium'];
                $reaction_duration = $savedRecords['reaction_duration'];
                $reaction_duration_ago = $savedRecords['reaction_duration_ago'];
                $reaction_medication_status = $savedRecords['reaction_medication_status'];
                $consultation_location = $savedRecords['consultation_location'];
                $stopped_by_doctor = $savedRecords['stopped_by_doctor'];
                $similar_medication = $savedRecords['similar_medication'];
                $same_problem = $savedRecords['same_problem'];

                $count ++;




                ?>
                <tr style="cursor: pointer">
                    <td data-toggle="modal" data-target="#kt_modal_7_view" onclick="getstep2('view_allergies', '<?php echo $medication_id ?>','main_content_allergies')"><?php echo $count ?></td>

                    <td data-toggle="modal" data-target="#kt_modal_7_view" onclick="getstep2('view_allergies', '<?php echo $medication_id ?>','main_content_allergies')"><?php echo strtoupper('<span style="font-weight:700">Drug Allergy:</span>'.' '.$drug.' '.'<span style="font-weight:700"><br>Food Allergy:</span>'.' '.$food) ?></td>
                    <td data-toggle="modal" data-target="#kt_modal_7_view" onclick="getstep2('view_allergies', '<?php echo $medication_id ?>','main_content_allergies')"><?php echo strtoupper(date("jS F Y ", strtotime($date_of_consultation))) ?></td>
                    <td>
                        <span><i class="flaticon-delete" style="color: #FF416C; cursor: pointer" onclick="getstepDelete('deleteAllergy','<?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>



<div class="modal modal-stick-to-bottom fade" id="kt_modal_7_view" 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">Patient Drug Allergy History</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_allergies">


                </div>


            </div>

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

<!--begin::Modal-->
<div class="modal modal-stick-to-bottom fade" id="kt_modal_7_allergy123" 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 Allergy</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                </button>
            </div>
            <div class="modal-body">

                <div id="main-content-allergy" class="kt-portlet__body" style="background-color: #fff; color: #000000; box-shadow: 10px 10px 10px 10px rgba(0,0,0,0.05)">
                    <form name="" id="add_allergies" method="post">
                        <div class="row">
                            <div class="col-12">
                                <input type="hidden" name="member_no" id="member_no" value="<?php echo $member_no ?>">
                                <label for="hirePurchase" class="col-12 col-form-label" id="bold_item">Have you ever taken any medication(s) that have caused a reaction ?</label>
                                <div class="form-group row">
                                    <div class="col-10">
                                        <label class="kt-radio kt-radio--bold kt-radio--success">
                                            <input type="radio"  name="reaction_medication" id="reaction_medication" value="Yes" onchange="display_yes(this.value)"> Yes
                                            <span></span>
                                        </label>
                                            &nbsp;&nbsp;&nbsp;
                                        <label class="kt-radio kt-radio--bold kt-radio--danger">
                                            <input type="radio"  name="reaction_medication" id="reaction_medication" value="No" checked onchange="display_yes(this.value)"> No
                                            <span></span>
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <span id="display_if_yes" style="display: none; width:100%">
                            <div class="row">
                            <div class="col-md-6" style="width:100%">
                                <label for="hirePurchase" class="col-12 col-form-label" id="bold_item">What medication(s) caused the reaction(Paracetamol, Amoksiklav)</label>
                                <div class="form-group row">
                                    <div class="col-12">
                                             <select class="form-control the-select" name="medication_reaction[]" id="medication_reaction"  multiple="multiple" style="width:100%">
                                            <option value="">Please Select</option>
                                            <?php
                                            if ($get_saved_record_json_drug['STATCODE'] == "200") {
                                                foreach ($savedRecord_drug as $savedRecord_drugs) { ?>
                                                    <option value="<?php echo $savedRecord_drugs['item'] ?>"><?php echo $savedRecord_drugs['item'] ?></option>
                                                 <?php
                                                }
                                            }
                                            ?>
                                             </select>
                                    </div>
                                </div>
                            </div>

                                <div class="col-md-6">
                                    <label for="hirePurchase" class="col-12 col-form-label" id="bold_item">What food(s) caused the reaction Eg.(Rice, Apple)</label>
                                <div class="form-group row">
                                    <div class="col-12">
                                        <input type="text" name="food_reaction[]" id="food_reaction" class="form-control">
                                    </div>
                                </div>
                                </div>
                        </div>



                            <div class="row">
                            <div class="col-md-6" style="width:100%">
                                <label for="hirePurchase" class="col-12 col-form-label" id="bold_item">Describe the reaction you had</label><br><br>
                                <div class="form-group row">
                                    <div class="col-12">
                                             <select class="form-control the-select" name="reaction_true_allergy[]" id="reaction_true_allergy"  multiple="multiple" style="width:100%">
                                            <option value="">Please Select</option>
                                                <?php if ($get_saved_record_json_reaction['STATCODE'] == "200") {
                                                foreach ($savedRecord_reaction as $savedRecord_reactions) { ?>
                                                    <option value="<?php echo $savedRecord_reactions['allergy_reaction'] ?>"><?php echo $savedRecord_reactions['allergy_reaction'] ?></option>
                                                    <?php }} ?>
                                             </select>
                                    </div>
                                </div>
                            </div>

                                <div class="col-md-6">


                                    <label for="hirePurchase" class="col-12 col-form-label" id="bold_item">How Soon after starting the medication did the reaction happen?</label>
                                <dxiv class="form-group row">
                                    <div class="col-6">
                                             <input type="text" name="duration" id="duration" class="form-control">
                                    </div>

                                            <div class="col-6">
                                             <select class="form-control" name="duration_specific" id="duration_specific" style="width:100%">
                                            <option value="">Please Select</option>
                                            <option value="Hours">Hour(s)</option>
                                                 <option value="Days">Day(s)</option>
                                                 <option value="Weeks">Week(s)</option>
                                                 <option value="Months">Month(s)</option>
                                                 <option value="Years">Year(s)</option>
                                                 <option value="Unclear/Vague">Unclear/vague</option>
                                             </select>
                                    </div>
                                </dxiv>





                                </div>
                        </div>


                            <div class="row">
                            <div class="col-md-6" style="width:100%">
                                <label for="hirePurchase" class="col-12 col-form-label" id="bold_item">How did you take this medication</label>
                                <div class="form-group row">
                                    <div class="col-12">
                                             <select class="form-control the-select" name="medication_taken[]" id="medication_taken"  multiple="multiple" style="width:100%">
                                            <option value="">Please Select</option>
                                            <option value="Inhaled">Inhaled</option>
                                                 <option value="By Mouth">By Mouth</option>
                                                 <option value="On the Skin">On the Skin</option>
                                                 <option value="Injection">Injection</option>
                                                 <option value="Unclear/Vague">Unclear/Vague</option>
                                             </select>
                                    </div>
                                </div>
                            </div>

                                <div class="col-md-6">
                                    <label for="hirePurchase" class="col-12 col-form-label" id="bold_item">How long ago did this reaction happen ?</label>
                                <div class="form-group row">
                                    <div class="col-12">
                                              <select class="form-control" name="duration_for_reaction" id="duration_for_reaction" style="width:100%">
                                            <option value="">Please Select</option>
                                            <option value="< 5 years ago">< 5 years ago</option>
                                                 <option value="5 - 10 years ago">5 - 10 years ago</option>
                                                 <option value="> 10 years"> > 10 years</option>
                                                 <option value="Unclear/Vague">Unclear/vague</option>
                                             </select>
                                    </div>
                                </div>
                        </div>



                            <div class="row">
                            <div class="col-md-6" style="width:100%; padding-left:30px">
                                <label for="hirePurchase" class="col-12 col-form-label" id="bold_item">Did you seek medical Attention for the reaction ?</label>
                                    <label class="kt-radio kt-radio--bold kt-radio--success">
                                            <input type="radio"  name="reaction_medical_attention" id="reaction_medical_attention" value="Yes" onchange="question_six_a(this.value)"> Yes
                                            <span></span>
                                        </label>
&nbsp;&nbsp;&nbsp;
                                    <label class="kt-radio kt-radio--bold kt-radio--danger">
                                        <input type="radio"  name="reaction_medical_attention" id="reaction_medical_attention" value="No" checked onchange="question_six_a(this.value)"> No
                                        <span></span>
                                    </label>
                            </div>

                                <div class="col-md-6">
                                        <label for="hirePurchase" class="col-12 col-form-label" id="bold_item">Was the medication stopped by a doctor ?</label>
                                <div class="form-group row">
                                    <div class="col-12">
                                            <label class="kt-radio kt-radio--bold kt-radio--success">
                                            <input type="radio"  name="medication_stopped" id="medication_stopped" value="Yes"> Yes
                                            <span></span>
                                        </label>
                                            &nbsp;&nbsp;&nbsp;
                                        <label class="kt-radio kt-radio--bold kt-radio--danger">
                                            <input type="radio"  name="medication_stopped" id="medication_stopped" value="No" checked> No
                                            <span></span>
                                        </label
                                    </div>
                                </div>
                                </div>
                        </div>

                            <span id="question_six_a_display" style="width:100%; display:none">
                                 <div class="row">
                            <div class="col-md-6" style="width:100%">
                                <label for="hirePurchase" class="col-12 col-form-label" id="bold_item">Where were you seen ?</label>
                                <div class="form-group row">
                                    <div class="col-12">
                                              <select class="form-control" name="location_seen" id="location_seen" style="width:100%">
                                            <option value="">Please Select</option>
                                            <option value="Hospital/Clinic">Hospital/Clinic</option>
                                                <option value="Pharmacy">Pharmacy</option>
                                             </select>
                                    </div>
                                </div>
                            </div>

                                <div class="col-md-6">

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



                            <div class="row">
                                <div class="col-md-6" style="width:100%; padding-left:30px">
                                    <label for="hirePurchase" class="col-12 col-form-label" id="bold_item">Have you ever taken a this medication or similar one again ?</label>
                                       <div class="col-12">
                                            <label class="kt-radio kt-radio--bold kt-radio--success">
                                            <input type="radio"  name="similar_medication" id="similar_medication" value="Yes"> Yes
                                            <span></span>
                                        </label>
                                            &nbsp;&nbsp;&nbsp;
                                        <label class="kt-radio kt-radio--bold kt-radio--danger">
                                            <input type="radio"  name="similar_medication" id="similar_medication" value="No" checked> No
                                            <span></span>
                                        </label>
                                    </div>
                                </div>


                                <div class="col-md-6" style="width:100%">
                                    <label for="hirePurchase" class="col-12 col-form-label" id="bold_item">If yes, did you experience the same problem ?</label>
                                                                        <div class="col-12">
                                            <label class="kt-radio kt-radio--bold kt-radio--success">
                                            <input type="radio"  name="new_problem" id="new_problem" value="Yes"> Yes
                                            <span></span>
                                        </label>
                                            &nbsp;&nbsp;&nbsp;
                                        <label class="kt-radio kt-radio--bold kt-radio--danger">
                                            <input type="radio"  name="new_problem" id="new_problem" value="No" checked> No
                                            <span></span>
                                        </label>
                                    </div>
                                </div>


                            </div
                        </span>


                        <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_allergies');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
            });
        });

        $('.the-select').select2();
        $('.the-select').on("select2:select", function(e) {
            if (e){
                var sum = 0;
                $('.the-select :selected').each(function() {
                    eachPayment('eachPayment','paymentInformation');
                    var response = String($(this).val()).split("@@");
                    var options = response[2];
                    sum += parseInt(options);
                });

                document.getElementById("loan_amt").value = sum;
            }
        });
        $(".the-select").on("select2:unselect", (e) => {
            var sum1 = 0;
            $('.the-select :selected').each(function() {
                eachPayment('eachPayment','paymentInformation');
                var response = String($(this).val()).split("@@");
                var options = response[2];
                sum1 += parseInt(options);
            });
            document.getElementById("loan_amt").value = sum1;

        })

    });

    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]);
        }
    }

</script>

