<?php
// Include la libreria TCPDF
include 'include/headscript.php';
ob_start();

// Include l'autoloader di Dompdf
require 'dompdf/autoload.inc.php';

// Utilizza il namespace Dompdf
use Dompdf\Dompdf;

$dompdf = new Dompdf();
$dompdf->set_option('isHtml5ParserEnabled', true);
$dompdf->set_option('isRemoteEnabled', true);
$dompdf->set_option('debugCss', true);
$dompdf->set_option('debugLayout', true);
$dompdf->set_option('debugLayoutLines', true);


require_once '../Connections/cmctrfdb.php';
require_once '../webassist/mysqli/rsobj.php';
//include 'include/headscript.php';

include('languages/' . $_SESSION['langselect'] . '/tdgen.php');
include('languages/' . $_SESSION['langselect'] . '/general.php');
$idcompany = $_SESSION["compid"];
if (isset($_GET['idtrftd'])) {
    $idtrftd = $_GET['idtrftd'];
}
if (isset($_POST['idtrftd'])) {
    $idtrftd = $_POST['idtrftd'];
}
if (isset($_POST['iddata_td'])) {
    $idtd = $_POST['iddata_td'];
}
if (isset($_GET['iddata_td'])) {
    $idtd = $_GET['iddata_td'];
}
if (isset($_GET['idtd'])) {
    $idtd = $_GET['idtd'];
}
$base_url = "https://www.cimac.it/modulo_certificazione/public/";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Preparazione di un array per contenere i valori sanificati
    $sanitizedPost = [];
    $errors = [];

    // Lista dei campi da sanificare e controllare se sono piene
    $fields = [
        'productionplace_same',
        'classificationshoes',
        'destinationuseppe',
        'manufacutringprocess',
        'ppeageing',
        'obsolescencedeadline',
        'localisationppemarking',
        'manufacturerlogoid',
        'sizeexamplecemark',
        'monthyearprod',
        'serialbatchnumber',
        'standarduse',
        'symbolsaddreq',
        'proddescription',
        'packaging',
        'declarconformity',
        'webaddress',
        'techspecificationapplied'
    ];

    foreach ($fields as $field) {
        if (!empty($_POST[$field])) {
            // Utilizzo FILTER_SANITIZE_STRING per rimuovere i tag e sanificare il testo
            $sanitizedPost[$field] = filter_input(INPUT_POST, $field, FILTER_SANITIZE_STRING);
        }
    }

    // Controllo se ci sono stati errori
    if (count($errors) === 0) {
        // Tutti i campi sono stati compilati e sanificati
        // Qui puoi procedere con l'elaborazione dei dati
        // Ad esempio, stampare i valori o salvarli in un database
        foreach ($sanitizedPost as $key => $value) {
        }
    } else {
        // Ci sono stati errori, ad esempio alcuni campi potrebbero essere vuoti
        // Puoi gestire gli errori qui, ad esempio stampandoli
        foreach ($errors as $key => $message) {
            echo "Errore nel campo $key: $message<br>";
        }
    }
}

// *: update data_td
// Assicurati che la richiesta sia di tipo POST e che l'ID sia stato fornito

$conn = mysqli_connect($servername, $username, $password, $dbname);



if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($idtrftd)) {
    // Preparazione della parte iniziale della query di aggiornamento
    $updateQuery = "UPDATE data_td SET ";
    $updateParts = [];
    $queryParams = [];

    // Iterazione sui campi sanificati per costruire la query di aggiornamento
    foreach ($sanitizedPost as $key => $value) {
        // Escludi idtrftd dalla parte di aggiornamento della query
        if ($key !== 'idtrftd') {
            $updateParts[] = "$key = ?";
            $queryParams[] = $value;
        }
    }

    // Controllo se ci sono campi da aggiornare
    if (count($updateParts) > 0) {
        $updateQuery .= join(', ', $updateParts) . " WHERE idtrf = ?";
        $queryParams[] = $idtrftd; // Aggiungi l'ID alla fine dei parametri della query

        // Preparazione della query
        $stmt = $conn->prepare($updateQuery);

        // Costruzione del tipo di parametri (stringhe, in questo caso)
        $types = str_repeat('s', count($queryParams));

        // Aggiunta dei parametri alla statement
        $stmt->bind_param($types, ...$queryParams);

        // Esecuzione della query
        if ($stmt->execute()) {
        }

        // Chiusura dello statement
        $stmt->close();
    }
}


$conn = new mysqli($servername, $username, $password, $dbname);
$checkQuery = "SELECT COUNT(*) as count FROM fillrisk_td WHERE iddata_td = ?";
$stmt = $conn->prepare($checkQuery);
$stmt->bind_param("i", $idtd);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if ($row['count'] == 0) {
    // Non ci sono record, quindi procedi con l'inserimento dei dati da riskarea_td

    // Prendi tutti i record da riskarea_td
    $selectQuery = "SELECT * FROM riskarea_td";
    $result = $conn->query($selectQuery);



    while ($riskRow = $result->fetch_assoc()) {
        // Prepara l'insert per ogni riga trovata in riskarea_td

        $insertQuery = "INSERT INTO fillrisk_td (idriskarea_td, applicable, idcompany, iddata_td, idtrf) VALUES (?, ?, ?, ?, ?)";
        $stmt = $conn->prepare($insertQuery);

        // Converte il valore 'Y'/'N' della colonna default in un intero (1/0)
        $applicableValue = ($riskRow['default'] == 'Y') ? 1 : 0;

        $stmt->bind_param("iiiii", $riskRow['idriskarea_td'], $applicableValue, $idcompany, $idtd, $idtrftd);
        $stmt->execute();
    }
}


