@extends('layouts.aura-main')

@section('main-content')
<div class="max-w-7xl mx-auto pb-20">
    <div class="flex justify-between items-end mb-8">
        <div>
            <h1 class="text-2xl font-black text-slate-900 tracking-tighter uppercase">Fixed Asset Register</h1>
            <p class="text-[10px] font-bold text-slate-400 tracking-widest mt-1 italic uppercase">Management of long-term investments & depreciation</p>
        </div>
        <div class="flex gap-3">
            <form action="{{ route('assets.depreciate') }}" method="POST">
                @csrf
                <button type="submit" class="bg-indigo-600 text-white px-6 py-2.5 text-[10px] font-black tracking-widest shadow-xl hover:bg-slate-900 transition-all uppercase">
                    Run Monthly Depreciation
                </button>
            </form>
            <a href="{{ route('assets.create') }}" class="bg-slate-900 text-white px-6 py-2.5 text-[10px] font-black tracking-widest shadow-xl hover:bg-indigo-600 transition-all uppercase">
                + Register New Asset
            </a>
        </div>
    </div>

    <div class="bg-white border border-slate-200 rounded-sm shadow-sm overflow-hidden">
        <table class="w-full text-left border-collapse">
            <thead>
                <tr class="border-b border-slate-900 bg-slate-50 text-[9px] font-black text-slate-500 tracking-widest uppercase">
                    <th class="px-6 py-4">Asset Detail</th>
                    <th class="px-6 py-4">Acquisition</th>
                    <th class="px-6 py-4 text-right">Original Cost</th>
                    <th class="px-6 py-4 text-right">Accum. Dep.</th>
                    <th class="px-6 py-4 text-right">Book Value</th>
                    <th class="px-6 py-4">Life Progress</th>
                    <th class="px-6 py-4 text-center">Status</th>
                </tr>
            </thead>
            <tbody class="text-[11px] font-medium divide-y divide-slate-100 tracking-tight">
                @forelse($assets as $asset)
                @php
                    $accumulated = $asset->depreciationLogs->sum('depreciation_amount');
                    $bookValue = $asset->purchase_cost - $accumulated;
                    $percent = ($asset->purchase_cost > 0) ? ($accumulated / ($asset->purchase_cost - $asset->salvage_value)) * 100 : 0;
                @endphp
                <tr class="hover:bg-slate-50/50 transition">
                    <td class="px-6 py-4">
                        <span class="block font-black text-slate-900 uppercase">{{ $asset->name }}</span>
                        <span class="text-[9px] font-mono text-slate-400">{{ $asset->asset_number }}</span>
                    </td>
                    <td class="px-6 py-4 text-slate-500">
                        {{ \Carbon\Carbon::parse($asset->purchase_date)->format('d M Y') }}
                    </td>
                    <td class="px-6 py-4 text-right font-mono text-slate-600">
                        {{ number_format($asset->purchase_cost, 2) }}
                    </td>
                    <td class="px-6 py-4 text-right font-mono text-rose-600">
                        ({{ number_format($accumulated, 2) }})
                    </td>
                    <td class="px-6 py-4 text-right font-mono font-black text-slate-900">
                        {{ number_format($bookValue, 2) }}
                    </td>
                    <td class="px-6 py-4">
                        <div class="w-full bg-slate-100 h-1.5 rounded-full overflow-hidden">
                            <div class="bg-indigo-500 h-full" style="width: {{ min($percent, 100) }}%"></div>
                        </div>
                        <span class="text-[8px] text-slate-400 font-bold uppercase mt-1 block">{{ round($percent) }}% Depreciated</span>
                    </td>
                    <td class="px-6 py-4 text-center">
                        @if($asset->status === 'active')
                            <span class="text-emerald-600 font-black tracking-widest bg-emerald-50 px-2 py-0.5 rounded-full text-[8px] uppercase">Active</span>
                        @else
                            <span class="text-slate-500 font-black tracking-widest bg-slate-50 px-2 py-0.5 rounded-full text-[8px] uppercase">{{ $asset->status }}</span>
                        @endif
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="7" class="px-6 py-20 text-center uppercase">
                        <p class="text-[10px] font-black text-slate-300 tracking-widest">No fixed assets registered in the system.</p>
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="mt-6">
        {{ $assets->links() }}
    </div>
</div>
@endsection