@extends('layouts.app')
@section('title','Fixed Asset Reports')
@section('content')
<x-ui.page-toolbar title="Fixed Asset Reports" description="Asset register, depreciation and maintenance reporting across accessible branches." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Fixed Assets','url'=>route('fixed-assets.index')],['label'=>'Reports']]">
<x-slot:actions>@can('fixed-assets.reports.export')<a class="mf-btn" href="{{ route('fixed-assets.reports.export',request()->query()) }}">Download CSV</a>@endcan<button type="button" class="mf-btn" onclick="window.print()">Print</button></x-slot:actions>
</x-ui.page-toolbar>

<div class="mf-card mf-card-body mb-4 print:hidden">
<div class="flex flex-wrap gap-2 mb-3">@foreach($reports as $slug=>$label)<a class="mf-btn {{ $report===$slug?'mf-btn-primary':'' }}" href="{{ route('fixed-assets.reports',array_merge(request()->except('report'),['report'=>$slug])) }}">{{ $label }}</a>@endforeach</div>
<form method="GET" class="grid gap-2 md:grid-cols-6"><input type="hidden" name="report" value="{{ $report }}"><div><label class="mf-field-label">From</label><input type="date" class="mf-input" name="from" value="{{ $from }}"></div><div><label class="mf-field-label">To</label><input type="date" class="mf-input" name="to" value="{{ $to }}"></div><div class="md:col-span-3"><label class="mf-field-label">Branches</label><select class="mf-input min-h-24" name="branch_ids[]" multiple>@foreach($branches as $branch)<option value="{{ $branch->id }}" @selected($selectedBranchIds->contains($branch->id))>{{ $branch->name }}</option>@endforeach</select><div class="text-[11px] text-muted-foreground mt-1">Clear selection for all accessible branches.</div></div><div class="flex items-end"><button class="mf-btn mf-btn-primary w-full">Run Report</button></div></form>
</div>

<div class="grid gap-3 md:grid-cols-5 mb-4"><div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Assets</div><div class="text-lg font-semibold">{{ number_format($data['asset_count']) }}</div></div><div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Acquisition cost</div><div class="text-lg font-semibold">{{ number_format((float)$data['total_cost'],2) }}</div></div><div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Accum. depreciation</div><div class="text-lg font-semibold">{{ number_format((float)$data['accumulated_depreciation'],2) }}</div></div><div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Net book value</div><div class="text-lg font-semibold">{{ number_format((float)$data['net_book_value'],2) }}</div></div><div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Maintenance in period</div><div class="text-lg font-semibold">{{ number_format((float)$data['period_maintenance'],2) }}</div></div></div>

<div class="mb-3"><div class="text-lg font-semibold">{{ $reports[$report] }}</div><div class="text-xs text-muted-foreground">{{ \Carbon\Carbon::parse($from)->format('d M Y') }} — {{ \Carbon\Carbon::parse($to)->format('d M Y') }} · {{ $selectedBranches->pluck('name')->join(', ') }}</div></div>

@if($report==='register')
<div class="mf-card"><div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>Asset</th><th>Category</th><th>Branch</th><th>Acquired</th><th class="text-right">Cost</th><th class="text-right">Accum. Dep.</th><th class="text-right">Book Value</th><th>Status</th></tr></thead><tbody>@forelse($data['rows'] as $asset)<tr><td><a class="text-primary hover:underline" href="{{ route('fixed-assets.show',$asset) }}">{{ $asset->asset_number }}</a><div class="text-xs">{{ $asset->name }}</div></td><td>{{ $asset->category }}</td><td>{{ $asset->branch->name }}</td><td>{{ $asset->acquisition_date->format('d M Y') }}</td><td class="text-right">{{ number_format((float)$asset->acquisition_cost,2) }}</td><td class="text-right">{{ number_format((float)$asset->accumulated_depreciation,2) }}</td><td class="text-right font-medium">{{ number_format((float)$asset->book_value,2) }}</td><td>{{ \App\Models\FixedAsset::STATUSES[$asset->status] ?? $asset->status }}</td></tr>@empty<tr><td colspan="8" class="text-center text-muted-foreground">No assets in this scope.</td></tr>@endforelse</tbody></table></div></div>
@elseif($report==='depreciation')
<div class="mf-card mf-card-body mb-3"><div class="text-xs text-muted-foreground">Depreciation posted in period</div><div class="text-xl font-semibold">{{ number_format((float)$data['period_depreciation'],2) }}</div></div>
<div class="mf-card"><div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>Period</th><th>Asset</th><th>Branch</th><th class="text-right">Amount</th><th class="text-right">Accumulated</th><th class="text-right">Book Value</th><th>Journal</th></tr></thead><tbody>@forelse($data['rows'] as $row)<tr><td>{{ $row->period_date->format('M Y') }}</td><td>{{ $row->asset->asset_number }}<div class="text-xs">{{ $row->asset->name }}</div></td><td>{{ $row->asset->branch->name }}</td><td class="text-right">{{ number_format((float)$row->amount,2) }}</td><td class="text-right">{{ number_format((float)$row->accumulated_after,2) }}</td><td class="text-right font-medium">{{ number_format((float)$row->book_value_after,2) }}</td><td>{{ $row->journal_number }}</td></tr>@empty<tr><td colspan="7" class="text-center text-muted-foreground">No depreciation postings in this period.</td></tr>@endforelse</tbody></table></div></div>
@else
<div class="mf-card"><div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>Date</th><th>Maintenance</th><th>Asset</th><th>Branch</th><th>Supplier</th><th>Account</th><th class="text-right">Cost</th><th>Next Due</th></tr></thead><tbody>@forelse($data['rows'] as $row)<tr><td>{{ $row->maintenance_date->format('d M Y') }}</td><td>{{ $row->maintenance_number }}<div class="text-xs text-muted-foreground">{{ \App\Models\FixedAssetMaintenance::TYPES[$row->maintenance_type] ?? $row->maintenance_type }} · {{ $row->description }}</div></td><td>{{ $row->asset->asset_number }} · {{ $row->asset->name }}</td><td>{{ $row->branch->name }}</td><td>{{ $row->supplier ?: '—' }}</td><td>{{ $row->financialAccount?->name ?? 'No cash cost' }}</td><td class="text-right">{{ number_format((float)$row->cost,2) }}</td><td>{{ $row->next_due_date?->format('d M Y') ?? '—' }}</td></tr>@empty<tr><td colspan="8" class="text-center text-muted-foreground">No maintenance records in this period.</td></tr>@endforelse</tbody></table></div></div>
@endif

@if($data['by_category']->isNotEmpty())<div class="mf-card mt-4"><div class="border-b p-4"><h2 class="font-semibold">Assets by category</h2></div><div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>Category</th><th>Assets</th><th class="text-right">Book Value</th></tr></thead><tbody>@foreach($data['by_category'] as $row)<tr><td>{{ $row['category'] }}</td><td>{{ $row['count'] }}</td><td class="text-right">{{ number_format((float)$row['book_value'],2) }}</td></tr>@endforeach</tbody></table></div></div>@endif
@endsection
@push('styles')<style>@media print{.mf-sidebar,.mf-topbar,.print\:hidden{display:none!important}.mf-content{margin:0!important}.mf-page{padding:0!important}.mf-card{box-shadow:none!important}}</style>@endpush