<?php
include("dbConfig.php");
if (isset($_POST['patientId']) || isset($_POST['contact'])) {
    $patientId = $_POST['patientId'];
    $contact = $_POST['contact'];

    // Query based on either Patient ID or Contact
    $query = "";
    if (!empty($patientId)) {
        $query = "SELECT PayableAmount FROM finalbill WHERE PatientId = '$patientId' LIMIT 1";
    } elseif (!empty($contact)) {
        $query = "SELECT PayableAmount FROM finalbill WHERE contact = '$contact' LIMIT 1"; // Assuming you have 'contact' column
    }

    $result = mysqli_query($db, $query);

    // If patient found, return TotalAmount
    if ($result && mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        echo $row['PayableAmount']; // Return the total amount
    } else {
        echo "0"; // If no record found, return 0
    }
}
?>
