@extends('layouts.app')
@section('title', 'Processing')
@section('breadcrumb')<span class="text-white">Processing</span>@endsection

@section('content')
<div class="space-y-5">
    <div class="flex items-center justify-between">
        <div><h1>Processing Orders</h1><p>Track cabinet production and factory orders</p></div>
    </div>

    <div class="card overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead>
                    <tr class="text-xs text-slate-500 border-b border-slate-700" style="background: #0f172a;">
                        <th class="text-left px-4 py-3 font-medium">Job</th>
                        <th class="text-left px-4 py-3 font-medium">Client</th>
                        <th class="text-left px-4 py-3 font-medium">Stage</th>
                        <th class="text-left px-4 py-3 font-medium">Orders</th>
                        <th class="text-left px-4 py-3 font-medium">Production Progress</th>
                        <th class="px-4 py-3"></th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($jobs as $job)
                    <tr class="table-row" onclick="window.location='{{ route('processing.show', $job) }}'">
                        <td class="px-4 py-3">
                            <div class="text-sm font-medium text-white">{{ $job->job_number }}</div>
                            <div class="text-xs text-slate-500">{{ $job->project_type ? ucfirst($job->project_type) : '—' }}</div>
                        </td>
                        <td class="px-4 py-3">
                            <div class="flex items-center gap-2">
                                <div class="w-7 h-7 rounded-full bg-blue-900 flex items-center justify-center text-xs font-bold text-blue-300">{{ $job->contact?->initials ?? '??' }}</div>
                                <div>
                                    <div class="text-sm text-white">{{ $job->contact?->full_name ?? '—' }}</div>
                                    <div class="text-xs text-slate-500">{{ $job->site_suburb }}</div>
                                </div>
                            </div>
                        </td>
                        <td class="px-4 py-3"><span class="badge badge-{{ $job->stageColor() }}">{{ $job->stageLabel() }}</span></td>
                        <td class="px-4 py-3">
                            @php $orderCount = $job->processingOrders?->count() ?? 0; @endphp
                            <span class="text-sm text-white">{{ $orderCount }} order{{ $orderCount !== 1 ? 's' : '' }}</span>
                        </td>
                        <td class="px-4 py-3" style="min-width: 160px;">
                            @php $avgProgress = $job->processingOrders?->avg('progress_pct') ?? 0; @endphp
                            <div class="progress-bar w-32"><div class="progress-fill" style="width: {{ $avgProgress }}%"></div></div>
                            <div class="text-xs text-slate-500 mt-1">{{ round($avgProgress) }}% complete</div>
                        </td>
                        <td class="px-4 py-3" onclick="event.stopPropagation()">
                            <a href="{{ route('processing.show', $job) }}" class="w-7 h-7 rounded flex items-center justify-center hover:bg-slate-700 text-slate-400 hover:text-white">
                                <i class="fas fa-eye text-xs"></i>
                            </a>
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="6" class="px-4 py-12 text-center text-slate-500">
                        <i class="fas fa-industry text-3xl mb-3 text-slate-700 block"></i>No jobs in processing.
                    </td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($jobs->hasPages())
        <div class="px-4 py-3 border-t border-slate-700">{{ $jobs->links() }}</div>
        @endif
    </div>
</div>
@endsection
