@extends('layouts.admin-simple')

@section('title', 'Asset Allocations')

@section('breadcrumb')
    <li class="breadcrumb-item">Warehouse</li>
    <li class="breadcrumb-item">Fixed Assets</li>
    <li class="breadcrumb-item active">Allocations</li>
@endsection

@section('page-title')
    <div class="d-flex justify-content-between align-items-center w-100">
        <div class="page-title-left">
            <h3 class="mb-1">Asset Allocations</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">Manage asset allocations to projects</p>
        </div>
        <div class="page-actions-right d-flex">
            @can('warehouse.fixed-asset-allocations.create')
            <a href="{{ route('warehouse.fixed-assets.allocations.create') }}" class="btn btn-sm btn-primary">
                <i class="material-icons mr-1" style="font-size: 16px;">add</i> New Allocation
            </a>
            @endcan
        </div>
    </div>
@endsection

@section('content')
    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show"><span>{{ session('success') }}</span><button type="button" class="close" data-dismiss="alert"><span>&times;</span></button></div>
    @endif

    <div class="card mb-3">
        <div class="card-body py-2">
            <form method="GET" class="row align-items-center">
                <div class="col-md-3">
                    <input type="text" name="search" class="form-control form-control-sm" placeholder="Search..." value="{{ request('search') }}">
                </div>
                <div class="col-md-2">
                    <select name="status" class="form-control form-control-sm">
                        <option value="">All Status</option>
                        @foreach(['pending_approval' => 'Pending Allocation', 'partially_allocated' => 'Partially Allocated', 'allocated' => 'Allocated', 'cancelled' => 'Cancelled'] as $value => $label)
                            <option value="{{ $value }}" {{ request('status') == $value ? 'selected' : '' }}>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3">
                    <select name="project_id" class="form-control form-control-sm">
                        <option value="">All Projects</option>
                        @foreach($projects as $p)
                            <option value="{{ $p->id }}" {{ request('project_id') == $p->id ? 'selected' : '' }}>{{ $p->project_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-sm btn-primary">Filter</button>
                    <a href="{{ route('warehouse.fixed-assets.allocations.index') }}" class="btn btn-sm btn-outline-secondary">Clear</a>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="thead-light">
                        <tr><th>#</th><th>Allocation #</th><th>Project</th><th>Assets</th><th>Requested By</th><th>Date</th><th>Status</th><th>Actions</th></tr>
                    </thead>
                    <tbody>
                        @forelse($allocations as $i => $alloc)
                        <tr>
                            <td>{{ $allocations->firstItem() + $i }}</td>
                            <td><a href="{{ route('warehouse.fixed-assets.allocations.show', $alloc->id) }}"><code>{{ $alloc->allocation_number }}</code></a></td>
                            <td>{{ $alloc->project ? $alloc->project->project_name : '-' }}</td>
                            <td><span class="badge badge-info">{{ $alloc->items->count() }}</span></td>
                            <td>{{ $alloc->requester ? $alloc->requester->name : '-' }}</td>
                            <td>{{ $alloc->created_at ? $alloc->created_at->format('M d, Y') : '-' }}</td>
                            <td>
                                @php
                                    $colors = ['pending_approval' => 'warning', 'partially_allocated' => 'info', 'allocated' => 'success', 'cancelled' => 'secondary', 'draft' => 'secondary'];
                                    $labels = ['pending_approval' => 'Pending Allocation', 'partially_allocated' => 'Partially Allocated'];
                                @endphp
                                <span class="badge badge-{{ $colors[$alloc->status] ?? 'secondary' }}">{{ $labels[$alloc->status] ?? ucfirst(str_replace('_', ' ', $alloc->status)) }}</span>
                            </td>
                            <td>
                                <div class="d-flex align-items-center" style="gap: 4px;">
                                    <a href="{{ route('warehouse.fixed-assets.allocations.show', $alloc->id) }}" class="btn btn-sm btn-info" title="View"><i class="material-icons" style="font-size: 14px;">visibility</i></a>
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr><td colspan="8" class="text-center text-muted py-4">No allocations found.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($allocations->hasPages())
        <div class="card-footer">{{ $allocations->links() }}</div>
        @endif
    </div>
@endsection
