<?php
session_start();
include_once('../includes/config.php');

// Check if user is logged in
if (!isset($_SESSION['uid'])) {
    header('location:../login.php');
    exit();
}

$invoiceNumber = isset($_GET['invoice']) ? mysqli_real_escape_string($con, $_GET['invoice']) : '';

if (empty($invoiceNumber)) {
    die('Invalid invoice number');
}

function formatAmountToWords($amount)
{
    $value = (int) floor(floatval($amount));

    if ($value < 0) {
        return 'Minus ' . formatAmountToWords(abs($value));
    }

    if (class_exists('NumberFormatter')) {
        $formatter = new NumberFormatter('en_IN', NumberFormatter::SPELLOUT);
        return ucwords($formatter->format($value));
    }

    return fallbackNumberToWords($value);
}

function fallbackNumberToWords($number)
{
    if ($number === 0) {
        return 'Zero';
    }

    static $words = [
        0 => 'Zero',
        1 => 'One',
        2 => 'Two',
        3 => 'Three',
        4 => 'Four',
        5 => 'Five',
        6 => 'Six',
        7 => 'Seven',
        8 => 'Eight',
        9 => 'Nine',
        10 => 'Ten',
        11 => 'Eleven',
        12 => 'Twelve',
        13 => 'Thirteen',
        14 => 'Fourteen',
        15 => 'Fifteen',
        16 => 'Sixteen',
        17 => 'Seventeen',
        18 => 'Eighteen',
        19 => 'Nineteen'
    ];

    static $tens = [
        20 => 'Twenty',
        30 => 'Thirty',
        40 => 'Forty',
        50 => 'Fifty',
        60 => 'Sixty',
        70 => 'Seventy',
        80 => 'Eighty',
        90 => 'Ninety'
    ];

    static $scales = [
        10000000 => 'Crore',
        100000 => 'Lakh',
        1000 => 'Thousand',
        100 => 'Hundred'
    ];

    $result = '';

    foreach ($scales as $value => $label) {
        if ($number >= $value) {
            $count = (int) floor($number / $value);
            $number %= $value;
            $separator = $value === 100 && $number > 0 ? ' and ' : ' ';
            $result .= fallbackNumberToWords($count) . ' ' . $label;
            if ($number > 0) {
                $result .= $separator;
            }
        }
    }

    if ($number > 0) {
        if ($number < 20) {
            $result .= $words[$number];
        } else {
            $tensValue = (int) floor($number / 10) * 10;
            $unitsValue = $number % 10;
            $result .= $tens[$tensValue];
            if ($unitsValue > 0) {
                $result .= ' ' . $words[$unitsValue];
            }
        }
    }

    $cleanResult = trim(preg_replace('/\s+/', ' ', $result));
    return ucwords($cleanResult);
}

// Get store settings
$storeSettingsQuery = "SELECT * FROM tblstore_settings WHERE StoreCode = 'ST0002' LIMIT 1";
$storeSettingsResult = mysqli_query($con, $storeSettingsQuery);
$storeSettings = null;

if ($storeSettingsResult && mysqli_num_rows($storeSettingsResult) > 0) {
    $storeSettings = mysqli_fetch_assoc($storeSettingsResult);
}

// Get sale data with customer balance and GST information
$saleSql = "SELECT s.*, c.CustomerName, c.CustomerCode, c.Mobile, c.Address, c.City, c.State, c.GSTNumber, c.CurrentBalance 
            FROM tblsales s 
            JOIN tblcustomers c ON s.CustomerID = c.ID 
            WHERE s.InvoiceNumber = '$invoiceNumber'";
$saleResult = mysqli_query($con, $saleSql);

if (!$saleResult) {
    die('Database query error: ' . mysqli_error($con));
}

if (mysqli_num_rows($saleResult) == 0) {
    die('Invoice not found: ' . htmlspecialchars($invoiceNumber));
}

$sale = mysqli_fetch_assoc($saleResult);

// Get sale details with GST information
$detailsSql = "SELECT * FROM tblsales_gst_details WHERE SaleID = " . $sale['ID'] . " ORDER BY ID";
$detailsResult = mysqli_query($con, $detailsSql);

