@extends('layouts.admin-simple')

@section('title', 'Fixed Assets - Project 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">Project 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">Project Assets</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">Fixed assets currently allocated to projects</p>
        </div>
        <div class="page-actions-right d-flex align-items-center">
            <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')
    <!-- Filter -->
    <div class="card mb-3">
        <div class="card-body py-2">
            <form method="GET" action="{{ route('warehouse.fixed-assets.inventory.project-assets') }}" class="row align-items-end">
                <div class="col-md-4">
                    <label class="mb-1" style="font-size: 12px;">Project</label>
                    <select name="project_id" class="form-control form-control-sm select2-dropdown">
                        <option value="">All Projects</option>
                        @foreach($projects ?? [] as $project)
                            <option value="{{ $project->id }}" {{ request('project_id') == $project->id ? 'selected' : '' }}>{{ $project->project_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3">
                    <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-3">
                    <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-2 d-flex">
                    <button type="submit" class="btn btn-sm btn-primary mr-2"><i class="material-icons mr-1" style="font-size: 14px;">filter_list</i> Filter</button>
                    <a href="{{ route('warehouse.fixed-assets.inventory.project-assets') }}" class="btn btn-sm btn-outline-secondary">Clear</a>
                </div>
            </form>
        </div>
    </div>

    <!-- Assets Table -->
    <div class="card">
        <div class="card-header">
            <h6 class="mb-0"><i class="material-icons mr-2 align-middle">business</i>Project Assets</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>Project</th>
                            <th>Serial Number</th>
                            <th>Condition</th>
                            <th class="text-right">Book Value</th>
                            <th>Allocated Since</th>
                            <th style="width: 60px;"></th>
                        </tr>
                    </thead>
                    <tbody>
                        @php
                            $condColors = ['new'=>'success','good'=>'info','fair'=>'warning','poor'=>'danger','damaged'=>'danger','non_functional'=>'dark'];
                        @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>{{ $asset->currentProject ? $asset->currentProject->project_name : '-' }}</td>
                            <td>{{ $asset->serial_number ?? '-' }}</td>
                            <td>
                                <span class="badge badge-{{ $condColors[$asset->condition] ?? 'secondary' }}">{{ ucfirst($asset->condition) }}</span>
                            </td>
                            <td class="text-right">AED {{ number_format($asset->current_book_value ?? $asset->purchase_price, 2) }}</td>
                            <td>{{ $asset->allocation_date ? \Carbon\Carbon::parse($asset->allocation_date)->format('M d, Y') : '-' }}</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;">business</i>
                                <p class="mt-2 mb-0">No assets currently allocated to projects.</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-dropdown').select2({ placeholder: 'Select project...', allowClear: true });
        });
    </script>
@endpush
