<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Invoice Return - {{ $invoiceReturn->code }}</title>
    <style>
        body {
            font-family: 'DejaVu Sans', 'Arial', sans-serif;
            font-size: 12px;
            line-height: 1.4;
            color: #333;
            margin: 0;
            padding: 20mm;
        }

        .return-container {
            max-width: 170mm;
            margin: 0 auto;
        }

        .return-header {
            border-bottom: 2px solid #333;
            padding-bottom: 15px;
            margin-bottom: 25px;
            text-align: center;
        }

        .return-header h2 {
            margin: 0;
            color: #333;
            font-size: 24px;
            font-weight: bold;
        }

        .return-header p {
            margin: 5px 0;
            color: #666;
            font-size: 12px;
        }

        .return-details {
            display: table;
            width: 100%;
            margin-bottom: 25px;
        }

        .return-info,
        .financial-info {
            display: table-cell;
            vertical-align: top;
        }

        .financial-info {
            padding-left: 30px;
        }

        .section-title {
            margin: 0 0 10px 0;
            color: #333;
            border-bottom: 1px solid #ddd;
            padding-bottom: 3px;
            font-size: 14px;
            font-weight: bold;
        }

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

        .info-table td {
            padding: 3px 0;
            font-size: 11px;
        }

        .info-table .label {
            font-weight: bold;
            width: 120px;
        }

        .status-badge {
            display: inline-block;
            padding: 2px 6px;
            border-radius: 3px;
            font-size: 10px;
            font-weight: bold;
            color: white;
        }

        .status-success { background-color: #28a745; }
        .status-warning { background-color: #ffc107; }
        .status-danger { background-color: #dc3545; }
        .status-secondary { background-color: #6c757d; }

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

        .financial-table td {
            padding: 4px 0;
            font-size: 11px;
        }

        .financial-table .amount {
            text-align: right;
            font-weight: bold;
        }

        .return-items {
            margin-bottom: 25px;
        }

        .items-table {
            width: 100%;
            border-collapse: collapse;
            border: 1px solid #ddd;
            font-size: 11px;
        }

        .items-table th {
            background-color: #f8f9fa;
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
            font-weight: bold;
        }

        .items-table th:nth-child(3),
        .items-table th:nth-child(4),
        .items-table th:nth-child(5) {
            text-align: center;
        }

        .items-table th:last-child {
            text-align: right;
        }

        .items-table td {
            border: 1px solid #ddd;
            padding: 6px 8px;
        }

        .items-table td:nth-child(3) {
            text-align: center;
        }

        .items-table td:nth-child(4),
        .items-table td:nth-child(5) {
            text-align: right;
        }

        .items-table tbody tr:nth-child(even) {
            background-color: #f8f9fa;
        }

        .return-footer {
            border-top: 2px solid #333;
            padding-top: 15px;
            margin-top: 30px;
            text-align: center;
            color: #666;
            font-size: 10px;
        }

        .return-footer p {
            margin: 3px 0;
        }
    </style>
</head>
<body>
    <div class="return-container">
        <!-- Header -->
        @include('components.pdf.header', ['title' => 'Invoice Return - ' . ($return->code ?? 'N/A'), 'shop' => $shop ?? null])
        <div style="text-align: center; margin-bottom: 20px;">
            <h2 style="margin: 0; color: #333; font-size: 20px; font-weight: bold;">INVOICE RETURN</h2>
        </div>

        <!-- Return Details -->
        <div class="return-details">
            <div class="return-info">
                <h3 class="section-title">Return Information</h3>
                <table class="info-table">
                    <tr>
                        <td class="label">Return Code:</td>
                        <td>{{ $invoiceReturn->code ?? 'N/A' }}</td>
                    </tr>
                    <tr>
                        <td class="label">Original Invoice:</td>
                        <td>{{ $invoiceReturn->invoice_code ?? 'N/A' }}</td>
                    </tr>
                    <tr>
                        <td class="label">Contact Code:</td>
                        <td>{{ $invoiceReturn->contact_code ?? 'N/A' }}</td>
                    </tr>
                    <tr>
                        <td class="label">Branch Code:</td>
                        <td>{{ $invoiceReturn->branch_code ?? 'N/A' }}</td>
                    </tr>
                    <tr>
                        <td class="label">Return Date:</td>
                        <td>{{ $invoiceReturn->return_date ? \Carbon\Carbon::parse($invoiceReturn->return_date)->format('d/m/Y') : 'N/A' }}</td>
                    </tr>
                    <tr>
                        <td class="label">Status:</td>
                        <td>
                            <span class="status-badge status-{{ strtolower($invoiceReturn->status ?? 'secondary') }}">
                                {{ $invoiceReturn->status ? ucfirst($invoiceReturn->status) : 'N/A' }}
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td class="label">Return Reason:</td>
                        <td>{{ $invoiceReturn->return_reason ?? 'N/A' }}</td>
                    </tr>
                </table>
            </div>

            <div class="financial-info">
                <h3 class="section-title">Financial Summary</h3>
                <table class="financial-table">
                    <tr>
                        <td>Total Amount:</td>
                        <td class="amount">Rs. {{ number_format($invoiceReturn->total_amount ?? 0, 2) }}</td>
                    </tr>
                    <tr>
                        <td>Refund Amount:</td>
                        <td class="amount">Rs. {{ number_format($invoiceReturn->refund_amount ?? 0, 2) }}</td>
                    </tr>
                    <tr class="balance-row">
                        <td>Balance:</td>
                        <td class="amount">Rs. {{ number_format(($invoiceReturn->total_amount ?? 0) - ($invoiceReturn->refund_amount ?? 0), 2) }}</td>
                    </tr>
                </table>
            </div>
        </div>

        <!-- Return Items -->
        @if($invoiceReturn->items && $invoiceReturn->items->count() > 0)
        <div class="return-items">
            <h3 class="section-title">Return Items</h3>
            <table class="items-table">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>SKU</th>
                        <th>Return Qty</th>
                        <th>Unit Price</th>
                        <th>Total</th>
                        <th>Reason</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($invoiceReturn->items as $item)
                    <tr>
                        <td>{{ $item->product ? $item->product->name : ($item->local_product_code ?? 'N/A') }}</td>
                        <td>{{ $item->product ? $item->product->shop_product_sku : ($item->local_product_code ?? 'N/A') }}</td>
                        <td>{{ $item->return_quantity ?? $item->quantity ?? 0 }}</td>
                        <td>Rs. {{ number_format($item->unit_price ?? 0, 2) }}</td>
                        <td>Rs. {{ number_format($item->total_price ?? 0, 2) }}</td>
                        <td>{{ $item->return_reason ?? 'N/A' }}</td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        @endif

        <!-- Footer -->
        <div style="text-align: center; margin: 20px 0; padding: 15px; background-color: #f8f9fa; border-radius: 4px;">
            <p style="margin: 0; color: #333; font-size: 12px; font-weight: bold;">Invoice return processed successfully.</p>
        </div>
        @include('components.pdf.footer')
    </div>
</body>
</html>
