<?php
// generate_receipt.php - corrected and cleaned
session_start();
include 'layouts/dbconn.php';
date_default_timezone_set('Africa/Nairobi');

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$user_id      = $_SESSION['user_id'];
$order_id     = isset($_GET['order_id']) ? intval($_GET['order_id']) : 0;
$payment_id   = isset($_GET['payment_id']) ? intval($_GET['payment_id']) : 0;
$shipment_id  = isset($_GET['shipment_id']) ? intval($_GET['shipment_id']) : 0;

$is_shipment_receipt = false;
$payment_data   = null;
$shipment_data  = null;
$customer_data  = null;
$receipt_number = '';

// --- PAYMENT RECEIPT ---
if ($payment_id) {
    $payment_sql = "SELECT 
        p.*,
        pm.method_name AS payment_method_name,
        s.tracking_number,
        s.sender_name,
        s.receiver_name,
        s.total_charges AS shipment_total,
        s.currency,
        s.booking_date,
        c.customer_code,
        c.first_name AS customer_first_name,
        c.last_name AS customer_last_name,
        c.company_name,
        c.customer_type,
        c.phone AS customer_phone,
        c.email AS customer_email,
        processor.full_name AS processed_by_name,
        i.invoice_number
    FROM payments p
    LEFT JOIN payment_methods pm ON p.payment_method_id = pm.method_id
    LEFT JOIN shipments s ON p.shipment_id = s.shipment_id
    LEFT JOIN customers c ON p.customer_id = c.customer_id
    LEFT JOIN users processor ON p.created_by = processor.user_id
    LEFT JOIN invoices i ON p.invoice_id = i.invoice_id
    WHERE p.payment_id = ?";
    
    $payment_stmt = $conn->prepare($payment_sql);
    $payment_stmt->bind_param("i", $payment_id);
    $payment_stmt->execute();
    $payment_result = $payment_stmt->get_result();

    if ($payment_result->num_rows > 0) {
        $payment_data        = $payment_result->fetch_assoc();
        $is_shipment_receipt = true;

        $customer_data = [
            'customer_code' => $payment_data['customer_code'],
            'first_name'    => $payment_data['customer_first_name'],
            'last_name'     => $payment_data['customer_last_name'],
            'company_name'  => $payment_data['company_name'],
            'customer_type' => $payment_data['customer_type'],
            'phone'         => $payment_data['customer_phone'],
            'email'         => $payment_data['customer_email'],
        ];
    } else {
        die('Payment not found');
    }

// --- SHIPMENT RECEIPT ---
} elseif ($shipment_id) {
    $shipment_sql = "SELECT 
        s.*,
        c.customer_code,
        c.first_name AS customer_first_name,
        c.last_name AS customer_last_name,
        c.company_name,
        c.customer_type,
        c.phone AS customer_phone,
        c.email AS customer_email
    FROM shipments s
    LEFT JOIN customers c ON s.sender_customer_id = c.customer_id
    WHERE s.shipment_id = ?";
    
    $shipment_stmt = $conn->prepare($shipment_sql);
    $shipment_stmt->bind_param("i", $shipment_id);
    $shipment_stmt->execute();
    $shipment_result = $shipment_stmt->get_result();

    if ($shipment_result->num_rows > 0) {
        $shipment_data       = $shipment_result->fetch_assoc();
        $is_shipment_receipt = true;

        $customer_data = [
            'customer_code' => $shipment_data['customer_code'],
            'first_name'    => $shipment_data['customer_first_name'],
            'last_name'     => $shipment_data['customer_last_name'],
            'company_name'  => $shipment_data['company_name'],
            'customer_type' => $shipment_data['customer_type'],
            'phone'         => $shipment_data['customer_phone'],
            'email'         => $shipment_data['customer_email'],
        ];
    } else {
        die('Shipment not found');
    }

// --- ORDER RECEIPT ---
} elseif ($order_id) {
    $sql_order = "SELECT o.*, 
                         c.first_name, c.last_name, c.phone, c.email, c.address, c.customer_code,
                         creator.full_name AS created_by_name,
                         pm_latest.method_name AS latest_payment_method
                  FROM orders o
                  LEFT JOIN customers c ON o.customer_id = c.customer_id
                  LEFT JOIN users creator ON o.created_by = creator.user_id
                  LEFT JOIN (
                      SELECT p1.order_id, pm.method_name 
                      FROM payments p1
                      INNER JOIN payment_methods pm ON p1.payment_method_id = pm.method_id
                      WHERE p1.payment_id = (
                          SELECT MAX(p2.payment_id) FROM payments p2 WHERE p2.order_id = p1.order_id
                      )
                  ) pm_latest ON o.order_id = pm_latest.order_id
                  WHERE o.order_id = ?";

    $stmt_order = $conn->prepare($sql_order);
    $stmt_order->bind_param("i", $order_id);
    $stmt_order->execute();
    $order = $stmt_order->get_result()->fetch_assoc();

    if (!$order) {
        die('Order not found');
    }

} else {
    die('Invalid receipt request');
}