// Chiudi lo statement e la connessione se non ti servono più
$stmt->close();
$conn->close();


// query data_td
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM data_td LEFT JOIN logo_td ON data_td.manufacturerlogoid=logo_td.idlogo_Td LEFT JOIN qualcheck_td ON data_td.proddescription=qualcheck_td.idqualcheck_td  WHERE iddata_td = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $idtd); // "i" indica che l'id è un intero
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$prodplace = $row['productionplace_same'];
$classshoes = $row['classificationshoes'];

if ($classshoes == "classone") {
    $classfinal = $classone;  // Assumo che il valore che vuoi assegnare sia una stringa "classone"
} elseif ($classshoes == "classtwo") {
    $classfinal = $classtwo;  // Assumo che il valore che vuoi assegnare sia una stringa "classtwo"
} else {
    $classfinal = "default";   // Opzionale: Un valore default se non corrisponde a nessuno dei casi
}

$stmt->close();
$conn->close();

$tdquery = new WA_MySQLi_RS("tdquery", $cmctrfdb, 1);
$tdquery->setQuery("SELECT * FROM `trf-details` LEFT JOIN modelarticle ON modelarticle.idmodelarticle=`trf-details`.model  WHERE `trf-details`.idtrfdetails='$idtrftd'");
$tdquery->execute();

$description = $tdquery->getColumnVal("sample_description");
$trfn = $tdquery->getColumnVal("trfnumber");
$trfrev  = $tdquery->getColumnVal("revtrf");
$mandat  = $tdquery->getColumnVal("otherclient");
$tdrev  = $tdquery->getColumnVal("td_rev");
$tdnumber  = $row['tdnumber'];
$trfnumb = $trfn . ' VER.' . $trfrev;
$trftdnumber = $trfn . 'TF';
$photocover  = $tdquery->getColumnVal("photofilename");
$photoone  = $tdquery->getColumnVal("photoone");
$phototwo  = $tdquery->getColumnVal("phototwo");
$virusprot = $tdquery->getColumnVal("virusprotection");
$idarttype = $tdquery->getColumnVal("idarticletype");


$slippingtext = $tdquery->getColumnVal("slipping") === 'Y' ? 'Sì' : ($tdquery->getColumnVal("slipping") === '' ? 'No' : $tdquery->getColumnVal("slipping"));
$autoclavabletext = $tdquery->getColumnVal("autoclavable") === 'Y' ? 'Sì' : ($tdquery->getColumnVal("autoclavable") === '' ? 'No' : $tdquery->getColumnVal("autoclavable"));
$ukcatext = $tdquery->getColumnVal("ukca") === 'Y' ? 'Sì' : ($tdquery->getColumnVal("ukca") === '' ? 'No' : $tdquery->getColumnVal("ukca"));
$esdtext = $tdquery->getColumnVal("esd") === 'Y' ? 'Sì' : ($tdquery->getColumnVal("esd") === '' ? 'No' : $tdquery->getColumnVal("esd"));
$shoesorthopedictext = $tdquery->getColumnVal("shoesorthopedic") === 'Y' ? 'Sì' : ($tdquery->getColumnVal("shoesorthopedic") === '' ? 'No' : $tdquery->getColumnVal("shoesorthopedic"));
$shoesorthopedicmodtext = $tdquery->getColumnVal("shoesorthopedicmod") === 'Y' ? 'Sì' : ($tdquery->getColumnVal("shoesorthopedicmod") === '' ? 'No' : $tdquery->getColumnVal("shoesorthopedicmod"));

$conn = new mysqli($servername, $username, $password, $dbname);


$kindcont = "headercertificate";
// Usa segnaposti per i parametri


$sqlcontact = "SELECT * FROM contacts LEFT JOIN countries ON countries.idcountries=contacts.country WHERE contacts.idtrf='$idtrftd' AND contacts.kindofcontacts='$kindcont'";

// Esecuzione della query
$resultcontact = $conn->query($sqlcontact);
$rowcontact = $resultcontact->fetch_assoc();
$companyname = $rowcontact["companyname"];
$address = $rowcontact["address"] . ' ' . $rowcontact["cap"] . ' ' . $rowcontact["city"];
$country = $rowcontact["namecountry"];
$phone = $rowcontact["telephone"];
$emailtd = $rowcontact["email"];
$vat = $rowcontact["piva"];
$ceinside = $rowcontact["ce"];
$mark = $tdquery->getColumnVal("registeredmark");

$contactperson = $rowcontact["contactname"] . ' ' . $rowcontact["contactsurname"];

// Chemical agent
$conn = new mysqli($servername, $username, $password, $dbname);
$sqlchemical = "SELECT trfchemicalagent.level, trfchemicalagent.degradationpercentage, chemicalagent.name_chemicalagent 
                FROM trfchemicalagent 
                LEFT JOIN chemicalagent ON trfchemicalagent.idchemicalagent = chemicalagent.idchemicalagent 
                WHERE trfchemicalagent.idtrf = '$idtrftd'";
