@extends('backEnd.layouts.master')
@section('title','Delivery Men')
@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="page-title-box d-flex align-items-center justify-content-between">
                <h4 class="page-title"><i class="fe-truck me-2"></i>Delivery Men</h4>
                <a href="{{ route('admin.delivery-men.requests') }}" class="btn btn-warning btn-sm rounded-pill shadow-sm me-2">
                    <i class="fe-user-check me-1"></i> Requests @if(($pendingCount ?? 0) > 0)<span class="badge bg-danger">{{ $pendingCount }}</span>@endif
                </a>
                <a href="{{ route('admin.delivery-men.create') }}" class="btn btn-primary btn-sm rounded-pill shadow-sm">
                    <i class="fe-plus me-1"></i> Add New
                </a>
            </div>
        </div>
    </div>

    @if(session('success'))
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <i class="fe-check-circle me-2"></i>{{ session('success') }}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif

    <div class="row">
        <div class="col-12">
            <div class="card shadow-sm border-0">
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="bg-light">
                                <tr>
                                    <th class="ps-4">#</th>
                                    <th>Photo</th>
                                    <th>Name</th>
                                    <th>Phone</th>
                                    <th>Vehicle</th>
                                    <th>Assigned Orders</th>
                                    <th>Status</th>
                                    <th>Availability (Online/Offline)</th>
                                    <th class="text-end pe-4">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($deliveryMen as $man)
                                <tr>
                                    <td class="ps-4 text-muted">{{ $loop->iteration }}</td>
                                    <td>
                                        @if($man->photo)
                                            <img src="{{ asset($man->photo) }}" class="rounded-circle border" width="40" height="40" style="object-fit:cover;">
                                        @else
                                            <div class="bg-soft-primary rounded-circle d-flex align-items-center justify-content-center" style="width:40px;height:40px;">
                                                <i class="fe-user text-primary"></i>
                                            </div>
                                        @endif
                                    </td>
                                    <td><span class="fw-semibold text-dark">{{ $man->name }}</span></td>
                                    <td>{{ $man->phone }}</td>
                                    <td>
                                        @if($man->vehicle_type)
                                            <span class="badge bg-soft-info text-info border">{{ $man->vehicle_type }}</span>
                                            @if($man->vehicle_number) <small class="text-muted">({{ $man->vehicle_number }})</small> @endif
                                        @else
                                            <span class="text-muted">—</span>
                                        @endif
                                    </td>
                                    <td><span class="badge bg-soft-primary text-primary border">{{ $man->shippings->count() }} Orders</span></td>
                                    <td>
                                        @if($man->status === 'active')
                                            <span class="badge bg-success-subtle text-success border border-success-subtle"><i class="fe-check me-1"></i>Active</span>
                                        @else
                                            <span class="badge bg-danger-subtle text-danger border border-danger-subtle"><i class="fe-x me-1"></i>Inactive</span>
                                        @endif
                                    </td>
                                    <td>
                                        <form action="{{ route('admin.delivery-men.toggle-online', $man->id) }}" method="POST" class="d-inline">
                                            @csrf
                                            <button type="submit" class="btn btn-sm border-0 bg-transparent p-0" title="Click to toggle availability">
                                                @if($man->is_online)
                                                    <span class="badge bg-success text-white border-0 d-inline-flex align-items-center gap-1 shadow-sm px-2 py-1 animate-pulse" style="font-size: 0.75rem; border-radius: 20px; cursor: pointer;">
                                                        <span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true" style="width: 8px; height: 8px; color: #fff;"></span> Online
                                                    </span>
                                                @else
                                                    <span class="badge bg-secondary text-white border-0 d-inline-flex align-items-center gap-1 shadow-sm px-2 py-1" style="font-size: 0.75rem; border-radius: 20px; cursor: pointer;">
                                                        <span style="display:inline-block; width: 6px; height: 6px; border-radius:50%; background-color:#fff;"></span> Offline
                                                    </span>
                                                @endif
                                            </button>
                                        </form>
                                    </td>
                                    <td class="text-end pe-4">
                                        <a href="{{ route('admin.delivery-men.edit', $man) }}" class="btn btn-sm btn-outline-primary rounded-pill me-1"><i class="fe-edit"></i></a>
                                        <form action="{{ route('admin.delivery-men.destroy', $man) }}" method="POST" class="d-inline" onsubmit="return confirm('Delete this delivery man?')">
                                            @csrf @method('DELETE')
                                            <button type="submit" class="btn btn-sm btn-outline-danger rounded-pill"><i class="fe-trash-2"></i></button>
                                        </form>
                                    </td>
                                </tr>
                                @empty
                                <tr><td colspan="8" class="text-center py-5 text-muted"><i class="fe-truck fs-1 d-block mb-2"></i>No delivery men added yet.</td></tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
                @if($deliveryMen->hasPages())
                <div class="card-footer bg-light border-0 p-3">{{ $deliveryMen->links() }}</div>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection
