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

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

$slNo = isset($_GET['SlNo']) ? $_GET['SlNo'] : null;
if (!$slNo) {
    echo "SlNo is required.";
    exit();
}

// Fetch examination details to populate the form
$examQuery = "SELECT * FROM examination WHERE SlNo='$slNo'";
$examResult = mysqli_query($db, $examQuery);
if (mysqli_num_rows($examResult) > 0) {
    $examData = mysqli_fetch_assoc($examResult);
    $particulars = $examData['Particulars'];
    $consultant = $examData['consultant'];
    $department = $examData['optionlab'];
    $quantity = $examData['Quantity'];
    $price = $examData['UnitPrice'];
    $amount = $examData['Amount'];
} else {
    echo "No data found for this SlNo.";
    exit();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Get form values
    $newParticulars = $_POST['Particulars'];
    $newConsultant = $_POST['Consultant'];
    $newDepartment = $_POST['Department'];
    $newQuantity = $_POST['Quantity'];
    $newPrice = $_POST['Price'];
    $newAmount = $newQuantity * $newPrice;

    // Update the database
    $updateQuery = "UPDATE examination SET Particulars='$newParticulars', consultant='$newConsultant', optionlab='$newDepartment', Quantity='$newQuantity', UnitPrice='$newPrice', Amount='$newAmount', modified_at=NOW(), modified_by='$username' WHERE SlNo='$slNo'";
    if (mysqli_query($db, $updateQuery)) {
        echo "Data updated successfully!";
        header("Location: examination_print.php?PatId=".$examData['PatId']."&billNo=".$examData['BillNo']); // Redirect to the original page after update
        exit();
    } else {
        echo "Error updating record: " . mysqli_error($db);
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Examination Form</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        .form-container {
            background: #fff;
            padding: 20px 25px;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 500px;
        }

        .form-container h2 {
            margin-bottom: 20px;
            font-size: 22px;
            text-align: center;
            color: #333;
        }

        .form-container div {
            margin-bottom: 15px;
        }

        .form-container label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }

        .form-container input[type="text"],
        .form-container input[type="number"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 14px;
        }

        .form-container input[type="text"]:focus,
        .form-container input[type="number"]:focus {
            outline: none;
            border-color: #007bff;
            box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
        }

        .form-container button {
            width: 100%;
            padding: 10px;
            background: #007bff;
            border: none;
            border-radius: 4px;
            color: #fff;
            font-size: 16px;
            cursor: pointer;
        }

        .form-container button:hover {
            background: #0056b3;
        }

        .form-container input[readonly] {
            background-color: #f9f9f9;
        }
    </style>
</head>
<body>
    <div class="form-container">
        <h2>Update Form</h2>
        <form method="POST" action="">
            <div>
                <label for="Particulars">Particulars</label>
                <input type="text" name="Particulars" value="<?php echo $particulars; ?>" required />
            </div>
            <div>
                <label for="Consultant">Consultant</label>
                <input type="text" name="Consultant" value="<?php echo $consultant; ?>" required />
            </div>
            <div>
                <label for="Department">Department</label>
                <input type="text" name="Department" value="<?php echo $department; ?>" required />
            </div>
            <div>
                <label for="Quantity">Quantity</label>
                <input type="number" name="Quantity" value="<?php echo $quantity; ?>" required id="Quantity" oninput="calculateAmount()" />
            </div>
            <div>
                <label for="Price">Price</label>
                <input type="number" name="Price" value="<?php echo $price; ?>" required id="Price" oninput="calculateAmount()" />
            </div>
            <div>
                <label for="Amount">Amount</label>
                <input type="text" value="<?php echo $amount; ?>" readonly id="Amount" />
            </div>
            <div>
                <button type="submit">Update</button>
            </div>
        </form>
    </div>

  
<script>
    // Function to calculate the total amount (Quantity * Price)
    function calculateAmount() {
        var quantity = document.getElementById('Quantity').value;
        var price = document.getElementById('Price').value;

        if (quantity && price) {
            var amount = quantity * price;
            document.getElementById('Amount').value = amount.toFixed(2); // Display the result rounded to two decimal places
        }
    }
</script>
</body>
</html>


