@extends('layouts.app')
@section('title',$fixedAsset->asset_number)
@section('content')
<x-ui.page-toolbar :title="$fixedAsset->asset_number" :description="$fixedAsset->name.' · '.$fixedAsset->category" :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Fixed Assets','url'=>route('fixed-assets.index')],['label'=>$fixedAsset->asset_number]]">
<x-slot:actions><a class="mf-btn" href="{{ route('fixed-assets.reports') }}">Asset Reports</a></x-slot:actions>
</x-ui.page-toolbar>

<div class="grid gap-3 md:grid-cols-4 mb-4">
<div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Acquisition cost</div><div class="mt-1 text-xl font-semibold">{{ number_format((float)$fixedAsset->acquisition_cost,2) }}</div></div>
<div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Accumulated depreciation</div><div class="mt-1 text-xl font-semibold">{{ number_format((float)$fixedAsset->accumulated_depreciation,2) }}</div></div>
<div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Book value</div><div class="mt-1 text-xl font-semibold">{{ number_format((float)$fixedAsset->book_value,2) }}</div></div>
<div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Monthly depreciation</div><div class="mt-1 text-xl font-semibold">{{ number_format((float)$monthlyDepreciation,2) }}</div></div>
</div>

<div class="grid gap-4 lg:grid-cols-3">
<div class="lg:col-span-2 space-y-4">
<div class="mf-card"><div class="border-b p-4"><h2 class="font-semibold">Asset register details</h2></div><div class="p-4 grid gap-3 md:grid-cols-3 text-sm">
<div><div class="text-xs text-muted-foreground">Branch</div><div class="font-medium">{{ $fixedAsset->branch->name }}</div></div>
<div><div class="text-xs text-muted-foreground">Status</div><div class="font-medium">{{ \App\Models\FixedAsset::STATUSES[$fixedAsset->status] ?? str($fixedAsset->status)->title() }}</div></div>
<div><div class="text-xs text-muted-foreground">Serial / registration</div><div class="font-medium">{{ $fixedAsset->serial_number ?: '—' }}</div></div>
<div><div class="text-xs text-muted-foreground">Acquired</div><div class="font-medium">{{ $fixedAsset->acquisition_date->format('d M Y') }}</div></div>
<div><div class="text-xs text-muted-foreground">Source</div><div class="font-medium">{{ \App\Models\FixedAsset::ACQUISITION_SOURCES[$fixedAsset->acquisition_source] ?? $fixedAsset->acquisition_source }}</div></div>
<div><div class="text-xs text-muted-foreground">Purchase account</div><div class="font-medium">{{ $fixedAsset->financialAccount?->name ?? 'Opening / existing asset' }}</div></div>
<div><div class="text-xs text-muted-foreground">Depreciation</div><div class="font-medium">{{ \App\Models\FixedAsset::DEPRECIATION_METHODS[$fixedAsset->depreciation_method] ?? $fixedAsset->depreciation_method }}</div></div>
<div><div class="text-xs text-muted-foreground">Useful life</div><div class="font-medium">{{ $fixedAsset->useful_life_months ? $fixedAsset->useful_life_months.' months' : 'Not depreciated' }}</div></div>
<div><div class="text-xs text-muted-foreground">Residual value</div><div class="font-medium">{{ number_format((float)$fixedAsset->residual_value,2) }}</div></div>
<div><div class="text-xs text-muted-foreground">Location</div><div class="font-medium">{{ $fixedAsset->location ?: '—' }}</div></div>
<div><div class="text-xs text-muted-foreground">Custodian</div><div class="font-medium">{{ $fixedAsset->custodian?->name ?? '—' }}</div></div>
<div><div class="text-xs text-muted-foreground">Next maintenance</div><div class="font-medium">{{ $fixedAsset->next_maintenance_date?->format('d M Y') ?? '—' }}</div></div>
@if($fixedAsset->description)<div class="md:col-span-3"><div class="text-xs text-muted-foreground">Description</div><div>{{ $fixedAsset->description }}</div></div>@endif
</div></div>

<div class="mf-card"><div class="border-b p-4"><h2 class="font-semibold">Depreciation history</h2><p class="mt-1 text-xs text-muted-foreground">Each month can be posted only once. The journal debits depreciation expense and credits accumulated depreciation.</p></div>
<div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>Period</th><th class="text-right">Amount</th><th class="text-right">Accumulated</th><th class="text-right">Book value</th><th>Journal</th><th>Posted by</th></tr></thead><tbody>@forelse($fixedAsset->depreciations as $row)<tr><td>{{ $row->period_date->format('M Y') }}</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><td>{{ $row->poster?->name ?? 'System' }}</td></tr>@empty<tr><td colspan="6" class="text-center text-muted-foreground">No depreciation has been posted.</td></tr>@endforelse</tbody></table></div>
@if(auth()->user()->can('fixed-assets.depreciate') && $fixedAsset->isDepreciable())<form method="POST" action="{{ route('fixed-assets.depreciate',$fixedAsset) }}" class="border-t p-4">@csrf<div class="grid gap-2 md:grid-cols-4"><div><label class="mf-field-label">Depreciation month</label><input type="month" class="mf-input" name="period" value="{{ now()->format('Y-m') }}" required></div><div class="md:col-span-2"><label class="mf-field-label">Notes</label><input class="mf-input" name="notes" placeholder="Optional posting note"></div><div class="flex items-end"><button class="mf-btn mf-btn-primary w-full">Post Depreciation</button></div></div></form>@endif
</div>

