@props([
    'header' => [],
    'receipts' => [],
    'returns' => [],
    'payments' => [],
    'payables' => [],
    'exceptions' => [],
])

@php
    $documentTableColumns = [
        'code' => __('dberp.fields.document_code'),
        'business_time' => __('dberp.fields.business_time'),
        'status' => __('dberp.fields.status'),
        'quantity' => __('dberp.fields.quantity'),
        'amount' => __('dberp.fields.amount'),
    ];

    $relatedSections = [
        [
            'title' => __('dberp.sections.purchase_order_related_receipts'),
            'rows' => $receipts,
            'empty' => __('dberp.messages.purchase_order_overview_no_related_receipts'),
        ],
        [
            'title' => __('dberp.sections.purchase_order_related_returns'),
            'rows' => $returns,
            'empty' => __('dberp.messages.purchase_order_overview_no_related_returns'),
        ],
        [
            'title' => __('dberp.sections.purchase_order_related_payments'),
            'rows' => $payments,
            'empty' => __('dberp.messages.purchase_order_overview_no_related_payments'),
        ],
        [
            'title' => __('dberp.sections.purchase_order_related_payables'),
            'rows' => $payables,
            'empty' => __('dberp.messages.purchase_order_overview_no_related_payables'),
        ],
        [
            'title' => __('dberp.sections.purchase_order_related_exceptions'),
            'rows' => $exceptions,
            'empty' => __('dberp.messages.purchase_order_overview_no_related_exceptions'),
        ],
    ];
@endphp

<div class="space-y-6">
    @if (count($header) > 0)
        <dl class="grid grid-cols-2 gap-x-4 gap-y-3 sm:grid-cols-4">
            @foreach ($header as $item)
                <div class="min-w-0">
                    <dt class="text-xs font-medium text-gray-500 dark:text-gray-400">
                        {{ $item['label'] }}
                    </dt>
                    <dd class="mt-0.5 text-sm font-semibold text-gray-950 dark:text-white break-words">
                        {{ $item['value'] }}
                    </dd>
                </div>
            @endforeach
        </dl>
    @endif

    <div class="grid grid-cols-1 gap-4 xl:grid-cols-2">
        @foreach ($relatedSections as $section)
            <div class="rounded-lg border border-gray-200 dark:border-white/10 overflow-hidden">
                <div class="border-b border-gray-200 bg-gray-50 px-3 py-2 dark:border-white/10 dark:bg-white/5">
                    <h4 class="text-xs font-semibold text-gray-700 dark:text-gray-200">
                        {{ $section['title'] }}
                    </h4>
                </div>

                @if (count($section['rows']) === 0)
                    <p class="px-3 py-4 text-xs text-gray-500 dark:text-gray-400">
                        {{ $section['empty'] }}
                    </p>
                @else
                    <div class="overflow-x-auto">
                        <table class="w-full min-w-full divide-y divide-gray-200 text-xs dark:divide-white/10">
                            <thead class="bg-white dark:bg-gray-900">
                                <tr>
                                    @foreach ($documentTableColumns as $columnLabel)
                                        <th scope="col" class="px-2 py-2 text-left font-medium text-gray-500 dark:text-gray-400 whitespace-nowrap">
                                            {{ $columnLabel }}
                                        </th>
                                    @endforeach
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-100 bg-white dark:divide-white/5 dark:bg-gray-900">
                                @foreach ($section['rows'] as $row)
                                    <tr>
                                        <td class="px-2 py-2 whitespace-nowrap font-medium text-gray-950 dark:text-white">
                                            @if (filled($row['url'] ?? null))
                                                <a href="{{ $row['url'] }}" class="text-primary-600 hover:underline dark:text-primary-400">
                                                    {{ $row['code'] }}
                                                </a>
                                            @else
                                                {{ $row['code'] }}
                                            @endif
                                        </td>
                                        <td class="px-2 py-2 whitespace-nowrap text-gray-700 dark:text-gray-300">
                                            {{ $row['business_time'] }}
                                        </td>
                                        <td class="px-2 py-2 whitespace-nowrap text-gray-700 dark:text-gray-300">
                                            {{ $row['status'] }}
                                        </td>
                                        <td class="px-2 py-2 whitespace-nowrap text-gray-700 dark:text-gray-300">
                                            {{ $row['quantity'] }}
                                        </td>
                                        <td class="px-2 py-2 whitespace-nowrap text-gray-700 dark:text-gray-300">
                                            {{ $row['amount'] }}
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @endif
            </div>
        @endforeach
    </div>
</div>