if (!$detailsResult) {
    die('Database query error: ' . mysqli_error($con));
}

// Calculate GST summary
$gstSummary = [];
$totalTaxableAmount = 0;
$totalCGST = 0;
$totalSGST = 0;
$totalIGST = 0;
$totalGSTAmount = 0;
$totalDiscountAmount = 0;

mysqli_data_seek($detailsResult, 0);
while ($item = mysqli_fetch_assoc($detailsResult)) {
    $taxPercent = floatval($item['TaxPercentage']);
    
    // Get discount directly from table
    $itemDiscount = floatval($item['DiscountAmount']);
    
    if (!isset($gstSummary[$taxPercent])) {
        $gstSummary[$taxPercent] = [
            'taxable' => 0,
            'cgst' => 0,
            'sgst' => 0,
            'igst' => 0,
            'total' => 0
        ];
    }
    
    $gstSummary[$taxPercent]['taxable'] += floatval($item['TaxableAmount']);
    $gstSummary[$taxPercent]['cgst'] += floatval($item['CGSTAmount']);
    $gstSummary[$taxPercent]['sgst'] += floatval($item['SGSTAmount']);
    $gstSummary[$taxPercent]['igst'] += floatval($item['IGSTAmount']);
    $gstSummary[$taxPercent]['total'] += floatval($item['TotalTaxAmount']);
    
    $totalTaxableAmount += floatval($item['TaxableAmount']);
    $totalCGST += floatval($item['CGSTAmount']);
    $totalSGST += floatval($item['SGSTAmount']);
    $totalIGST += floatval($item['IGSTAmount']);
    $totalGSTAmount += floatval($item['TotalTaxAmount']);
    $totalDiscountAmount += $itemDiscount;
}

