@extends('layouts.sash')

@section('title', 'Walk-In Queue')

@section('content')
<div class="row mt-4">
<div class="col-12">
    <div class="page-header">
        <div>
            <h1 class="page-title">Walk-In Queue</h1>
            <p class="page-subtitle">Track guests waiting in the lobby and convert them to appointments.</p>
        </div>
        <div class="page-actions">
            <a href="{{ route('walk-ins.create') }}" class="btn btn-primary"><i class="fe fe-plus me-1"></i>Add Walk-In</a>
        </div>
    </div>

    <div class="card mb-4">
        <div class="card-body">
            <form method="get" action="{{ route('walk-ins.index') }}" class="row g-3 align-items-end">
                <div class="col-12 col-md-6 col-lg-4">
                    <label class="form-label">Search</label>
                    <input type="text" name="search" value="{{ request('search') }}" placeholder="Token, name, mobile..." class="form-control">
                </div>
                <div class="col-12 col-md-6 col-lg-3">
                    <label class="form-label">Branch</label>
                    <select name="branch_id" class="form-control form-select">
                        <option value="">All</option>
                        @foreach($branches as $b)
                            <option value="{{ $b->id }}" {{ (string)request('branch_id') === (string)$b->id ? 'selected' : '' }}>{{ $b->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-12 col-md-6 col-lg-2">
                    <label class="form-label">Status</label>
                    <select name="status" class="form-control form-select">
                        <option value="">All</option>
                        <option value="waiting" {{ request('status') === 'waiting' ? 'selected' : '' }}>Waiting</option>
                        <option value="assigned" {{ request('status') === 'assigned' ? 'selected' : '' }}>Assigned</option>
                        <option value="in_service" {{ request('status') === 'in_service' ? 'selected' : '' }}>In Service</option>
                        <option value="completed" {{ request('status') === 'completed' ? 'selected' : '' }}>Completed</option>
                        <option value="cancelled" {{ request('status') === 'cancelled' ? 'selected' : '' }}>Cancelled</option>
                        <option value="converted" {{ request('status') === 'converted' ? 'selected' : '' }}>Converted</option>
                    </select>
                </div>
                <div class="col-12 col-lg-auto">
                    <button type="submit" class="btn btn-primary"><i class="fe fe-filter me-1"></i>Filter</button>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table border text-nowrap text-md-nowrap table-hover mb-0">
                    <thead>
                        <tr>
                            <th>Token</th>
                            <th>Customer</th>
                            <th>Branch</th>
                            <th>Preferred Staff</th>
                            <th>Status</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($walkIns as $wi)
                        <tr>
                            <td>{{ $wi->token_no }}</td>
                            <td>
                                {{ $wi->customer?->display_name ?? $wi->customer_name ?? 'Walk-in' }}
                                @if($wi->mobile)
                                    <span class="d-block small text-muted">{{ $wi->mobile }}</span>
                                @endif
                            </td>
                            <td>{{ $wi->branch?->name ?? '—' }}</td>
                            <td>{{ $wi->preferredStaff?->full_name ?? '—' }}</td>
                            <td>
                                @php
                                    $badgeClass = match($wi->status) {
                                        'waiting' => 'badge-warning',
                                        'assigned', 'in_service' => 'badge-info',
                                        'completed', 'converted' => 'badge-success',
                                        'cancelled' => 'badge-muted',
                                        default => 'badge-muted',
                                    };
                                @endphp
                                <span class="badge {{ $badgeClass }}">{{ ucfirst(str_replace('_', ' ', $wi->status)) }}</span>
                            </td>
                            <td class="text-end table-actions">
                        <a href="{{ route('walk-ins.show', $wi) }}" class="btn btn-icon btn-sm btn-light" title="View"><i class="fe fe-eye"></i></a>
                        <a href="{{ route('walk-ins.edit', $wi) }}" class="btn btn-icon btn-sm btn-light" title="Edit"><i class="fe fe-edit"></i></a>
                        <form action="{{ route('walk-ins.destroy', $wi) }}" method="POST" class="d-inline" onsubmit="return confirm('Delete this walk-in?');">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-icon btn-sm btn-light text-danger" title="Delete"><i class="fe fe-trash-2"></i></button>
                        </form>
                    </td>
                </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="text-center text-muted py-4">No walk-ins in the queue.</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    @if($walkIns->hasPages())
    <div class="mt-4">
        {{ $walkIns->withQueryString()->links() }}
    </div>
    @endif
</div>
</div>
@endsection
