@extends('layouts.admin-simple')

@section('title', 'Fixed Assets - Inventory Overview')

@section('breadcrumb')
    <li class="breadcrumb-item">Warehouse</li>
    <li class="breadcrumb-item">Fixed Assets</li>
    <li class="breadcrumb-item active">Inventory</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 Inventory Overview</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">Summary of all fixed assets across locations</p>
        </div>
        <div class="page-actions-right d-flex align-items-center">
            <a href="{{ route('warehouse.fixed-assets.inventory.warehouse-assets') }}" class="btn btn-sm btn-outline-primary mr-2">
                <i class="material-icons mr-1" style="font-size: 16px;">store</i> Warehouse Assets
            </a>
            <a href="{{ route('warehouse.fixed-assets.inventory.project-assets') }}" class="btn btn-sm btn-outline-info mr-2">
                <i class="material-icons mr-1" style="font-size: 16px;">business</i> Project Assets
            </a>
            <a href="{{ route('warehouse.fixed-assets.inventory.condition-report') }}" class="btn btn-sm btn-outline-warning mr-2">
                <i class="material-icons mr-1" style="font-size: 16px;">assessment</i> Condition Report
            </a>
            <a href="{{ route('warehouse.fixed-assets.inventory.movement-history') }}" class="btn btn-sm btn-outline-secondary">
                <i class="material-icons mr-1" style="font-size: 16px;">timeline</i> Movement History
            </a>
        </div>
    </div>
@endsection

