<?php
session_start();
include("dbConfig.php");
if(isset($_SESSION['username']) && isset($_SESSION['name']))
{
    $username=$_SESSION['username'];
    $name=$_SESSION['name'];
}
else
{
     echo '<META http-equiv="refresh" content="0;logout.php">';
}

?><?php
include('dbConfig.php');
if(isset($_GET['partid'])){
	$partid=$_GET['partid'];
    mysqli_query($db,"DELETE FROM particulars WHERE PartId='$partid'");
    echo "<script>alert('Particulars Deleted!')</script>";
$url="/Billing/ViewParticulars.php";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />"; 
}

if(isset($_GET['Reportid'])){
	$Reportid=$_GET['Reportid'];
	$patid=$_GET['patid'];
    mysqli_query($db,"DELETE FROM uploadreports WHERE SlNo='$Reportid'");
    echo "<script>alert('Report Deleted!')</script>";
$url="/Billing/ReportList.php?patid=$patid";
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url\" />"; 
}

// 

if (isset($_GET['Billno'])) {
    $patientId = $_GET['Billno'];

    // Begin a transaction to ensure atomicity
    mysqli_begin_transaction($db);

    try {
        // Delete from finalbill table
        $query1 = "DELETE FROM `finalbill` WHERE PatientId = '$patientId'";
        mysqli_query($db, $query1);

        // Delete from bill table
        $query2 = "DELETE FROM `bill` WHERE PatientId = '$patientId'";
        mysqli_query($db, $query2);

        // Delete from advance table (use PatId for this table)
        $query3 = "DELETE FROM `advance` WHERE PatId = '$patientId'";
        mysqli_query($db, $query3);

        // Commit the transaction if all queries are successful
        mysqli_commit($db);

        // Display success message and redirect to the list page
        echo "<script>alert('Records deleted successfully!'); window.location.href='index.php';</script>";
    } catch (Exception $e) {
        // If any query fails, rollback the transaction
        mysqli_rollback($db);

        // Display error message and redirect back
        echo "<script>alert('Error deleting records: " . $e->getMessage() . "'); window.location.href='index.php';</script>";
    }
}

?>
