@extends('layouts.app')

@section('title', 'Documents')
@section('heading', 'Documents')

@section('content')
@include('documents.top-submenu')
    <x-card>
        <div class="mb-4 flex justify-between items-center">
            <p class="text-slate-600">Uploaded documents.</p>
            <a href="{{ route('documents.create') }}" class="inline-flex items-center rounded-lg bg-slate-800 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700">Upload Document</a>
        </div>
        <form method="GET" action="{{ route('documents.index') }}" class="mb-5 flex flex-nowrap items-end gap-2 overflow-x-auto rounded-lg border border-slate-200 bg-slate-50 p-3">
            <div class="min-w-[12rem] flex-1">
                <label class="mb-1 block text-xs font-medium text-slate-500">Search</label>
                <input type="text" name="search" value="{{ $filters['search'] ?? '' }}" placeholder="Name / number / uploader" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
            </div>
            <div class="min-w-[9rem]">
                <label class="mb-1 block text-xs font-medium text-slate-500">Category</label>
                <select name="category" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm js-select2">
                    <option value="">All Categories</option>
                    @foreach($categories as $key => $label)
                        <option value="{{ $key }}" @selected(($filters['category'] ?? '') === $key)>{{ $label }}</option>
                    @endforeach
                </select>
            </div>
            <div class="min-w-[8rem]">
                <label class="mb-1 block text-xs font-medium text-slate-500">Status</label>
                <select name="status" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm js-select2">
                    <option value="">All Status</option>
                    @foreach($statusOptions as $status)
                        <option value="{{ $status }}" @selected(($filters['status'] ?? '') === $status)>{{ ucwords(str_replace('_', ' ', $status)) }}</option>
                    @endforeach
                </select>
            </div>
            <div class="min-w-[8rem]">
                <label class="mb-1 block text-xs font-medium text-slate-500">Visibility</label>
                <select name="visibility" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm js-select2">
                    <option value="">All Visibility</option>
                    @foreach($visibilityOptions as $key => $label)
                        <option value="{{ $key }}" @selected(($filters['visibility'] ?? '') === $key)>{{ $label }}</option>
                    @endforeach
                </select>
            </div>
            <div class="min-w-[8.5rem]">
                <label class="mb-1 block text-xs font-medium text-slate-500">From</label>
                <input type="date" name="from" value="{{ $filters['from'] ?? '' }}" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
            </div>
            <div class="min-w-[8.5rem]">
                <label class="mb-1 block text-xs font-medium text-slate-500">To</label>
                <input type="date" name="to" value="{{ $filters['to'] ?? '' }}" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
            </div>
            <div class="flex shrink-0 gap-2 pb-px">
                <button type="submit" class="rounded-lg bg-slate-800 px-4 py-2 text-sm font-medium text-white whitespace-nowrap">Apply</button>
                <a href="{{ route('documents.index') }}" class="rounded-lg bg-slate-100 px-4 py-2 text-sm font-medium text-slate-700 whitespace-nowrap">Reset</a>
            </div>
        </form>
        <x-table>
            <x-slot name="head">
                <tr>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Name</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Category</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Status</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Issued</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Expires</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Visibility</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Uploaded By</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Date</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Actions</th>
                </tr>
            </x-slot>
            @forelse($documents as $doc)
                <tr>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-900">{{ $doc->name }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $categories[$doc->category] ?? $doc->category ?? $doc->type ?? '—' }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm">
                        @php($status = $doc->computed_status)
                        <span class="inline-flex rounded-full px-2 py-1 text-xs font-medium
                            {{ $status === 'expired' ? 'bg-rose-100 text-rose-700' : '' }}
                            {{ $status === 'expiring_soon' ? 'bg-amber-100 text-amber-700' : '' }}
                            {{ $status === 'archived' ? 'bg-slate-200 text-slate-700' : '' }}
                            {{ $status === 'active' ? 'bg-emerald-100 text-emerald-700' : '' }}">
                            {{ ucwords(str_replace('_', ' ', $status)) }}
                        </span>
                    </td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ optional($doc->issued_on)->format('M d, Y') ?: '—' }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ optional($doc->expires_on)->format('M d, Y') ?: '—' }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ ucfirst($doc->visibility ?? 'company') }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $doc->uploader?->name ?? '—' }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $doc->created_at?->format('M d, Y') }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">
                        <div class="flex items-center gap-2">
                            <a href="{{ route('documents.download', $doc) }}" class="btn-icon-action btn-icon-action--view" title="Download" aria-label="Download"><i class="bi bi-download"></i></a>
                            @if(auth()->user()?->isTenantManager() || auth()->user()?->hasPermission('documents_edit') || (int) $doc->uploaded_by === (int) auth()->id())
                                <form action="{{ route('documents.destroy', ['document' => $doc, 'redirect' => request()->fullUrl()]) }}" method="POST" onsubmit="return confirm('Delete this document?');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn-icon-action btn-icon-action--delete" title="Delete" aria-label="Delete"><i class="bi bi-trash3"></i></button>
                                </form>
                            @endif
                        </div>
                    </td>
                </tr>
            @empty
                <tr><td colspan="10" class="px-4 py-8 text-center text-sm text-slate-500">No documents found for the selected filters.</td></tr>
            @endforelse
        </x-table>
        @if($documents->hasPages())<div class="mt-4">{{ $documents->links() }}</div>@endif
    </x-card>
@endsection
