@extends('layouts.app')
@section('title', 'Visitors')
@section('page_title', 'Visitor Management')
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Home</a></li><li class="breadcrumb-item active">Visitors</li>
@endsection
@section('page_actions')
    @can('visitors.create')<a href="{{ route('visitors.create') }}" class="btn btn-primary"><i class="bi bi-person-plus me-1"></i>New Visitor</a>@endcan
@endsection

@section('content')
    <div class="row g-3 mb-3">
        <div class="col-sm-4"><div class="card stat-card"><div class="card-body d-flex align-items-center gap-3">
            <div class="icon bg-soft-primary"><i class="bi bi-people"></i></div>
            <div><div class="value">{{ $visitors->count() }}</div><div class="label">Visitors Today</div></div>
        </div></div></div>
        <div class="col-sm-4"><div class="card stat-card"><div class="card-body d-flex align-items-center gap-3">
            <div class="icon bg-soft-success"><i class="bi bi-door-open"></i></div>
            <div><div class="value">{{ $insideCount }}</div><div class="label">Currently Inside</div></div>
        </div></div></div>
    </div>

    <div class="card mb-3"><div class="card-body py-2">
        <form method="GET" class="row g-2 align-items-end">
            <div class="col-md-3"><label class="form-label small">Date</label><input type="date" name="date" value="{{ $date }}" class="form-control form-control-sm"></div>
            <div class="col-md-3"><label class="form-label small">Status</label>
                <select name="status" class="form-select form-select-sm">
                    <option value="">All</option>
                    <option value="checked_in" @selected($status==='checked_in')>Inside</option>
                    <option value="checked_out" @selected($status==='checked_out')>Left</option>
                </select></div>
            <div class="col-md-3"><button class="btn btn-sm btn-primary"><i class="bi bi-search me-1"></i>Filter</button></div>
        </form>
    </div></div>

    <div class="card"><div class="card-body">
        <table class="table table-hover align-middle">
            <thead><tr><th>Pass</th><th>Visitor</th><th>Phone</th><th>To Meet</th><th>Purpose</th><th>In</th><th>Out</th><th class="text-center">Status</th><th class="text-end">Actions</th></tr></thead>
            <tbody>
                @forelse($visitors as $v)
                    <tr>
                        <td class="small fw-semibold">{{ $v->pass_number }}</td>
                        <td>{{ $v->name }}@if($v->party_size > 1)<span class="text-muted small"> +{{ $v->party_size - 1 }}</span>@endif<div class="small text-muted">{{ $v->cnic }}</div></td>
                        <td class="small">{{ $v->phone ?: '—' }}</td>
                        <td class="small">{{ $v->whom_to_meet ?: '—' }}</td>
                        <td class="small">{{ $v->purpose ?: '—' }}</td>
                        <td class="small">{{ optional($v->check_in)->format('H:i') }}</td>
                        <td class="small">{{ optional($v->check_out)->format('H:i') ?? '—' }}</td>
                        <td class="text-center"><span class="badge bg-soft-{{ $v->status_color }}">{{ $v->status === 'checked_in' ? 'Inside' : 'Left' }}</span></td>
                        <td class="text-end">
                            <a href="{{ route('visitors.pass', $v) }}" target="_blank" class="btn btn-sm btn-light border" title="Pass"><i class="bi bi-printer"></i></a>
                            @can('visitors.update')@if($v->status === 'checked_in')
                                <form method="POST" action="{{ route('visitors.checkout', $v) }}" class="d-inline">@csrf<button class="btn btn-sm btn-outline-secondary" title="Check out"><i class="bi bi-box-arrow-right"></i></button></form>
                            @endif @endcan
                            @can('visitors.delete')<form method="POST" action="{{ route('visitors.destroy', $v) }}" class="d-inline" data-confirm="Delete record?">@csrf @method('DELETE')<button class="btn btn-sm btn-light border text-danger"><i class="bi bi-trash"></i></button></form>@endcan
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="9" class="text-center text-muted py-4">No visitors for this date.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div></div>
@endsection
