please fix in this codes (<?php
include('../db_connect.php');

// Retrieve the list of services for the dropdown
$services = [];
$sql_services = "SELECT service_id, service_name, service_price FROM services";
$stmt_services = $pdo->prepare($sql_services);
$stmt_services->execute();
$services = $stmt_services->fetchAll(PDO::FETCH_ASSOC);

// Fetch patient details via AJAX
$selected_patient = null;
if (isset($_GET['search_patient'])) {
    $search_query = '%' . $_GET['search_patient'] . '%';
    $sql_patient = "SELECT id, name, phone, email, date_of_birth, registration_date FROM patients WHERE name LIKE :search_query OR id LIKE :search_query";
    $stmt_patient = $pdo->prepare($sql_patient);
    $stmt_patient->bindParam(':search_query', $search_query, PDO::PARAM_STR);
    $stmt_patient->execute();
    $patients = $stmt_patient->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode($patients);
    exit();
}

// Handle invoice submission
if (isset($_POST['submit_invoice'])) {
    if (!isset($_POST['patient_id']) || $_POST['patient_id'] == '') {
        $error = "Please select a patient.";
    } elseif (!isset($_POST['services']) || empty($_POST['services'])) {
        $error = "Please select at least one service.";
    } else {
        try {
            $pdo->beginTransaction();

            $patient_id = $_POST['patient_id'];
            $services_selected = $_POST['services'];
            $notes = $_POST['notes'];

            // Calculate subtotal
            $subtotal = 0;
            foreach ($services_selected as $service_id) {
                $sql_service = "SELECT service_name, service_price FROM services WHERE service_id = :service_id";
                $stmt_service = $pdo->prepare($sql_service);
                $stmt_service->bindParam(':service_id', $service_id, PDO::PARAM_INT);
                $stmt_service->execute();
                $service = $stmt_service->fetch(PDO::FETCH_ASSOC);

                if ($service) {
                    $subtotal += floatval($service['service_price']);
                }
            }

            // Get discount and tax from POST data
            $discount = isset($_POST['discount']) ? floatval($_POST['discount']) : 0; // Discount as amount
            $tax = isset($_POST['tax']) ? floatval($_POST['tax']) : 0; // Tax as percentage

            // Apply discount first
            $taxable_amount = $subtotal - $discount;
            if ($taxable_amount < 0) {
                $taxable_amount = 0; // Ensure it does not go negative
            }

            // Calculate tax amount
            $tax_amount = $taxable_amount * $tax / 100;

            // Calculate total amount
            $total = $taxable_amount + $tax_amount;

            // Insert invoice into invoices table
            $sql_invoice = "INSERT INTO invoices (patient_id, total_amount, invoice_date, notes, discount, tax)
                            VALUES (:patient_id, :total_amount, CURRENT_DATE, :notes, :discount, :tax)";
            $stmt_invoice = $pdo->prepare($sql_invoice);
            $stmt_invoice->bindParam(':patient_id', $patient_id, PDO::PARAM_INT);
            $stmt_invoice->bindParam(':total_amount', $total);
            $stmt_invoice->bindParam(':notes', $notes);
            $stmt_invoice->bindParam(':discount', $discount);
            $stmt_invoice->bindParam(':tax', $tax);
            $stmt_invoice->execute();

            $invoice_id = $pdo->lastInsertId();

            // Insert services into invoice_services table
            foreach ($services_selected as $service_id) {
                $sql_service = "SELECT service_name, service_price FROM services WHERE service_id = :service_id";
                $stmt_service = $pdo->prepare($sql_service);
                $stmt_service->bindParam(':service_id', $service_id, PDO::PARAM_INT);
                $stmt_service->execute();
                $service = $stmt_service->fetch(PDO::FETCH_ASSOC);

                if ($service) {
                    $sql_invoice_service = "INSERT INTO invoice_services (invoice_id, service_id, service_name, cost)
                                            VALUES (:invoice_id, :service_id, :service_name, :cost)";
                    $stmt_invoice_service = $pdo->prepare($sql_invoice_service);
                    $stmt_invoice_service->bindParam(':invoice_id', $invoice_id, PDO::PARAM_INT);
                    $stmt_invoice_service->bindParam(':service_id', $service_id, PDO::PARAM_INT);
                    $stmt_invoice_service->bindParam(':service_name', $service['service_name']);
                    $stmt_invoice_service->bindParam(':cost', $service['service_price']);
                    $stmt_invoice_service->execute();
                }
            }

            $pdo->commit();

            header('Location: view_invoice.php?invoice_id=' . $invoice_id);
            exit();
        } catch (Exception $e) {
            $pdo->rollBack();
            $error = "Error creating invoice: " . $e->getMessage();
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Meta tags for responsive design and character encoding -->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Page title -->
    <title>Create Invoice</title>
    <!-- Import Google Fonts for modern typography -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">

    <style>
        body {
            background-color: #f4f4f4;
            font-family: 'Poppins', sans-serif;
            color: #333;
        }
        .container {
            max-width: 1000px;
            margin: 40px auto;
            padding: 40px;
            background-color: #fff;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            border-radius: 12px;
        }
        h1, h2 {
            color: #4F3F02;
        }
        .form-group {
            margin-bottom: 25px;
        }
        .form-control {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 6px;
        }
        .btn {
            background-color: #D1C287;
            padding: 12px 24px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
        }
        .invoice-summary {
            margin-top: 30px;
        }
        .total-row {
            display: flex;
            justify-content: space-between;
            padding: 10px 0;
            border-top: 1px solid #ddd;
            font-weight: 600;
        }
        .error {
            color: #ff0033;
        }
        /* Styles for service rows */
        .service-row {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }
        .service-row select {
            flex: 1;
            margin-right: 10px;
        }
        .service-row .btn {
            background-color: #ff4d4d;
            color: #fff;
        }
        .service-row .btn:hover {
            background-color: #ff1a1a;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Create Invoice</h1>
        <?php if (isset($error)): ?>
            <div class="error"><?= $error ?></div>
        <?php endif; ?>
        <form method="POST" action="create_invoice.php" id="invoiceForm">
            <div class="form-group">
                <label for="search_patient">Search Patient:</label>
                <input type="text" name="search_patient" id="search_patient" class="form-control" placeholder="Type patient name or ID" onkeyup="fetchPatients()">
                <div id="patient_results" style="background: #fff; border: 1px solid #ddd; position: relative; display: none;"></div>
            </div>

            <div id="patient_details" style="display:none;">
                <h2>Patient Details</h2>
                <p><strong>Name:</strong> <span id="patient_name"></span></p>
                <p><strong>Phone:</strong> <span id="patient_phone"></span></p>
                <p><strong>Email:</strong> <span id="patient_email"></span></p>
                <p><strong>Date of Birth:</strong> <span id="patient_dob"></span></p>
                <p><strong>Registration Date:</strong> <span id="patient_registration"></span></p>
                <input type="hidden" name="patient_id" id="patient_id">
            </div>

            <h2>Services</h2>
            <div id="services">
                <div class="service-row">
                    <select name="services[]" class="form-control" onchange="calculateSubtotal()">
                        <option value="">Select a Service</option>
                        <?php foreach ($services as $service): ?>
                            <option value="<?= $service['service_id'] ?>" data-price="<?= $service['service_price'] ?>">
                                <?= $service['service_name'] ?> - $<?= number_format($service['service_price'], 2) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <button type="button" class="btn" onclick="removeServiceRow(this)">Remove</button>
                </div>
            </div>
            <button type="button" class="btn" onclick="addServiceRow()">Add Another Service</button>

            <div class="invoice-summary">
                <h3>Invoice Summary</h3>
                <div class="form-group">
                    <label for="subtotal">Subtotal:</label>
                    <input type="text" name="subtotal" id="subtotal" value="0.00" class="form-control" readonly>
                </div>
                <div class="form-group">
                    <label for="discount">Discount:</label>
                    <input type="text" name="discount" id="discount" value="0" class="form-control" oninput="calculateTotal()">
                </div>
                <div class="form-group">
                    <label for="tax">Tax (%):</label>
                    <input type="text" name="tax" id="tax" value="0" class="form-control" oninput="calculateTotal()">
                </div>
                <div class="form-group">
                    <label for="total">Total:</label>
                    <input type="text" name="total" id="total" value="0.00" class="form-control" readonly>
                </div>
            </div>

            <div class="form-group">
                <label for="notes">Notes:</label>
                <textarea name="notes" id="notes" class="form-control" placeholder="Add notes..."></textarea>
            </div>

            <button type="submit" name="submit_invoice" class="btn">Submit Invoice</button>
        </form>

        <a href="index.php" class="btn btn-primary m-5">Back to Patient Invoices</a>
    </div>

<script>
    function fetchPatients() {
        const query = document.getElementById('search_patient').value;
        const resultsDiv = document.getElementById('patient_results');

        if (query.length > 2) {
            const xhr = new XMLHttpRequest();
            xhr.open('GET', 'create_invoice.php?search_patient=' + encodeURIComponent(query), true);
            xhr.onreadystatechange = function () {
                if (this.readyState === 4 && this.status === 200) {
                    const patients = JSON.parse(this.responseText);
                    let resultsHTML = '';

                    if (patients.length > 0) {
                        patients.forEach(patient => {
                            resultsHTML += `<div onclick="selectPatient(${patient.id}, '${patient.name}', '${patient.phone}', '${patient.email}', '${patient.date_of_birth}', '${patient.registration_date}')" style="padding: 10px; cursor: pointer;">
                                <strong>${patient.name}</strong><br>ID: ${patient.id}<br>
                                Phone: ${patient.phone}<br>Email: ${patient.email}<hr></div>`;
                        });
                        resultsDiv.style.display = 'block';
                    } else {
                        resultsHTML = '<p>No patients found</p>';
                        resultsDiv.style.display = 'block';
                    }

                    resultsDiv.innerHTML = resultsHTML;
                }
            };
            xhr.send();
        } else {
            resultsDiv.style.display = 'none';
        }
    }

    function selectPatient(id, name, phone, email, dob, registration) {
        document.getElementById('patient_id').value = id;
        document.getElementById('patient_name').innerText = name;
        document.getElementById('patient_phone').innerText = phone;
        document.getElementById('patient_email').innerText = email;
        document.getElementById('patient_dob').innerText = dob;
        document.getElementById('patient_registration').innerText = registration;

        document.getElementById('patient_details').style.display = 'block';
        document.getElementById('patient_results').style.display = 'none';
        document.getElementById('search_patient').value = '';
    }

    function addServiceRow() {
        const servicesDiv = document.getElementById('services');
        const newServiceRow = document.createElement('div');
        newServiceRow.classList.add('service-row');
        newServiceRow.innerHTML = `
            <select name="services[]" class="form-control" onchange="calculateSubtotal()">
                <option value="">Select a Service</option>
                <?php foreach ($services as $service): ?>
                    <option value="<?= $service['service_id'] ?>" data-price="<?= $service['service_price'] ?>">
                        <?= $service['service_name'] ?> - $<?= number_format($service['service_price'], 2) ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <button type="button" class="btn" onclick="removeServiceRow(this)">Remove</button>
        `;
        servicesDiv.appendChild(newServiceRow);
    }

    function removeServiceRow(button) {
        button.parentElement.remove();
        calculateSubtotal();
    }

    function calculateSubtotal() {
        let subtotal = 0;
        document.querySelectorAll('select[name="services[]"]').forEach(select => {
            const selectedOption = select.options[select.selectedIndex];
            if (selectedOption && selectedOption.dataset.price) {
                subtotal += parseFloat(selectedOption.dataset.price);
            }
        });
        document.getElementById('subtotal').value = subtotal.toFixed(2);
        calculateTotal();
    }

    function calculateTotal() {
        const subtotal = parseFloat(document.getElementById('subtotal').value) || 0;
        const discount = parseFloat(document.getElementById('discount').value) || 0;
        const tax = parseFloat(document.getElementById('tax').value) || 0;

        // Apply discount first
        let taxable_amount = subtotal - discount;
        if (taxable_amount < 0) {
            taxable_amount = 0; // Ensure taxable amount is not negative
        }

        // Calculate tax on discounted subtotal
        const tax_amount = taxable_amount * tax / 100;

        // Calculate total
        const total = taxable_amount + tax_amount;

        document.getElementById('total').value = total.toFixed(2);
    }
</script>
</body>
</html>) about Search Patient: 
Type patient name or ID
, when type something doesn't show anything and make it as search input and this database (j. Employees
id (INT, AUTO_INCREMENT, PRIMARY KEY): Unique identifier for each employee.
name (VARCHAR(255) NOT NULL): Full name of the employee.
role (ENUM('admin', 'doctor', 'staff') NOT NULL): Role of the employee, which can be 'admin', 'doctor', or 'staff'.
email (VARCHAR(255) NOT NULL UNIQUE): Email address of the employee, ensuring it is unique.
phone (VARCHAR(20)): Phone number of the employee.
department (VARCHAR(100)): Department where the employee works.
date_of_joining (DATE): The date when the employee joined the organization.
status (ENUM('active', 'inactive') DEFAULT 'active'): Employment status, either 'active' or 'inactive', with 'active' as the default.
username (VARCHAR(100) NOT NULL UNIQUE): Username for employee login, ensuring it is unique.
password (VARCHAR(255) NOT NULL): Hashed password for the employee.
created_at (TIMESTAMP DEFAULT CURRENT_TIMESTAMP): Timestamp when the employee record was created.
updated_at (TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP): Timestamp when the employee record was last updated.
profile_picture (VARCHAR(255) DEFAULT 'default_profile.png'): Profile picture of the employee.
specialization (VARCHAR(255) DEFAULT NULL): Specialization of the employee.
) and type full codes