@extends('layouts.admin-simple')

@section('title', 'Fixed Assets — All Assets')

@section('breadcrumb')
    <li class="breadcrumb-item">Warehouse</li>
    <li class="breadcrumb-item">Fixed Assets</li>
    <li class="breadcrumb-item"><a href="{{ route('warehouse.fixed-assets.inventory.index') }}">Inventory</a></li>
    <li class="breadcrumb-item active">All Assets</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">All Assets</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">All fixed assets — projects, warehouse, and repair shop</p>
        </div>
        <div class="page-actions-right">
            <a href="{{ route('warehouse.fixed-assets.inventory.index') }}" class="btn btn-sm btn-outline-secondary">
                <i class="material-icons mr-1" style="font-size: 16px;">arrow_back</i> Back to Overview
            </a>
        </div>
    </div>
@endsection

@section('content')
    {{-- Summary Cards --}}
    <div class="row mb-3">
        <div class="col-md-3">
            <div class="card text-center {{ !request('location_type') || request('location_type') === 'all' ? 'border-primary' : '' }}">
                <div class="card-body py-3">
                    <h4 class="mb-0 text-primary">{{ $totalCount }}</h4>
                    <small class="text-muted">Total Assets</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card text-center {{ request('location_type') === 'project' ? 'border-success' : '' }}">
                <div class="card-body py-3">
                    <h4 class="mb-0 text-success">{{ $atProjectCount }}</h4>
                    <small class="text-muted">At Projects</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card text-center {{ request('location_type') === 'warehouse' ? 'border-info' : '' }}">
                <div class="card-body py-3">
                    <h4 class="mb-0 text-info">{{ $inWarehouseCount }}</h4>
                    <small class="text-muted">In Warehouse</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card text-center {{ request('location_type') === 'repair_shop' ? 'border-warning' : '' }}">
                <div class="card-body py-3">
                    <h4 class="mb-0 text-warning">{{ $repairShopCount }}</h4>
                    <small class="text-muted">Repair Shop</small>
                </div>
            </div>
        </div>
    </div>

    {{-- Filters --}}
    <div class="card mb-3">
        <div class="card-body py-2">
            <form method="GET" action="{{ route('warehouse.fixed-assets.inventory.all-assets') }}" class="row align-items-end" style="gap: 0;">
                <div class="col-md-2">
                    <label class="mb-1" style="font-size: 12px;">Location</label>
                    <select name="location_type" class="form-control form-control-sm">
                        <option value="all" {{ request('location_type', 'all') === 'all' ? 'selected' : '' }}>All Locations</option>
                        <option value="project" {{ request('location_type') === 'project' ? 'selected' : '' }}>At Project</option>
                        <option value="warehouse" {{ request('location_type') === 'warehouse' ? 'selected' : '' }}>In Warehouse</option>
                        <option value="repair_shop" {{ request('location_type') === 'repair_shop' ? 'selected' : '' }}>Repair Shop</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="mb-1" style="font-size: 12px;">Category</label>
                    <select name="category_id" class="form-control form-control-sm">
                        <option value="">All Categories</option>
                        @foreach($categories as $cat)
                            <option value="{{ $cat->id }}" {{ request('category_id') == $cat->id ? 'selected' : '' }}>{{ $cat->category_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="mb-1" style="font-size: 12px;">Condition</label>
                    <select name="condition" class="form-control form-control-sm">
                        <option value="">All Conditions</option>
                        <option value="new" {{ request('condition') === 'new' ? 'selected' : '' }}>New</option>
                        <option value="good" {{ request('condition') === 'good' ? 'selected' : '' }}>Good</option>
                        <option value="fair" {{ request('condition') === 'fair' ? 'selected' : '' }}>Fair</option>
                        <option value="poor" {{ request('condition') === 'poor' ? 'selected' : '' }}>Poor</option>
                        <option value="damaged" {{ request('condition') === 'damaged' ? 'selected' : '' }}>Damaged</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="mb-1" style="font-size: 12px;">Project</label>
                    <select name="project_id" class="form-control form-control-sm select2-sm">
                        <option value="">All Projects</option>
                        @foreach($projects as $p)
                            <option value="{{ $p->id }}" {{ request('project_id') == $p->id ? 'selected' : '' }}>{{ $p->project_number }} - {{ $p->project_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="mb-1" style="font-size: 12px;">Search</label>
                    <input type="text" name="search" class="form-control form-control-sm" placeholder="Name, code, serial..." value="{{ request('search') }}">
                </div>
                <div class="col-md-1 d-flex align-items-end">
                    <button type="submit" class="btn btn-sm btn-primary mr-1"><i class="material-icons" style="font-size: 14px;">filter_list</i></button>
                    <a href="{{ route('warehouse.fixed-assets.inventory.all-assets') }}" class="btn btn-sm btn-outline-secondary"><i class="material-icons" style="font-size: 14px;">clear</i></a>
                </div>
            </form>
        </div>
    </div>

    {{-- Table --}}
    <div class="card">
        <div class="card-header">
            <h6 class="mb-0">
                <i class="material-icons mr-2 align-middle">devices</i>
                All Fixed Assets
                @if(request()->hasAny(['location_type', 'category_id', 'condition', 'project_id', 'search']))
                    <span class="badge badge-secondary ml-1">Filtered</span>
                @endif
            </h6>
        </div>
        <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>Asset Code</th>
                            <th>Asset Name</th>
                            <th>Category</th>
                            <th>Current Location</th>
                            <th>Serial Number</th>
                            <th>Condition</th>
                            <th>Status</th>
                            <th class="text-right">Book Value</th>
                            <th style="width: 60px;"></th>
                        </tr>
                    </thead>
                    <tbody>
                        @php
                            $condColors = ['new'=>'success','good'=>'info','fair'=>'warning','poor'=>'danger','damaged'=>'danger','written_off'=>'secondary'];
                            $lcColors   = ['in_use'=>'success','in_warehouse'=>'info','returned'=>'secondary','under_maintenance'=>'warning','acquired'=>'light','disposed'=>'dark','retired'=>'dark'];
                            $lcLabels   = ['in_use'=>'At Project','in_warehouse'=>'In Warehouse','returned'=>'In Warehouse','under_maintenance'=>'Repair Shop','acquired'=>'In Warehouse','disposed'=>'Disposed','retired'=>'Retired'];
                        @endphp
                        @forelse($assets as $i => $asset)
                        <tr>
                            <td>{{ $assets->firstItem() + $i }}</td>
                            <td><code>{{ $asset->asset_code }}</code></td>
                            <td>{{ $asset->asset_name }}</td>
                            <td>{{ $asset->category ? $asset->category->category_name : '-' }}</td>
                            <td>
                                @if($asset->lifecycle_status === 'in_use' && $asset->currentProject)
                                    <small class="text-muted">{{ $asset->currentProject->project_number }}</small><br>
                                    {{ $asset->currentProject->project_name }}
                                @elseif($asset->lifecycle_status === 'under_maintenance')
                                    <span class="text-warning"><i class="material-icons align-middle" style="font-size:14px;">build</i> Repair Shop</span>
                                @else
                                    <span class="text-info"><i class="material-icons align-middle" style="font-size:14px;">store</i> Warehouse</span>
                                @endif
                            </td>
                            <td>{{ $asset->serial_number ?? '-' }}</td>
                            <td>
                                <span class="badge badge-{{ $condColors[$asset->condition] ?? 'secondary' }}">{{ ucfirst($asset->condition) }}</span>
                            </td>
                            <td>
                                <span class="badge badge-{{ $lcColors[$asset->lifecycle_status] ?? 'secondary' }}">
                                    {{ $lcLabels[$asset->lifecycle_status] ?? ucfirst(str_replace('_', ' ', $asset->lifecycle_status)) }}
                                </span>
                            </td>
                            <td class="text-right">AED {{ number_format($asset->current_book_value ?? $asset->purchase_price, 2) }}</td>
                            <td>
                                <a href="{{ route('warehouse.fixed-assets.catalog.show', $asset->id) }}" class="btn btn-sm btn-info">
                                    <i class="material-icons" style="font-size: 14px;">visibility</i>
                                </a>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="10" class="text-center text-muted py-4">
                                <i class="material-icons" style="font-size: 36px;">devices</i>
                                <p class="mt-2 mb-0">No assets found matching your filters.</p>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($assets->hasPages())
        <div class="card-footer">
            <div class="d-flex justify-content-between align-items-center">
                <small class="text-muted">Showing {{ $assets->firstItem() }} to {{ $assets->lastItem() }} of {{ $assets->total() }} assets</small>
                {{ $assets->appends(request()->query())->links() }}
            </div>
        </div>
        @endif
    </div>
@endsection

@push('styles')
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css">
    <style>
        .select2-container { width: 100% !important; }
        .select2-container--default .select2-selection--single { height: 31px; border: 1px solid #ced4da; font-size: 13px; }
        .select2-container--default .select2-selection--single .select2-selection__rendered { line-height: 29px; }
        .select2-container--default .select2-selection--single .select2-selection__arrow { height: 29px; }
    </style>
@endpush

@push('scripts')
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <script>
        $(document).ready(function() {
            $('.select2-sm').select2({ placeholder: 'All Projects', allowClear: true });
        });
    </script>
@endpush
