@extends('layouts.app')

@section('title', 'Shifts')
@section('heading', 'Shifts')

@section('content')
@include('settings.partials.back-to-home')
    <x-card>
        <div class="mb-4 flex justify-between items-center">
            <p class="text-slate-600">Manage shifts and work timings.</p>
            @if(auth()->user()->isTenantManager())
            <a href="{{ route('shifts.create') }}" class="inline-flex items-center rounded-lg bg-slate-800 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700"><i class="bi bi-plus-lg me-1"></i>Add Shift</a>
            @endif
        </div>
        <x-table>
            <x-slot name="head">
                <tr>
                    <th class="whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Name</th>
                    <th class="whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Clock In</th>
                    <th class="whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Clock Out</th>
                    <th class="whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Grace</th>
                    <th class="whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Night</th>
                    <th class="whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Status</th>
                    @if(auth()->user()->isTenantManager())
                    <th class="whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Assigned Employees (Auto Calculated)</th>
                    <th class="whitespace-nowrap px-4 py-3 text-right text-xs font-medium uppercase text-slate-500">Actions</th>
                    @endif
                </tr>
            </x-slot>
            @forelse($shifts as $shift)
                <tr>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-900">{{ $shift->name }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ \Carbon\Carbon::parse($shift->clock_in_time)->format('h:i A') }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ \Carbon\Carbon::parse($shift->clock_out_time)->format('h:i A') }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $shift->grace_period_minutes }} min</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $shift->is_night_shift ? 'Yes' : 'No' }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm">
                        @if($shift->is_active)
                            <span class="inline-flex rounded-full bg-emerald-50 px-2 py-0.5 text-xs font-medium text-emerald-700">Active</span>
                        @else
                            <span class="inline-flex rounded-full bg-slate-100 px-2 py-0.5 text-xs font-medium text-slate-600">Inactive</span>
                        @endif
                    </td>
                    @if(auth()->user()->isTenantManager())
                    <td class="px-4 py-3 text-sm text-slate-700">{{ $shift->users_count ?? 0 }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-right text-sm">
                        <a href="{{ route('shifts.edit', $shift) }}" class="btn-icon-action btn-icon-action--edit" title="Edit" aria-label="Edit"><i class="bi bi-pencil-square"></i></a>
                        <form action="{{ route('shifts.destroy', $shift) }}" method="POST" class="inline" onsubmit="return confirm('Delete this shift?');">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn-icon-action btn-icon-action--delete" title="Delete" aria-label="Delete"><i class="bi bi-trash3"></i></button>
                        </form>
                    </td>
                    @endif
                </tr>
            @empty
                <tr><td colspan="8" class="px-4 py-8 text-center text-sm text-slate-500">No shifts yet.</td></tr>
            @endforelse
        </x-table>
        @if($shifts->hasPages())<div class="mt-4">{{ $shifts->links() }}</div>@endif
    </x-card>
@endsection