<div class="mf-card"><div class="border-b p-4"><h2 class="font-semibold">Maintenance history</h2><p class="mt-1 text-xs text-muted-foreground">Paid maintenance reduces the selected branch account and is posted as maintenance expense.</p></div>
<div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>Date</th><th>Type / Description</th><th>Supplier</th><th>Account</th><th class="text-right">Cost</th><th>Next due</th></tr></thead><tbody>@forelse($fixedAsset->maintenances as $row)<tr><td>{{ $row->maintenance_date->format('d M Y') }}<div class="text-xs text-muted-foreground">{{ $row->maintenance_number }}</div></td><td>{{ \App\Models\FixedAssetMaintenance::TYPES[$row->maintenance_type] ?? str($row->maintenance_type)->title() }}<div class="text-xs text-muted-foreground">{{ $row->description }}</div></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="6" class="text-center text-muted-foreground">No maintenance records.</td></tr>@endforelse</tbody></table></div>
@if(auth()->user()->can('fixed-assets.maintenance'))<form method="POST" action="{{ route('fixed-assets.maintenance',$fixedAsset) }}" class="border-t p-4">@csrf<div class="grid gap-2 md:grid-cols-4"><div><label class="mf-field-label">Date</label><input type="date" class="mf-input" name="maintenance_date" value="{{ now()->toDateString() }}" required></div><div><label class="mf-field-label">Type</label><select class="mf-input" name="maintenance_type">@foreach(\App\Models\FixedAssetMaintenance::TYPES as $value=>$label)<option value="{{ $value }}">{{ $label }}</option>@endforeach</select></div><div><label class="mf-field-label">Cost</label><input class="mf-input" name="cost" value="0.00" inputmode="decimal" required></div><div><label class="mf-field-label">Paid from account</label><select class="mf-input" name="financial_account_id"><option value="">No cash cost</option>@foreach($accounts as $account)<option value="{{ $account->id }}">{{ $account->name }} · {{ number_format((float)$account->current_balance,2) }}</option>@endforeach</select></div><div class="md:col-span-2"><label class="mf-field-label">Description</label><input class="mf-input" name="description" required></div><div><label class="mf-field-label">Supplier</label><input class="mf-input" name="supplier"></div><div><label class="mf-field-label">Reference</label><input class="mf-input" name="reference"></div><div><label class="mf-field-label">Next due date</label><input type="date" class="mf-input" name="next_due_date"></div><div class="md:col-span-2"><label class="mf-field-label">Notes</label><input class="mf-input" name="notes"></div><div class="flex items-end"><button class="mf-btn mf-btn-primary w-full">Record Maintenance</button></div></div></form>@endif
</div>
</div>

<div class="space-y-4">
@if(auth()->user()->can('fixed-assets.manage'))<div class="mf-card"><div class="border-b p-4"><h2 class="font-semibold">Update register details</h2><p class="mt-1 text-xs text-muted-foreground">Accounting values are locked after registration. Update operational details only.</p></div><form method="POST" action="{{ route('fixed-assets.update',$fixedAsset) }}" class="p-4 space-y-3">@csrf @method('PUT')<div><label class="mf-field-label">Asset name</label><input class="mf-input" name="name" value="{{ $fixedAsset->name }}" required></div><div><label class="mf-field-label">Category</label><input class="mf-input" name="category" value="{{ $fixedAsset->category }}" required></div><div><label class="mf-field-label">Serial / registration</label><input class="mf-input" name="serial_number" value="{{ $fixedAsset->serial_number }}"></div><div><label class="mf-field-label">Location</label><input class="mf-input" name="location" value="{{ $fixedAsset->location }}"></div><div><label class="mf-field-label">Custodian</label><select class="mf-input" name="custodian_user_id"><option value="">No custodian</option>@foreach($custodians as $user)<option value="{{ $user->id }}" @selected($fixedAsset->custodian_user_id===$user->id)>{{ $user->name }}</option>@endforeach</select></div><div><label class="mf-field-label">Status</label><select class="mf-input" name="status">@foreach(\App\Models\FixedAsset::STATUSES as $value=>$label)<option value="{{ $value }}" @selected($fixedAsset->status===$value)>{{ $label }}</option>@endforeach</select></div><div><label class="mf-field-label">Next maintenance</label><input type="date" class="mf-input" name="next_maintenance_date" value="{{ $fixedAsset->next_maintenance_date?->toDateString() }}"></div><div><label class="mf-field-label">Description</label><textarea class="mf-input" name="description" rows="2">{{ $fixedAsset->description }}</textarea></div><div><label class="mf-field-label">Notes</label><textarea class="mf-input" name="notes" rows="2">{{ $fixedAsset->notes }}</textarea></div><button class="mf-btn w-full">Save Asset Details</button></form></div>@endif

<div class="mf-card"><div class="border-b p-4"><h2 class="font-semibold">Accounting trace</h2></div><div class="p-4 space-y-2 text-sm"><div class="flex justify-between gap-3"><span class="text-muted-foreground">Supplier</span><span>{{ $fixedAsset->supplier ?: '—' }}</span></div><div class="flex justify-between gap-3"><span class="text-muted-foreground">Purchase reference</span><span>{{ $fixedAsset->purchase_reference ?: '—' }}</span></div><div class="flex justify-between gap-3"><span class="text-muted-foreground">Depreciation starts</span><span>{{ $fixedAsset->depreciation_start_date?->format('d M Y') }}</span></div><div class="flex justify-between gap-3"><span class="text-muted-foreground">Original account</span><span>{{ $fixedAsset->financialAccount?->name ?? 'Capital / opening asset' }}</span></div></div></div>
</div>
</div>
@endsection
