<?php
session_start();
include("dbConfig.php");

// Ensure the user is logged in
if (!isset($_SESSION['username']) || !isset($_SESSION['name'])) {
    echo '<META http-equiv="refresh" content="0;logout.php">';
    exit();
}

$username = $_SESSION['username'];
$name = $_SESSION['name'];

// Fetch all examination records from the database
$query = "SELECT * FROM examination WHERE is_deleted = 0";  // Exclude deleted records
$result = mysqli_query($db, $query);

// Calculate total amount
$totalAmount = 0;
while ($row = mysqli_fetch_assoc($result)) {
    $totalAmount += $row['Amount'];
}

mysqli_data_seek($result, 0); // Reset the result pointer after calculation

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Examination Records</title>
    <link rel="stylesheet" href="path/to/bootstrap.min.css"> <!-- Add Bootstrap for styling -->
    <script src="path/to/jquery.min.js"></script> <!-- jQuery for AJAX and DOM manipulation -->
</head>
<body>

<div class="container">
    <h2 class="text-center mt-4">Examination Records</h2>
    
    <!-- Table to display records -->
    <table class="table table-bordered mt-4">
        <thead>
            <tr>
                <th>Sl No</th>
                <th>Bill No</th>
                <th>Patient ID</th>
                <th>Particulars</th>
                <th>Quantity</th>
                <th>Unit Price</th>
                <th>Amount</th>
                <th>Department</th>
                <th>Consultant</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php while ($row = mysqli_fetch_assoc($result)): ?>
                <tr>
                    <td><?php echo $row['SlNo']; ?></td>
                    <td><?php echo $row['BillNo']; ?></td>
                    <td><?php echo $row['PatId']; ?></td>
                    <td><?php echo $row['Particulars']; ?></td>
                    <td><?php echo $row['Quantity']; ?></td>
                    <td><?php echo $row['UnitPrice']; ?></td>
                    <td><?php echo $row['Amount']; ?></td>
                    <td><?php echo $row['optionlab']; ?></td>
                    <td><?php echo $row['consultant']; ?></td>
                    <td>
                        <a href="edit_examination.php?SlNo=<?php echo $row['SlNo']; ?>" class="btn btn-warning btn-sm">Edit</a>
                    </td>
                </tr>
            <?php endwhile; ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="6" class="text-right"><strong>Total Amount:</strong></td>
                <td><strong><?php echo number_format($totalAmount, 2); ?></strong></td>
                <td colspan="3"></td>
            </tr>
        </tfoot>
    </table>

    <!-- Print and Cancel Buttons -->
    <div class="text-center mt-4">
        <a href="examinationprint.php" class="btn btn-success">Print</a>
        <a href="previouspage.php" class="btn btn-danger">Cancel</a>
    </div>
</div>

</body>
</html>