// --- RECEIPT NUMBER GENERATION ---
if ($is_shipment_receipt) {
    $receipt_sql = "SELECT receipt_number FROM receipts 
                    WHERE " . ($payment_id ? "payment_id = ?" : "shipment_id = ?") . " 
                    LIMIT 1";

    $receipt_stmt = $conn->prepare($receipt_sql);

    // assign first, then bind
    $bind_id = $payment_id ?: $shipment_id;
    $receipt_stmt->bind_param("i", $bind_id);

    $receipt_stmt->execute();
    $receipt_result = $receipt_stmt->get_result();

    if ($receipt_result->num_rows > 0) {
        $receipt_number = $receipt_result->fetch_assoc()['receipt_number'];

        // update print count
        $update_print_sql = "UPDATE receipts SET 
                                printed_count = printed_count + 1,
                                last_printed_at = CURRENT_TIMESTAMP
                             WHERE " . ($payment_id ? "payment_id = ?" : "shipment_id = ?");
        $update_stmt = $conn->prepare($update_print_sql);
        $update_stmt->bind_param("i", $bind_id);
        $update_stmt->execute();

    } else {
        $insert_receipt_sql = "INSERT INTO receipts (
            payment_id, shipment_id, customer_id, receipt_type, 
            total_amount, payment_method, issued_by, printed_count
        ) VALUES (?, ?, ?, 'PAYMENT', ?, ?, ?, 1)";
        $insert_stmt = $conn->prepare($insert_receipt_sql);

        $customer_id_value     = null;
        $total_amount_value    = 0;
        $payment_method_value  = 'Cash';

        if ($payment_data) {
            $customer_id_value    = $payment_data['customer_id'] ?? null;
            $total_amount_value   = $payment_data['amount'];
            $payment_method_value = $payment_data['payment_method_name'];
        } elseif ($shipment_data) {
            $customer_id_value    = null;
            $total_amount_value   = $shipment_data['total_charges'];
            $payment_method_value = 'Cash';
        }

        // bind params: int, int, int, double, string, int
        $insert_stmt->bind_param(
            "iiidsi",
            $payment_id,
            $shipment_id,
            $customer_id_value,
            $total_amount_value,
            $payment_method_value,
            $user_id
        );

        if ($insert_stmt->execute()) {
            $receipt_id = $conn->insert_id;
            $receipt_result = $conn->query("SELECT receipt_number FROM receipts WHERE receipt_id = $receipt_id");
            $receipt_number = ($receipt_result && $receipt_result->num_rows > 0)
                                ? $receipt_result->fetch_assoc()['receipt_number']
                                : 'REC' . date('Y') . str_pad($receipt_id, 4, '0', STR_PAD_LEFT);
        } else {
            $receipt_number = 'REC' . date('YmdHis');
        }
    }
} else {
    $existing_receipt = $conn->query("SELECT receipt_number FROM receipts WHERE order_id = $order_id LIMIT 1");
    $receipt_number   = ($existing_receipt && $existing_receipt->num_rows > 0)
                        ? $existing_receipt->fetch_assoc()['receipt_number']
                        : 'REC' . date('YmdHis');
}

// --- SETTINGS FETCH ---..
$sql_settings = "SELECT setting_key, setting_value FROM settings 
                 WHERE setting_key IN (
                     'company_name', 'company_phone', 'company_email', 
                     'company_address', 'company_city', 'company_logo',
                     'receipt_footer', 'currency_symbol', 'company_tax_id'
                 )";