$resultchemical = $conn->query($sqlchemical);
$chemicalAgents = []; // Array per memorizzare i risultati

if ($resultchemical && $resultchemical->num_rows > 0) {
    // Riempie l'array con i risultati della query
    while ($rowchemical = $resultchemical->fetch_assoc()) {
        $chemicalAgents[] = $rowchemical;
    }
}
$conn->close();



// Protection cat add
$conn = new mysqli($servername, $username, $password, $dbname);
$sqlprotect = "SELECT * FROM trfaddrequirements LEFT JOIN additionalrequirements ON trfaddrequirements.idadditionalrequirements = additionalrequirements.idadditionalrequirements WHERE trfaddrequirements.idtrf = '$idtrftd'";
$resultprotect = $conn->query($sqlprotect);
$protectionAdd = []; // Array per memorizzare i risultati

if ($resultprotect && $resultprotect->num_rows > 0) {;
    // Riempie l'array con i risultati della query
    while ($rowprotect = $resultprotect->fetch_assoc()) {

        $protectionAdd[] = $rowprotect["name_additionalrequirements_it"];
    }
}
$conn->close();

// Connessione al database
$conn = new mysqli($servername, $username, $password, $dbname);

// Controlla la connessione
if ($conn->connect_error) {
    die("Connessione fallita: " . $conn->connect_error);
}

// Preparazione della query

$tdquerystd = "SELECT * FROM trfstandards 
          LEFT JOIN standards ON trfstandards.idstandards = standards.idstandards 
          LEFT JOIN protectioncategory ON protectioncategory.idprotectioncategory = trfstandards.idprotectioncategory 
          LEFT JOIN dpicategory ON dpicategory.iddpicategory = trfstandards.iddpicategory 
          WHERE trfstandards.idtrfdetails = '$idtrftd'";

// Esecuzione della query
$resultstd = $conn->query($tdquerystd);

if (!$resultstd) {
    die("Errore nell'esecuzione della query: " . $conn->error);
}


$conn = new mysqli($servername, $username, $password, $dbname);

// Controlla la connessione
if ($conn->connect_error) {
    die("Connessione fallita: " . $conn->connect_error);
}
// Preparazione della query
$addprot = "SELECT * FROM trfaddrequirements 
            LEFT JOIN additionalrequirements ON trfaddrequirements.idadditionalrequirements = additionalrequirements.idadditionalrequirements 
            WHERE trfaddrequirements.idtrf = '$idtrftd'";
// Esecuzione della query
$resultaddreq = $conn->query($addprot);
if (!$resultaddreq) {
    die("Errore nell'esecuzione della query: " . $conn->error);
}


$tdquerystd = new WA_MySQLi_RS("tdquerystd", $cmctrfdb, 1);
$tdquerystd->setQuery("SELECT * FROM trfstandards LEFT JOIN standards ON trfstandards.idstandards=standards.idstandards LEFT JOIN protectioncategory ON protectioncategory.idprotectioncategory=trfstandards.idprotectioncategory LEFT JOIN dpicategory ON dpicategory.iddpicategory=trfstandards.iddpicategory WHERE trfstandards.idtrfdetails='$idtrftd'");
$tdquerystd->execute();

$archivetrflist = new WA_MySQLi_RS("archivetrflist", $cmctrfdb, 0);
$archivetrflist->setQuery("SELECT * FROM `trf-details` LEFT JOIN auth_users ON `trf-details`.iduser=auth_users.id LEFT JOIN article_type ON `trf-details`.idarticletype=article_type.idarticletype LEFT JOIN certificationtype ON certificationtype.idcertificationtype=`trf-details`.idcertification WHERE `trf-details`.idcompany='$idcompany'  AND `trf-details`.signedon <>'' ORDER BY `trf-details`.trfnumber");
$archivetrflist->execute();

// query prod place

//query location place
// Assumendo che $idt sia già definito e sanificato per prevenire SQL Injection

$conn = new mysqli($servername, $username, $password, $dbname);
$querylocation = "SELECT idcontactstd, companyName, address, city FROM contacts_td WHERE idtd = ?";
$stmt = $conn->prepare($querylocation);
$stmt->bind_param("i", $idtd); // "i" indica che il parametro è un intero
$stmt->execute();
$result = $stmt->get_result();

$rowslocation = [];
while ($rowlocation = $result->fetch_assoc()) {
    $rowslocation[] = $rowlocation;
}
$stmt->close();

$companylogofinal = !empty($companylogo) ? $base_url . "logos/" . $companylogo : '';


