{{-- Documents Card - Card with documents table for customers --}}
{{-- Props: $customer, $documents, $canEdit = true --}}
@php
    $canEdit = $canEdit ?? true;
    $documentTypes = [
        'contract' => __('Contract'),
        'license' => __('License'),
        'certificate' => __('Certificate'),
        'insurance' => __('Insurance'),
        'tax_document' => __('Tax Document'),
        'agreement' => __('Agreement'),
        'invoice' => __('Invoice'),
        'other' => __('Other'),
    ];

    // Helper function to check expiry status
    $getExpiryClass = function($expiryDate) {
        if (!$expiryDate) return null;
        $now = now();
        $expiry = \Carbon\Carbon::parse($expiryDate);

        if ($expiry->isPast()) {
            return 'danger'; // Expired
        } elseif ($expiry->diffInDays($now) <= 30) {
            return 'warning'; // Expiring soon (within 30 days)
        }
        return null;
    };
@endphp

<div class="card mb-4">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="mb-0">
            <i class="bx bx-file me-1"></i>{{ __('Documents') }}
            <span class="badge bg-label-primary ms-2">{{ $documents->count() }}</span>
        </h5>
        @if($canEdit)
        <button type="button" class="btn btn-sm btn-primary" onclick="openDocumentOffcanvas()">
            <i class="bx bx-plus me-1"></i>{{ __('Add') }}
        </button>
        @endif
    </div>
    <div class="card-body">
        @if($documents->count() > 0)
            <div class="table-responsive">
                <table class="table table-sm table-hover">
                    <thead>
                        <tr>
                            <th>{{ __('Title') }}</th>
                            <th>{{ __('Number') }}</th>
                            <th>{{ __('Type') }}</th>
                            <th>{{ __('Issue Date') }}</th>
                            <th>{{ __('Expiry Date') }}</th>
                            <th class="text-center" style="width: 100px;">{{ __('Actions') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($documents as $document)
                        @php
                            $expiryClass = $getExpiryClass($document->expiry_date);
                        @endphp
                        <tr>
                            <td>
                                <div class="d-flex align-items-center">
                                    <div class="avatar avatar-sm me-2">
                                        <span class="avatar-initial rounded bg-label-secondary">
                                            <i class="bx bx-file"></i>
                                        </span>
                                    </div>
                                    <div>
                                        <span class="fw-medium">{{ $document->document_title }}</span>
                                        @if($document->file_name)
                                        <br><small class="text-muted">{{ $document->file_name }}</small>
                                        @endif
                                    </div>
                                </div>
                            </td>
                            <td>{{ $document->document_number ?? '-' }}</td>
                            <td>
                                <span class="badge bg-label-info">
                                    {{ $documentTypes[$document->document_type] ?? ucfirst(str_replace('_', ' ', $document->document_type)) }}
                                </span>
                            </td>
                            <td>
                                @if($document->issue_date)
                                {{ \Carbon\Carbon::parse($document->issue_date)->format('M d, Y') }}
                                @else
                                <span class="text-muted">-</span>
                                @endif
                            </td>
                            <td>
                                @if($document->expiry_date)
                                    @if($expiryClass === 'danger')
                                    <span class="badge bg-danger">
                                        <i class="bx bx-error-circle me-1"></i>{{ __('Expired') }}
                                    </span>
                                    <br><small class="text-muted">{{ \Carbon\Carbon::parse($document->expiry_date)->format('M d, Y') }}</small>
                                    @elseif($expiryClass === 'warning')
                                    <span class="badge bg-warning">
                                        <i class="bx bx-time me-1"></i>{{ __('Expiring Soon') }}
                                    </span>
                                    <br><small class="text-muted">{{ \Carbon\Carbon::parse($document->expiry_date)->format('M d, Y') }}</small>
                                    @else
                                    {{ \Carbon\Carbon::parse($document->expiry_date)->format('M d, Y') }}
                                    @endif
                                @else
                                <span class="text-muted">-</span>
                                @endif
                            </td>
                            <td class="text-center">
                                <div class="d-flex justify-content-center gap-1">
                                    @if($document->file_path)
                                    <a href="{{ route('systemcore.customers.documents.download', [$customer->id, $document->id]) }}"
                                       class="btn btn-sm btn-icon btn-text-primary rounded-pill"
                                       title="{{ __('Download') }}">
                                        <i class="bx bx-download"></i>
                                    </a>
                                    @endif
                                    @if($canEdit)
                                    <button type="button"
                                            class="btn btn-sm btn-icon btn-text-secondary rounded-pill"
                                            onclick="editDocument({{ $document->id }})"
                                            title="{{ __('Edit') }}">
                                        <i class="bx bx-edit"></i>
                                    </button>
                                    <button type="button"
                                            class="btn btn-sm btn-icon btn-text-danger rounded-pill"
                                            onclick="deleteDocument({{ $document->id }})"
                                            title="{{ __('Delete') }}">
                                        <i class="bx bx-trash"></i>
                                    </button>
                                    @endif
                                </div>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @else
            <div class="text-center py-5">
                <div class="mb-3">
                    <i class="bx bx-folder-open bx-lg text-muted"></i>
                </div>
                <p class="text-muted mb-3">{{ __('No documents found') }}</p>
                @if($canEdit)
                <button type="button" class="btn btn-primary" onclick="openDocumentOffcanvas()">
                    <i class="bx bx-plus me-1"></i>{{ __('Add First Document') }}
                </button>
                @endif
            </div>
        @endif
    </div>
</div>

{{-- Include the offcanvas form --}}
@include('systemcore::customers._document_offcanvas', [
    'customer' => $customer
])
