@extends('layouts.app')
@section('title', 'Staff Timesheets')
@section('breadcrumb')<a href="{{ route('timesheet.index') }}" class="hover:text-white">Timesheet</a> <i class="fas fa-chevron-right text-xs mx-1 text-slate-600"></i><span class="text-white">All Staff</span>@endsection

@section('content')
<div class="flex items-center justify-between mb-6">
    <div>
        <h1 class="mb-1">Staff Timesheets</h1>
        <p>View all time entries across factory employees, processors and trades</p>
    </div>
    <a href="{{ route('staff-invoices.index') }}" class="btn btn-primary">
        <i class="fas fa-file-invoice-dollar"></i> Manage Invoices
    </a>
</div>

<!-- Staff with hourly rates -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
    @foreach($users as $u)
    @php
        $weekHours = $u->timeEntries()->where('status','completed')->where('started_at','>=',now()->startOfWeek())->sum('duration_minutes') / 60;
        $weekEarnings = $weekHours * $u->hourly_rate;
        $running = $u->timeEntries()->where('status','running')->first();
    @endphp
    <div class="stat-card">
        <div class="flex items-center gap-3 mb-3">
            <div class="w-9 h-9 rounded-full bg-blue-900 flex items-center justify-center text-blue-300 font-bold text-sm">{{ $u->initials }}</div>
            <div>
                <div class="text-sm font-semibold text-white">{{ $u->name }}</div>
                <div class="text-xs text-slate-500">{{ $u->role?->display_name }}</div>
            </div>
            @if($running)
            <div class="ml-auto w-2 h-2 rounded-full bg-green-400 animate-pulse"></div>
            @endif
        </div>
        <div class="grid grid-cols-2 gap-2 text-xs">
            <div><span class="text-slate-400">Rate:</span> <span class="text-blue-300">${{ number_format($u->hourly_rate, 2) }}/hr</span></div>
            <div><span class="text-slate-400">This week:</span> <span class="text-white">{{ number_format($weekHours,1) }}h</span></div>
            <div><span class="text-slate-400">Earnings:</span> <span class="text-green-400">${{ number_format($weekEarnings,2) }}</span></div>
            <div><span class="text-slate-400">Status:</span> <span class="{{ $running ? 'text-green-400' : 'text-slate-500' }}">{{ $running ? 'Working' : 'Idle' }}</span></div>
        </div>
    </div>
    @endforeach
</div>

<!-- All Entries -->
<div class="card overflow-hidden">
    <div class="p-5 border-b border-slate-700">
        <h3>All Time Entries</h3>
    </div>
    <div class="overflow-x-auto">
        <table class="w-full">
            <thead><tr class="text-xs text-slate-400 uppercase border-b border-slate-700">
                <th class="px-5 py-3 text-left">Staff Member</th>
                <th class="px-5 py-3 text-left">Date</th>
                <th class="px-5 py-3 text-left">Task</th>
                <th class="px-5 py-3 text-left">Job</th>
                <th class="px-5 py-3 text-left">Duration</th>
                <th class="px-5 py-3 text-left">Amount</th>
                <th class="px-5 py-3 text-left">Status</th>
                <th class="px-5 py-3 text-left">Invoice</th>
            </tr></thead>
            <tbody>
                @forelse($entries as $entry)
                <tr class="table-row">
                    <td class="px-5 py-3">
                        <div class="text-sm font-medium text-white">{{ $entry->user->name }}</div>
                        <div class="text-xs text-slate-400">{{ $entry->user->role?->display_name }}</div>
                    </td>
                    <td class="px-5 py-3 text-sm text-slate-300">{{ $entry->started_at->format('d M Y H:i') }}</td>
                    <td class="px-5 py-3 text-sm text-white">{{ $entry->taskType?->name ?? '—' }}</td>
                    <td class="px-5 py-3 text-sm text-slate-300">{{ $entry->job?->job_number ?? '—' }}</td>
                    <td class="px-5 py-3 text-sm font-mono text-blue-300">{{ $entry->elapsed }}</td>
                    <td class="px-5 py-3 text-sm text-green-400">
                        @if($entry->user->hourly_rate && $entry->duration_hours > 0)
                        ${{ number_format($entry->duration_hours * $entry->user->hourly_rate, 2) }}
                        @else —
                        @endif
                    </td>
                    <td class="px-5 py-3">
                        <span class="badge {{ $entry->status === 'running' ? 'badge-green' : ($entry->invoiced_in ? 'badge-purple' : 'badge-blue') }}">
                            {{ $entry->status === 'running' ? 'Running' : ($entry->invoiced_in ? 'Invoiced' : ucfirst($entry->status)) }}
                        </span>
                    </td>
                    <td class="px-5 py-3 text-sm text-slate-400">
                        @if($entry->invoiced_in)
                        <a href="{{ route('staff-invoices.show', $entry->invoiced_in) }}" class="text-purple-400 hover:underline">SI-{{ str_pad($entry->invoiced_in, 5, '0', STR_PAD_LEFT) }}</a>
                        @else —
                        @endif
                    </td>
                </tr>
                @empty
                <tr><td colspan="8" class="px-5 py-8 text-center text-slate-500">No time entries found.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
    @if($entries->hasPages())
    <div class="p-4 border-t border-slate-700">{{ $entries->links() }}</div>
    @endif
</div>
@endsection