// Reset pointer for item display
mysqli_data_seek($detailsResult, 0);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Tax Invoice - <?php echo $invoiceNumber; ?></title>
    <style>
        @page {
            size: A4;
            margin: 10mm;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', 'Poppins', -apple-system, BlinkMacSystemFont, sans-serif;
            font-size: 10px;
            line-height: 1.4;
            color: #000;
        }
        
        .invoice-container {
            width: 210mm;
            min-height: 297mm;
            border: 1px solid #000;
            padding: 8mm;
            background: #fff;
        }
        
        .header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            border: 1px solid #000;
            padding: 10px;
            margin-bottom: 5px;
        }
        
        .company-logo {
            width: 70px;
            height: 70px;
            border: 1px solid #000;
            border-radius: 3px;
            background: #fff;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
        }
        
        .company-logo img {
            max-width: 100%;
            max-height: 100%;
        }
        
        .company-details {
            flex: 1;
            padding: 0 15px;
        }
        
        .company-details h1 {
            font-size: 20px;
            font-weight: bold;
            margin-bottom: 4px;
            color: #000;
        }
        
        .company-details p {
            margin: 2px 0;
            font-size: 10px;
        }
        
        .invoice-title {
            text-align: center;
            padding: 5px;
            border: 1px solid #000;
            background: #fff;
            margin-bottom: 5px;
        }
        
        .invoice-title h2 {
            font-size: 16px;
            font-weight: bold;
        }
        
        .invoice-title p {
            font-size: 10px;
            margin-top: 3px;
        }
        
        .bill-details {
            display: flex;
            justify-content: space-between;
            margin-bottom: 5px;
            gap: 8px;
        }
        
        .bill-to, .ship-to, .invoice-info {
            border: 1px solid #000;
            padding: 8px;
        }
        
        .bill-to {
            width: 50%;
        }
        
        .ship-to {
            width: 50%;
        }
        
        .invoice-info {
            width: 35%;
        }
        
        .section-title {
            background: #f0f0f0;
            padding: 4px 8px;
            font-weight: bold;
            font-size: 11px;
            margin-bottom: 6px;
            border: 1px solid #000;
            margin: -8px -8px 6px -8px;
        }
        
        .detail-row {
            display: flex;
            margin: 3px 0;
            font-size: 10px;
        }
        
        .detail-label {
            font-weight: bold;
            width: 120px;
        }
        
        .detail-value {
            flex: 1;
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 5px;
            font-size: 10px;
        }
        
        table th {
            background: #f0f0f0;
            border: 1px solid #000;
            padding: 6px 4px;
            text-align: center;
            font-weight: bold;
            font-size: 10px;
        }
        
        table td {
            border: 1px solid #000;
            padding: 5px 4px;
            text-align: left;
        }
        
        .text-right {
            text-align: right !important;
        }
        
        .text-center {
            text-align: center !important;
        }
        
        .summary-section {
            display: flex;
            justify-content: space-between;
            margin-bottom: 5px;
            margin-top: 10px;
            gap: 8px;
        }
        
        .bank-details {
            width: 55%;
            border: 1px solid #000;
            padding: 8px;
        }
        
        .amount-summary {
            width: 42%;
        }
        
        .amount-summary table {
            margin-bottom: 0;
        }
        
        .amount-summary table td {
            padding: 4px 8px;
            font-size: 10px;
        }
        
        .grand-total {
            background: #f9f9f9;
            font-weight: bold;
            font-size: 11px;
        }
        
        .footer-section {
            display: flex;
            justify-content: space-between;
            margin-top: 10px;
            margin-bottom: 5px;
            gap: 8px;
        }
        
        .declaration {
            width: 60%;
            border: 1px solid #000;
            padding: 8px;
            font-size: 9px;
        }
        
        .signature-section {
            width: 37%;
            border: 1px solid #000;
            padding: 8px;
            text-align: center;
        }
        
        .qr-code {
            width: 90px;
            height: 90px;
            border: 1px solid #000;
            margin: 8px auto;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #fff;
        }
        
        .thank-you {
            text-align: center;
            padding: 6px;
            border: 1px solid #000;
            margin-top: 5px;
            font-weight: bold;
            font-size: 11px;
        }
        
        .footer-image {
            margin-top: 15px;
            text-align: center;
            padding-top: 8px;
            border-top: 1px solid #000;
        }
        
        .footer-image img {
            max-width: 100%;
            height: auto;
            display: block;
            margin: 0 auto;
        }
        
        @media print {
            body {
                padding: 0;
            }
            
            .no-print {
                display: none !important;
            }
            
            .invoice-container {
                border: none;
                padding: 0;
            }
        }
        
        .gst-summary th {
            font-size: 10px;
            padding: 4px;
        }
        
        .gst-summary td {
            font-size: 10px;
            padding: 4px;
        }
    </style>
