@extends('layouts.app')

@section('title', 'Payslip')
@section('heading', 'Payslip')

@section('content')
@php
    $cycle = $employeePayroll->payrollCycle;
    $employee = $employeePayroll->user;
    $company = $employee?->company;
    $netPay = (float) ($employeePayroll->net_monthly ?? 0);
    $toWords = function (float $amount): string {
        $value = round(max(0, $amount), 2);
        $rupees = (int) floor($value);
        $paise = (int) round(($value - $rupees) * 100);
        if (class_exists(\NumberFormatter::class)) {
            $formatter = new \NumberFormatter('en_IN', \NumberFormatter::SPELLOUT);
            $rupeesWords = ucfirst(trim((string) $formatter->format($rupees)));
            $words = $rupeesWords . ' rupees';
            if ($paise > 0) {
                $paiseWords = trim((string) $formatter->format($paise));
                $words .= ' and ' . $paiseWords . ' paise';
            }
            return $words . ' only';
        }
        return number_format($value, 2) . ' rupees only';
    };

    $preview = [
        'source' => 'v2',
        'source_id' => (int) $employeePayroll->id,
        'source_year' => (int) ($cycle?->year ?? 0),
        'period' => ($cycle && (int) $cycle->year > 0 && (int) $cycle->month > 0)
            ? \Carbon\Carbon::createFromDate((int) $cycle->year, (int) $cycle->month, 1)->format('F Y')
            : 'N/A',
        'view_url' => route('payroll.my.payslips.show', $employeePayroll),
        'pdf_url' => route('payroll.my.payslips.download', $employeePayroll),
        'company' => [
            'name' => $company?->name ?? config('app.name'),
            'address' => $company?->address,
            'email' => $company?->email,
            'phone' => $company?->phone,
            'logo_url' => !empty($company?->logo) ? asset('storage/' . ltrim((string) $company->logo, '/')) : null,
        ],
        'employee' => [
            'name' => $employee?->name ?? 'Employee',
            'employee_id' => $employee?->employee_number ?: ('EMP-' . str_pad((string) ($employee?->id ?? 0), 3, '0', STR_PAD_LEFT)),
            'designation' => $employee?->designation?->name ?? 'N/A',
            'department' => $employee?->department?->name ?? 'N/A',
            'location' => $employee?->location?->name ?? 'N/A',
            'bank_name' => $employee?->bank_name ?? data_get($employee, 'bankName') ?? 'N/A',
            'bank_account_no' => $employee?->account_number ?? data_get($employee, 'bank_account_no') ?? data_get($employee, 'bankAccountNo') ?? 'N/A',
            'ifsc_code' => $employee?->ifsc_code ?? data_get($employee, 'ifscCode') ?? 'N/A',
            'pan_no' => data_get($employee, 'pan_no') ?? data_get($employee, 'pan_number') ?? data_get($employee, 'panNo') ?? 'N/A',
            'work_days' => (int) ($employeePayroll->working_days ?? 0),
            'lop_days' => (float) ($employeePayroll->lop_days ?? 0),
        ],
        'earnings' => $employeePayroll->components->where('category', 'earning')->map(fn ($c) => [
            'name' => (string) ($c->name ?? 'Earning'),
            'amount' => (float) ($c->amount ?? 0),
        ])->values()->all(),
        'deductions' => $employeePayroll->components->where('category', 'deduction')->map(fn ($c) => [
            'name' => (string) ($c->name ?? 'Deduction'),
            'amount' => abs((float) ($c->amount ?? 0)),
        ])->values()->all(),
        'totals' => [
            'earnings' => (float) ($employeePayroll->gross_monthly ?? 0),
            'deductions' => (float) ($employeePayroll->total_deductions ?? abs($employeePayroll->components->where('category', 'deduction')->sum('amount'))),
            'net' => $netPay,
            'net_in_words' => $toWords($netPay),
        ],
    ];
@endphp

<div class="max-w-4xl space-y-6">
    @include('payroll.my.partials.payslip-preview', ['preview' => $preview, 'showActions' => true])
    <div class="flex justify-end">
        <a href="{{ route('payroll.index', ['tab' => 'my', 'my_sub' => 'payslips', 'year' => (int) ($cycle?->year ?? date('Y')), 'source' => 'v2', 'payslip_id' => (int) $employeePayroll->id]) }}" class="rounded-lg border border-slate-300 bg-white px-3 py-1.5 text-sm font-medium text-slate-700 hover:bg-slate-50">Back to My Payslips</a>
    </div>
</div>
@endsection

