@extends('layouts.admin-simple')

@section('title', 'View Fixed Asset')

@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.catalog.index') }}">Catalog</a></li>
    <li class="breadcrumb-item active">{{ $asset->asset_code }}</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->asset_name }}</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">{{ $asset->asset_code }} | {{ $asset->category ? $asset->category->category_name : 'Uncategorized' }}</p>
        </div>
        <div class="page-actions-right d-flex align-items-center">
            @can('warehouse.fixed-assets.edit')
            <a href="{{ route('warehouse.fixed-assets.catalog.edit', $asset->id) }}" class="btn btn-sm btn-warning mr-2">
                <i class="material-icons mr-1" style="font-size: 16px;">edit</i> Edit
            </a>
            @endcan
            <a href="{{ route('warehouse.fixed-assets.catalog.index') }}" class="btn btn-sm btn-outline-secondary">
                <i class="material-icons mr-1" style="font-size: 16px;">arrow_back</i> Back
            </a>
        </div>
    </div>
@endsection

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

    <div class="row">
        <!-- Asset Details -->
        <div class="col-md-8">
            <div class="card mb-3">
                <div class="card-header"><h6 class="mb-0"><i class="material-icons mr-2 align-middle">info</i>Asset Details</h6></div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <table class="table table-sm table-borderless">
                                <tr><td class="text-muted" width="40%">Asset Code</td><td><code>{{ $asset->asset_code }}</code></td></tr>
                                <tr><td class="text-muted">Asset Name</td><td>{{ $asset->asset_name }}</td></tr>
                                <tr><td class="text-muted">Asset Tag</td><td>{{ $asset->asset_tag ?? '-' }}</td></tr>
                                <tr><td class="text-muted">Serial Number</td><td>{{ $asset->serial_number ?? '-' }}</td></tr>
                                <tr><td class="text-muted">Model</td><td>{{ $asset->model_number ?? '-' }}</td></tr>
                                <tr><td class="text-muted">Manufacturer</td><td>{{ $asset->manufacturer ?? '-' }}</td></tr>
                                <tr><td class="text-muted">Category</td><td><span class="badge badge-secondary">{{ $asset->category ? $asset->category->category_name : '-' }}</span></td></tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table class="table table-sm table-borderless">
                                <tr><td class="text-muted" width="40%">Condition</td><td><span class="badge badge-{{ $asset->condition == 'new' ? 'success' : ($asset->condition == 'good' ? 'info' : ($asset->condition == 'fair' ? 'warning' : 'danger')) }}">{{ ucfirst($asset->condition) }}</span></td></tr>
                                <tr><td class="text-muted">Lifecycle</td><td><span class="badge badge-primary">{{ ucfirst(str_replace('_', ' ', $asset->lifecycle_status)) }}</span></td></tr>
                                <tr><td class="text-muted">Status</td><td><span class="badge badge-{{ $asset->status == 'active' ? 'success' : 'secondary' }}">{{ ucfirst($asset->status) }}</span></td></tr>
                                <tr><td class="text-muted">Current Project</td><td>{{ $asset->currentProject ? $asset->currentProject->project_name : '-' }}</td></tr>
                                <tr><td class="text-muted">Location</td><td>{{ $asset->current_location ?? '-' }}</td></tr>
                                <tr><td class="text-muted">Supplier</td><td>{{ $asset->supplier ? $asset->supplier->name : '-' }}</td></tr>
                            </table>
                        </div>
                    </div>
                    @if($asset->notes)
                        <hr>
                        <p class="text-muted mb-1"><strong>Notes:</strong></p>
                        <p>{{ $asset->notes }}</p>
                    @endif
                </div>
            </div>

            <!-- Financial -->
            <div class="card mb-3">
                <div class="card-header"><h6 class="mb-0"><i class="material-icons mr-2 align-middle">attach_money</i>Financial Information</h6></div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4">
                            <table class="table table-sm table-borderless">
                                <tr><td class="text-muted">Purchase Price</td><td class="font-weight-bold">{{ $asset->currency ?? 'AED' }} {{ number_format($asset->purchase_price, 2) }}</td></tr>
                                <tr><td class="text-muted">Purchase Date</td><td>{{ $asset->purchase_date ? $asset->purchase_date->format('M d, Y') : '-' }}</td></tr>
                                <tr><td class="text-muted">Invoice #</td><td>{{ $asset->invoice_number ?? '-' }}</td></tr>
                            </table>
                        </div>
                        <div class="col-md-4">
                            <table class="table table-sm table-borderless">
                                <tr><td class="text-muted">Depreciation Method</td><td>{{ $asset->depreciation_method ? ucfirst(str_replace('_', ' ', $asset->depreciation_method)) : '-' }}</td></tr>
                                <tr><td class="text-muted">Useful Life</td><td>{{ $asset->useful_life_years ? $asset->useful_life_years . ' years' : '-' }}</td></tr>
                                <tr><td class="text-muted">Salvage Value</td><td>{{ $asset->salvage_value ? 'AED ' . number_format($asset->salvage_value, 2) : '-' }}</td></tr>
                            </table>
                        </div>
                        <div class="col-md-4">
                            <table class="table table-sm table-borderless">
                                <tr><td class="text-muted">Current Book Value</td><td class="font-weight-bold text-success">AED {{ number_format($asset->current_book_value, 2) }}</td></tr>
                                <tr><td class="text-muted">Accumulated Depr.</td><td class="text-danger">AED {{ number_format($asset->accumulated_depreciation, 2) }}</td></tr>
                                <tr><td class="text-muted">Monthly Depr.</td><td>AED {{ number_format($asset->monthly_depreciation, 2) }}</td></tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Warranty -->
            @if($asset->warranty_start_date || $asset->warranty_end_date)
            <div class="card mb-3">
                <div class="card-header"><h6 class="mb-0"><i class="material-icons mr-2 align-middle">verified_user</i>Warranty</h6></div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4"><strong class="text-muted">Start:</strong> {{ $asset->warranty_start_date ? $asset->warranty_start_date->format('M d, Y') : '-' }}</div>
                        <div class="col-md-4"><strong class="text-muted">End:</strong> {{ $asset->warranty_end_date ? $asset->warranty_end_date->format('M d, Y') : '-' }}</div>
                        <div class="col-md-4"><strong class="text-muted">Status:</strong>
                            @if($asset->is_under_warranty)
                                <span class="badge badge-success">Under Warranty</span>
                            @else
                                <span class="badge badge-secondary">Expired</span>
                            @endif
                        </div>
                    </div>
                    @if($asset->warranty_terms)
                        <p class="mt-2 mb-0 text-muted">{{ $asset->warranty_terms }}</p>
                    @endif
                </div>
            </div>
            @endif
        </div>

        <!-- Sidebar Summary -->
        <div class="col-md-4">
            <div class="card mb-3">
                <div class="card-header"><h6 class="mb-0">Quick Summary</h6></div>
                <div class="card-body text-center">
                    <h4 class="text-success mb-1">AED {{ number_format($asset->current_book_value, 2) }}</h4>
                    <small class="text-muted">Current Book Value</small>
                    <hr>
                    <div class="row text-center">
                        <div class="col-6">
                            <h5 class="mb-0">{{ $asset->age_in_months }}</h5>
                            <small class="text-muted">Months Old</small>
                        </div>
                        <div class="col-6">
                            <h5 class="mb-0">{{ $movements->count() }}</h5>
                            <small class="text-muted">Movements</small>
                        </div>
                    </div>
                </div>
            </div>

            @if($asset->photo)
            <div class="card mb-3">
                <div class="card-header"><h6 class="mb-0">Photo</h6></div>
                <div class="card-body text-center">
                    <img src="{{ asset('storage/' . $asset->photo) }}" alt="Asset Photo" class="img-fluid rounded" style="max-height: 200px;">
                </div>
            </div>
            @endif
        </div>
    </div>

    <!-- Movement History -->
    <div class="card mb-3">
        <div class="card-header"><h6 class="mb-0"><i class="material-icons mr-2 align-middle">swap_horiz</i>Recent Movements</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>Date</th>
                            <th>Type</th>
                            <th>From</th>
                            <th>To</th>
                            <th>By</th>
                            <th>Notes</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($movements as $movement)
                        <tr>
                            <td>{{ $movement->movement_date ? \Carbon\Carbon::parse($movement->movement_date)->format('M d, Y') : '-' }}</td>
                            <td><span class="badge badge-info">{{ ucfirst(str_replace('_', ' ', $movement->movement_type)) }}</span></td>
                            <td>{{ $movement->from_location ?? '-' }} {{ $movement->fromProject ? '(' . $movement->fromProject->project_name . ')' : '' }}</td>
                            <td>{{ $movement->to_location ?? '-' }} {{ $movement->toProject ? '(' . $movement->toProject->project_name . ')' : '' }}</td>
                            <td>{{ $movement->performedBy ? $movement->performedBy->name : '-' }}</td>
                            <td>{{ Str::limit($movement->notes, 50) }}</td>
                        </tr>
                        @empty
                        <tr><td colspan="6" class="text-center text-muted py-3">No movement records.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Maintenance Records -->
    <div class="card mb-3">
        <div class="card-header"><h6 class="mb-0"><i class="material-icons mr-2 align-middle">build</i>Maintenance Records</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>Number</th><th>Type</th><th>Status</th><th>Date</th><th>Cost</th></tr>
                    </thead>
                    <tbody>
                        @forelse($maintenanceRecords as $record)
                        <tr>
                            <td><a href="{{ route('warehouse.fixed-assets.maintenance.show', $record->id) }}">{{ $record->maintenance_number }}</a></td>
                            <td>{{ ucfirst($record->maintenance_type) }}</td>
                            <td><span class="badge badge-{{ $record->status == 'completed' ? 'success' : ($record->status == 'in_progress' ? 'warning' : 'secondary') }}">{{ ucfirst(str_replace('_', ' ', $record->status)) }}</span></td>
                            <td>{{ $record->scheduled_date ? $record->scheduled_date->format('M d, Y') : '-' }}</td>
                            <td>AED {{ number_format($record->total_cost ?? 0, 2) }}</td>
                        </tr>
                        @empty
                        <tr><td colspan="5" class="text-center text-muted py-3">No maintenance records.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
