<?php ob_start(); ?>
<?php include('./config/db.php'); ?>
<?php 
$selPlan=(isset($_GET['selPlan']))?$_GET['selPlan']:"";

$sql=$conexion->prepare("select fi.id, pp.nombrePlan, cli.nombre, act.detalle, fi.fechaRealizacion, fi.adjunto 
from formularioingreso fi inner join planprevencion pp on fi.planId=pp.id
inner join cliente cli on cli.id = fi.clienteId
inner join actividades act on act.id = fi.actividadId
where pp.id in (:planId) order by fi.fechaRealizacion asc");
$sql->bindParam(":planId",$selPlan, PDO::PARAM_INT);
$sql->execute();
$ingresos = $sql->fetchAll(PDO::FETCH_ASSOC);
?>
<style>
    th {
    font-family: 'font';
    font-style: normal;
    font-weight: normal;
}
.align {
    text-align: right;      
}
</style>
<table border="0">
<tr>
    </tr>
    <tr>
        <th colspan="6">&nbsp;</th>
    </tr>
    <tr>
        <th colspan="6"><strong><u><?php 
                    $sql=$conexion->prepare("select nombrePlan from planprevencion where id=:id;");  
                    $sql->bindParam(":id",$selPlan);
                    $sql->execute();
                    $nomPlan = $sql->fetch(PDO::FETCH_LAZY);

                    echo $nomPlan['nombrePlan']; 
                    ?></u></strong></th>
    </tr>
    <tr>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th colspan="2"><h6><p class="align">Las Condes,&nbsp;&nbsp;<?php 
                    $today = date("Y-m-d H:i:s");  
                    echo date("d-m-Y",strtotime($today)); ?>.
                    </p></h6></th>
    </tr>
    <tr>
        <th colspan="6">&nbsp;</th>
    </tr>
    <tr >
        <th colspan="6" align="left">
            El siguiente plan cuenta con las siguientes acciones ingresadas: 
        </th>
    </tr>
    <tr>
        <th colspan="6">&nbsp;</th>
    </tr>
    <thead>
        <tr>
            <th><strong>Id</strong></th>
            <th><strong>Cliente</strong></th>
            <th><strong>Actividad</strong></th>
            <th><strong>F. realizaci&oacute;n</strong></th>
            <th><strong>Adjunto</strong></th>
            <th><strong>Instalaci&oacute;n</strong></th>
        </tr>
    </thead>
    <tbody>
        <?php 
        
        foreach($ingresos as $acta) { ?>
        <tr>
            <td align="center"><?php echo $acta['id'] ?></td>
            <td ><?php echo $acta['nombre'] ?></td>
            <td ><?php echo $acta['detalle'] ?></td>
            <td ><?php echo date("d-m-Y",strtotime($acta['fechaRealizacion']))?></td>
            <td ><?php echo $acta['adjunto'] ?></td>
            <td >
                <?php 
                    $sql=$conexion->prepare("SELECT inst.id, inst.nombre as nomInsti FROM formularioingreso ing inner JOIN 
                    formularioinstalacion ins on ins.formularioId = ing.id inner JOIN instalacion inst on inst.id = ins.instalacionId 
                    where ing.id in (:id) order by ins.id asc;");
                    $sql->bindParam(":id",$acta['id']);
                    $sql->execute();
                    $instalaciones = $sql->fetchAll(PDO::FETCH_ASSOC);
                    foreach($instalaciones as $inst) { 
                        echo ($inst['nomInsti']."<br>");
                    }
                ?>
            </td>

        </tr>
        <?php } ?>
    </tbody>
    
</table>

<?php 

$html = ob_get_clean();
//echo ($html);

//requiere_once './libreria/dompdf/autoload.inc.php';
include('./libreria/dompdf/autoload.inc.php'); 
use Dompdf\Dompdf;
$dompdf = new Dompdf();

$options = $dompdf->getOptions();
$options->set(array('isRemoteEnabled' => true));
$dompdf->setOptions($options);

$dompdf->loadHtml($html);

$dompdf->setPaper('letter');
//$dompdf->setPaper('A4','landscape');  //A4 horizontal

$dompdf->render();

$dompdf->stream('archivo_.pdf',array("Attachment" => true));  //true (lo descarga) - false (lo visualiza en navegador)

?>