@extends('layouts.app')

@section('title', 'Marketing Requests')

@section('content')
<div class="space-y-6">

    <!-- Header & Action -->
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 pb-4 border-b border-slate-200">
        <div>
            <h1 class="text-2xl font-bold text-slate-900 tracking-tight">Marketing Requests</h1>
            <p class="text-xs text-slate-500 mt-1">Browse, filter, and search all Harcourts marketing request submissions.</p>
        </div>
        @if(auth()->user()->isHarcourtsAdmin() || auth()->user()->isSuperAdmin())
            <a href="{{ route('requests.create') }}" class="px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-white font-bold text-xs uppercase tracking-wider rounded-md shadow transition-colors">
                + New Request
            </a>
        @endif
    </div>

    <!-- Filters & Search Form -->
    <div class="bg-white p-4 rounded-lg border border-slate-200 shadow-sm">
        <form method="GET" action="{{ route('requests.index') }}" class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-5 gap-3">
            <!-- Search -->
            <div class="md:col-span-2">
                <label class="block text-[10px] font-bold text-slate-500 uppercase mb-1">Search Keyword</label>
                <input type="text" name="search" value="{{ request('search') }}" placeholder="Search request number, address, URL, agent..."
                    class="w-full px-3 py-1.5 text-xs border border-slate-300 rounded focus:ring-1 focus:ring-harcourts-blue">
            </div>

            <!-- Status Filter -->
            <div>
                <label class="block text-[10px] font-bold text-slate-500 uppercase mb-1">Status</label>
                <select name="status" class="w-full px-3 py-1.5 text-xs border border-slate-300 rounded">
                    <option value="">All Statuses</option>
                    @foreach(['NEW', 'IN_PROGRESS', 'AWAITING_ADMIN_REVIEW', 'CHANGES_REQUESTED', 'IN_REVISION', 'APPROVED', 'COMPLETED', 'CANCELLED'] as $st)
                        <option value="{{ $st }}" {{ request('status') === $st ? 'selected' : '' }}>
                            {{ str_replace('_', ' ', $st) }}
                        </option>
                    @endforeach
                </select>
            </div>

            <!-- Request Type Filter -->
            <div>
                <label class="block text-[10px] font-bold text-slate-500 uppercase mb-1">Request Type</label>
                <select name="request_type_id" class="w-full px-3 py-1.5 text-xs border border-slate-300 rounded">
                    <option value="">All Request Types</option>
                    @foreach($requestTypes as $type)
                        <option value="{{ $type->id }}" {{ request('request_type_id') == $type->id ? 'selected' : '' }}>
                            {{ $type->name }}
                        </option>
                    @endforeach
                </select>
            </div>

            <!-- Submit Filter Button -->
            <div class="flex items-end space-x-2">
                <button type="submit" class="w-full py-1.5 bg-harcourts-navy hover:bg-blue-900 text-white font-bold text-xs rounded transition-colors">
                    Filter
                </button>
                <a href="{{ route('requests.index') }}" class="px-3 py-1.5 bg-slate-100 hover:bg-slate-200 text-slate-600 text-xs rounded font-medium">
                    Reset
                </a>
            </div>
        </form>
    </div>

    <!-- Requests Table -->
    <div class="bg-white rounded-lg border border-slate-200 shadow-sm overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left text-xs text-slate-700">
                <thead class="bg-slate-50 text-slate-500 uppercase tracking-wider font-semibold border-b border-slate-200">
                    <tr>
                        <th class="px-4 py-3">Request ID</th>
                        <th class="px-4 py-3">Type</th>
                        <th class="px-4 py-3">Property / Address</th>
                        <th class="px-4 py-3">Agent</th>
                        <th class="px-4 py-3">Submitted</th>
                        <th class="px-4 py-3">Required By</th>
                        <th class="px-4 py-3">Priority</th>
                        <th class="px-4 py-3">Status</th>
                        <th class="px-4 py-3">Revisions</th>
                        <th class="px-4 py-3">Designer</th>
                        <th class="px-4 py-3 text-right">Action</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-200">
                    @forelse($requests as $req)
                        <tr class="hover:bg-slate-50 transition-colors">
                            <td class="px-4 py-3.5 font-bold text-harcourts-navy">
                                <a href="{{ route('requests.show', $req->id) }}" class="hover:underline">{{ $req->request_number }}</a>
                            </td>
                            <td class="px-4 py-3.5 font-semibold text-slate-800">{{ $req->requestType->name }}</td>
                            <td class="px-4 py-3.5 max-w-xs">
                                <div class="font-medium text-slate-900 truncate">{{ $req->property_address ?? 'N/A' }}</div>
                                @if($req->property_url)
                                    <a href="{{ $req->property_url }}" target="_blank" class="text-[11px] text-harcourts-blue hover:underline block truncate">
                                        Listing Link &nearr;
                                    </a>
                                @endif
                            </td>
                            <td class="px-4 py-3.5">{{ $req->agent ? $req->agent->name : 'N/A' }}</td>
                            <td class="px-4 py-3.5 text-slate-500 whitespace-nowrap">{{ $req->submitted_at ? $req->submitted_at->format('d M, g:i A') : '' }}</td>
                            <td class="px-4 py-3.5 text-slate-500 whitespace-nowrap">{{ $req->required_at ? $req->required_at->format('d M, g:i A') : '' }}</td>
                            <td class="px-4 py-3.5 whitespace-nowrap">
                                <x-priority-badge :priority="$req->calculated_priority" :isOverdue="$req->is_overdue" />
                            </td>
                            <td class="px-4 py-3.5 whitespace-nowrap">
                                <x-status-badge :status="$req->status" />
                            </td>
                            <td class="px-4 py-3.5 text-center font-bold">
                                {{ $req->revisions_count }}
                            </td>
                            <td class="px-4 py-3.5 whitespace-nowrap">{{ $req->designer ? $req->designer->name : 'Unassigned' }}</td>
                            <td class="px-4 py-3.5 text-right">
                                <a href="{{ route('requests.show', $req->id) }}" class="px-2.5 py-1 bg-slate-100 hover:bg-slate-200 text-slate-700 font-semibold rounded text-xs">
                                    View
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="11" class="px-6 py-8 text-center text-slate-400">
                                No marketing requests found.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="px-6 py-3 border-t border-slate-200">
            {{ $requests->links() }}
        </div>
    </div>

</div>
@endsection