// Crea una nuova istanza di Dompdf
$dompdf = new Dompdf();
$html = <<<HTML
<!DOCTYPE html>
<html>
<head>
<style>
    @page { 
        margin: 120px 25px 80px 25px; /* Aumenta il margine superiore e inferiore */
    }
    body { 
        font-family: 'DejaVu Sans', sans-serif; 
    }
    header { 
        position: fixed; 
        top: -120px; /* Posizionato 120px sopra il margine superiore della pagina */
        left: 0; 
        right: 0; 
        height: 100px; /* Assicurati che l'altezza sia sufficiente per il tuo logo */
        text-align: center; 
    }
    footer { 
        position: fixed; 
        bottom: -80px; /* Posizionato 80px sopra il margine inferiore della pagina */
        left: 0; 
        right: 0; 
        height: 50px; 
        text-align: center; 
    }
    .page-number:before { 
        content: "Pagina " counter(page); /* Aggiunge "Pagina" prima del numero di pagina */
    }
    table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 20px; /* Mantieni o regola secondo necessità */
    }
    th, td {
        border: 1px solid #000000;
        padding: 4px;
        font-size: 8pt;
        background-color: #fff;
        word-wrap: break-word;
        word-break: break-all;
    }
    th {
        background-color: #909090;
        color: white;
        text-align: left;
        font-size: 9pt;
    }
    td.header-data {
        background-color: #E7F4FA;
    }
    td.firstrisk {
        background-color: #ffffff;
        color: #000000;
    }
    .boldstyle {
        font-weight: normal; /* Stile per testo in grassetto, correggi secondo necessità */
        background-color: #ffffff;
        color: #000000;
        font-size: 8pt;
    }
</style>



</head>
<body>
<header>
    <img src="{$companylogofinal}" alt="Logo Azienda" style="height: 80px;">
</header>
<footer style="font-size: smaller;">
    <div>
       
    </div>
    <div style="font-size: x-small; margin-top: 3px;">
        <span class="page-number"></span>
    </div>
</footer>

<h3 style="text-align: center;">File Tecnico n./ Technical File N.: $tdnumber</h3>

<table>
    <thead>
        <tr>
            <th colspan="2">DATI DEL FABBRICANTE / MANUFACTURER'S DATA</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="first-column">Nome Azienda / Company Name</td>
            <td class="header-data">$companyname</td>
        </tr>
        <tr>
            <td class="first-column">Indirizzo / Address</td>
            <td class="header-data">$address</td>
        </tr>
        <tr>
            <td class="first-column">Paese / Country</td>
            <td class="header-data">$country</td>
        </tr>
        <tr>
            <td class="first-column">Telefono / Phone</td>
            <td class="header-data">$phone</td>
        </tr>
        <tr>
            <td class="first-column">Email</td>
            <td class="header-data">$emailtd</td>
        </tr>
        <tr>
            <td class="first-column">Partita IVA / VAT Number</td>
            <td class="header-data">$vat</td>
        </tr>
        <tr>
            <td class="first-column">Marchio / Mark</td>
            <td class="header-data">$mark</td>
        </tr>
        <tr>
            <td class="first-column">Persona di Contatto / Contact Person</td>
            <td class="header-data">$contactperson</td>
        </tr>
    </tbody>
</table>


HTML;

if ($ceinside != 'Y') {
    $kindcont = "extrace";
    $sqlcontactextrace = "SELECT * FROM contacts LEFT JOIN countries ON countries.idcountries=contacts.country WHERE contacts.idtrf='$idtrftd' AND contacts.kindofcontacts='$kindcont'";

    $resultcontactextrace = $conn->query($sqlcontactextrace);
    if ($resultcontactextrace->num_rows > 0) {
        // Esecuzione della query
        $rowcontactextrace = $resultcontactextrace->fetch_assoc();
        $companynameextrace = $rowcontactextrace["companyname"];
        $addressextrace = $rowcontactextrace["address"] . ' ' . $rowcontact["cap"] . ' ' . $rowcontact["city"];
        $countryextrace = $rowcontactextrace["namecountry"];
        $phoneextrace = $rowcontactextrace["telephone"];
        $emailtdextrace = $rowcontactextrace["email"];
        $vatextrace = $rowcontactextrace["piva"];


        $contactpersonextrace = $rowcontactextrace["contactname"] . ' ' . $rowcontactextrace["contactsurname"];


        $html .= '<table>
    <thead>
        <tr>
            <th colspan="2">MANDATARIO / REPRESENTATIVE</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="first-column">Nome Azienda / Company Name</td>
            <td class="header-data">' . $companynameextrace . '</td>
        </tr>
        <tr>
            <td class="first-column">Indirizzo / Address</td>
            <td class="header-data">' . $addressextrace . '</td>
        </tr>
        <tr>
            <td class="first-column">Paese / Country</td>
            <td class="header-data">' . $countryextrace . '</td>
        </tr>
        <tr>
            <td class="first-column">Telefono / Phone</td>
            <td class="header-data">' . $phoneextrace . '</td>
        </tr>
        <tr>
            <td class="first-column">Email</td>
            <td class="header-data">' . $emailtdextrace . '</td>
        </tr>
        <tr>
            <td class="first-column">Partita IVA / VAT Number</td>
            <td class="header-data">' . $vatextrace . '</td>
        </tr>
        <tr>
            <td class="first-column">Persona di Contatto / Contact Person</td>
            <td class="header-data">' . $contactpersonextrace . '</td>
        </tr>
    </tbody>
</table>';
    }
}

// prod place
$html .= '<table>
    <thead>
    <tr>
    <th colspan="3">LUOGO DI PRODUZIONE / PRODUCTION SITE</th>
</tr>';

