<?php

$methodid=$_REQUEST['mid'];
require_once('config.php');
require_once('fpdf/fpdf.php');

$linkID = new PDO('mysql:host='.Config::DB_HOST.';dbname='.Config::DB_NAME, Config::DB_USERNAME, Config::DB_PASSWORD);
$linkID->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Setting ERRMODE

$checkifexists = $linkID->query("SELECT count(*) FROM amjay_config_doc WHERE method_id=".$methodid)->fetchAll(PDO::FETCH_ASSOC);

if($checkifexists[0]['count(*)'] ==0) {
    echo "Form Not Found";
}else {
    $custinfo = $linkID->query("SELECT *FROM amjay_config_doc WHERE method_id=" . $methodid)->fetchAll(PDO::FETCH_ASSOC);
    $keyval = json_decode($custinfo[0]['key_val'], true);


    $name_arr = [];
    $var_arr = [];
    foreach ($keyval as $x => $val) {
        array_push($name_arr, $x);
        array_push($var_arr, $val);

    }


    //grab the field in row, json decode it and then create a for loop that gets key value pairs and generate pdf form, and then do the rest of the forms and test and add this link to button.


    class PDF extends FPDF
    {
        // Page header
        function Header()
        {
            $this->Image('amjaylogo1.png', 10, 15, 30);

            // Set font-family and font-size
            $this->SetFont('Times', 'B', 20);

            // Move to the right
            $this->Cell(80);

            // Set the title of pages.
            $this->Cell(30, 20, 'AMJAY SOFTWARE CONFIG DOC', 0, 2, 'C');

            // Break line with given space
            $this->Ln(5);
        }

        // Page footer
        function Footer()
        {
            // Position at 1.5 cm from bottom
            $this->SetY(-15);

            // Set font-family and font-size of footer.
            $this->SetFont('Arial', 'I', 8);

            // set page number
            $this->Cell(0, 10, 'Page ' . $this->PageNo() .
                '/{nb}', 0, 0, 'C');
        }
    }

// Create new object.
    $pdf = new PDF();
    $pdf->AliasNbPages();

// Add new pages
    $pdf->AddPage();

// Set font-family and font-size.
    $pdf->SetFont('Times', '', 12);
    $needtocheck=[];
    for ($i = 0; $i < count($var_arr); $i++) {
        if (strlen($var_arr[$i]) < 1000) {
            $pdf->MultiCell(190, 5, $name_arr[$i], 0);
            $pdf->MultiCell(190, 5, $var_arr[$i], 1);
            $pdf->MultiCell(40, 5, '');

        }else{
            array_push($needtocheck,$i);
        }
    }

    $pdf->AddPage();
// Loop to display line number content
    $j = 0;
    for ($i = 0; $i < count($needtocheck); $i++) {
        if ($name_arr[$needtocheck[$i]] == 'Signature') {
            $img = explode(',', $var_arr[$needtocheck[$i]], 2)[1];
            $img = str_replace("\/", "/", $img);
            $pic = 'data://text/plain;base64,' . $img;
            $pdf->MultiCell(150, 5, $pdf->Image($pic, 0, 200, 100, 50, 'png'));

        } elseif (strlen($var_arr[$needtocheck[$i]]) > 1000) {

            $img = explode(',', $var_arr[$needtocheck[$i]], 2)[1];
            $img = str_replace("\/", "/", $img);
            $pic = 'data://text/plain;base64,' . $img;
            $pdf->MultiCell(190, 5, $name_arr[$needtocheck[$i]], 0);
            $pdf->MultiCell(0, 0, $pdf->Image($pic, 20, (50 + ($j * 50)), 50, 50, 'png'));
            $j++;

        }


    }
    $pdf->Output();
}
