@extends('layouts.app')

@section('title', 'Forms')
@section('heading', 'Forms')

@section('content')
@include('documents.top-submenu')
@include('settings.partials.back-to-home')
    <x-card>
        <div class="mb-4 flex justify-between items-center">
            <p class="text-slate-600">Custom forms and their status.</p>
            <div class="flex flex-wrap gap-2">
                <a href="{{ route('forms.portal') }}" class="inline-flex items-center rounded-lg border border-slate-300 bg-white px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Employee portal</a>
                <a href="{{ route('form-submissions.index') }}" class="inline-flex items-center rounded-lg border border-slate-300 bg-white px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Submissions</a>
                <a href="{{ route('forms.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"><i class="bi bi-plus-lg me-1"></i>Add Form</a>
            </div>
        </div>
        <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">Fields</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">Actions</th>
                </tr>
            </x-slot>
            @forelse($forms as $f)
                <tr>
                    <td class="px-4 py-3 text-sm text-slate-900">{{ $f->name }}</td>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ count($f->fields ?? []) }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm">
                        <x-badge :status="$f->status">{{ $f->status }}</x-badge>
                        <form action="{{ route('forms.update-status', $f) }}" method="post" class="inline ml-2">
                            @csrf
                            <select name="status" class="text-xs rounded border border-slate-300 py-0.5 js-select2" onchange="this.form.submit()" style="width: 100px;">
                                <option value="draft" {{ $f->status === 'draft' ? 'selected' : '' }}>Draft</option>
                                <option value="published" {{ $f->status === 'published' ? 'selected' : '' }}>Published</option>
                                <option value="archived" {{ $f->status === 'archived' ? 'selected' : '' }}>Archived</option>
                            </select>
                        </form>
                    </td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm space-x-2">
                        <a href="{{ route('forms.edit', $f) }}" class="btn-icon-action btn-icon-action--edit" title="Edit" aria-label="Edit"><i class="bi bi-pencil-square"></i></a>
                        @if($f->status === 'published')
                            <a href="{{ route('forms.fill', $f) }}" class="font-medium text-slate-600 hover:underline">Fill</a>
                        @endif
                        <form action="{{ route('forms.destroy', $f) }}" method="post" class="inline" onsubmit="return confirm('Delete this form and all submissions?');">
                            @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>
                    </td>
                </tr>
            @empty
                <tr><td colspan="4" class="px-4 py-8 text-center text-sm text-slate-500">No forms yet.</td></tr>
            @endforelse
        </x-table>
        @if($forms->hasPages())<div class="mt-4">{{ $forms->links() }}</div>@endif
    </x-card>
@endsection