if ($prodplace == 'Y') {
    $html .= '<tr>
        <th class="boldstyle" colspan="3">coincide con il fabbricante / as manufacturer</th>
    </tr>';
} else {
    $html .= '<tr>
        <th class="boldstyle">Nome Azienda / Companu Name</th>
        <th class="boldstyle">Indirizzo / Address</th>
        <th class="boldstyle">Città / City</th>
    </tr>';
    $html .= '</thead>
    <tbody>';

    foreach ($rowslocation as $rowlocation) {
        $html .= "<tr>
            <td class='header-data'>{$rowlocation['companyName']}</td>
            <td class='header-data'>{$rowlocation['address']}</td>
            <td class='header-data'>{$rowlocation['city']}</td>
        </tr>";
    }

    $html .= '</tbody>';
}

$html .= '</table>';

$html .= '<br><a href="techdossier_start.php?idtd=' . $idtd . '&idtrftd=' . $idtrftd . '" target="_blank">
    <button type="button" class="btn btn-gradient-primary waves-effect waves-light">' . $editbutton . ' TF</button>
</a>';

//DPI DATA

$model = $tdquery->getColumnVal("namemodelarticle");
$measuremin = $tdquery->getColumnVal("measurefrom");
$measuremax = $tdquery->getColumnVal("measureto");
$destppe = $row['destinationuseppe'];
$manprocess = $row['manufacutringprocess'];
$ppeage = $row['ppeageing'];
$obsol = $row['obsolescencedeadline'];
$techspec = $row['techspecificationapplied'];
if ($ppeage == 'Y') {
    $ppeagetext = 'Sì';
} else {
    $ppeagetext = 'No';
}
$html .= <<<HTML

<table>
    <thead>
        <tr>
            <th colspan="2">DATI RELATIVI AL DPI / PPE DATA</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="first-column">Codice Articolo / Article Code</td>
            <td class="header-data">{$description}</td>
        </tr>
        <tr>
            <td class="first-column">Modello / Model</td>
            <td class="header-data">{$model}</td>
        </tr>
HTML;

if ($idarttype == 1) {
    $html .= <<<HTML
        <tr>
            <td class="first-column">Classificazione / Classification</td>
            <td class="header-data">{$classfinal}</td>
        </tr>
HTML;
}

$html .= <<<HTML
        <tr>
            <td class="first-column">Misura / Measure</td>
            <td class="header-data">{$measuremin} - {$measuremax}</td>
        </tr>
        <tr>
            <td class="first-column">Destinazione d'uso del DPI / PPE Destination Use</td>
            <td class="header-data">{$destppe}</td>
        </tr>
        <tr>
            <td class="first-column">Processo di lavorazione / Production process</td>
            <td class="header-data">{$manprocess}</td>
        </tr>
        <tr>
            <td class="first-column">DPI soggetto ad invecchiamento / PPE subject to aging</td>
            <td class="header-data">{$ppeagetext}</td>
        </tr>
        <tr>
            <td class="first-column">{$obsoldate}</td>
            <td class="header-data">{$obsol}</td>
        </tr>
          <tr>
            <td class="first-column">{$techapplied}</td>
            <td class="header-data">{$techspec}</td>
        </tr>
    </tbody>
</table>
HTML;



//  dpi standard

$html .= '<table>
    
    <tbody>';

while ($rowstd = $resultstd->fetch_assoc()) {
    $stdcode = $rowstd['standardcode'];
    $dpicat = $rowstd['value_dpicategory'];
    $html .= "<tr>
        <td style='font-weight: bold; width: 20%;'>Norme armonizzate di riferimento / Applicable harmonized standards</td>
        <td class='header-data' style='width: 20%;'>{$stdcode}</td>
        <td style='font-weight: bold; width: 15%;'>Cat Protezione DPI / PPE Protection Cat</td>
        <td class='header-data' style='width: 15%;'>{$rowstd['name_protectioncategory']}</td>
        <td style='font-weight: bold; width: 15%;'>Categoria del DPI / PPE Category</td>
        <td class='header-data' style='width: 15%;'>{$rowstd['value_dpicategory']}</td>
    </tr>";
};
$html .= '</tbody></table>';

//other info

if ($idarttype == 1) {
    $html .= <<<HTML
<table>
    <thead>
        <tr>
            <th colspan="2">Altre informazioni / Other information</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="first-column">Resistenza allo scivolamento / Slip resistance</td>
            <td class="header-data" style="width: 50%;">{$slippingtext}</td>
        </tr>
        <tr>
            <td class="first-column">Autoclavabile / Autoclavable</td>
            <td class="header-data">{$autoclavabletext}</td>
        </tr>
        <tr>
            <td class="first-column">Certificazione UKCA / UKCA Certification</td>
            <td class="header-data">{$ukcatext}</td>
        </tr>
        <tr>
            <td class="first-column">Calzature con plantari personalizzati (DGUV) / Footwear with customized insoles (DGUV)</td>
            <td class="header-data">{$shoesorthopedictext}</td>
        </tr>
        <tr>
            <td class="first-column">Calzature modificate con rialzi o inserti nel fondo (DGUV) / Footwear modified with lifts or sole inserts (DGUV)</td>
            <td class="header-data">{$shoesorthopedicmodtext}</td>
        </tr>
        <tr>
            <td class="first-column">Calzature ESD / ESD Footwear</td>
            <td class="header-data">{$esdtext}</td>
        </tr>
    </tbody>
</table>
HTML;
}


//  Add prot category

$html .= '<table>
    
    <tbody>';

