@extends('layouts.app')

@section('title', 'Salary Breakup')
@section('heading', 'Salary Breakup')

@section('content')
<x-card>
    @if($employee->latestEffectiveSalaryRevision() && $employee->latestEffectiveSalaryRevision()?->ctc_annual > 0)
        <div class="flex flex-wrap items-center justify-between gap-2">
            <p class="text-base text-slate-800">Salary Breakup</p>
            <p class="text-sm text-slate-700">Effective Date: <span class="font-semibold">{{ $salaryBreakup['effective_date'] ?? '—' }}</span></p>
        </div>                
        <div class="mt-3 overflow-hidden rounded border border-slate-400 bg-white">
            <table class="min-w-full divide-y divide-slate-200">
                <thead class="bg-primary text-white">
                    <tr>
                        <th class="px-3 py-2 text-left text-xs font-medium font-semibold uppercase tracking-wide">Earnings</th>
                        <th class="px-3 py-2 text-right text-xs font-medium font-semibold uppercase tracking-wide">Monthly</th>
                        <th class="px-3 py-2 text-right text-xs font-medium font-semibold uppercase tracking-wide">Annually</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-200">
                    @if(!empty($componentDetails))
                        <tr class="text-slate-700">
                            <td class="px-3 py-2 text-sm">
                                <span class="font-medium text-slate-700">Salary Group Components</span>
                                <p class="text-xs text-slate-500">{{ implode(', ', $componentDetails) }}</p>
                            </td>
                            <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums">₹{{ number_format($salaryBreakup['salary_group_monthly'], 2) }}</td>
                            <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums">₹{{ number_format($salaryBreakup['salary_group_annual'], 2) }}</td>
                        </tr>
                    @endif
                    @forelse($salaryBreakup['earning_rows'] as $row)
                        <tr>
                            <td class="px-3 py-2 text-sm text-slate-700">{{ $row['name'] }}</td>
                            <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums text-slate-900">₹{{ number_format($row['monthly'], 2) }}</td>
                            <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums text-slate-900">₹{{ number_format($row['annual'], 2) }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="3" class="px-3 py-4 text-center text-sm text-slate-500">No earning components in salary structure.</td>
                        </tr>
                    @endforelse
                    <tr class="font-medium text-slate-800 {{!count($salaryBreakup['employer_rows']) > 0 ? 'bg-slate-300' : 'bg-slate-50/80'}}">
                        <td class="px-3 py-2 text-sm">@if(!count($salaryBreakup['employer_rows']) > 0) Total Earnings (CTC) @else Sub Total @endif</td>
                        <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums">₹{{ number_format($salaryBreakup['subtotal_monthly'], 2) }}</td>
                        <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums">₹{{ number_format($salaryBreakup['subtotal_annual'], 2) }}</td>
                    </tr>
                    @foreach($salaryBreakup['employer_rows'] as $row)
                        <tr class="text-slate-700">
                            <td class="px-3 py-2 text-sm">
                                <span>{{ $row['name'] }}</span>
                            </td>
                            <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums">₹{{ number_format($row['monthly'], 2) }}</td>
                            <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums">₹{{ number_format($row['annual'], 2) }}</td>
                        </tr>
                    @endforeach
                    @if(count($salaryBreakup['employer_rows']) > 0)
                    <tr class="font-medium text-slate-800 bg-primary-light">
                        <td class="px-3 py-2 text-sm">Total Earnings (CTC)</td>
                        <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums">₹{{ number_format($salaryBreakup['total_earnings_monthly'], 2) }}</td>
                        <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums">₹{{ number_format($salaryBreakup['total_earnings_annual'], 2) }}</td>
                    </tr>
                    @endif

                    <tr class="bg-slate-50/80">
                        <td class="px-3 py-2 text-xs font-semibold uppercase tracking-wide  text-slate-800">Deductions</td>
                        <th class="px-3 py-2 text-right text-xs font-medium font-semibold uppercase tracking-wide text-slate-500">Monthly</th>
                        <th class="px-3 py-2 text-right text-xs font-medium font-semibold uppercase tracking-wide text-slate-500">Annually</th>
                    </tr>
                    @forelse($salaryBreakup['deduction_rows'] as $row)
                        <tr>
                            <td class="px-3 py-2 text-sm text-slate-700">{{ $row['name'] }}</td>
                            <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums text-slate-900">₹{{ number_format($row['monthly'], 2) }}</td>
                            <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums text-slate-900">₹{{ number_format($row['annual'], 2) }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="3" class="px-3 py-2 text-sm text-slate-500">No deductions.</td>
                        </tr>
                    @endforelse
                    <tr class="font-medium text-slate-800">
                        <td class="px-3 py-2 text-sm">Total Deductions</td>
                        <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums">₹{{ number_format($salaryBreakup['total_deductions_monthly'], 2) }}</td>
                        <td class="whitespace-nowrap px-3 py-2 text-right text-sm tabular-nums">₹{{ number_format($salaryBreakup['total_deductions_annual'], 2) }}</td>
                    </tr>

                    <tr class="bg-primary-light">
                        <td class="px-3 py-2 text-sm font-bold uppercase tracking-wide text-slate-800">Net Pay</td>
                        <td class="whitespace-nowrap px-3 py-2 text-right text-sm font-bold uppercase tracking-wide text-slate-800">₹{{ number_format($salaryBreakup['net_monthly'], 2) }}</td>
                        <td class="whitespace-nowrap px-3 py-2 text-right text-sm font-bold uppercase tracking-wide text-slate-800">₹{{ number_format($salaryBreakup['net_annual'], 2) }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class="mt-4 flex items-start gap-2 text-xs text-slate-500 bg-green-100 rounded p-3">
            <svg class="mt-0.5 h-4 w-4 shrink-0 text-amber-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true">
                <path stroke-linecap="round" stroke-linejoin="round" d="M12 18v-5.25m0 0a6.01 6.01 0 001.5-.189m-1.5.189a6.01 6.01 0 01-1.5-.189m3.75 7.478a12.06 12.06 0 01-4.5 0m3.75 2.383a14.406 14.406 0 01-3 0M14.25 18v-.192c0-.983.658-1.823 1.508-2.316a7.5 7.5 0 10-7.517 0c.85.493 1.509 1.333 1.509 2.316V18" />
            </svg>
            <p><span class="font-medium text-slate-600">Note:</span> Figures are indicative for a full month (no attendance adjustments). Salary group components are shown first, followed by statutory PF/ESI/PT as per company settings. Net pay does not include income tax or other statutory/ad-hoc deductions that may apply at payroll.</p>
        </div>
    @else
        <div class="mt-3 overflow-hidden rounded border border-slate-300 bg-white">
            <table class="min-w-full">
                <tbody>
                    <tr class="border-t border-slate-200">
                        <td colspan="3" class="px-3 py-4 text-center text-sm text-slate-500">No salary structure available.</td>
                    </tr>
                </tbody>
            </table>
        </div>
    @endif
</x-card>
@endsection