<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Payroll;
use App\Models\PayrollItem;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Http\Response;
use Inertia\Inertia;

class PayslipController extends Controller
{
    public function show(Payroll $payroll, PayrollItem $payrollItem): \Inertia\Response
    {
        return Inertia::render('Admin/Payroll/Payslip', [
            'item' => $payrollItem->load(['employee.employeeProfile', 'payroll']),
        ]);
    }

    public function download(Payroll $payroll, PayrollItem $payrollItem): Response
    {
        $item = $payrollItem->load(['employee.employeeProfile', 'payroll']);
        $companyName = config('branding.theme.app_name', config('app.name'));

        $pdf = Pdf::loadView('pdf.payslip', [
            'item' => $item,
            'companyName' => $companyName,
        ])->setPaper('a4');

        $filename = 'payslip_'.$item->employee->name.'_'.$item->payroll->month->format('Y-m').'.pdf';

        return $pdf->download($filename);
    }
}
