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

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'])) {
   $METHOD=trim($decoded['METHOD']);
}


if ($METHOD == "GET_MEMBER_DETAILS") {
	$apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $email_address = trim($decoded['EMAIL']);
    $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 patient_info WHERE email = '$email_address' AND member_no = '$member_no' AND status = 'Active'",$conn);
    }else{
      echo $api_validation;
  }
}else if ($METHOD == "GET_DEPENDANTS") {
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $principal_id =trim($decoded['PRINCIPAL_ID']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT * FROM patient_info WHERE principal_id = '$principal_id' AND member_type = 'Dependent' AND status = 'Active'",$conn);
    }else{
      echo $api_validation;
  }
}else if ($METHOD == "GET_VISIT_HISTORY") {
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $member_no =trim($decoded['MEMBER_NO']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT date_of_consultation, process_claim_no,type_of_visit,attending_officer, sum(qty*unit_price) AS totalClaim  FROM claims_details WHERE member_no = '$member_no' GROUP BY process_claim_no ORDER BY date_of_consultation ASC",$conn);
    }else{
      echo $api_validation;
  }
}else if ($METHOD == "GET_VISIT_HISTORY_DETAILS") {
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $process_claim_no =trim($decoded['PROCESS_CLAIM_NO']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT item, item_service,qty, unit_price, (qty*unit_price) AS amount  FROM claims_details WHERE process_claim_no = '$process_claim_no' ORDER BY date_of_consultation ASC",$conn);
    }else{
      echo $api_validation;
  }
}else if ($METHOD == "GET_PRINCIPAL") {
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $principal_id =trim($decoded['PRINCIPAL_ID']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT * FROM patient_info WHERE principal_id = '$principal_id' AND member_type = 'Principal' AND status = 'Active'",$conn);
    }else{
      echo $api_validation;
  }
}else if ($METHOD == "GET_VISIT_HISTORY_DRUGS"){
    $apikey =trim($decoded['APIKEY']);
    $username =trim($decoded['USERNAME']);
    $password =trim($decoded['PASSWORD']);
    $provider_id =trim($decoded['PROVIDERID']);
    $member_no =trim($decoded['MEMBER_NO']);
    $api_validation = api_authentication($apikey,$username,$password,$provider_id,$conn);
    if ($api_validation == "Ok"){
        echo $results = selectQuery("SELECT date_of_consultation, process_claim_no,type_of_visit,attending_officer,provider_id, item  FROM claims_details WHERE member_no = '$member_no' AND item_service = 'Drugs' ORDER BY date_of_consultation ASC",$conn);
    }else{
        echo $api_validation;
    }
}

?>