please fix this codes and fix styles and layout of invoice and add more codes to fix (<?php
// Enable error reporting for debugging purposes
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();
require_once '../db_connect.php';

// Include Dompdf library
require '../dompdf/autoload.inc.php';

// Use Dompdf namespaces
use Dompdf\Dompdf;
use Dompdf\Options;

// Check if the admin is logged in
if (!isset($_SESSION['admin_id'])) {
    header("Location: ../admin_login.php");
    exit();
}

// Get the invoice ID from the URL and validate it
if (!isset($_GET['invoice_id']) || !is_numeric($_GET['invoice_id'])) {
    echo "Invoice ID not provided or invalid.";
    exit();
}

$invoice_id = intval($_GET['invoice_id']);

// Fetch invoice details along with patient information
$sql_invoice = "SELECT i.*, p.name AS patient_name, p.email, p.phone, p.date_of_birth, p.registration_date
                FROM invoices i
                JOIN patients p ON i.patient_id = p.id
                WHERE i.id = :invoice_id";
$stmt_invoice = $pdo->prepare($sql_invoice);
$stmt_invoice->bindParam(':invoice_id', $invoice_id, PDO::PARAM_INT);
$stmt_invoice->execute();
$invoice = $stmt_invoice->fetch(PDO::FETCH_ASSOC);

if (!$invoice) {
    echo "Invoice not found.";
    exit();
}

// Fetch services associated with the invoice
$sql_services = "SELECT service_name, cost FROM invoice_services WHERE invoice_id = :invoice_id";
$stmt_services = $pdo->prepare($sql_services);
$stmt_services->bindParam(':invoice_id', $invoice_id, PDO::PARAM_INT);
$stmt_services->execute();
$services = $stmt_services->fetchAll(PDO::FETCH_ASSOC);

// Calculate totals
$subtotal = array_sum(array_column($services, 'cost'));
$discount = isset($invoice['discount']) ? $invoice['discount'] : 0; // Discount as amount
$tax = isset($invoice['tax']) ? $invoice['tax'] : 0; // Tax rate as percentage

$taxable_amount = $subtotal - $discount;

// Ensure taxable amount is not negative
if ($taxable_amount < 0) {
    $taxable_amount = 0;
}

// Calculate Tax Amount based on Tax Rate
$tax_amount = $taxable_amount * $tax / 100;

// Calculate Total Amount
$total_amount_calculated = $taxable_amount + $tax_amount;

// Update the invoice total amount if it has changed
if (round($total_amount_calculated, 2) != round($invoice['total_amount'], 2)) {
    $sql_update = "UPDATE invoices SET total_amount = :total_amount WHERE id = :invoice_id";
    $stmt_update = $pdo->prepare($sql_update);
    $stmt_update->bindParam(':total_amount', $total_amount_calculated, PDO::PARAM_STR);
    $stmt_update->bindParam(':invoice_id', $invoice_id, PDO::PARAM_INT);
    $stmt_update->execute();
    // Refresh the invoice data after update
    $invoice['total_amount'] = $total_amount_calculated;
}

// Convert images to data URIs for embedding in PDF
function getImageDataUri($filePath)
{
    if (file_exists($filePath) && is_readable($filePath)) {
        $imageData = base64_encode(file_get_contents($filePath));
        $mimeType = mime_content_type($filePath);
        return 'data:' . $mimeType . ';base64,' . $imageData;
    }
    return '';
}

// Load the logo image
$logo_src = getImageDataUri('../images/gold-logo.png');

// Company Information
$company_name = 'Longevacare';
$company_address = 'HDS Tower, Cluster M, JLT, Dubai';
$company_email = 'info@longevacare.com';
$company_phone = '+971 528 863 600';

// Start output buffering to capture HTML content
ob_start();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Invoice - <?php echo htmlspecialchars($invoice['patient_name']); ?></title>
    <style>
        /* Global Styles */
        @page {
            margin: 20mm 15mm;
        }

        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            margin: 0;
            padding: 0;
            color: #333;
            position: relative;
        }

        .container {
            position: relative;
            min-height: 100%;
            padding: 0 15mm;
        }

        h1, h2, h3, h4, h5, h6 {
            margin: 0;
            padding-bottom: 5px;
        }

        /* Invoice Header */
        .invoice-header {
            text-align: center;
            margin-bottom: 30px;
            margin-top: 20px;
            border-bottom: 2px solid #D1C287;
            padding-bottom: 10px;
        }

        .invoice-header h1 {
            font-size: 28px;
            color: #4F3F02;
        }

        .invoice-header img {
            max-width: 150px;
            margin-bottom: 10px;
        }

        /* From and To Sections */
        .addresses {
            display: grid;
            grid-template-columns: 1fr 1fr;
            grid-gap: 20px;
            margin-bottom: 30px;
        }

        .address-box {
            border: 1px solid #ccc;
            padding: 10px;
            border-radius: 5px;
        }

        .address-box h3 {
            margin-bottom: 10px;
            color: #4F3F02;
        }

        .address-box p {
            margin: 5px 0;
            font-size: 14px;
        }

        /* Section Titles */
        .section-title {
            font-size: 20px;
            font-weight: bold;
            color: #4F3F02;
            margin-top: 20px;
            margin-bottom: 10px;
        }

        /* Tables */
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
            margin-bottom: 20px;
        }

        table, th, td {
            border: 1px solid #ccc;
        }

        th {
            background-color: #D1C287;
            color: #4F3F02;
            padding: 8px;
            text-align: left;
        }

        td {
            padding: 8px;
            vertical-align: top;
        }

        /* Total Amount */
        .total-section {
            margin-top: 20px;
        }

        .total-section p {
            font-size: 16px;
            margin: 5px 0;
        }

        .total-amount {
            font-size: 20px;
            font-weight: bold;
            color: #4F3F02;
        }

        hr {
            border: none;
            border-top: 1px solid #ccc;
            margin: 10px 0;
        }

        /* Footer */
        .footer {
            position: absolute;
            bottom: 10mm;
            left: 15mm;
            right: 15mm;
            text-align: center;
            font-size: 12px;
            color: #7f8c8d;
            border-top: 1px solid #bdc3c7;
            padding-top: 10px;
        }
    </style>
