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

@section('content')
<div class="space-y-5">
    <div class="flex items-center justify-between">
        <div><h1>Check Measure</h1><p>Schedule and track on-site measurement appointments</p></div>
        <a href="{{ route('check_measure.create') }}" class="btn btn-primary"><i class="fas fa-plus mr-1"></i> Book Check Measure</a>
    </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">Measurer</th>
                        <th class="text-left px-4 py-3 font-medium">Scheduled Date</th>
                        <th class="text-left px-4 py-3 font-medium">Status</th>
                        <th class="text-left px-4 py-3 font-medium">Checklist</th>
                        <th class="px-4 py-3"></th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($checkMeasures as $cm)
                    <tr class="table-row" onclick="window.location='{{ route('check_measure.show', $cm) }}'">
                        <td class="px-4 py-3">
                            <div class="text-sm font-medium text-white">{{ $cm->job?->job_number ?? '—' }}</div>
                            <div class="text-xs text-slate-500">{{ $cm->job?->project_type ? ucfirst($cm->job->project_type) : '' }}</div>
                        </td>
                        <td class="px-4 py-3 text-sm text-white">{{ $cm->job?->contact?->full_name ?? '—' }}</td>
                        <td class="px-4 py-3 text-sm text-slate-300">{{ $cm->measurer?->name ?? '—' }}</td>
                        <td class="px-4 py-3">
                            <div class="text-sm text-white">{{ $cm->scheduled_date?->format('d M Y') ?? '—' }}</div>
                            @if($cm->arrival_time)
                            <div class="text-xs text-slate-500">{{ \Carbon\Carbon::parse($cm->arrival_time)->format('g:ia') }}</div>
                            @endif
                        </td>
                        <td class="px-4 py-3">
                            @php $sc = match($cm->status) { 'scheduled' => 'blue', 'in_progress' => 'yellow', 'completed' => 'green', 'cancelled' => 'red', default => 'gray' }; @endphp
                            <span class="badge badge-{{ $sc }}">{{ ucfirst($cm->status) }}</span>
                        </td>
                        <td class="px-4 py-3">
                            @php $total = $cm->checklistItems->count(); $done = $cm->checklistItems->where('checked', true)->count(); @endphp
                            @if($total > 0)
                            <div class="progress-bar w-24"><div class="progress-fill" style="width: {{ ($done / $total) * 100 }}%"></div></div>
                            <div class="text-xs text-slate-500 mt-1">{{ $done }}/{{ $total }}</div>
                            @else
                            <span class="text-xs text-slate-500">—</span>
                            @endif
                        </td>
                        <td class="px-4 py-3" onclick="event.stopPropagation()">
                            <a href="{{ route('check_measure.show', $cm) }}" 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="7" class="px-4 py-12 text-center text-slate-500">
                        <i class="fas fa-ruler-combined text-3xl mb-3 text-slate-700 block"></i>No check measures scheduled.
                    </td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($checkMeasures->hasPages())
        <div class="px-4 py-3 border-t border-slate-700">{{ $checkMeasures->links() }}</div>
        @endif
    </div>
</div>
@endsection