while ($rowaddreq = $resultaddreq->fetch_assoc()) {

    $html .= "<tr>
        <td style='font-weight: bold; width: 15%;'>Categoria di protezione aggiuntiva / Additional protection category</td>
        <td class='header-data' style='width: 15%;'>{$rowaddreq['name_additionalrequirements_it']}</td>

    </tr>";
};
$html .= '</tbody></table>';




//photos da fare
$html .= '<table class="table table-bordered mb-0 table-centered">
    <tbody>
    <tr>
        <td style="text-align: center; vertical-align: middle;"><img src="uploadimages/' . (!empty($photocover) ? $photocover : 'notav.jpg') . '" style="max-height: 200px;"></td>
    </tr>
    </tbody>
</table>';



$html .= '<table class="table table-bordered mb-0 table-centered">
    <tbody>
    <tr>
        <td style="text-align: center; vertical-align: middle;"><img src="uploaddocuments/' . (!empty($photoone) ? $photoone : 'notav.jpg') . '" style="max-height: 200px;"></td>
        <td style="text-align: center; vertical-align: middle;"><img src="uploaddocuments/' . (!empty($phototwo) ? $phototwo : 'notav.jpg') . '" style="max-height: 200px;"></td>
    </tr>
    </tbody>
</table>';

// virus prot
if ($virusprot == "Y") :
    $html .= '<table>
    <tbody>
    <tr>
    <td style="width: 50%;" colspan="1">Protezione da Virus / Virus Protection</td>
    <td>{}</td>
</tr>
        
    </tbody>';



    $html .= '</tbody></table>';
endif;


// chem agent
if (!empty($chemicalAgents)) :
    $html .= '<table>
    <thead>
    <tr>
    <td>Agenti Chimici / Chemical Agent</td>';

    if ($idarttype == 2) {
        $html .= '<td>Livello / Level</td>
                  <td>Percentuale di degradazione / Degradation Percentage</td>';
    }

    $html .= '</tr>
    </thead>
    <tbody>';

    foreach ($chemicalAgents as $agent) :
        $html .= "<tr>
        <td>{$agent['name_chemicalagent']}</td>";

        if ($idarttype == 2) {
            $html .= "<td class='header-data'>{$agent['level']}</td>
                      <td class='header-data'>{$agent['degradationpercentage']}</td>";
        }

        $html .= '</tr>';
    endforeach;
    $html .= '</tbody></table>';
endif;

$html .= '<br><a href="techdossier_start.php?idtd=' . $idtd . '&idtrftd=' . $idtrftd . '"  target="_blank">
    <button type="button" class="btn btn-gradient-primary waves-effect waves-light">' . $editbutton . ' TF</button>
</a>';

if ((Auth::user()->hasRole('Admin')) || (Auth::user()->hasRole('CustomerService')) || (Auth::user()->hasRole('Superuser')) || (Auth::user()->hasRole('Certification'))) {
    $html .= '<a href="trfdetails.php?idtrf=' . $idtrftd . '" target="_blank"  target="_blank">
        <button type="button" class="btn btn-gradient-dark waves-effect waves-light">MODIFICA TRF</button>
    </a>';
}
//table risk query

$riskquery = new WA_MySQLi_RS("riskquery", $cmctrfdb, 0);
$riskquery->setQuery("SELECT * FROM fillrisk_td LEFT JOIN riskarea_td ON riskarea_td.idriskarea_td=fillrisk_td.idriskarea_td WHERE fillrisk_td.iddata_td = '$idtd' ORDER BY fillrisk_td.idfillrisk_td");
$riskquery->execute();

