<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Order Confirmation</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .container {
            max-width: 600px;
            margin: auto;
            padding: 20px;
            background-color: #ffffff;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        .logo {
            margin-bottom: 20px;
        }

        p {
            margin-bottom: 16px;
            text-align: left;
        }

        strong {
            color: #007bff;
        }

        .footer {
            margin-top: 20px;
            padding-top: 10px;
            border-top: 1px solid #ddd;
            text-align: center;
            font-size: 14px;
            color: #888;
        }

        .footer a {
            color: #007bff;
            text-decoration: none;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }

        th, td {
            padding: 10px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #2a6496;
            color: white;
        }

        .totals-row {
            font-weight: bold;
            background-color: #f8f8f8;
        }

        .grand-total {
            font-weight: bold;
            font-size: 1.1em;
            border-top: 2px solid #2a6496;
            border-bottom: 2px solid #2a6496;
        }
    </style>
</head>

<body>
    <div class="container">
        <img src="https://beeimg.com/images/f47833961462.png" alt="Company Logo" class="logo" style="max-width: 200px;">

        <div style="background-color: #f8f8f8; padding: 20px; border-radius: 5px;">
            <h1 style="color: #2a6496; margin-top: 0;">Order Confirmation</h1>
            <p>Dear {{ $customername }},</p>
            <p>Thank you for your order! We're pleased to confirm that your order <strong>#{{ $orderid }}</strong>
                has been received and is being processed.</p>
        </div>

        <!-- Customer Details Section -->
        <div style="margin: 25px 0; background: #ffffff; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); overflow: hidden;">
            <div style="background: #2a6496; padding: 12px 20px;">
                <h3 style="color: #ffffff; margin: 0; font-size: 16px; font-weight: 600; letter-spacing: 0.5px;">CUSTOMER DETAILS</h3>
            </div>
            <div style="padding: 20px;">
                <table>
                    <tr>
                        <td style="width: 35%; font-weight: 600; color: #555;">Name:</td>
                        <td>{{ $customername ?? '' }}</td>
                    </tr>
                    <tr>
                        <td style="font-weight: 600; color: #555;">Phone:</td>
                        <td>{{ $customerphone ?? '' }}</td>
                    </tr>
                    <tr>
                        <td style="font-weight: 600; color: #555;">Address:</td>
                        <td style="white-space: pre-line;">{{ $customeraddress }}{{ $customeraddresstwo ? ', ' . $customeraddresstwo : '' }}</td>
                    </tr>
                    <tr>
                        <td style="font-weight: 600; color: #555;">Order Date:</td>
                        <td>{{ date('F j, Y', strtotime($orderdate)) }}</td>
                    </tr>
                </table>
            </div>
        </div>

        <div style="margin: 30px 0;">
            <h2 style="color: #2a6496; border-bottom: 2px solid #2a6496; padding-bottom: 5px;">Order Summary</h2>

            <table>
                <thead>
                    <tr>
                        <th>Item</th>
                        <th style="text-align: right;">Color</th>
                        <th style="text-align: right;">Qty</th>
                        <th style="text-align: right;">Unit Price</th>
                        <th style="text-align: right;">Total</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($orderItems as $item)
                    <tr>
                        <td>{{ $item['product_name'] }}</td>
                        <td style="text-align: right;">{{ $item['settextcolor'] }}</td>
                        <td style="text-align: right;">{{ $item['quantity'] }}</td>
                        <td style="text-align: right;">LKR {{ number_format($item['unit_price'], 2) }}</td>
                        <td style="text-align: right;">LKR {{ number_format($item['total_price'], 2) }}</td>
                    </tr>
                    @endforeach
                </tbody>
                <tfoot>
                    @php
$subtotal = $totalOrderAmount ?? 0;
$shipping = $totalServiceFee ?? 0;
$discount = $discountAmount ?? 0;
$tax = $taxAmount ?? 0;
$points = $pointsApplied ?? 0;

$finalamount = max(0, $subtotal + $shipping + $tax - $discount - $points);
                    @endphp
                    <tr class="totals-row">
                        <td colspan="4" style="text-align: right;">Subtotal:</td>
                        <td style="text-align: right;">LKR {{ number_format($totalOrderAmount, 2) }}</td>
                    </tr>
                    <tr class="totals-row">
                        <td colspan="4" style="text-align: right;">Shipping Charges:</td>
                        <td style="text-align: right;">LKR {{ number_format($totalServiceFee, 2) }}</td>
                    </tr>
                    @if(isset($discountAmount) && $discountAmount > 0)
                    <tr class="totals-row discount-row" style="color: green;">
                        <td colspan="4" style="text-align: right;">Discount:</td>
                        <td style="text-align: right;">- LKR {{ number_format($discountAmount, 2) }}</td>
                    </tr>
                    @endif
                    @if(isset($pointsApplied) && $pointsApplied > 0)
                    <tr class="totals-row" style="color: blue;">
                        <td colspan="4" style="text-align: right;">Points Applied:</td>
                        <td style="text-align: right;">- LKR {{ number_format($pointsApplied, 2) }}</td>
                    </tr>
                    @endif
                    @if(isset($taxAmount) && $taxAmount > 0)
                    <tr class="totals-row">
                        <td colspan="4" style="text-align: right;">Tax ({{ $taxPercentage }}%):</td>
                        <td style="text-align: right;">LKR {{ number_format($taxAmount, 2) }}</td>
                    </tr>
                    @endif
                    <tr class="grand-total">
                        <td colspan="4" style="text-align: right;">Total Amount:</td>
                        <td style="text-align: right;">LKR {{ number_format($finalamount, 2) }}</td>
                    </tr>
                </tfoot>
            </table>
        </div>

        <div style="background-color: #f8f8f8; padding: 20px; border-radius: 5px; margin-top: 20px;">
            <h3 style="color: #2a6496; margin-top: 0;">Next Steps</h3>
            <p>Your order is now being processed. You'll receive another email when your order ships.</p>
            <p>If you have any questions about your order, please contact our customer service team.</p>
        </div>

        <div class="footer">
            <p><strong>American Plastics Pvt (Ltd)</strong></p>
            <p>Contact us: <a href="mailto:info@americanplastics.lk">sales@americanplastic.lk</a></p>
            <p>Hotline: +94 77 286 4877</p>
        </div>
    </div>
</body>
</html>
