<?php
if (!isset($_SESSION)) {
    session_start();
}
include("../Connections/courier.php");
include("../function/function.php");


$currentUser = $_SESSION['myMM_Userid'];

if (strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0) {
    throw new Exception('Request method must be POST!');
}

$recieved = trim(file_get_contents('php://input'), true);
$decoded = json_decode($recieved, true);

if (isset($decoded['METHOD'])) {
    # code...
    $METHOD=trim($decoded['METHOD']);
}


if ($METHOD == "ONLINEAPI"){
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery('SELECT * FROM onlineapi',$conn);
    }else{
      echo $api_validation;
    }
}else if ($METHOD == "DRUGTYPE" || $METHOD == "DRUGTYPE1") {
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $member_plan = trim($decoded['member_plan']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){


        if ($METHOD == "DRUGTYPE1") {
            echo $results = selectQuery("SELECT typeofdrug, id, plan FROM `formulary` WHERE (typeofdrug <> '' OR typeofdrug <> NULL) AND typeofdrug <> '-' AND plan = '$member_plan' GROUP BY typeofdrug",$conn);
        }else if($METHOD == "DRUGTYPE"){
            echo $results = selectQuery("SELECT typeofdrug, id FROM `formulary` WHERE (typeofdrug <> '' OR typeofdrug <> NULL) AND typeofdrug <>'-' AND plan = '$member_plan' GROUP BY typeofdrug",$conn);
        }
        
    }else{
      echo $api_validation;
    }
}else if ($METHOD == "GETALLDRUGS") {
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $typeofdrug = trim($decoded['TYPEOFDRUG']);
    $drugtype1 = trim($decoded['drugtype1']);
    $drugname = trim($decoded['drugname']);
    $member_plan = trim($decoded['member_plan']);



    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
           echo $results = selectQuery("SELECT * FROM `formulary` WHERE typeofdrug = '$typeofdrug' AND plan = '$member_plan' ORDER BY id DESC LIMIT 0,6",$conn); 
    }else{
      echo $api_validation;
    }
}else if ($METHOD == "NUM_ROWS") {
     $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $typeofdrug = trim($decoded['TYPEOFDRUG']);
    $postid = trim($decoded['POSTID']);
    $member_plan = trim($decoded['member_plan']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT COUNT(DISTINCT(typeofdrug)) as num_rows FROM formulary WHERE id < '$postid' AND plan = '$member_plan' ORDER BY id DESC",$conn);
    }else{
      echo $api_validation;
    }
}else if ($METHOD == "NEXTRECORD") {
        $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $typeofdrug = trim($decoded['TYPEOFDRUG']);
    $postid = trim($decoded['POSTID']);
    $showlimit = trim($decoded['SHOWLIMIT']);
    $member_plan = trim($decoded['member_plan']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT typeofdrug, id FROM formulary WHERE (typeofdrug <> '' OR typeofdrug <> NULL)AND typeofdrug <> '-' AND id < '$postid' AND plan = '$member_plan' GROUP BY typeofdrug ORDER BY id DESC LIMIT $showlimit",$conn);
    }else{
      echo $api_validation;
    }
}else if ($METHOD == "SEARCHDRUGTYPE") {
           $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $typeofdrug = trim($decoded['TYPEOFDRUG']);
    $drugtype = trim($decoded['drugtype']);
    $drugname = trim($decoded['drugname']);
    $member_plan = trim($decoded['member_plan']);
    $condition = "where id <> ''";
    if(!empty($drugtype)){
    $condition.=" and typeofdrug = '$drugtype'";
    }
    if(!empty($drugname)){
    $condition.=" and item like '%$drugname%'";
    }

    //echo "SELECT typeofdrug, id FROM formulary  $condition AND (typeofdrug <> '' OR typeofdrug <> NULL)AND typeofdrug <> '-' AND plan = '$member_plan' GROUP BY typeofdrug";

    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT typeofdrug, id FROM formulary  $condition AND (typeofdrug <> '' OR typeofdrug <> NULL)AND typeofdrug <> '-' AND plan = '$member_plan' GROUP BY typeofdrug",$conn);
    }else{
      echo $api_validation;
    }
}else if ($METHOD == "GETALLDRUGSSEARCH") {
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $typeofdrug = trim($decoded['TYPEOFDRUG']);
    $drugtype1 = trim($decoded['drugtype1']);
    $drugname = trim($decoded['drugname']);
    $member_plan = trim($decoded['member_plan']);

    $condition = "where id <> ''";
    if(!empty($drugname)){
    $condition.=" and item like '%$drugname%'";
    }

    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT * FROM formulary  $condition AND typeofdrug = '$typeofdrug' AND plan = '$member_plan'",$conn);
    }else{
      echo $api_validation;
    }
}else if ($METHOD == "GET_PLAN") {
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $member_plan =trim($decoded['MEMBER_PLAN']);
     $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT * FROM plan_category WHERE plan_type = '$member_plan'",$conn);
    }else{
      echo $api_validation;
    }
}else if ($METHOD == "NUM_ROWS_ITEM") {
         $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $typeofdrug = trim($decoded['TYPEOFDRUG']);
    $postid = trim($decoded['POSTID']);
    $member_plan = trim($decoded['MEMBER_PLAN']);

    //echo "SELECT COUNT((item)) as num_rows FROM formulary WHERE id < '$postid' AND plan = '$member_plan' AND typeofdrug = '$typeofdrug'";

    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT COUNT((item)) as num_rows FROM formulary WHERE id < '$postid' AND plan = '$member_plan' AND typeofdrug = '$typeofdrug'",$conn);
    }else{
      echo $api_validation;
    }
}else if ($METHOD == "NEXTRECORDITEM") {
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $typeofdrug = trim($decoded['TYPEOFDRUG']);
    $postid = trim($decoded['POSTID']);
    $showlimit = trim($decoded['SHOWLIMIT']);
    $member_plan = trim($decoded['MEMBER_PLAN']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);

    //echo"SELECT * FROM formulary WHERE (typeofdrug <> '' OR typeofdrug <> NULL)AND typeofdrug <> '-' AND id < '$postid' AND plan = '$member_plan' AND typeofdrug = '$typeofdrug' LIMIT $showlimit";

    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT * FROM formulary WHERE (typeofdrug <> '' OR typeofdrug <> NULL)AND typeofdrug <> '-' AND id < '$postid' AND plan = '$member_plan' AND typeofdrug = '$typeofdrug' ORDER BY id DESC LIMIT $showlimit",$conn);
    }else{
      echo $api_validation;
    }
}else if ($METHOD == "SAVE_PERSONAL_MEDICATION"){
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $member_no = trim($decoded['member_no']);
    $name_medication = trim($decoded['name_medication']);
    $medication_desceription = trim($decoded['medication_desceription']);
    $pharmacy_name = trim($decoded['pharmacy_name']);
    $user_image1 = $decoded['user_image1'];
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);

    if ($api_validation == "Ok"){
        echo $results = selectQuery("INSERT INTO `personl_medication`(`member_no`,`item`,`description`, `pharmacy`, `image`) VALUES ('$member_no','$name_medication','$medication_desceription','$pharmacy_name','$user_image1')",$conn);
    }else{
        echo $api_validation;
    }
}else if ($METHOD == "GET_VISIT_HISTORY_DRUGS_LOCAL"){
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $member_no = trim($decoded['MEMBER_NO']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT * FROM personl_medication WHERE member_no = '$member_no'",$conn);
    }else{
        echo $api_validation;
    }
}else if($METHOD == "DELETE_ITEM"){
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $delete_id = trim($decoded['delete_id']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("DELETE FROM personl_medication WHERE id = '$delete_id'",$conn);
    }else{
        echo $api_validation;
    }
}else if ($METHOD == "GET_VISIT_HISTORY_DRUGS_LOCAL_SINGLE"){
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $item_id = trim($decoded['ITEM_ID']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT * FROM personl_medication WHERE id = '$item_id'",$conn);
    }else{
        echo $api_validation;
    }
}else if ($METHOD == "SAVE_PERSONAL_MEDICATION_UPDATE"){
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $item_id = trim($decoded['update_id']);
    $name_medication = trim($decoded['name_medication']);
    $medication_desceription = trim($decoded['medication_desceription']);
    $pharmacy_name = trim($decoded['pharmacy_name']);
    $user_image1 = $decoded['user_image1'];
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        if (empty($user_image1)){
            echo $results = selectQuery("UPDATE personl_medication SET `item` = '$name_medication',`description` = '$medication_desceription', `pharmacy`= '$pharmacy_name' WHERE id = '$item_id'",$conn);
        }else{
            echo $results = selectQuery("UPDATE personl_medication SET `item` = '$name_medication',`description` = '$medication_desceription', `pharmacy`= '$pharmacy_name', `image` = '$user_image1' WHERE id = '$item_id'",$conn);
        }
    }else{
        echo $api_validation;
    }
}else if ($METHOD == "SAVE_PERSONAL_ALLERGY"){
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $member_no = trim($decoded['member_no']);
    $reaction_medication = $decoded['reaction_medication'];
    $reaction_true_allergy = $decoded['reaction_true_allergy'];
    $reaction_side = $decoded['reaction_side'];
    $medication_taken = $decoded['medication_taken'];
    $duration = $decoded['duration'];
    $duration_specific = $decoded['duration_specific'];
    $duration_for_reaction = $decoded['duration_for_reaction'];
    $reaction_medical_attention = $decoded['reaction_medical_attention'];
    $location_seen = $decoded['location_seen'];
    $medication_stopped = $decoded['medication_stopped'];
    $similar_medication = $decoded['similar_medication'];
    $new_problem = $decoded['new_problem'];
    $medication_reaction = $decoded['medication_reaction'];
    $food_reaction = $decoded['food_reaction'];

    $duration_actual = $duration.' '.$duration_specific;

    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
         $results = selectQuery("INSERT INTO `allergy_history`( `drug`,`food`,`member_no`, `reaction_status`, `true_allergy_description`, `adverse_effect_description`, `medication_medium`, `reaction_duration`, `reaction_duration_ago`, `reaction_medication_status`, `consultation_location`, `stopped_by_doctor`, `similar_medication` ,`same_problem`) VALUES ('$medication_reaction','$food_reaction','$member_no','$reaction_medication','$reaction_true_allergy','$reaction_side','$medication_taken','$duration_actual','$duration_for_reaction','$reaction_medical_attention','$location_seen','$medication_stopped','$similar_medication', '$new_problem')",$conn);
    }else{
        echo $api_validation;
    }
}else if ($METHOD == "GET_VISIT_HISTORY_ALLERGY"){
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $member_no = trim($decoded['MEMBER_NO']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT * FROM allergy_history WHERE member_no = '$member_no'",$conn);
    }else{
        echo $api_validation;
    }

}else if ($METHOD == "GET_VISIT_HISTORY_ALLERGY_SINGLE"){
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $member_no = trim($decoded['ITEM_ID']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT * FROM allergy_history WHERE id = '$member_no'",$conn);
    }else{
        echo $api_validation;
    }
}else if ($METHOD == "DELETE_PERSONAL_ALLERGY"){
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $member_no = trim($decoded['member_no']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("DELETE FROM allergy_history WHERE id = '$member_no'",$conn);
    }else{
        echo $api_validation;
    }
}else if ($METHOD == "GET_DRUG_LIST"){
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT * FROM service_items WHERE service = 'Drugs'",$conn);
    }else{
        echo $api_validation;
    }
}else if ($METHOD == "GET_REACTION_LIST"){
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $currentUser = trim($decoded['USERID']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT * FROM allergy_reactions",$conn);
    }else{
        echo $api_validation;
    }
}

?>