$html .= "<table>
    <thead>
    <tr>
    <th colspan='4'> VALUTAZIONE DEI RISCHI (REQUISITI ESSENZIALI DI SALUTE E SICUREZZA IN ACCORDO ALL'ALLEGATO II DEL REGOLAMENTO (UE) 2016/425) /
    RISK ASSESSMENT (ESSENTIAL HEALTH AND SAFETY REQUIREMENT ACCORDING TO ANNEX II OF THE REGULATION (EU) 2016/425)</th>
</tr>
        <tr>
            <th class='boldstyle' style='width: 10%;'>{$requirementnumbertd}</th>
            <th class='boldstyle' style='width: 45%;'>{$requirementnametd}</th>
            <th class='boldstyle' style='width: 10%;'>{$applicabletd}</th>
            <th class='boldstyle' style='width: 35%;'>{$covertbytd}</th>
        </tr>
    </thead>
    <tbody>";

$html .= <<<HTML
    <tbody>
    HTML;

// Aggiungi qui il codice PHP fornito
while (!$riskquery->atEnd()) {
    $risknumber = $riskquery->getColumnVal("risknumber");
    $riskname_it = $riskquery->getColumnVal($_SESSION['langselect'] == 'en' ? "riskname_en" : "riskname_it");
    $applicable = ($riskquery->getColumnVal("applicable") == "1") ? 'checked' : '';
    $customCheckId = "customCheck{$risknumber}";
    $covertext = '';
    if ($riskquery->getColumnVal("coveredby") == 'coverone') {
        $covertext = $coverone;
    } else if ($riskquery->getColumnVal("coveredby") == 'covertwo') {
        $covertext = $covertwo;
    } else if ($riskquery->getColumnVal("coveredby") == 'coverthree') {
        $covertext = $coverthree;
    }

    $html .= <<<HTML
        <tr>
            <td scope="row" >{$risknumber}</td>
            <td>{$riskname_it}</td>
            <td  class='header-data' style="text-align: center;">
                <div class="custom-control custom-checkbox">
                <input disabled type="checkbox" class="custom-control-input blue-highlight" id="{$customCheckId}" {$applicable} data-parsley-multiple="groups" data-parsley-mincheck="2">

                <label class="custom-control-label" for="{$customCheckId}"></label>
                </div>
            </td>

            <td  class='header-data'>
                <div class="col-sm-24">
                    {$covertext}
                </div>
            </td>
            <input class="form-control" type="hidden" value="{$riskquery->getColumnVal("idfillrisk_td")}" name="fillrisktd{$riskquery->getColumnVal("idfillrisk_td")}">
        </tr>
    HTML;

    $riskquery->moveNext();
}

$html .= <<<HTML
    </tbody>
    HTML;

$html .= '</tbody></table>';

$html .= '<br><a href="techdossier_step2.php?idtd=' . $idtd . '&idtrftd=' . $idtrftd . '"  target="_blank">
    <button type="button" class="btn btn-gradient-primary waves-effect waves-light">' . $editbutton . ' TF</button>
</a>';

//parts

$html .= "<table>
    <thead>
    <tr>
    <th colspan='7'>  COMPONENTI DEL DPI / PPE PARTS</th>
</tr>
        <tr>
            <th class='boldstyle' style='width: 5%;' >N.</th>
            <th class='boldstyle' style='width: 20%;'>{$descriptionpart}</th>
            <th class='boldstyle' style='width: 20%;'>{$articlepart}</th>
            <th class='boldstyle' style='width: 15%;'>{$colorpart}</th>
            <th class='boldstyle' style='width: 20%;'>{$descriptionpartlist}</th>
            <th class='boldstyle' style='width: 10%;'>{$reprtonumbertrdlabtitle}</th>
            <th class='boldstyle' style='width: 10%;'>{$trddatereporttitle}</th>

        </tr>
    </thead>
    <tbody>";

$partsquery = new WA_MySQLi_RS("partsquery", $cmctrfdb, 0);
$partsquery->setQuery("SELECT * FROM identificationparts WHERE identificationparts.idtrfdetails='$idtrftd'");
$partsquery->execute();

while (!$partsquery->atEnd()) {
    $html .= "<tr>
        <td  class='header-data'>{$partsquery->getColumnVal('partsidnumber')}</td>
        <td class='header-data'>{$partsquery->getColumnVal('description_identificationparts')}</td>
        <td class='header-data'>{$partsquery->getColumnVal('article_identificationparts')}</td>
        <td class='header-data'>{$partsquery->getColumnVal('color_identificationparts')}</td>
        <td class='header-data'>{$partsquery->getColumnVal('material_identificationparts')}</td>
        <td class='header-data'>{$partsquery->getColumnVal('cmcreportnumber_identificationparts')}</td>
        <td class='header-data'>{$partsquery->getColumnVal('cmcreportdate_identificationparts')}</td>
    </tr>";
    $partsquery->moveNext();
}
$html .= '</tbody></table>';

$html .= '<br><a href="techdossier_step3.php?idtd=' . $idtd . '&idtrftd=' . $idtrftd . '"  target="_blank">
    <button type="button" class="btn btn-gradient-primary waves-effect waves-light">' . $editbutton . ' TF</button>
</a>';
//ce mark example

$localisationppemarking = $row['localisationppemarking'];
$sizeexamplecemark = $row['sizeexamplecemark'];
$manufacturerlogoid = $row['manufacturerlogoid'];
$filenamelogo = $row['filenamelogo'];
$monthyearprod = $row['monthyearprod'];
$serialbatchnumber = $row['serialbatchnumber'];
$standarduse = $row['standarduse'];
$symbolsaddreq = $row['symbolsaddreq'];
$proddescription = $row['qualchecktext'];
$organismnumber = $row['organismnumber'];
$cemarkup = $row['cemarkupload'];
$filenamelogowithpath = "logos/" . $filenamelogo;



// Connessione al database
$conn = new mysqli($servername, $username, $password, $dbname);
$querytdfile = "SELECT idtdfileattached, filename_fileattached, description_fileattached FROM tdfileattached WHERE iddata_td = ? AND description_fileattached = 'CE mark example'";
$stmt = $conn->prepare($querytdfile);
$stmt->bind_param("i", $idtd);
$stmt->execute();
$resulttdfile = $stmt->get_result();

$fileUploaded = $resulttdfile->num_rows > 0;
$fileDetails = $resulttdfile->fetch_assoc();

$html .= <<<HTML
<table>
    <thead>
        <tr>
            <th colspan="2">ESEMPIO DI MARCATURA CE / EXAMPLE OF CE MARKING </th>
        </tr>
    </thead>
    <tbody>
HTML;

if ($cemarkup == 'Y' && $fileUploaded) {
    $filePath = 'uploadtddocuments/' . htmlspecialchars($fileDetails['filename_fileattached']);
    $fileDescription = htmlspecialchars($fileDetails['description_fileattached']);
    $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));

    $textexmark = ($_SESSION['langselect'] === 'en')
        ? 'FILE (if different from image, the file is included in the zip as an attachment)'
        : 'FILE (se diverso da immagine il file è caricato nello zip come allegato)';

    $html .= <<<HTML