@section('content')
    <!-- Summary Cards -->
    <div class="row mb-4">
        <div class="col-md">
            <div class="card text-center">
                <div class="card-body py-3">
                    <i class="material-icons text-primary" style="font-size: 28px;">inventory_2</i>
                    <h4 class="mt-2 mb-0">{{ $totalAssets ?? 0 }}</h4>
                    <small class="text-muted">Total Assets</small>
                </div>
            </div>
        </div>
        <div class="col-md">
            <div class="card text-center">
                <div class="card-body py-3">
                    <i class="material-icons text-info" style="font-size: 28px;">store</i>
                    <h4 class="mt-2 mb-0">{{ $inWarehouse ?? 0 }}</h4>
                    <small class="text-muted">In Warehouse</small>
                </div>
            </div>
        </div>
        <div class="col-md">
            <div class="card text-center">
                <div class="card-body py-3">
                    <i class="material-icons text-success" style="font-size: 28px;">check_circle</i>
                    <h4 class="mt-2 mb-0">{{ $inUse ?? 0 }}</h4>
                    <small class="text-muted">In Use</small>
                </div>
            </div>
        </div>
        <div class="col-md">
            <div class="card text-center">
                <div class="card-body py-3">
                    <i class="material-icons text-warning" style="font-size: 28px;">build</i>
                    <h4 class="mt-2 mb-0">{{ $underMaintenance ?? 0 }}</h4>
                    <small class="text-muted">Under Maintenance</small>
                </div>
            </div>
        </div>
        <div class="col-md">
            <div class="card text-center">
                <div class="card-body py-3">
                    <i class="material-icons text-danger" style="font-size: 28px;">delete_forever</i>
                    <h4 class="mt-2 mb-0">{{ $disposed ?? 0 }}</h4>
                    <small class="text-muted">Disposed</small>
                </div>
            </div>
        </div>
    </div>

    <!-- Financial Summary -->
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card">
                <div class="card-body text-center py-3">
                    <small class="text-muted d-block">Total Purchase Value</small>
                    <h4 class="text-primary mb-0">AED {{ number_format($totalPurchaseValue ?? 0, 2) }}</h4>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-body text-center py-3">
                    <small class="text-muted d-block">Current Book Value</small>
                    <h4 class="text-success mb-0">AED {{ number_format($totalBookValue ?? 0, 2) }}</h4>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-body text-center py-3">
                    <small class="text-muted d-block">Total Depreciation</small>
                    <h4 class="text-danger mb-0">AED {{ number_format($totalDepreciation ?? 0, 2) }}</h4>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- Assets by Category -->
        <div class="col-md-6">
            <div class="card mb-3">
                <div class="card-header">
                    <h6 class="mb-0"><i class="material-icons mr-2 align-middle">category</i>Assets by Category</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>Category</th>
                                    <th class="text-center">Count</th>
                                    <th class="text-right">Total Value</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($byCategory ?? [] as $category)
                                <tr>
                                    <td>{{ $category->category_name ?? 'Uncategorized' }}</td>
                                    <td class="text-center"><span class="badge badge-primary">{{ $category->assets_count }}</span></td>
                                    <td class="text-right">AED {{ number_format($category->total_value ?? 0, 2) }}</td>
                                </tr>
                                @empty
                                <tr><td colspan="3" class="text-center text-muted py-3">No data available.</td></tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Assets by Condition -->
        <div class="col-md-6">
            <div class="card mb-3">
                <div class="card-header">
                    <h6 class="mb-0"><i class="material-icons mr-2 align-middle">star_half</i>Assets by Condition</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>Condition</th>
                                    <th class="text-center">Count</th>
                                    <th class="text-right">Percentage</th>
                                </tr>
                            </thead>
                            <tbody>
                                @php
                                    $conditionColors = ['new' => 'success', 'good' => 'info', 'fair' => 'warning', 'poor' => 'danger', 'damaged' => 'danger', 'non_functional' => 'dark'];
                                @endphp
                                @forelse($byCondition ?? [] as $condition)
                                <tr>
                                    <td>
                                        <span class="badge badge-{{ $conditionColors[$condition->condition] ?? 'secondary' }}">
                                            {{ ucfirst(str_replace('_', ' ', $condition->condition)) }}
                                        </span>
                                    </td>
                                    <td class="text-center">{{ $condition->count }}</td>
                                    <td class="text-right">
                                        @php $pct = ($totalAssets ?? 0) > 0 ? round(($condition->count / $totalAssets) * 100, 1) : 0; @endphp
                                        <div class="progress" style="height: 18px;">
                                            <div class="progress-bar bg-{{ $conditionColors[$condition->condition] ?? 'secondary' }}" style="width: {{ $pct }}%;">{{ $pct }}%</div>
                                        </div>
                                    </td>
                                </tr>
                                @empty
                                <tr><td colspan="3" class="text-center text-muted py-3">No data available.</td></tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Assets by Lifecycle Status -->
    <div class="card mb-3">
        <div class="card-header">
            <h6 class="mb-0"><i class="material-icons mr-2 align-middle">autorenew</i>Assets by Lifecycle Status</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>Lifecycle Status</th>
                            <th class="text-center">Count</th>
                            <th class="text-right">Total Purchase Value</th>
                            <th class="text-right">Total Book Value</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php
                            $lifecycleColors = ['in_warehouse' => 'info', 'allocated' => 'primary', 'in_use' => 'success', 'under_maintenance' => 'warning', 'returned' => 'secondary', 'disposed' => 'dark'];
                        @endphp
                        @forelse($byLifecycle ?? [] as $lifecycle)
                        <tr>
                            <td>
                                <span class="badge badge-{{ $lifecycleColors[$lifecycle->lifecycle_status] ?? 'secondary' }}">
                                    {{ ucfirst(str_replace('_', ' ', $lifecycle->lifecycle_status)) }}
                                </span>
                            </td>
                            <td class="text-center">{{ $lifecycle->count }}</td>
                            <td class="text-right">AED {{ number_format($lifecycle->total_purchase_value ?? 0, 2) }}</td>
                            <td class="text-right">AED {{ number_format($lifecycle->total_book_value ?? 0, 2) }}</td>
                        </tr>
                        @empty
                        <tr><td colspan="4" class="text-center text-muted py-3">No data available.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Recent Movements -->
    <div class="card mb-3">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h6 class="mb-0"><i class="material-icons mr-2 align-middle">timeline</i>Recent Movements</h6>
            <a href="{{ route('warehouse.fixed-assets.inventory.movement-history') }}" class="btn btn-sm btn-outline-primary">View All</a>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="thead-light">
                        <tr>
                            <th>Date</th>
                            <th>Asset</th>
                            <th>Type</th>
                            <th>From</th>
                            <th>To</th>
                            <th>By</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php
                            $moveColors = ['incoming' => 'success', 'allocation' => 'primary', 'return' => 'info', 'transfer' => 'warning', 'maintenance' => 'secondary', 'disposal' => 'danger'];
                        @endphp
                        @forelse($recentMovements ?? [] as $movement)
                        <tr>
                            <td>{{ $movement->movement_date ? \Carbon\Carbon::parse($movement->movement_date)->format('M d, Y') : '-' }}</td>
                            <td>
                                @if($movement->fixedAsset)
                                    <a href="{{ route('warehouse.fixed-assets.inventory.movement-history', $movement->fixedAsset->id) }}">
                                        <code>{{ $movement->fixedAsset->asset_code }}</code>
                                        <span class="ml-1">{{ $movement->fixedAsset->asset_name }}</span>
                                    </a>
                                @else
                                    -
                                @endif
                            </td>
                            <td><span class="badge badge-{{ $moveColors[$movement->movement_type] ?? 'secondary' }}">{{ ucfirst($movement->movement_type) }}</span></td>
                            <td>{{ $movement->from_location ?? '-' }}</td>
                            <td>{{ $movement->to_location ?? '-' }}</td>
                            <td>{{ $movement->performedBy ? $movement->performedBy->name : '-' }}</td>
                        </tr>
                        @empty
                        <tr><td colspan="6" class="text-center text-muted py-3">No recent movements.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection

@push('styles')
    <link rel="stylesheet" href="{{ asset('admin-assets/css/warehouse-reports.css') }}">
    <style>
        .progress { border-radius: 10px; }
        .progress-bar { font-size: 11px; line-height: 18px; }
    </style>
@endpush
