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

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Credit_Note_{{ $returnRecord->return_no }}</title>
    <style>
        @page {
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Helvetica', 'Arial', sans-serif;
            font-size: 12px;
            color: #0f172a;
            margin: 0;
            padding: 0;
            line-height: 1.3;
        }

        .page-container {
            padding: 20px 30px;
        }

        /* --- Header --- */
        .header-section {
            width: 100%;
            border-bottom: 2px solid #0f172a;
            padding-bottom: 5px;
            margin-bottom: 10px;
        }

        .company-name {
            font-size: 24px;
            font-weight: 800;
            text-transform: uppercase;
            letter-spacing: 1px;
            margin-bottom: 2px;
            color: #000;
        }

        .company-sub {
            font-size: 10px;
            color: #334155;
            line-height: 1.3;
        }

        .invoice-title {
            font-size: 22px;
            font-weight: 700;
            text-transform: uppercase;
            text-align: right;
            color: #000;
        }

        .invoice-meta {
            text-align: right;
            font-size: 11px;
            color: #334155;
            margin-top: 2px;
        }

        /* --- Info Grid (Compact) --- */
        .info-table {
            width: 100%;
            margin-bottom: 10px;
            border-spacing: 10px;
            margin-left: -10px;
            width: calc(100% + 20px);
        }

        .info-box {
            width: 50%;
            vertical-align: top;
            padding: 8px;
            border: 1px solid #0f172a;
            border-radius: 4px;
        }

        .info-label {
            font-size: 10px;
            text-transform: uppercase;
            color: #000;
            font-weight: 800;
            margin-bottom: 2px;
            border-bottom: 1px solid #0f172a;
            padding-bottom: 2px;
            display: block;
        }

        .box-content {
            font-size: 11px;
            line-height: 1.3;
        }

        /* --- Items Table --- */
        .items-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 11px;
            margin-bottom: 10px;
        }

        .items-table th {
            padding: 6px 4px;
            border-bottom: 2px solid #0f172a;
            border-top: 1px solid #e2e8f0;
            color: #000;
            font-weight: 700;
            text-transform: uppercase;
            font-size: 10px;
        }

        .items-table td {
            padding: 6px 4px;
            border-bottom: 1px solid #e2e8f0;
            color: #334155;
        }

        .text-right {
            text-align: right !important;
        }

        .text-center {
            text-align: center !important;
        }

        .text-left {
            text-align: left !important;
        }

        .font-bold {
            font-weight: 700;
            color: #000;
        }

        /* --- Totals --- */
        .totals-container {
            border-top: 2px solid #0f172a;
            padding-top: 10px;
            page-break-inside: avoid;
        }

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

        .totals-table td {
            padding: 2px 0;
            font-size: 11px;
        }

        .final-row td {
            font-size: 13px;
            font-weight: 800;
            color: #000;
            border-top: 1px solid #0f172a;
            padding-top: 5px;
        }

        .terms-box {
            font-size: 9px;
            color: #475569;
            padding-right: 10px;
        }

        .terms-title {
            font-weight: 700;
            text-transform: uppercase;
            margin-bottom: 2px;
            color: #000;
        }

        footer {
            position: fixed;
            bottom: -30px;
            left: 0;
            right: 0;
            text-align: center;
            font-size: 8px;
            color: #94a3b8;
            height: 15px;
            border-top: 1px solid #e2e8f0;
            padding-top: 5px;
        }
    </style>
</head>