<tr>
    <td class="first-column">$textexmark</td>
    <td>
HTML;


    if (in_array($fileExtension, ['jpg', 'jpeg', 'png', 'gif'])) {
        $html .= "<img src='$filePath' alt='$fileDescription' style='max-height: 200px;'>";
    } else {
        $html .= "<a href='$filePath' target='_blank'>$fileDescription</a>";
    }

    $html .= <<<HTML
            </td>
        </tr>
HTML;
} else {
    $html .= <<<HTML
    <tr>
        <td class="first-column">Posizione della marcatura sul DPI / Position of the marking on the PPE</td>
        <td class="header-data">{$localisationppemarking}</td>
    </tr>
    <tr>
        <td class="first-column">Marchio del fabbricante / Manufacturer's mark</td>
        <td><img src="{$filenamelogowithpath}" style="max-height: 80px;"></td>
    </tr>
    <tr>
        <td class="first-column">Codice Articolo / Article Code</td>
        <td class="header-data">$description</td>
    </tr>
    <tr>
        <td class="first-column">Indirizzo del fabbricante / manufacturer's address</td>
        <td class="header-data">$address - $country</td>
    </tr>
    <tr>
        <td class="first-column">Misura / Measure</td>
        <td class="header-data">{$sizeexamplecemark}</td>
    </tr>
    <tr>
        <td class="first-column">Mese ed anno di produzione / Production month and year</td>
        <td class="header-data">{$monthyearprod}</td>
    </tr>
    <tr>
        <td class="first-column">Numero di serie e/o di lotto / Serial number and/or lot</td>
        <td class="header-data">{$serialbatchnumber}</td>
    </tr>
    <tr>
        <td class="first-column">Numero ed anno della norma armonizzata utilizzata / Number and year of the harmonised standard used</td>
        <td class="header-data">{$standarduse}</td>
    </tr>
    <tr>
        <td class="first-column">Simbolo/i dei requisiti supplementari / Symbol(s) of additional requirements</td>
        <td class="header-data">{$symbolsaddreq}</td>
    </tr>
    <tr>
        <td class="first-column">Marcatura CE / CE marking</td>
        <td>
            <img src="{$base_url}assets/images/ce.jpg" alt="CE Image" style="width:100px;">
HTML;

    if (!is_null($organismnumber)) {
        $html .= " Numero Organismo: $organismnumber";
    }

    $html .= <<<HTML
        </td>
    </tr>
</tbody>
</table>
HTML;
}
$stmt->close();
$conn->close();




$html .= <<<HTML
            </td>
        </tr>

    </tbody>
</table>
HTML;

$html .= '<br><a href="techdossier_start.php?idtd=' . $idtd . '&idtrftd=' . $idtrftd . '"  target="_blank">
    <button type="button" class="btn btn-gradient-primary waves-effect waves-light">' . $editbutton . ' TF</button>
</a>';

//mezzi di controllo

$html .= <<<HTML

<table>
    <thead>
        <tr>
            <th colspan="1">MEZZI DI CONTROLLO E PROVA IN PRODUZIONE PER GARANTIRE LA CONFORMITÀ / MEANS USED DURING THE PRODUCTION TO ENSURE THE CONFORMITY</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            
            <td class="header-data">$proddescription</td>
        </tr>
       
    </tbody>
</table>
HTML;


//imballaggio

$packaging = $row['packaging'];
$declarconformity = $row['declarconformity'];
$webaddress = $row['webaddress'];
if ($declarconformity == 'declarone') {
    $declartext = $declarone;
} else {
    $declartext = $declartwo;
}

$html .= '<br><a href="techdossier_start.php?idtd=' . $idtd . '&idtrftd=' . $idtrftd . '"  target="_blank">
    <button type="button" class="btn btn-gradient-primary waves-effect waves-light">' . $editbutton . ' TF</button>
</a>';
$html .= <<<HTML

<table>
    <thead>
        <tr>
            <th colspan="2">IMBALLAGGIO / PACKAGING - DICHIARAZIONE DI CONFORMITÀ UE / EU DECLARATION OF CONFORMITY</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="first-column">{$packtitle}</td>
            <td class="header-data">{$packaging}</td>
        </tr>
        <tr>
            <td class="first-column">{$dectitle}</td>
            <td class="header-data">{$declartext}</td>
        </tr>
        <tr>
            <td class="first-column">{$webtitle}</td>
            <td class="header-data">{$webaddress}</td>
        </tr>

    </tbody>
</table>
HTML;

$html .= '<br><a href="techdossier_start.php?idtd=' . $idtd . '&idtrftd=' . $idtrftd . '"  target="_blank">
    <button type="button" class="btn btn-gradient-primary waves-effect waves-light">' . $editbutton . ' TF</button>
</a><br><br>';
// chiusura



$html .= '</tbody></table></body></html>';

echo $html;
// Carica il tuo HTML nel Dompdf
//$dompdf->loadHtml($html);

// (Opzionale) Imposta il formato e l'orientamento della pagina
// Puoi specificare la dimensione del documento e l'orientamento come segue:
// $dompdf->setPaper('A4', 'landscape');

// Renderizza il PDF
//$dompdf->render();

// Stream il PDF al browser
//$dompdf->stream("documento.pdf", array("Attachment" => false));
// Cambia "Attachment" a true se vuoi forzare il download del PDF
