@extends('layouts.app')

@section('title', 'Harcourts Admin Dashboard')

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

    <!-- Header & Action Button -->
    <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">Harcourts Admin Dashboard</h1>
            <p class="text-xs text-slate-500 mt-1">Manage property marketing requests, review designer proofs, and track campaign approvals.</p>
        </div>
        <div>
            <a href="{{ route('requests.create') }}" class="inline-flex items-center px-4 py-2.5 bg-emerald-600 hover:bg-emerald-700 text-white font-bold text-xs uppercase tracking-wider rounded-md shadow focus:outline-none focus:ring-2 focus:ring-emerald-500 transition-colors">
                <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>
                New Marketing Request
            </a>
        </div>
    </div>

    <!-- Summary Cards -->
    <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4">

        <div class="bg-white p-4 rounded-lg border border-slate-200 shadow-sm">
            <div class="text-[11px] font-bold text-slate-500 uppercase tracking-wider">Awaiting My Review</div>
            <div class="text-2xl font-black text-purple-700 mt-1">{{ $counts['awaiting_review'] }}</div>
        </div>

        <div class="bg-white p-4 rounded-lg border border-slate-200 shadow-sm">
            <div class="text-[11px] font-bold text-slate-500 uppercase tracking-wider">Changes Requested</div>
            <div class="text-2xl font-black text-rose-600 mt-1">{{ $counts['changes_requested'] }}</div>
        </div>

        <div class="bg-white p-4 rounded-lg border border-slate-200 shadow-sm">
            <div class="text-[11px] font-bold text-slate-500 uppercase tracking-wider">My Open Jobs</div>
            <div class="text-2xl font-black text-blue-700 mt-1">{{ $counts['my_open'] }}</div>
        </div>

        <div class="bg-white p-4 rounded-lg border border-slate-200 shadow-sm">
            <div class="text-[11px] font-bold text-slate-500 uppercase tracking-wider">Approved</div>
            <div class="text-2xl font-black text-emerald-600 mt-1">{{ $counts['approved'] }}</div>
        </div>

        <div class="bg-white p-4 rounded-lg border border-slate-200 shadow-sm">
            <div class="text-[11px] font-bold text-slate-500 uppercase tracking-wider">Completed</div>
            <div class="text-2xl font-black text-slate-700 mt-1">{{ $counts['completed'] }}</div>
        </div>

        <div class="bg-white p-4 rounded-lg border border-slate-200 shadow-sm">
            <div class="text-[11px] font-bold text-slate-500 uppercase tracking-wider">Overdue</div>
            <div class="text-2xl font-black text-red-600 mt-1">{{ $counts['overdue'] }}</div>
        </div>

    </div>

    <!-- Prominent Section: Requests Requiring My Review -->
    @if($awaitingReviewRequests->count() > 0)
        <div class="bg-purple-50 border-2 border-purple-300 rounded-lg p-5 shadow-sm">
            <div class="flex items-center justify-between mb-3">
                <div class="flex items-center space-x-2">
                    <span class="relative flex h-3 w-3">
                      <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-purple-400 opacity-75"></span>
                      <span class="relative inline-flex rounded-full h-3 w-3 bg-purple-600"></span>
                    </span>
                    <h2 class="text-base font-bold text-purple-900 uppercase tracking-wide">Proofs Awaiting Your Review</h2>
                </div>
                <span class="text-xs font-semibold text-purple-700 bg-purple-200 px-2 py-0.5 rounded-full">{{ $awaitingReviewRequests->count() }} Ready</span>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                @foreach($awaitingReviewRequests as $req)
                    <div class="bg-white rounded-md border border-purple-200 p-4 shadow-sm flex flex-col justify-between">
                        <div>
                            <div class="flex items-center justify-between text-xs mb-2">
                                <span class="font-black text-harcourts-navy">{{ $req->request_number }}</span>
                                <x-priority-badge :priority="$req->calculated_priority" :isOverdue="$req->is_overdue" />
                            </div>
                            <h3 class="font-bold text-sm text-slate-900 line-clamp-1">{{ $req->requestType->name }}</h3>
                            <p class="text-xs text-slate-600 mt-1 line-clamp-1">
                                {{ $req->property_address ?? $req->property_url }}
                            </p>
                            <div class="text-[11px] text-slate-500 mt-2 flex items-center justify-between">
                                <span>Agent: <strong>{{ $req->agent ? $req->agent->name : 'N/A' }}</strong></span>
                                <span>Version <strong>#{{ $req->latestVersion ? $req->latestVersion->version_number : 1 }}</strong></span>
                            </div>
                        </div>

                        <div class="mt-4 pt-3 border-t border-slate-100 flex items-center justify-between">
                            <span class="text-[10px] text-purple-800 font-medium">Proof uploaded {{ $req->latest_proof_at ? $req->latest_proof_at->diffForHumans() : '' }}</span>
                            <a href="{{ route('requests.show', $req->id) }}" class="px-3 py-1.5 bg-purple-700 hover:bg-purple-800 text-white font-bold text-xs rounded transition-colors">
                                Review Creative &rarr;
                            </a>
                        </div>
                    </div>
                @endforeach
            </div>
        </div>
    @endif

    <!-- All My Requests Table -->
    <div class="bg-white rounded-lg border border-slate-200 shadow-sm overflow-hidden">
        <div class="px-6 py-4 border-b border-slate-200 flex items-center justify-between">
            <h2 class="text-base font-bold text-slate-900">My Submitted Requests</h2>
            <a href="{{ route('requests.index') }}" class="text-xs text-harcourts-blue font-semibold hover:underline">View All &rarr;</a>
        </div>

        <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-6 py-3">Request ID</th>
                        <th class="px-6 py-3">Type</th>
                        <th class="px-6 py-3">Property / Details</th>
                        <th class="px-6 py-3">Agent</th>
                        <th class="px-6 py-3">Submitted</th>
                        <th class="px-6 py-3">Required By</th>
                        <th class="px-6 py-3">Priority</th>
                        <th class="px-6 py-3">Status</th>
                        <th class="px-6 py-3">Revisions</th>
                        <th class="px-6 py-3 text-right">Action</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-200">
                    @forelse($myRequests as $req)
                        <tr class="hover:bg-slate-50 transition-colors">
                            <td class="px-6 py-4 font-bold text-harcourts-navy">
                                <a href="{{ route('requests.show', $req->id) }}" class="hover:underline">{{ $req->request_number }}</a>
                            </td>
                            <td class="px-6 py-4 font-semibold text-slate-800">{{ $req->requestType->name }}</td>
                            <td class="px-6 py-4">
                                <div class="font-medium text-slate-900 max-w-xs 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 truncate block">
                                        Listing Link &nearr;
                                    </a>
                                @endif
                            </td>
                            <td class="px-6 py-4">{{ $req->agent ? $req->agent->name : 'N/A' }}</td>
                            <td class="px-6 py-4 text-slate-500 whitespace-nowrap">{{ $req->submitted_at ? $req->submitted_at->format('d M Y, g:i A') : '' }}</td>
                            <td class="px-6 py-4 text-slate-500 whitespace-nowrap">{{ $req->required_at ? $req->required_at->format('d M Y, g:i A') : '' }}</td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <x-priority-badge :priority="$req->calculated_priority" :isOverdue="$req->is_overdue" />
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <x-status-badge :status="$req->status" />
                            </td>
                            <td class="px-6 py-4 text-center font-bold text-slate-700">
                                {{ $req->revisions_count }}
                            </td>
                            <td class="px-6 py-4 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 transition-colors">
                                    View
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="10" class="px-6 py-8 text-center text-slate-400">
                                You have not submitted any marketing requests yet.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="px-6 py-3 border-t border-slate-200">
            {{ $myRequests->links() }}
        </div>
    </div>

</div>
@endsection
