@extends('layouts.app')

@section('title', 'F&F Settlement')
@section('heading', 'Full & Final Settlement')

@section('content')
@include('hr.top-submenu')

@php
    $breakdown = $fnf->breakdown_json ?? [];
    $statusLabels = [
        'draft' => 'Draft',
        'pending_approval' => 'Pending approval',
        'approved' => 'Approved',
        'paid' => 'Paid',
        'cancelled' => 'Cancelled',
    ];
@endphp

<div class="max-w-5xl space-y-6">
    @if(session('success'))
        <div class="rounded-lg border border-emerald-200 bg-emerald-50 px-4 py-3 text-sm text-emerald-800">{{ session('success') }}</div>
    @endif
    @if(session('error'))
        <div class="rounded-lg border border-red-200 bg-red-50 px-4 py-3 text-sm text-red-800">{{ session('error') }}</div>
    @endif

    <div class="flex flex-wrap items-start justify-between gap-4">
        <div>
            <h1 class="text-xl font-bold text-slate-900">{{ $fnf->user?->name }}</h1>
            <p class="mt-1 text-sm text-slate-600">F&F worksheet · LWD {{ $fnf->last_working_date?->format('d M Y') ?? '—' }}</p>
        </div>
        <div class="flex flex-wrap gap-2">
            <a href="{{ route('fnf.download', $fnf) }}" class="rounded-lg border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700">Download PDF</a>
            @if($fnf->offboarding)
                <a href="{{ route('offboardings.show', $fnf->offboarding) }}" class="rounded-lg border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700">Clearance checklist</a>
            @endif
        </div>
    </div>

    <div class="grid gap-4 sm:grid-cols-4">
        <x-card><p class="text-xs text-slate-500">Status</p><p class="mt-1 font-semibold">{{ $statusLabels[$fnf->status] ?? $fnf->status }}</p></x-card>
        <x-card><p class="text-xs text-slate-500">Daily rate</p><p class="mt-1 font-semibold">₹{{ number_format((float) $fnf->daily_rate, 2) }}</p></x-card>
        <x-card><p class="text-xs text-slate-500">Total earnings</p><p class="mt-1 font-semibold text-emerald-700">₹{{ number_format((float) $fnf->total_earnings, 2) }}</p></x-card>
        <x-card><p class="text-xs text-slate-500">Net payable</p><p class="mt-1 text-lg font-bold text-slate-900">₹{{ number_format((float) $fnf->net_payable, 2) }}</p></x-card>
    </div>

    @if($fnf->isEditable())
        <x-card>
            <h2 class="mb-4 text-lg font-semibold text-slate-900">Edit worksheet</h2>
            <form action="{{ route('fnf.update', $fnf) }}" method="post" class="grid gap-4 sm:grid-cols-2">
                @csrf
                @method('PUT')
                <div>
                    <label class="block text-sm text-slate-700">Notice period (days)</label>
                    <input type="number" name="notice_period_days" value="{{ $fnf->notice_period_days }}" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-sm text-slate-700">Notice served (days)</label>
                    <input type="number" name="notice_served_days" value="{{ $fnf->notice_served_days }}" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-sm text-slate-700">Notice pay (₹)</label>
                    <input type="number" step="0.01" name="notice_pay_amount" value="{{ $fnf->notice_pay_amount }}" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-sm text-slate-700">Leave encashment days</label>
                    <input type="number" step="0.01" name="leave_encashment_days" value="{{ $fnf->leave_encashment_days }}" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-sm text-slate-700">Salary dues (₹)</label>
                    <input type="number" step="0.01" name="salary_dues_amount" value="{{ $fnf->salary_dues_amount }}" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-sm text-slate-700">Bonus / gratuity (₹)</label>
                    <input type="number" step="0.01" name="bonus_gratuity_amount" value="{{ $fnf->bonus_gratuity_amount }}" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-sm text-slate-700">Other earnings (₹)</label>
                    <input type="number" step="0.01" name="other_earnings" value="{{ $fnf->other_earnings }}" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-sm text-slate-700">Loan recovery (₹)</label>
                    <input type="number" step="0.01" name="loan_recovery" value="{{ $fnf->loan_recovery }}" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-sm text-slate-700">Asset recovery (₹)</label>
                    <input type="number" step="0.01" name="asset_recovery" value="{{ $fnf->asset_recovery }}" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-sm text-slate-700">Other recovery (₹)</label>
                    <input type="number" step="0.01" name="other_recovery" value="{{ $fnf->other_recovery }}" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">
                </div>
                <div class="sm:col-span-2">
                    <label class="block text-sm text-slate-700">Notes</label>
                    <textarea name="notes" rows="2" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">{{ $fnf->notes }}</textarea>
                </div>
                <div class="sm:col-span-2 flex flex-wrap gap-2">
                    <button type="submit" class="rounded-lg bg-slate-800 px-4 py-2 text-sm font-medium text-white">Recalculate</button>
                </div>
            </form>
            <form action="{{ route('fnf.recalculate', $fnf) }}" method="post" class="mt-2">
                @csrf
                <button type="submit" class="rounded-lg border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700">Refresh from leave balances</button>
            </form>
        </x-card>
    @endif

    <x-card>
        <h2 class="mb-4 text-lg font-semibold text-slate-900">Settlement breakdown</h2>
        <div class="grid gap-6 lg:grid-cols-2">
            <div>
                <h3 class="mb-2 text-sm font-semibold text-emerald-800">Earnings</h3>
                <dl class="space-y-2 text-sm">
                    <div class="flex justify-between"><dt>Notice pay</dt><dd>₹{{ number_format((float) $fnf->notice_pay_amount, 2) }}</dd></div>
                    <div class="flex justify-between"><dt>Leave encashment ({{ $fnf->leave_encashment_days }} days)</dt><dd>₹{{ number_format((float) $fnf->leave_encashment_amount, 2) }}</dd></div>
                    <div class="flex justify-between"><dt>Salary dues</dt><dd>₹{{ number_format((float) $fnf->salary_dues_amount, 2) }}</dd></div>
                    <div class="flex justify-between"><dt>Bonus / gratuity</dt><dd>₹{{ number_format((float) $fnf->bonus_gratuity_amount, 2) }}</dd></div>
                    <div class="flex justify-between"><dt>Other earnings</dt><dd>₹{{ number_format((float) $fnf->other_earnings, 2) }}</dd></div>
                    <div class="flex justify-between border-t border-slate-200 pt-2 font-semibold"><dt>Total earnings</dt><dd>₹{{ number_format((float) $fnf->total_earnings, 2) }}</dd></div>
                </dl>
            </div>
            <div>
                <h3 class="mb-2 text-sm font-semibold text-red-800">Recoveries</h3>
                <dl class="space-y-2 text-sm">
                    <div class="flex justify-between"><dt>Notice shortfall recovery</dt><dd>₹{{ number_format((float) $fnf->notice_recovery_amount, 2) }}</dd></div>
                    <div class="flex justify-between"><dt>Loan recovery</dt><dd>₹{{ number_format((float) $fnf->loan_recovery, 2) }}</dd></div>
                    <div class="flex justify-between"><dt>Asset recovery</dt><dd>₹{{ number_format((float) $fnf->asset_recovery, 2) }}</dd></div>
                    <div class="flex justify-between"><dt>Other recovery</dt><dd>₹{{ number_format((float) $fnf->other_recovery, 2) }}</dd></div>
                    <div class="flex justify-between border-t border-slate-200 pt-2 font-semibold"><dt>Total recovery</dt><dd>₹{{ number_format((float) $fnf->total_recovery, 2) }}</dd></div>
                </dl>
            </div>
        </div>

        @if(!empty($breakdown['leave_encashment']['lines']))
            <div class="mt-6">
                <h3 class="mb-2 text-sm font-semibold text-slate-800">Leave encashment detail</h3>
                <table class="min-w-full text-sm">
                    <thead><tr class="border-b text-left text-slate-500"><th class="pb-2">Leave type</th><th class="pb-2">Days</th></tr></thead>
                    <tbody>
                        @foreach($breakdown['leave_encashment']['lines'] as $line)
                            <tr class="border-b border-slate-100"><td class="py-2">{{ $line['name'] }}</td><td class="py-2">{{ $line['days'] }}</td></tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @endif
    </x-card>

    <div class="flex flex-wrap gap-2">
        @if(in_array($fnf->status, ['draft', 'pending_approval'], true))
            <form action="{{ route('fnf.approve', $fnf) }}" method="post">@csrf<button type="submit" class="rounded-lg bg-emerald-600 px-4 py-2 text-sm font-medium text-white">Approve settlement</button></form>
        @endif
        @if($fnf->status === 'approved')
            <form action="{{ route('fnf.mark-paid', $fnf) }}" method="post" onsubmit="return confirm('Mark F&F as paid? This will encash leave and deactivate the employee.');">
                @csrf
                <button type="submit" class="rounded-lg bg-primary px-4 py-2 text-sm font-medium text-white">Mark as paid</button>
            </form>
        @endif
    </div>
</div>
@endsection
