@extends('layouts.app')

@section('title', 'Biometric Devices')
@section('heading', 'Biometric Devices')

@section('content')
@include('settings.partials.back-to-home')
    <x-card>
        <div class="mb-4 flex flex-wrap justify-between items-center gap-3">
            <p class="text-slate-600 text-sm">Register ZKTeco terminals and copy webhook credentials into each device Api Settings form. Device User ID must match HRMS Employee ID.</p>
            <div class="flex gap-2">
                <a href="{{ route('attendance-punches.index') }}" class="inline-flex items-center rounded-lg bg-slate-100 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-200">Punch log</a>
                @if(auth()->user()->hasPermission('biometric_devices_manage') || auth()->user()->is_superadmin)
                <a href="{{ route('biometric-devices.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 device</a>
                @endif
            </div>
        </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">Alias</th>
                    <th class="whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Serial (SN)</th>
                    <th class="whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Mode</th>
                    <th class="whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">API username</th>
                    <th class="whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Location</th>
                    <th class="whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Status</th>
                    <th class="whitespace-nowrap px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Last punch</th>
                    <th class="whitespace-nowrap px-4 py-3 text-right text-xs font-medium uppercase text-slate-500">Actions</th>
                </tr>
            </x-slot>
            @forelse($devices as $device)
                <tr>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-900">{{ $device->terminal_alias ?: '—' }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm font-mono text-slate-700">{{ $device->terminal_sn }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm">
                        @php
                            $modeColors = [
                                'login' => 'bg-sky-50 text-sky-700',
                                'logout' => 'bg-violet-50 text-violet-700',
                                'both' => 'bg-slate-100 text-slate-700',
                            ];
                            $mode = $device->punch_mode ?: 'both';
                        @endphp
                        <span class="inline-flex rounded-full px-2 py-0.5 text-xs font-medium {{ $modeColors[$mode] ?? $modeColors['both'] }}">{{ $device->punchModeLabel() }}</span>
                    </td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm font-mono text-slate-600">{{ $device->api_username }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $device->location?->name ?: '—' }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm">
                        @if($device->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>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $device->last_punch_at?->format('Y-m-d H:i') ?: '—' }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-right text-sm">
                        <a href="{{ route('biometric-devices.show', $device) }}" class="text-slate-600 hover:text-slate-900">Setup</a>
                        @if(auth()->user()->hasPermission('biometric_devices_manage') || auth()->user()->is_superadmin)
                        <a href="{{ route('biometric-devices.edit', $device) }}" class="btn-icon-action btn-icon-action--edit" title="Edit" aria-label="Edit"><i class="bi bi-pencil-square"></i></a>
                        @endif
                    </td>
                </tr>
            @empty
                <tr><td colspan="8" class="px-4 py-8 text-center text-sm text-slate-500">No biometric devices yet.</td></tr>
            @endforelse
        </x-table>
        @if($devices->hasPages())<div class="mt-4">{{ $devices->links() }}</div>@endif
    </x-card>
@endsection