</head>
<body>

<div class="container">
    <!-- Invoice Header -->
    <div class="invoice-header">
        <?php if ($logo_src): ?>
            <img src="<?php echo $logo_src; ?>" alt="Logo">
        <?php endif; ?>
        <h1>Invoice</h1>
    </div>

    <!-- From and To Addresses -->
    <div class="addresses">
        <!-- From Address -->
        <div class="address-box">
            <h3>From:</h3>
            <p><strong><?php echo $company_name; ?></strong></p>
            <p><?php echo $company_address; ?></p>
            <p>Email: <?php echo $company_email; ?></p>
            <p>Phone: <?php echo $company_phone; ?></p>
        </div>

        <!-- To Address -->
        <div class="address-box">
            <h3>To:</h3>
            <p><strong><?php echo htmlspecialchars($invoice['patient_name']); ?></strong></p>
            <?php
            // If patient address exists, display it
            if (!empty($invoice['patient_address'])): ?>
                <p><?php echo htmlspecialchars($invoice['patient_address']); ?></p>
            <?php endif; ?>
            <p>Email: <?php echo htmlspecialchars($invoice['email']); ?></p>
            <p>Phone: <?php echo htmlspecialchars($invoice['phone']); ?></p>
        </div>
    </div>

    <!-- Invoice Details -->
    <h2 class="section-title">Invoice Information</h2>
    <div class="info-section">
        <p><strong>Invoice ID:</strong> <?php echo htmlspecialchars($invoice['id']); ?></p>
        <p><strong>Invoice Date:</strong> <?php echo date('F j, Y', strtotime($invoice['invoice_date'])); ?></p>
    </div>

    <!-- Services Rendered -->
    <h2 class="section-title">Services Rendered</h2>
    <table>
        <thead>
            <tr>
                <th style="width: 70%;">Service Name</th>
                <th style="width: 30%; text-align: right;">Cost</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($services as $service): ?>
            <tr>
                <td><?php echo htmlspecialchars($service['service_name']); ?></td>
                <td style="text-align: right;">$<?php echo number_format($service['cost'], 2); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <!-- Invoice Summary -->
    <div class="total-section">
        <p><strong>Subtotal:</strong> $<?php echo number_format($subtotal, 2); ?></p>
        <p><strong>Discount:</strong> -$<?php echo number_format($discount, 2); ?></p>
        <p><strong>Taxable Amount:</strong> $<?php echo number_format($taxable_amount, 2); ?></p>
        <p><strong>Tax (<?php echo number_format($tax, 2); ?>%):</strong> $<?php echo number_format($tax_amount, 2); ?></p>
        <hr>
        <p class="total-amount"><strong>Total Amount:</strong> $<?php echo number_format($total_amount_calculated, 2); ?></p>
    </div>

    <!-- Notes -->
    <?php if (!empty($invoice['notes'])): ?>
    <h2 class="section-title">Notes</h2>
    <p><?php echo nl2br(htmlspecialchars($invoice['notes'])); ?></p>
    <?php endif; ?>

    <!-- Footer -->
    <div class="footer">
        <p>
            Contact Us<br>
            Email: <?php echo $company_email; ?> | Phone: <?php echo $company_phone; ?><br>
            Location: <?php echo $company_address; ?>
        </p>
    </div>