$result_settings = $conn->query($sql_settings);

$settings = [];
if ($result_settings) {
    while ($row = $result_settings->fetch_assoc()) {
        $settings[$row['setting_key']] = $row['setting_value'];
    }
}

function getSetting($key, $default = '') {
    global $settings;
    return isset($settings[$key]) ? $settings[$key] : $default;
}

$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Receipt #<?php echo htmlspecialchars($receipt_number); ?> - <?php echo htmlspecialchars(getSetting('company_name', 'Freight Management System')); ?></title>
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Courier New', monospace;
            background: white;
            color: #000;
            line-height: 1.4;
        }
        
        .receipt-container {
            width: 80mm;
            margin: 0 auto;
            padding: 10px;
            background: white;
            min-height: 100vh;
        }
        
        .receipt-header {
            text-align: center;
            border-bottom: 2px solid #000;
            padding-bottom: 10px;
            margin-bottom: 15px;
        }
        
        .company-logo {
            max-width: 60px;
            max-height: 40px;
            margin-bottom: 5px;
        }
        
        .company-name {
            font-size: 16px;
            font-weight: bold;
            margin-bottom: 3px;
            text-transform: uppercase;
        }
        
        .company-details {
            font-size: 10px;
            margin-bottom: 2px;
        }
        
        .receipt-title {
            font-size: 14px;
            font-weight: bold;
            margin: 10px 0 5px 0;
            text-align: center;
            text-decoration: underline;
        }
        
        .receipt-info {
            margin-bottom: 15px;
            font-size: 11px;
        }
        
        .info-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 3px;
        }
        
        .customer-section {
            border-top: 1px dashed #000;
            border-bottom: 1px dashed #000;
            padding: 8px 0;
            margin: 10px 0;
        }
        
        .section-title {
            font-weight: bold;
            margin-bottom: 5px;
            text-decoration: underline;
        }
        
        .payment-details {
            background: #f8f9fa;
            border: 1px solid #000;
            padding: 10px;
            margin: 15px 0;
            font-size: 11px;
        }
        
        .payment-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 3px;
        }
        
        .total-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 3px;
        }
        
        .total-row.grand-total {
            font-weight: bold;
            font-size: 13px;
            border-top: 1px solid #000;
            border-bottom: 1px solid #000;
            padding: 5px 0;
            margin-top: 5px;
        }
        
        .receipt-footer {
            text-align: center;
            margin-top: 20px;
            border-top: 1px dashed #000;
            padding-top: 10px;
            font-size: 10px;
        }
        
        .thank-you {
            font-weight: bold;
            margin-bottom: 5px;
        }
        
        .footer-info {
            margin-bottom: 3px;
        }
        
        .barcode-section {
            text-align: center;
            margin: 15px 0;
        }
        
        .barcode-text {
            font-size: 8px;
            font-family: monospace;
            letter-spacing: 1px;
            margin-top: 5px;
        }
        
        .no-print {
            display: block;
        }
        
        @media print {
            body {
                margin: 0;
                padding: 0;
            }
            
            .receipt-container {
                width: 100%;
                margin: 0;
                padding: 5px;
            }
            
            .no-print {
                display: none !important;
            }
            
            @page {
                margin: 0;
                size: 80mm auto;
            }
        }
        
        @media (max-width: 768px) {
            .receipt-container {
                width: 100%;
                max-width: 350px;
                padding: 15px;
            }
        }
        
        .action-buttons {
            position: fixed;
            top: 20px;
            right: 20px;
            display: flex;
            gap: 10px;
        }
        
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 5px;
            font-weight: 600;
        }
        
        .btn-primary {
            background: #3b82f6;
            color: white;
        }
        
        .btn-secondary {
            background: #64748b;
            color: white;
        }
        
        .btn:hover {
            opacity: 0.9;
        }
    </style>
</head>