<body>
    <footer>Generated on {{ now()->format('d M, Y h:i A') }} • {{ $settings['company_name'] ?? 'Maa Trader' }}</footer>

    <div class="page-container">

        <!-- Header Section -->
        <table class="header-section">
            <tr>
                <td width="60%" style="vertical-align: top;">
                    <div class="company-name">{{ $settings['company_name'] ?? 'Maa Trader' }}</div>
                    <div class="company-sub">
                        {{ $settings['company_address'] ?? 'Orma Highway, Siwan - 841236' }}<br>
                        Phone: {{ $settings['company_phone'] ?? '+91 99999 99999' }}<br>
                        @if(isset($settings['company_gst']))
                            GSTIN: {{ $settings['company_gst'] }}
                        @endif
                    </div>
                </td>
                <td width="40%" style="vertical-align: top;">
                    <div class="invoice-title">Credit Note</div>
                    <div class="invoice-meta">
                        <strong>#{{ $returnRecord->return_no }}</strong><br>
                        Date: {{ \Carbon\Carbon::parse($returnRecord->created_at)->format('d M, Y') }}<br>
                    </div>
                </td>
            </tr>
        </table>

        <!-- Info Grid -->
        <table class="info-table">
            <tr>
                <td class="info-box">
                    <span class="info-label">Customer Details</span>
                    <div class="box-content">
                        {{-- Accessing customer via the first item or pass explicitly --}}
                        <div style="font-weight: 700; font-size: 12px; margin-bottom: 1px;">
                            {{ $customer->name ?? 'Walk-in Customer' }}
                        </div>
                        <div>{{ $customer->address ?? '' }}</div>
                        <div style="margin-top: 2px;">
                            Ph: <strong>{{ $customer->phone ?? 'N/A' }}</strong>
                        </div>
                    </div>
                </td>
                <td class="info-box">
                    <span class="info-label">Original Invoice</span>
                    <div class="box-content">
                        <div style="font-weight: 700;">Ref: {{ $returnRecord->invoice_no }}</div>
                        <div style="margin-top: 2px;">
                            Original Sales Order ID: #{{ $returnRecord->sales_order_id }}
                        </div>
                    </div>
                </td>
            </tr>
        </table>

        <!-- Items Table -->
        <table class="items-table">
            <thead>
                <tr>
                    <th width="8%" class="text-center">#</th>
                    <th width="50%" class="text-left">Description</th>
                    <th width="12%" class="text-right">Price</th>
                    <th width="10%" class="text-right">Qty</th>
                    <th width="20%" class="text-right">Total</th>
                </tr>
            </thead>
            <tbody>
                @foreach($items as $index => $item)
                    <tr>
                        <td class="text-center">{{ $index + 1 }}</td>
                        <td class="text-left">
                            <div style="font-weight: 700; color: #0f172a; font-size: 11px;">
                                {{ $item->batch->size->name ?? '' }} {{ $item->batch->subSize->name ?? '' }}
                                <span style="font-size: 9px; color: #64748b; font-weight: 400; margin-left: 5px;">
                                    (Batch: {{ $item->batch_no }})
                                </span>
                            </div>
                            @if($item->reason)
                                <div style="font-size: 9px; color: #dc2626; margin-top: 2px;">Reason: {{ $item->reason }}</div>
                            @endif
                        </td>
                        <td class="text-right">{{ number_format($item->price_per_piece, 2) }}</td>
                        <td class="text-right">{{ $item->quantity_pieces }}</td>
                        <td class="text-right font-bold">{{ number_format($item->amount, 2) }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>

        <!-- Totals -->
        <div class="totals-container">
            <table width="100%">
                <tr>
                    <td width="60%" style="vertical-align: top; padding-right: 20px;">
                        <div class="terms-box">
                            <div class="terms-title">Note</div>
                            <ol style="margin: 0; padding-left: 15px; margin-top: 2px;">
                                <li>The amount has been credited to the customer account or refunded.</li>
                            </ol>
                        </div>
                    </td>
                    <td width="40%" style="vertical-align: top;">
                        <table class="totals-table">
                            <tr class="final-row">
                                <td>Refund Amount</td>
                                <td class="text-right">Rs. {{ number_format($totalAmount, 2) }}</td>
                            </tr>
                        </table>

                        <div style="margin-top: 30px; text-align: right;">
                            <div
                                style="display: inline-block; border-top: 1px solid #0f172a; width: 150px; text-align: center; font-size: 8px; font-weight: 700;">
                                AUTHORIZED SIGNATORY
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>

    </div>
</body>

</html>