<?php
require __DIR__ . '/vendor/autoload.php';

use setasign\Fpdi\Fpdi;

// Get input values from the form submission
$inputNumber = isset($_POST['inputNumber']) ? $_POST['inputNumber'] : '';
$result = isset($_POST['result']) ? $_POST['result'] : '';

// Path to the existing PDF
$pdfPath = __DIR__ . '/assets/Ombrella_sample_payslip.pdf';

// Check if the file exists
if (!file_exists($pdfPath)) {
    die('PDF file does not exist.');
}

// Create the 'public' directory if it doesn't exist
$outputDir = __DIR__ . '/public';
if (!is_dir($outputDir)) {
    mkdir($outputDir, 0777, true);
}

// Create a new FPDI instance
$pdf = new FPDI();

// Set font for the PDF
$pdf->SetFont('Arial', '', 10);

// Add pages from the existing PDF and modify the first page
$pageCount = $pdf->setSourceFile($pdfPath);
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
    $pdf->AddPage();
    $tplIdx = $pdf->importPage($pageNo);
    $pdf->useTemplate($tplIdx, 0, 0);

    // Modify the first page
    if ($pageNo == 1) {
        // Basic pay
        $value3 = "450";
        $pdf->SetXY(100, 124);
        $pdf->Cell(40, 10, $value3);
        
        //Additional Pay
        $earnings = $inputNumber * 5;
        if($earnings - 0 - 0 >= 0) {
            $gross1 = $earnings-0-0;
            $gross2 = 175 * 0.138;
            $gross3 = 1 + 0.138;
            $gross4 = $gross1 + $gross2;
            $gross5 = $gross4 / $gross3;
            $gross6 = $gross5 - 450;
            $resultFieldValue = number_format($gross6, 2);
        }
        $value4 = "$resultFieldValue";
        // Update the value in the center of the PDF
        $pdf->SetXY(100, 129); // Adjust coordinates as needed  
        $pdf->Cell(40, 10, $value4);
        
        //Holiday Pay
        $earnings = $inputNumber * 5;
        $gross1 = $earnings - 0 - 0;
        $gross2 = 175 * 0.138;
        $gross3 = 1 + 0.138;
        $gross4 = $gross1 + $gross2;
        $grossfinal = $gross4 / $gross3;
        $gross = $grossfinal;
        $holidaypay = $gross * 0.1077;
        $holipay = number_format($holidaypay, 2);
        $hpay = "$holipay";
        $pdf->SetXY(100, 136);
        $pdf->Cell(40, 10, $hpay);

        // PAYE
        $earnings = $inputNumber * 5;
        $gross1 = $earnings - 0 - 0;
        $gross2 = 175 * 0.138;
        $gross3 = 1 + 0.138;
        $gross4 = $gross1 + $gross2;
        $grossfinal = $gross4 / $gross3;
        $gross = $grossfinal;
        $tax1 = $gross * 52;
        $tax2 = $tax1 - 12579;
        $tax3 = floor($tax2 / 52);
        $round = $tax3 * 52;
        $taxcalc = ($round < 0) ? 0 : (($round > 37700) ? 37700 * 0.2 : $round * 0.2);
        $hightax = ($round > 125140) ? 87440 * 0.4 : (($round > 37700) ? ($round - 37700) * 0.4 : 0);
        $addtax = ($round > 125140) ? ($round - 125140) * 0.45 : 0;
        $threetaxes = $taxcalc + $hightax + $addtax;
        $finaltax = $threetaxes / 52;
        $finaltax1 = number_format($finaltax, 2);
        $paye = "$finaltax1";
        $pdf->SetXY(185, 123);
        $pdf->Cell(40, 10, $paye);

        // NI
        if ($gross < 242) {
            $national = 0;
        } elseif ($gross < 967) {
            $national = ($gross - 242) * 0.08;
        } else {
            $national = (967 - 242) * 0.08 + ($gross - 967) * 0.02;
        }
        $resultFieldValue = number_format($national, 2);
        $ni = "$resultFieldValue";
        $pdf->SetXY(185, 129);
        $pdf->Cell(40, 10, $ni);

        // Total Gross Pay
        $resultFieldValue = number_format($gross, 2);
        $cw = "$resultFieldValue";
        $pdf->SetXY(117, 187);
        $pdf->Cell(40, 10, $cw);

        // Gross For Tax
        $value4 = "$cw";
        $pdf->SetXY(117, 192);
        $pdf->Cell(40, 10, $value4);

        // Earnings For NI
        $value5 = "$cw";
        $pdf->SetXY(117, 197);
        $pdf->Cell(40, 10, $value5);

        // Total deduction
        $totaldec = $co + $paye + $ni;
        $td = "$totaldec";
        $pdf->SetXY(117, 201);
        $pdf->Cell(40, 10, $td);

        // Employees NI
        if ($gross - 175 > 0) {
            $ersni = ($gross - 175) * 0.138;
            $result_ersni = number_format($ersni, 2);
        } else {
            $result_ersni = 0;
        }
        $er_ni1 = "$result_ersni";
        $pdf->SetXY(117, 207);
        $pdf->Cell(40, 10, $er_ni1);
        
        //Right Side
        // Gross Pay TD
        $pdf->SetXY(185, 186);
        $pdf->Cell(40, 10, $value4);
        
        // Gross For Tax TD 
        $pdf->SetXY(185, 191);
        $pdf->Cell(40, 10, $value4);

        // Tax Paid TD
        $paye1 = "$paye";
        $pdf->SetXY(185, 196);
        $pdf->Cell(40, 10, $paye1);

        // NI Earnings TD
        $NI1 = "$ni";
        $pdf->SetXY(185, 201);
        $pdf->Cell(40, 10, $NI1);

        // Ees NI TD
        $NI1 = "$er_ni1";
        $pdf->SetXY(185, 206);
        $pdf->Cell(40, 10, $NI1);

        // NET PAY
        $value2 = "$result";
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFont('Arial', '', 18);
        $pdf->SetXY(176, 212);
        $pdf->Cell(40, 10, $value2);
        $pdf->SetTextColor(0, 0, 0); // Reset to black
    }

    // Modify the second page
    if ($pageNo == 2) {
        $pdf->SetFont('Arial', '', 10);
        
        //Units
        $units = "126";
        $pdf->SetXY(63, 92);
        $pdf->Cell(40, 10, $units);
        
        //Rate
        $rate = "5";
        $pdf->SetXY(102, 92);
        $pdf->Cell(40, 10, $rate);
        
        // Basic pay
        $earnings = $inputNumber * 5;
        $value3 = "$earnings";
        $pdf->SetXY(146, 82);
        $pdf->Cell(40, 10, $value3);
        
        // TOTAL(A)
        $earnings = $inputNumber * 5;
        $totala = "$earnings";
        $pdf->SetXY(146, 92);
        $pdf->Cell(40, 10, $totala);
        
        //Compay overhead
         $co = "0";
        // Update the value in the center of the PDF
        $pdf->SetXY(143, 121); // Adjust coordinates as needed  
        $pdf->Cell(40, 10, $co);
        
        //Holiday Pay
        $holidaypay = $gross * 0.1077;
        $holipay = number_format($holidaypay, 2);
        $hpay = "$holipay";
        $pdf->SetXY(135, 142);
        $pdf->Cell(40, 10, $hpay);
        
        // Apprentice Levy
        if ($gross - 175 > 0) {
            $ersni = ($gross - 175) * 0.138;
            $result_ersni = number_format($ersni, 2);
        } else {
            $result_ersni = 0;
        }
        $er_ni1 = "$result_ersni";
        $pdf->SetXY(135, 148);
        $pdf->Cell(40, 10, $er_ni1);
        
        // TOTAL(B)
        $totalb1 = $hpay + $er_ni1;
        $totalb = "$totalb1";
        $pdf->SetXY(151, 169);
        $pdf->Cell(40, 10, $totalb);
        
        // TOTAL(C)
        //Holiday Pay
        $holidaypay = $gross * 0.1077;
        $holipay = number_format($holidaypay, 2);
        $hpay = "$holipay";
        $pdf->SetXY(151, 174);
        $pdf->Cell(40, 10, $hpay);
        
        //Gross for Tax (A) - (B) + (C)
        $total = $totala - $totalb + $hpay;
        $totalfinal = "$total";
        $pdf->SetXY(151, 181);
        $pdf->Cell(40, 10, $totalfinal);
    }
    
    // Modify the Third page
    if ($pageNo == 3) {
        $value3 = "$inputNumber";
        $pdf->SetXY(24, 187);
        $pdf->Cell(40, 10, $value3);
    }
    
    // Modify the Fourth page
    if ($pageNo == 4) {
        //Example Pay
        $earnings = $inputNumber * 5;
        $value3 = "$earnings";
        $pdf->SetXY(26, 128);
        $pdf->Cell(40, 10, $value3);
        
        //(NIERS and APP LEVY)
        $value3 = "$er_ni1";
        $pdf->SetXY(67, 146);
        $pdf->Cell(40, 10, $value3);
        
        //Holiday Pay
        $Holidaypay1 = "$hpay";
        $pdf->SetXY(51, 154);
        $pdf->Cell(40, 10, $Holidaypay1);
        
        //Basic
        $earnings = $inputNumber * 5;
        $value3 = "$earnings";
        $pdf->SetXY(36, 190);
        $pdf->Cell(40, 10, $value3);
        
        //Holiday Pay
        $Holidaypay1 = "$hpay";
        $pdf->SetXY(42, 194);
        $pdf->Cell(40, 10, $Holidaypay1);
        
        // Tax
        $value5 = "$paye";
        $pdf->SetXY(32, 212);
        $pdf->Cell(40, 10, $value5);
        
        //Employees NI 
        $value6 = "$er_ni1";
        $pdf->SetXY(52, 216);
        $pdf->Cell(40, 10, $value6);
        
        //Example net take home pay:
        $value7 = "$value2";
        $pdf->SetXY(28, 276);
        $pdf->Cell(0, 0, $value7);
    }
}

// Output the new PDF
$outputPath = 'public/ombrella_payslip.pdf';
$pdf->Output('F', $outputPath);

// Redirect to the modified PDF to display it
header('Location: ' . $outputPath);
exit;
?>