<body>
    <!-- Action Buttons -->
    <div class="action-buttons no-print">
        <button onclick="window.print()" class="btn btn-primary">
            Print Receipt
        </button>
        <?php if ($is_shipment_receipt): ?>
        <a href="view_shipment.php?id=<?php echo $payment_data['shipment_id'] ?? $shipment_id; ?>" class="btn btn-secondary">
            Back to Shipment
        </a>
        <?php else: ?>
        <a href="view_order.php?id=<?php echo $order_id; ?>" class="btn btn-secondary">
            Back to Order
        </a>
        <?php endif; ?>
    </div>

    <div class="receipt-container">
        <!-- Header -->
        <div class="receipt-header">
            <?php if (getSetting('company_logo')): ?>
            <img src="<?php echo htmlspecialchars(getSetting('company_logo')); ?>" alt="Logo" class="company-logo">
            <?php endif; ?>
            
            <div class="company-name"><?php echo htmlspecialchars(getSetting('company_name', 'Freight Management System')); ?></div>
            
            <?php if (getSetting('company_phone')): ?>
            <div class="company-details">Tel: <?php echo htmlspecialchars(getSetting('company_phone')); ?></div>
            <?php endif; ?>
            
            <?php if (getSetting('company_email')): ?>
            <div class="company-details">Email: <?php echo htmlspecialchars(getSetting('company_email')); ?></div>
            <?php endif; ?>
            
            <?php if (getSetting('company_address')): ?>
            <div class="company-details"><?php echo htmlspecialchars(getSetting('company_address')); ?></div>
            <div class="company-details"><?php echo htmlspecialchars(getSetting('company_city', 'Nairobi')); ?></div>
            <?php endif; ?>
            
            <?php if (getSetting('company_tax_id')): ?>
            <div class="company-details">Tax ID: <?php echo htmlspecialchars(getSetting('company_tax_id')); ?></div>
            <?php endif; ?>
        </div>

        <!-- Receipt Title -->
        <div class="receipt-title">PAYMENT RECEIPT</div>

        <!-- Receipt Information -->
        <div class="receipt-info">
            <div class="info-row">
                <span>Receipt #:</span>
                <span><strong><?php echo htmlspecialchars($receipt_number); ?></strong></span>
            </div>
            
            <?php if ($is_shipment_receipt): ?>
                <?php if ($payment_data): ?>
                <div class="info-row">
                    <span>Payment #:</span>
                    <span><strong><?php echo htmlspecialchars($payment_data['payment_number']); ?></strong></span>
                </div>
                <div class="info-row">
                    <span>Shipment #:</span>
                    <span><strong><?php echo htmlspecialchars($payment_data['tracking_number']); ?></strong></span>
                </div>
                <?php if (!empty($payment_data['invoice_number'])): ?>
                <div class="info-row">
                    <span>Invoice #:</span>
                    <span><?php echo htmlspecialchars($payment_data['invoice_number']); ?></span>
                </div>
                <?php endif; ?>
                <?php elseif ($shipment_data): ?>
                <div class="info-row">
                    <span>Shipment #:</span>
                    <span><strong><?php echo htmlspecialchars($shipment_data['tracking_number']); ?></strong></span>
                </div>
                <?php endif; ?>
            <?php elseif (isset($order)): ?>
                <div class="info-row">
                    <span>Order #:</span>
                    <span><strong><?php echo htmlspecialchars($order['order_number']); ?></strong></span>
                </div>
            <?php endif; ?>
            
            <div class="info-row">
                <span>Date:</span>
                <span><?php echo date('d/M/Y H:i'); ?></span>
            </div>
            <div class="info-row">
                <span>Cashier:</span>
                <span><?php echo htmlspecialchars($payment_data['processed_by_name'] ?? (isset($order) ? $order['created_by_name'] : 'System')); ?></span>
            </div>
        </div>

        <!-- Customer Information -->
        <?php if ($customer_data || isset($order)): ?>
        <div class="customer-section">
            <div class="section-title">CUSTOMER DETAILS</div>
            <div class="info-row">
                <span>Name:</span>
                <span>
                    <?php 
                    if ($is_shipment_receipt && $customer_data) {
                        if ($customer_data['customer_type'] === 'CORPORATE') {
                            echo htmlspecialchars($customer_data['company_name']);
                        } else {
                            echo htmlspecialchars(($customer_data['first_name'] ?? '') . ' ' . ($customer_data['last_name'] ?? ''));
                        }
                    } elseif (isset($order)) {
                        echo htmlspecialchars(($order['first_name'] ?? '') . ' ' . ($order['last_name'] ?? ''));
                    }
                    ?>
                </span>
            </div>
            <?php if (($customer_data && !empty($customer_data['customer_code'])) || (isset($order) && !empty($order['customer_code']))): ?>
            <div class="info-row">
                <span>Code:</span>
                <span><?php echo htmlspecialchars($customer_data['customer_code'] ?? $order['customer_code']); ?></span>
            </div>
            <?php endif; ?>
            <div class="info-row">
                <span>Phone:</span>
                <span><?php echo htmlspecialchars($customer_data['phone'] ?? (isset($order) ? $order['phone'] : '')); ?></span>
            </div>
            <?php if (($customer_data && !empty($customer_data['email'])) || (isset($order) && !empty($order['email']))): ?>
            <div class="info-row">
                <span>Email:</span>
                <span><?php echo htmlspecialchars($customer_data['email'] ?? $order['email']); ?></span>
            </div>
            <?php endif; ?>
        </div>
        <?php endif; ?>

        <!-- Payment Details -->
        <div class="payment-details">
            <div class="section-title">PAYMENT DETAILS</div>
            
            <?php if ($is_shipment_receipt): ?>
                <?php if ($payment_data): ?>
                <div class="info-row">
                    <span>Service:</span>
                    <span>Freight Charges - <?php echo htmlspecialchars($payment_data['tracking_number']); ?></span>
                </div>
                <div class="info-row">
                    <span>Route:</span>
                    <span><?php echo htmlspecialchars($payment_data['sender_name'] . ' → ' . $payment_data['receiver_name']); ?></span>
                </div>
                <div class="info-row">
                    <span>Booking Date:</span>
                    <span><?php echo date('d/M/Y', strtotime($payment_data['booking_date'])); ?></span>
                </div>
                
                <hr style="margin: 10px 0; border: none; border-top: 1px dotted #000;">
                
                <div class="total-row">
                    <span>Total Charges:</span>
                    <span><?php echo htmlspecialchars($payment_data['currency']); ?> <?php echo number_format($payment_data['shipment_total'], 2); ?></span>
                </div>
                <div class="payment-row">
                    <span>Payment Method:</span>
                    <span><strong><?php echo htmlspecialchars($payment_data['payment_method_name']); ?></strong></span>
                </div>
                <div class="payment-row">
                    <span>Payment Date:</span>
                    <span><?php echo date('d/M/Y', strtotime($payment_data['payment_date'])); ?></span>
                </div>
                <?php if (!empty($payment_data['reference_number'])): ?>
                <div class="payment-row">
                    <span>Reference:</span>
                    <span><?php echo htmlspecialchars($payment_data['reference_number']); ?></span>
                </div>
                <?php endif; ?>
                
                <div class="total-row grand-total">
                    <span>AMOUNT PAID:</span>
                    <span><?php echo htmlspecialchars($payment_data['currency']); ?> <?php echo number_format($payment_data['amount'], 2); ?></span>
                </div>
                
                <?php elseif ($shipment_data): ?>
                <div class="info-row">
                    <span>Service:</span>
                    <span>Freight Charges - <?php echo htmlspecialchars($shipment_data['tracking_number']); ?></span>
                </div>
                <div class="info-row">
                    <span>Route:</span>
                    <span><?php echo htmlspecialchars($shipment_data['sender_name'] . ' → ' . $shipment_data['receiver_name']); ?></span>
                </div>
                
                <div class="total-row grand-total">
                    <span>TOTAL AMOUNT:</span>
                    <span><?php echo htmlspecialchars($shipment_data['currency']); ?> <?php echo number_format($shipment_data['total_charges'], 2); ?></span>
                </div>
                <?php endif; ?>
                
            <?php elseif (isset($order)): ?>
                <!-- Original order payment details -->
                <div class="total-row">
                    <span>Subtotal:</span>
                    <span><?php echo htmlspecialchars(getSetting('currency_symbol', 'KSh')); ?> <?php echo number_format($order['subtotal'], 2); ?></span>
                </div>
                
                <?php if ($order['discount_amount'] > 0): ?>
                <div class="total-row">
                    <span>Discount:</span>
                    <span>-<?php echo htmlspecialchars(getSetting('currency_symbol', 'KSh')); ?> <?php echo number_format($order['discount_amount'], 2); ?></span>
                </div>
                <?php endif; ?>
                
                <?php if ($order['tax_amount'] > 0): ?>
                <div class="total-row">
                    <span>Tax:</span>
                    <span><?php echo htmlspecialchars(getSetting('currency_symbol', 'KSh')); ?> <?php echo number_format($order['tax_amount'], 2); ?></span>
                </div>
                <?php endif; ?>
                
                <div class="total-row grand-total">
                    <span>TOTAL:</span>
                    <span><?php echo htmlspecialchars(getSetting('currency_symbol', 'KSh')); ?> <?php echo number_format($order['total_amount'], 2); ?></span>
                </div>
                
                <div class="payment-row">
                    <span>Payment Method:</span>
                    <span><strong><?php echo htmlspecialchars($order['latest_payment_method'] ?? 'Cash'); ?></strong></span>
                </div>
                
                <div class="payment-row">
                    <span><strong>Amount Paid:</strong></span>
                    <span><strong><?php echo htmlspecialchars(getSetting('currency_symbol', 'KSh')); ?> <?php echo number_format($order['paid_amount'], 2); ?></strong></span>
                </div>
                
                <?php if ($order['balance_amount'] > 0): ?>
                <div class="payment-row">
                    <span><strong>Balance Due:</strong></span>
                    <span><strong><?php echo htmlspecialchars(getSetting('currency_symbol', 'KSh')); ?> <?php echo number_format($order['balance_amount'], 2); ?></strong></span>
                </div>
                <?php endif; ?>
            <?php endif; ?>
        </div>

        <!-- Barcode Section -->
        <div class="barcode-section">
            <div style="border: 1px solid #000; padding: 5px; font-size: 8px; letter-spacing: 2px;">
                |||| || ||| |||| | ||| ||||| || |||||
            </div>
            <div class="barcode-text">
                <?php 
                $barcode_text = '';
                if ($payment_data && !empty($payment_data['tracking_number'])) {
                    $barcode_text = $payment_data['tracking_number'];
                } elseif ($shipment_data && !empty($shipment_data['tracking_number'])) {
                    $barcode_text = $shipment_data['tracking_number'];
                } elseif (isset($order) && !empty($order['order_number'])) {
                    $barcode_text = $order['order_number'];
                }
                echo htmlspecialchars($barcode_text);
                ?>
            </div>
        </div>

        <!-- Footer -->
        <div class="receipt-footer">
            <div class="thank-you">THANK YOU FOR YOUR BUSINESS!</div>
            
            <?php if (getSetting('receipt_footer')): ?>
            <div class="footer-info"><?php echo htmlspecialchars(getSetting('receipt_footer')); ?></div>
            <?php endif; ?>
            
            <div class="footer-info">Please keep this receipt for your records</div>
            
            <div style="margin-top: 10px; font-size: 8px; color: #666;">
                Generated: <?php echo date('d/M/Y H:i:s'); ?>
            </div>
        </div>
    </div>

    <script>
        // Auto print on load for direct print links
        if (window.location.search.includes('auto_print=1')) {
            window.onload = function() {
                setTimeout(function() {
                    window.print();
                }, 500);
            };
        }
        
        // Keyboard shortcut for printing
        document.addEventListener('keydown', function(e) {
            if ((e.ctrlKey || e.metaKey) && e.key === 'p') {
                e.preventDefault();
                window.print();
            }
        });
        
        // Mobile-friendly adjustments
        if ('ontouchstart' in window || navigator.maxTouchPoints > 0) {
            document.querySelector('.action-buttons').style.position = 'relative';
            document.querySelector('.action-buttons').style.top = 'auto';
            document.querySelector('.action-buttons').style.right = 'auto';
            document.querySelector('.action-buttons').style.marginBottom = '20px';
            document.querySelector('.action-buttons').style.justifyContent = 'center';
        }
    </script>
</body>
</html>