@extends('layouts.app')

@section('title', 'My Advances')
@section('heading', 'My Salary Advances')

@section('content')
@php $currency = config('app.currency_symbol', '₹'); @endphp

@include('finance.top-submenu')

    <div class="mb-6 grid gap-4 sm:grid-cols-3">
        <div class="relative overflow-hidden rounded-2xl border border-slate-200/70 bg-white p-5 pl-6 shadow-sm">
            <span class="absolute inset-y-0 left-0 w-1.5 rounded-l-2xl bg-sky-500" aria-hidden="true"></span>
            <p class="text-xs font-semibold uppercase tracking-wider text-slate-500">Outstanding balance</p>
            <p class="mt-2 text-xl font-bold text-slate-900">{{ $currency }}{{ number_format($summary['outstanding'], 2) }}</p>
        </div>
        <div class="relative overflow-hidden rounded-2xl border border-slate-200/70 bg-white p-5 pl-6 shadow-sm">
            <span class="absolute inset-y-0 left-0 w-1.5 rounded-l-2xl bg-amber-500" aria-hidden="true"></span>
            <p class="text-xs font-semibold uppercase tracking-wider text-slate-500">Pending installments</p>
            <p class="mt-2 text-xl font-bold text-slate-900">{{ $summary['pending_installments'] }}</p>
        </div>
        <div class="relative overflow-hidden rounded-2xl border border-slate-200/70 bg-white p-5 pl-6 shadow-sm">
            <span class="absolute inset-y-0 left-0 w-1.5 rounded-l-2xl bg-violet-500" aria-hidden="true"></span>
            <p class="text-xs font-semibold uppercase tracking-wider text-slate-500">Next deduction</p>
            @if($summary['next_deduction'])
                <p class="mt-2 text-xl font-bold text-slate-900">{{ $currency }}{{ number_format((float) $summary['next_deduction']->amount, 2) }}</p>
                <p class="mt-1 text-xs text-slate-500">{{ date('M Y', mktime(0, 0, 0, $summary['next_deduction']->month, 1, $summary['next_deduction']->year)) }}</p>
            @else
                <p class="mt-2 text-lg font-medium text-slate-500">—</p>
            @endif
        </div>
    </div>

    @if($plans->isNotEmpty())
        <x-card class="mb-6">
            <h2 class="mb-3 text-sm font-semibold text-slate-800">Active repayment plans</h2>
            <div class="grid gap-3 sm:grid-cols-2">
                @foreach($plans as $plan)
                    <div class="rounded-lg border border-slate-200 bg-slate-50/80 p-4 text-sm">
                        <p class="font-medium text-slate-900">
                            @if($plan->advance_date)
                                Advance on {{ $plan->advance_date->format('d M Y') }}
                            @else
                                Salary advance
                            @endif
                        </p>
                        <p class="mt-1 text-slate-600">
                            {{ $currency }}{{ number_format($plan->remaining, 2) }} remaining
                            of {{ $currency }}{{ number_format($plan->total_amount, 2) }}
                        </p>
                        <p class="mt-1 text-xs text-slate-500">{{ $plan->pending_of }} pending installment(s)</p>
                    </div>
                @endforeach
            </div>
        </x-card>
    @endif

    <x-card>
        <p class="mb-4 text-sm text-slate-600">Read-only schedule — amounts deducted through monthly payroll.</p>

        <x-table>
            <x-slot name="head">
                <tr>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Advance date</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Deduction month</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Amount</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Plan</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Status</th>
                </tr>
            </x-slot>
            @forelse($advances as $advance)
                <tr>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ $advance->advance_date?->format('d M Y') ?? '—' }}</td>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ date('M Y', mktime(0, 0, 0, $advance->month, 1, $advance->year)) }}</td>
                    <td class="px-4 py-3 text-sm font-medium text-slate-900">{{ number_format((float) $advance->amount, 2) }}</td>
                    <td class="px-4 py-3 text-sm text-slate-600">
                        @if($advance->isInstallmentPlan())
                            {{ $advance->installmentLabel() }} of {{ number_format((float) $advance->total_amount, 2) }}
                        @else
                            Single
                        @endif
                    </td>
                    <td class="px-4 py-3 text-sm capitalize">{{ $advance->status }}</td>
                </tr>
            @empty
                <tr><td colspan="5" class="p-0">
                    @include('finance.partials.empty-state', [
                        'title' => 'No advance repayments',
                        'description' => 'When HR records a salary advance, your payroll deduction schedule will appear here (read-only).',
                        'icon' => 'bi-calendar-check',
                    ])
                </td></tr>
            @endforelse
        </x-table>

        @if($advances->hasPages())
            <div class="mt-4">{{ $advances->links() }}</div>
        @endif
    </x-card>
@endsection