</div>

</body>
</html>
<?php
// Capture the HTML content into a variable
$html = ob_get_clean();

// Initialize Dompdf with options
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true); // Enable loading of images

$dompdf = new Dompdf($options);

// Load the HTML content into Dompdf
$dompdf->loadHtml($html);

// Set paper size and orientation to A4 portrait
$dompdf->setPaper('A4', 'portrait');

try {
    // Render the HTML as PDF
    $dompdf->render();

    // Output the generated PDF to the browser
    $dompdf->stream('Invoice_' . $invoice_id . '.pdf', array("Attachment" => 0));

} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
    // Optionally, log the exception or handle it as needed
}
?>) and use this codes to help you (<?php
session_start();
require_once '../db_connect.php';

// Include Dompdf library
require '../dompdf/autoload.inc.php';

// Use Dompdf namespaces
use Dompdf\Dompdf;
use Dompdf\Options;

// Check if the admin is logged in
if (!isset($_SESSION['admin_id'])) {
    header("Location: ../admin_login.php");
    exit();
}

// Get the prescription ID from the URL
if (!isset($_GET['id'])) {
    echo "Prescription ID not provided.";
    exit();
}

$id = $_GET['id'];

// Fetch prescription details along with patient and doctor information
$stmt = $pdo->prepare("
    SELECT p.*,
           pat.name AS patient_name, pat.age AS patient_age, pat.gender AS patient_gender,
           doc.name AS doctor_name, doc.specialization AS doctor_specialization, doc.phone AS doctor_phone, doc.email AS doctor_email, doc.profile_picture AS doctor_profile_picture
    FROM prescriptions p
    LEFT JOIN patients pat ON p.patient_id = pat.id
    LEFT JOIN doctors doc ON p.doctor_id = doc.id
    WHERE p.id = ?
");
$stmt->execute([$id]);
$prescription = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$prescription) {
    echo "Prescription not found.";
    exit();
}

// Escape and format output data
$prescription_id = htmlspecialchars($prescription['id']);
$prescription_date = date('F j, Y', strtotime($prescription['date_prescribed']));
$patient_name = htmlspecialchars($prescription['patient_name']);
$patient_age = htmlspecialchars($prescription['patient_age']);
$patient_gender = htmlspecialchars(ucfirst($prescription['patient_gender']));
$doctor_name = htmlspecialchars($prescription['doctor_name']);
$doctor_specialization = htmlspecialchars($prescription['doctor_specialization']);
$doctor_phone = htmlspecialchars($prescription['doctor_phone']);
$doctor_email = htmlspecialchars($prescription['doctor_email']);

// Get the instructions from the prescription
$instructions = htmlspecialchars($prescription['instructions']);

// Prepare medications
$medications_data = $prescription['medications'];
$medications = json_decode($medications_data, true);

// Check if json_decode failed
if (json_last_error() !== JSON_ERROR_NONE) {
    // Treat medications as plain text
    $medications = null;
}

// Convert images to data URIs
function getImageDataUri($filePath)
{
    if (file_exists($filePath)) {
        $imageData = base64_encode(file_get_contents($filePath));
        $mimeType = mime_content_type($filePath);
        return 'data:' . $mimeType . ';base64,' . $imageData;
    }
    return '';
}

$header_shape = getImageDataUri('../images/header-shape.png');
$logo_src = getImageDataUri('../images/gold-logo.png');
$signature_src = getImageDataUri('../images/signature.png');
$stamp_src = getImageDataUri('../images/stamp.png');
$footer_shape = getImageDataUri('../images/footer-shape.png');

// Start output buffering to capture HTML content
ob_start();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Prescription - <?php echo $patient_name; ?></title>
    <style>
        /* Global Styles */
        @page {
            margin: 20mm 15mm;
        }

        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            margin: 0;
            padding: 0;
            color: #333;
            position: relative;
        }

        .container {
            position: relative;
            min-height: 100%;
        }

        h1, h2, h3, h4, h5, h6 {
            margin: 0;
        }

        /* Header */
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            border-bottom: 2px solid #7e6e32;
            padding-bottom: 10px;
            margin-top: 80px;
        }

        .header .doctor-info {
            flex: 1;
        }

        .header .doctor-info h1 {
            font-size: 24px;
            color: #7e6e32;
        }

        .header .doctor-info p {
            font-size: 12px;
            color: #7e6e32;
        }

        .header-shape img {
            margin: 0;
            padding: 0;
            position: fixed;
            top: -150px;
            left: -200px;
        }

        .footer-shape img {
            margin: 0;
            padding: 0;
            position: fixed;
            top: -150px;
            left: -100px;
        }

        .header .logo {
            max-width: 200px;
            float: right;
            position: fixed;
            top: 80px;
        }

        /* Patient Info */
        .patient-info {
            margin-bottom: 30px;
        }

        .patient-info h2 {
            font-size: 18px;
            color: #7e6e32;
            margin-bottom: 10px;
            padding-bottom: 5px;
        }

        .patient-info p {
            font-size: 14px;
            margin: 5px 0;
        }

        /* Medications */
        .medications h2 {
            font-size: 18px;
            color: #7e6e32;
            margin-bottom: 10px;
            padding-bottom: 5px;
        }

        .medications table {
            width: 100%;
            border-collapse: collapse;
        }

        .medications th, .medications td {
            border: 1px solid #7e6e32;
            padding: 10px;
            text-align: left;
        }

        .medications th {
            background-color: #7e6e32;
            color: #fff;
        }

        .medications td {
            vertical-align: top;
        }

        .medications tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        /* Instructions */
        .instructions {
            margin-top: 20px;
            font-size: 14px;
        }

        .instructions h2 {
            font-size: 18px;
            color: #7e6e32;
            margin-bottom: 10px;
            padding-bottom: 5px;
        }

        /* Signature and Stamp */
        .footer-section {
            position: absolute;
            bottom: 120px;
            left: 0;
            right: 0;
            padding: 0 15mm;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .signature, .stamp {
            text-align: center;
            margin-bottom: 10px;
        }

        .signature img {
            max-width: 180px;
        }

        .stamp img {
            max-width: 220px;
            position:fixed;
            left:130;
        }

        .signature p, .stamp p {
            margin-top: 5px;
            font-size: 14px;
        }

        /* Footer */
        .footer {
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            padding: 10px 15mm;
            text-align: center;
            font-size: 12px;
            color: #7e6e32;
            border-top: 1px solid #7e6e32;
        }
    </style>
</head>
<body>

<div class="container">
    <!-- Header -->
    <div class="header">
        <?php if ($header_shape): ?>
            <div class="header-shape">
                <img src="<?php echo $header_shape; ?>" alt="Header Shape">
            </div>
        <?php endif; ?>
        <div class="doctor-info">
            <h1><?php echo $doctor_name; ?></h1>
            <p><?php echo $doctor_specialization; ?></p>
            <p>Phone: <?php echo $doctor_phone; ?> | Email: <?php echo $doctor_email; ?></p>
        </div>
        <?php if ($logo_src): ?>
            <img src="<?php echo $logo_src; ?>" alt="Logo" class="logo">
        <?php endif; ?>
    </div>

    <!-- Patient Information -->
    <div class="patient-info">
        <h2>Patient Information</h2>
        <p><strong>Name:</strong> <?php echo $patient_name; ?></p>
        <p><strong>Age:</strong> <?php echo $patient_age; ?> | <strong>Gender:</strong> <?php echo $patient_gender; ?></p>
        <p><strong>Date:</strong> <?php echo $prescription_date; ?></p>
        <p><strong>Prescription ID:</strong> <?php echo $prescription_id; ?></p>
    </div>

    <!-- Medications -->
    <div class="medications">
        <h2>Medications</h2>
        <?php if ($medications && is_array($medications)): ?>
            <table>
                <thead>
                    <tr>
                        <th>Medicine Name & Dosage</th>
                        <th>Frequency</th>
                        <th>Duration</th>
                        <th>Instructions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($medications as $med): ?>
                        <tr>
                            <td>
                                <strong><?php echo htmlspecialchars($med['name']); ?></strong><br>
                                <?php echo htmlspecialchars($med['dosage']); ?>
                            </td>
                            <td><?php echo htmlspecialchars($med['frequency']); ?></td>
                            <td><?php echo htmlspecialchars($med['duration']); ?></td>
                            <td><?php echo htmlspecialchars($med['instructions']); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php elseif (!empty($prescription['medications'])): ?>
            <!-- Display medications as plain text -->
            <p><?php echo nl2br(htmlspecialchars($prescription['medications'])); ?></p>
        <?php else: ?>
            <p>No medications prescribed.</p>
        <?php endif; ?>
    </div>

    <!-- Instructions -->
    <?php if (!empty($instructions)): ?>
    <div class="instructions">
        <h2>Additional Instructions</h2>
        <p><?php echo nl2br($instructions); ?></p>
    </div>
    <?php endif; ?>

    <!-- Signature & Stamp -->
    <div class="footer-section">
        <div class="signature">
            <?php if ($signature_src): ?>
                <img src="<?php echo $signature_src; ?>" alt="Doctor's Signature">
            <?php endif; ?>
            <p>Doctor's Signature</p>
        </div>
        <div class="stamp">
            <?php if ($stamp_src): ?>
                <img src="<?php echo $stamp_src; ?>" alt="Stamp">
            <?php endif; ?>
        </div>
    </div>

    <!-- Footer -->
    <div class="footer">
        <?php if ($footer_shape): ?>
            <div class="footer-shape">
                <img src="<?php echo $footer_shape; ?>" alt="Footer Shape">
            </div>
        <?php endif; ?>
        <p>
            Contact Us<br>
            Email: info@longevacare.com | Phone: +971 528 863 600 | WhatsApp: +1 (702) 526-2050<br>
            Location: HDS Tower, Cluster M, JLT, Dubai
        </p>
    </div>
</div>

</body>
</html>

<?php
// Capture the HTML content into a variable
$html = ob_get_clean();

// Initialize Dompdf with options
$options = new Options();
$options->set('isPhpEnabled', true);
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true); // Enable loading of images
$dompdf = new Dompdf($options);

// Load HTML content
$dompdf->loadHtml($html);

// Set paper size and orientation
$dompdf->setPaper('A4', 'portrait');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to browser
$dompdf->stream('Prescription_' . $prescription_id . '.pdf', array("Attachment" => 0));
?>) and type full codes