<?php

namespace App\Http\Controllers;

use Codedge\Fpdf\Fpdf\Fpdf;
use Illuminate\Http\Request;
use setasign\Fpdi\Fpdi;

use App\Common\Helpers;

class documentOverlayController extends Controller
{
    public function wniosekNadaniePDF($userData, $decoration, $fireStation, $awardedDecorations){
        // initiate FPDI
        $pdf = new Fpdi();
        $pdf->fontpath = "../app/Common/fonts/";
        $pdf->AddFont('arialpl', '', 'arialpl.php');
        #$pdf->AddFont('arialpl', 'B', 'arialplb.php');
        // set the source file
        $pageCount = $pdf->setSourceFile("../app/Http/Controllers/documentTemplates/a.pdf");
        // import page 1

        for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
            // import a page
            $templateId = $pdf->importPage($pageNo);
            // get the size of the imported page
            $size = $pdf->getTemplateSize($templateId);

            // create a page (landscape or portrait depending on the imported page size)
            if ($size[0] > $size[1]) {
                $pdf->AddPage('L', array($size[0], $size[1]));
            } else {
                $pdf->AddPage('P', array($size[0], $size[1]));
            }
            $pdf->useTemplate($templateId);

            if($pageNo == 1){
                $str = iconv('UTF-8', 'cp1250', 'zazółcić gęślą jaźń');
//                $xyz = iconv('UTF-8', 'cp1250', $fireFighterID);
                $dec = iconv('UTF-8', 'cp1250', $decoration);
                // now write some text above the imported page
                $pdf->SetFont('arialpl');
                $pdf->SetTextColor(128, 128, 128);

                #Pieczęć
                $pdf->SetXY(30, 30);
//                $pdf->Write(0, $decoration);

                #Nazwa odznaczenia
                $pdf->SetXY(120, 50);
                //$pdf->Write(0, $decoration);
                $pdf->Cell(20,10, Helpers::textConvert($decoration), 0, 0, 'C');

                #Imie
                $pdf->SetXY(35, 74);
                //$pdf->Write(0, $decoration);
                $pdf->Cell(20,10, Helpers::textConvert($userData->name), 0, 0, 'C');

                #Nazwisko
                $pdf->SetXY(95, 74);
                //$pdf->Write(0, $decoration);
                $pdf->Cell(20,10, Helpers::textConvert($userData->surname), 0, 0, 'C');

                #Data Urodzenia
                $pdf->SetXY(35, 93);
                //$pdf->Write(0, $decoration);
                $pdf->Cell(20,10, Helpers::textConvert(Helpers::peselBirthdayDate($userData->PESEL)), 0, 0, 'C');


                #Funkcja
                $pdf->SetXY(124, 113);
                $pdf->Cell(20,10, Helpers::textConvert($userData->unitFunction), 0, 0, 'C');

                #Funkcja
                $pdf->SetXY(35, 113);
                $pdf->Cell(20,10, Helpers::textConvert($fireStation->postOffice), 0, 0, 'C');

                $txt = "";
                foreach($awardedDecorations as $awardedDecoration){
                    $txt = $txt." ".$awardedDecoration->decorationName."\n";
                }
                $pdf->SetXY(18, 132);
                $pdf->MultiCell(86,4, Helpers::textConvert($txt), 0, 'l', 0);
            }
        }



        $pdf->Output();


    }

    public function kartaEwidencyjnaPDF($userData, $fireStation, $userTrainings){
        // initiate FPDI
        $pdf = new Fpdi();
        $pdf->fontpath = "../app/Common/fonts/";
        $pdf->AddFont('arialpl', '', 'arialpl.php');
        // set the source file
        $pageCount = $pdf->setSourceFile("../app/Http/Controllers/documentTemplates/b.pdf");
        // import page 1

        for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
            // import a page
            $templateId = $pdf->importPage($pageNo);
            // get the size of the imported page
            $size = $pdf->getTemplateSize($templateId);

            // create a page (landscape or portrait depending on the imported page size)
            if ($size[0] > $size[1]) {
                $pdf->AddPage('L', array($size[0], $size[1]));
            } else {
                $pdf->AddPage('P', array($size[0], $size[1]));
            }
            $pdf->useTemplate($templateId);


            if($pageNo == 1) {
                $pdf->SetFont('arialpl');
                $pdf->SetTextColor(128, 128, 128);

                # Imie i nazwisko
                $pdf->SetXY(84, 31.5);
                $pdf->Cell(0, 10, Helpers::textConvert($userData->surname . ' ' . $userData->name), 0, 0, 'l');

                # PESEL
                $peselArray = str_split($userData->PESEL);
                $x = 116;
                foreach ($peselArray as $char) {
                    $pdf->SetXY($x, 62.5);
                    $pdf->Cell(0, 10, $char, 0, 0, 'l');
                    $x = $x + 7.35;
                }

                # Numer Telefonu
                $pdf->SetXY(102, 103);
                $pdf->Cell(0, 10, $userData->phoneNumber, 0, 0, 'l');

                # Data urodzenia
                $birthdayArray = Helpers::peselBirthdayDateARRAY($userData->PESEL);
                #Day
                $pdf->SetXY(16, 62.5);
                $pdf->Cell(0, 10, $birthdayArray[0], 0, 0, 'l');
                #Month
                $pdf->SetXY(26.5, 62.5);
                $pdf->Cell(0, 10, $birthdayArray[1], 0, 0, 'l');
                #Month
                $pdf->SetXY(37.5, 62.5);
                $pdf->Cell(0, 10, $birthdayArray[2], 0, 0, 'l');

                $y = 182;
                $lp = 1;
                foreach($userTrainings as $userTraining){
                    $pdf->SetXY(20, $y);
                    $pdf->Cell(0, 10, $lp, 0, 0, 'l');
                    $pdf->SetXY(26, $y);
                    $pdf->Cell(0, 10, Helpers::textConvert($userTraining->trainingName), 0, 0, 'l');
                    $pdf->SetXY(162, $y);
                    $pdf->Cell(0, 10, Helpers::textConvert($userTraining->dateOfComplete), 0, 0, 'l');
                    $y = $y + 5.5;
                    $lp = $lp + 1;
                }
            }
        }

        $pdf->Output();
    }
}