</head>
<body>
    <div class="invoice-container">
        <!-- Header -->
        <div class="header">
            <div class="company-logo">
                <?php if ($storeSettings && !empty($storeSettings['StoreLogo1'])): ?>
                    <img src="../<?php echo htmlspecialchars($storeSettings['StoreLogo1']); ?>" alt="Logo" onerror="this.parentElement.innerHTML='<div style=\'font-size:24px;color:#1976d2;\'>🏪</div>'">
                <?php else: ?>
                    <div style='font-size:24px;color:#1976d2;'>🏪</div>
                <?php endif; ?>
            </div>
            <div class="company-details">
                <h1><?php echo htmlspecialchars($storeSettings['StoreName'] ?? 'STORE NAME'); ?></h1>
                <p><strong><?php echo htmlspecialchars($storeSettings['City'] ?? ''); ?> - <?php echo htmlspecialchars($storeSettings['Postcode'] ?? ''); ?></strong></p>
                <p><?php echo htmlspecialchars($storeSettings['Address'] ?? ''); ?></p>
                <p><strong>Contact No. :</strong> <?php echo htmlspecialchars($storeSettings['Mobile'] ?? ''); ?><?php echo !empty($storeSettings['Phone']) ? ', ' . htmlspecialchars($storeSettings['Phone']) : ''; ?></p>
                <p><strong>Email :</strong> <?php echo htmlspecialchars($storeSettings['Email'] ?? ''); ?></p>
                <p><strong>GSTIN :</strong> <?php echo htmlspecialchars($storeSettings['GSTNumber'] ?? 'N/A'); ?></p>
                <?php if (!empty($storeSettings['StoreWebsite'])): ?>
                <p><strong>Website :</strong> <?php echo htmlspecialchars($storeSettings['StoreWebsite']); ?></p>
                <?php endif; ?>
            </div>
        </div>
        
        <!-- Invoice Title -->
        <div class="invoice-title">
            <h2>Tax Invoice</h2>
            <p>Original /Duplicate Bill</p>
        </div>
        
        <!-- Bill Details Row 1 -->
        <div class="bill-details">
            <div class="bill-to">
                <div class="section-title">Bill To</div>
                <div class="detail-row">
                    <span class="detail-label">Name :</span>
                    <span class="detail-value"><?php echo htmlspecialchars($sale['CustomerName']); ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Address :</span>
                    <span class="detail-value"><?php echo htmlspecialchars($sale['Address'] ?: 'N/A'); ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">State :</span>
                    <span class="detail-value"><?php echo htmlspecialchars($sale['State'] ?: 'N/A'); ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">GSTIN :</span>
                    <span class="detail-value"><?php echo htmlspecialchars($sale['GSTNumber'] ?: 'N/A'); ?></span>
                </div>
            </div>
            
            <div class="invoice-info">
                <div class="section-title">Invoice Details</div>
                <div class="detail-row">
                    <span class="detail-label"># Inv. No. :</span>
                    <span class="detail-value"><?php echo htmlspecialchars($sale['InvoiceNumber']); ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Inv. Date :</span>
                    <span class="detail-value"><?php echo date('d-m-Y', strtotime($sale['SaleDate'])); ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Payment Mode :</span>
                    <span class="detail-value"><?php echo htmlspecialchars($sale['PaymentMode'] ?: 'UPI'); ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Reverse Charge :</span>
                    <span class="detail-value">NO</span>
                </div>
            </div>
        </div>
        
        <!-- Bill Details Row 2 -->
        <div class="bill-details">
            <div class="ship-to">
                <div class="section-title">Ship To</div>
                <div class="detail-row">
                    <span class="detail-label">Name :</span>
                    <span class="detail-value"><?php echo htmlspecialchars($sale['CustomerName']); ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Address :</span>
                    <span class="detail-value"><?php echo htmlspecialchars($sale['ShippingAddress'] ?: $sale['Address'] ?: 'N/A'); ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">State :</span>
                    <span class="detail-value"><?php echo htmlspecialchars($sale['ShippingState'] ?: $sale['State'] ?: 'N/A'); ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">GSTIN :</span>
                    <span class="detail-value"><?php echo htmlspecialchars($sale['GSTNumber'] ?: 'N/A'); ?></span>
                </div>
            </div>
            
            <div class="invoice-info">
                <div class="section-title">Other Details</div>
                <div class="detail-row">
                    <span class="detail-label">Buyer's Order No:</span>
                    <span class="detail-value"><?php echo htmlspecialchars($sale['PurchaseOrderNo'] ?: 'N/A'); ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Supplier's Ref. :</span>
                    <span class="detail-value">N/A</span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Vehicle Number :</span>
                    <span class="detail-value"><?php echo htmlspecialchars($sale['VehicleNumber'] ?: 'N/A'); ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Transport Details :</span>
                    <span class="detail-value"><?php echo htmlspecialchars($sale['TransportName'] ?: 'N/A'); ?></span>
                </div>
            </div>
        </div>
        
        <!-- Items Table -->
        <table>
            <thead>
                <tr>
                    <th style="width: 3%;">Sr</th>
                    <th style="width: 23%;">Goods & Service Description</th>
                    <th style="width: 7%;">HSN</th>
                    <th style="width: 6%;">Quantity</th>
                    <th style="width: 8%;">Price</th>
                    <th style="width: 8%;">Discount</th>
                    <th style="width: 9%;">Basic Rate</th>
                    <th style="width: 10%;">Taxable</th>
                    <th style="width: 5%;">%</th>
                    <th style="width: 8%;">GST Amt.</th>
                    <th style="width: 9%;">Total</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                $sno = 1;
                while ($item = mysqli_fetch_assoc($detailsResult)): 
                    $itemTotal = floatval($item['FinalAmount']);
                    $itemDiscount = floatval($item['DiscountAmount']);
                    $rate = floatval($item['Rate']);
                    $quantity = floatval($item['Quantity']);
                    // Calculate basic rate per unit (taxable amount / quantity)
                    $basicRate = $quantity > 0 ? floatval($item['TaxableAmount']) / $quantity : 0;
                ?>
                <tr>
                    <td class="text-center"><?php echo $sno++; ?></td>
                    <td><?php echo htmlspecialchars($item['ProductName']); ?></td>
                    <td class="text-center"><?php echo htmlspecialchars($item['HSNCode'] ?: 'N/A'); ?></td>
                    <td class="text-right"><?php echo number_format($quantity, 2); ?></td>
                    <td class="text-right"><?php echo number_format($rate, 2); ?></td>
                    <td class="text-right"><?php echo number_format($itemDiscount, 2); ?></td>
                    <td class="text-right"><?php echo number_format($basicRate, 2); ?></td>
                    <td class="text-right"><?php echo number_format($item['TaxableAmount'], 2); ?></td>
                    <td class="text-center"><?php echo number_format($item['TaxPercentage'], 0); ?>%</td>
                    <td class="text-right"><?php echo number_format($item['TotalTaxAmount'], 2); ?></td>
                    <td class="text-right"><?php echo number_format($itemTotal, 2); ?></td>
                </tr>
                <?php endwhile; ?>
                
                <tr>
                    <td colspan="2" class="text-right"><strong>Sub-Total:</strong></td>
                    <td class="text-center"><strong><?php echo mysqli_num_rows($detailsResult) > 0 ? ($sno - 1) : 0; ?></strong></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td class="text-right"><strong><?php echo number_format($totalDiscountAmount, 2); ?></strong></td>
                    <td>&nbsp;</td>
                    <td class="text-right"><strong><?php echo number_format($totalTaxableAmount, 2); ?></strong></td>
                    <td>&nbsp;</td>
                    <td class="text-right"><strong><?php echo number_format($totalGSTAmount, 2); ?></strong></td>
                    <td class="text-right"><strong><?php echo number_format($sale['FinalAmount'] ?: $sale['TotalAmount'], 2); ?></strong></td>
                </tr>
            </tbody>
        </table>
        
        <!-- Summary Section -->
        <div class="summary-section">
            <div class="bank-details">
                <div class="section-title">Our Bank Details</div>
                <?php if ($storeSettings && !empty($storeSettings['BankDetails'])): 
                    $bankLines = explode("\n", $storeSettings['BankDetails']);
                    foreach ($bankLines as $line): 
                        $line = trim($line);
                        if (!empty($line)):
                ?>
                <div class="detail-row">
                    <span class="detail-value"><?php echo htmlspecialchars($line); ?></span>
                </div>
                <?php 
                        endif;
                    endforeach; 
                else: 
                ?>
                <div class="detail-row">
                    <span class="detail-label">Bank Name :</span>
                    <span class="detail-value">N/A</span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Branch :</span>
                    <span class="detail-value">N/A</span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Account No :</span>
                    <span class="detail-value">N/A</span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">IFSC Code :</span>
                    <span class="detail-value">N/A</span>
                </div>
                <?php endif; ?>
                <div style="margin-top: 8px; font-weight: bold; font-size: 10px;">
                    Invoice Total in Word
                </div>
                <div style="font-size: 10px; font-weight: bold; margin-top: 3px;">
                    Rupees <?php 
                    $amount = $sale['FinalAmount'] ?: $sale['TotalAmount'];
                    echo formatAmountToWords($amount);
                    ?> Only
                </div>
            </div>
            
            <div class="amount-summary">
                <table class="gst-summary">
                    <thead>
                        <tr>
                            <th colspan="2">SUMMARY</th>
                            <th>AMOUNT</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td colspan="2">CGST Amt :</td>
                            <td class="text-right"><?php echo number_format($totalCGST, 2); ?></td>
                        </tr>
                        <tr>
                            <td colspan="2">SGST Amt :</td>
                            <td class="text-right"><?php echo number_format($totalSGST, 2); ?></td>
                        </tr>
                        <tr>
                            <td colspan="2">IGST Amt :</td>
                            <td class="text-right"><?php echo number_format($totalIGST, 2); ?></td>
                        </tr>
                        <tr>
                            <td colspan="2">Freight Packing Charges :</td>
                            <td class="text-right"><?php echo number_format($sale['CooliAmount'], 2); ?></td>
                        </tr>
                        <tr>
                            <td colspan="2">Round off :</td>
                            <td class="text-right"><?php echo number_format($sale['RoundOffAmount'] ?: 0, 2); ?></td>
                        </tr>
                        <tr class="grand-total">
                            <td colspan="2"><strong>Total Amount :</strong></td>
                            <td class="text-right"><strong><?php echo number_format($sale['FinalAmount'] ?: $sale['TotalAmount'], 2); ?></strong></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        
        <!-- Footer Section -->
        <div class="footer-section">
            <div class="declaration">
                <div style="font-weight: bold; margin-bottom: 3px;">Declaration</div>
                <p>1. Subject to <?php echo htmlspecialchars($storeSettings['City'] ?? 'Local'); ?> jurisdiction</p>
                <p>2. Terms & conditions are subject to our trade policy</p>
                <p>3. Our risk & responsibility ceases after the delivery of goods.</p>
                <p style="margin-top: 3px;"><strong>E. & O.E.</strong></p>
            </div>
            
            <div class="signature-section">
                <div style="font-weight: bold; margin-bottom: 8px; font-size: 11px;">For, <?php echo strtoupper(htmlspecialchars($storeSettings['StoreName'] ?? 'STORE NAME')); ?></div>
                <?php if ($storeSettings && $storeSettings['ShowSignatureOnInvoice'] && !empty($storeSettings['SignatureImage'])): ?>
                <div style="margin: 8px auto; height: 60px; display: flex; align-items: center; justify-content: center;">
                    <img src="../<?php echo htmlspecialchars($storeSettings['SignatureImage']); ?>" alt="Signature" style="max-width: 120px; max-height: 60px;">
                </div>
                <?php else: ?>
                <div class="qr-code">
                    <img src="https://api.qrserver.com/v1/create-qr-code/?size=90x90&data=<?php echo urlencode($invoiceNumber); ?>" alt="QR Code" style="width: 100%; height: 100%;">
                </div>
                <?php endif; ?>
                <div style="margin-top: 12px; font-weight: bold; font-size: 10px;">Authorised Signatory</div>
            </div>
        </div>
        
        <div class="thank-you">
            Thank You For Business With Us!
        </div>
        
        <!-- Page Footer with Image -->
        <?php if ($storeSettings && !empty($storeSettings['StoreLogo2'])): ?>
        <div class="footer-image">
            <img src="../<?php echo htmlspecialchars($storeSettings['StoreLogo2']); ?>" alt="Footer Banner" style="width: 100%; max-width: 100%; height: auto;">
        </div>
        <?php endif; ?>
    </div>
    
    <!-- Print Button -->
    <div class="no-print" style="text-align: center; margin: 20px; position: fixed; top: 10px; right: 10px; background: white; padding: 10px; border: 2px solid #1976d2; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.2);">
        <button onclick="window.print()" style="padding: 10px 20px; font-size: 14px; cursor: pointer; background: #1976d2; color: white; border: none; border-radius: 3px; margin-right: 5px;">
            🖨️ Print
        </button>
        <button onclick="window.close()" style="padding: 10px 20px; font-size: 14px; cursor: pointer; background: #dc3545; color: white; border: none; border-radius: 3px;">
            ❌ Close
        </button>
    </div>
</body>
</html>
