<?php
    namespace App\Libraries\fpdf;

    use App\SuperUsuario;
    use Illuminate\Support\Facades\Auth;

    include "fpdf.php";
    // use App\Libraries\fpdf\FPDF;

    class EstructuraPDF extends FPDF {

        var $B=0;
        var $I=0;
        var $U=0;
        var $HREF='';
        var $ALIGN='';
        var $widths;
        var $minimoResaltado = 8;
        var $aligns;
        var $con_inserto_incluido;
        public $gris_claro = [253, 253, 254];
        public $gris_oscuro = [240, 240, 241];
        public $borde_table = [174, 182, 191];
        public $fondo_table = [214, 219, 223];


        function SetWidths($w){
            //Set the array of column widths
            $this->widths=$w;
        }


        function SetAligns($a){
            //Set the array of column alignments
            $this->aligns=$a;
        }


        function Row($data){
            //Calculate the height of the row
            $factor = 4.2;
            $nb=0;
            for($i=0;$i<count($data);$i++)
                $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
            $h=$factor*$nb;
            //Issue a page break first if needed
            $this->CheckPageBreak($h);
            //Draw the cells of the row
            for($i=0;$i<count($data);$i++){

                if($i == 0){
                    $this->SetFont('Arial','B',5);
                } else if($i == 1) {
                    $this->SetFont('Arial','',5);
                }

                $w=$this->widths[$i];
                $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
                $x=$this->GetX();
                $y=$this->GetY();
                $this->SetTextColor(40, 40, 40);
                $this->SetLineWidth(0.2);
                $this->SetDrawColor(146, 169, 185);
                $this->Cell($w,$h,"",0,0,0,1);
                $this->SetXY($x,$y);

                if($i == 0){
                    $this->MultiCell($w,$factor,$data[$i],"L",$a,false);
                } else if(($i+1) == count($data)) { // Si es el ultimo
                    $this->MultiCell($w,$factor,$data[$i],"R",$a,false);
                } else {
                    $this->MultiCell($w,$factor,$data[$i],0,$a,false);
                }

                $this->SetXY($x+$w,$y);
            }
            //Go to the next line
            $this->Ln($h);
        }

        function RowEncabezado($data){
            //Calculate the height of the row
            $nb=0;
            for($i=0;$i<count($data);$i++)
                $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
            $h=3.5*$nb;
            //Issue a page break first if needed
            $this->CheckPageBreak($h);
            //Draw the cells of the row
            for($i=0;$i<count($data);$i++)
            {

                $w=$this->widths[$i];
                $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
                //Save the current position
                $x=$this->GetX();
                $y=$this->GetY();
                //Draw the border
                // $this->SetFillColor(255, 255, 255);

                $this->SetTextColor(40, 40, 40);

                $this->SetLineWidth(0.2);
                $this->SetDrawColor(146, 169, 185);

                $this->Cell($w,$h,"",0,0,0,1);
                $this->SetXY($x,$y);

                if($data[$i] == "\n" || $data[$i] == "N/A\n"){
                    $this->SetTextColor(147,183,196);
                    $data[$i] = "N/A";
                } else {
                    $this->SetTextColor(40, 40, 40);
                }

                $this->MultiCell($w,3.5,$data[$i],0,$a,false);
                $this->Rect($x,$y,$w,$h);


                if($this->con_inserto_incluido){ // Si tiene el inserto incluido
                    if($i == 9 || $i == 16 || $i == 23){
                        $this->SetLineWidth(0.3);
                        $this->SetDrawColor(27, 100, 155);
                        $this->Line($x, $y, $x, $y + $h);
                    } else if($i == 28){
                        $this->SetLineWidth(0.3);
                        $this->SetDrawColor(27, 100, 155);
                        $this->Line($x + $w, $y, $x + $w, $y + $h);
                    }
                } else { // Si no va a estar el inserto
                    if($i == 9 || $i == 16){
                        $this->SetLineWidth(0.3);
                        $this->SetDrawColor(27, 100, 155);
                        $this->Line($x, $y, $x, $y + $h);
                    } else if($i == 22){
                        $this->SetLineWidth(0.3);
                        $this->SetDrawColor(27, 100, 155);
                        $this->Line($x + $w, $y, $x + $w, $y + $h);
                    }
                }


                $this->SetXY($x+$w,$y);
            }
            //Go to the next line
            $this->Ln($h);
        }


        function CheckPageBreak($h){
            //If the height h would cause an overflow, add a new page immediately
            if($this->GetY()+$h>$this->PageBreakTrigger)
                $this->AddPage($this->CurOrientation);
        }


        function NbLines($w,$txt){
            //Computes the number of lines a MultiCell of width w will take
            $cw=&$this->CurrentFont['cw'];
            if($w==0)
                $w=$this->w-$this->rMargin-$this->x;
            $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
            $s=str_replace("\r",'',$txt);
            $nb=strlen($s);
            if($nb>0 and $s[$nb-1]=="\n")
                $nb--;
            $sep=-1;
            $i=0;
            $j=0;
            $l=0;
            $nl=1;
            while($i<$nb)
            {
                $c=$s[$i];
                if($c=="\n")
                {
                    $i++;
                    $sep=-1;
                    $j=$i;
                    $l=0;
                    $nl++;
                    continue;
                }
                if($c==' ')
                    $sep=$i;
                $l+=$cw[$c];
                if($l>$wmax)
                {
                    if($sep==-1)
                    {
                        if($i==$j)
                            $i++;
                    }
                    else
                        $i=$sep+1;
                    $sep=-1;
                    $j=$i;
                    $l=0;
                    $nl++;
                }
                else
                    $i++;
            }
            return $nl;
        }


        function WriteHTML($html){
            //HTML parser
            $html=str_replace("\n",' ',$html);
            $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
            foreach($a as $i=>$e)
            {
                if($i%2==0)
                {
                    //Text
                    if($this->HREF)
                        $this->PutLink($this->HREF,$e);
                    elseif($this->ALIGN=='center')
                        $this->Cell(0,3,$e,0,1,'C');
                    else
                        $this->Write(3,$e);
                }
                else
                {
                    //Tag
                    if($e[0]=='/')
                        $this->CloseTag(strtoupper(substr($e,1)));
                    else
                    {
                        //Extract properties
                        $a2=explode(' ',$e);
                        $tag=strtoupper(array_shift($a2));
                        $prop=array();
                        foreach($a2 as $v)
                        {
                            if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
                                $prop[strtoupper($a3[1])]=$a3[2];
                        }
                        $this->OpenTag($tag,$prop);
                    }
                }
            }
        }


        function OpenTag($tag,$prop){
            //Opening tag
            if($tag=='B' || $tag=='I' || $tag=='U')
                $this->SetStyle($tag,true);
            if($tag=='A')
                $this->HREF=$prop['HREF'];
            if($tag=='BR')
                $this->Ln(5);
            if($tag=='P')
                $this->ALIGN=$prop['ALIGN'];
            if($tag=='HR')
            {
                if( !empty($prop['WIDTH']) )
                    $Width = $prop['WIDTH'];
                else
                    $Width = $this->w - $this->lMargin-$this->rMargin;
                $this->Ln(2);
                $x = $this->GetX();
                $y = $this->GetY();
                $this->SetLineWidth(0.4);
                $this->Line($x,$y,$x+$Width,$y);
                $this->SetLineWidth(0.2);
                $this->Ln(2);
            }
        }


        function CloseTag($tag){
            //Closing tag
            if($tag=='B' || $tag=='I' || $tag=='U')
                $this->SetStyle($tag,false);
            if($tag=='A')
                $this->HREF='';
            if($tag=='P')
                $this->ALIGN='';
        }


        function SetStyle($tag,$enable){
            //Modify style and select corresponding font
            $this->$tag+=($enable ? 1 : -1);
            $style='';
            foreach(array('B','I','U') as $s)
                if($this->$s>0)
                    $style.=$s;
            $this->SetFont('',$style);
        }


        function PutLink($URL,$txt){
            //Put a hyperlink
            $this->SetTextColor(0,0,255);
            $this->SetStyle('U',true);
            $this->Write(5,$txt,$URL);
            $this->SetStyle('U',false);
            $this->SetTextColor(0);
        }

        function RoundedRect($x, $y, $w, $h, $r, $corners = '1234', $style = '')
    {
        $k = $this->k;
        $hp = $this->h;
        if($style=='F')
            $op='f';
        elseif($style=='FD' || $style=='DF')
            $op='B';
        else
            $op='S';
        $MyArc = 4/3 * (sqrt(2) - 1);
        $this->_out(sprintf('%.2F %.2F m',($x+$r)*$k,($hp-$y)*$k ));

        $xc = $x+$w-$r;
        $yc = $y+$r;
        $this->_out(sprintf('%.2F %.2F l', $xc*$k,($hp-$y)*$k ));
        if (strpos($corners, '2')===false)
            $this->_out(sprintf('%.2F %.2F l', ($x+$w)*$k,($hp-$y)*$k ));
        else
            $this->_Arc($xc + $r*$MyArc, $yc - $r, $xc + $r, $yc - $r*$MyArc, $xc + $r, $yc);

        $xc = $x+$w-$r;
        $yc = $y+$h-$r;
        $this->_out(sprintf('%.2F %.2F l',($x+$w)*$k,($hp-$yc)*$k));
        if (strpos($corners, '3')===false)
            $this->_out(sprintf('%.2F %.2F l',($x+$w)*$k,($hp-($y+$h))*$k));
        else
            $this->_Arc($xc + $r, $yc + $r*$MyArc, $xc + $r*$MyArc, $yc + $r, $xc, $yc + $r);

        $xc = $x+$r;
        $yc = $y+$h-$r;
        $this->_out(sprintf('%.2F %.2F l',$xc*$k,($hp-($y+$h))*$k));
        if (strpos($corners, '4')===false)
            $this->_out(sprintf('%.2F %.2F l',($x)*$k,($hp-($y+$h))*$k));
        else
            $this->_Arc($xc - $r*$MyArc, $yc + $r, $xc - $r, $yc + $r*$MyArc, $xc - $r, $yc);

        $xc = $x+$r ;
        $yc = $y+$r;
        $this->_out(sprintf('%.2F %.2F l',($x)*$k,($hp-$yc)*$k ));
        if (strpos($corners, '1')===false)
        {
            $this->_out(sprintf('%.2F %.2F l',($x)*$k,($hp-$y)*$k ));
            $this->_out(sprintf('%.2F %.2F l',($x+$r)*$k,($hp-$y)*$k ));
        }
        else
            $this->_Arc($xc - $r, $yc - $r*$MyArc, $xc - $r*$MyArc, $yc - $r, $xc, $yc - $r);
        $this->_out($op);
    }

    function _Arc($x1, $y1, $x2, $y2, $x3, $y3)
    {
        $h = $this->h;
        $this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c ', $x1*$this->k, ($h-$y1)*$this->k,
            $x2*$this->k, ($h-$y2)*$this->k, $x3*$this->k, ($h-$y3)*$this->k));
    }


    function Header(){

        $this->SetLineWidth(0.3);

        // Recuadro ancho completo
        $this->SetXY(5,5);
        $this->SetDrawColor(28, 80, 164);
        $this->SetTextColor(24, 74, 156);
        $this->Cell(206,23,"",1,0,'L',0);


        // Recuadro imagen de logo de Quik
        $this->SetXY(5,5);
        $this->Cell(55,23,"",1,0,'L',0);

        // Logo Quik
        if(Auth::user()->rol_id_rol == 5){
    //        $this->Image(asset('img/logo-quik.png'),10,11,21);
            ////       $this->Image(asset('/img/logo-quik.png'), 10, 11, 21);
        }else{
            $clientReference = SuperUsuario::query()->where('id', Auth::user()->super_admin_id)->first();
            if(!isset($clientReference)){
        //        $this->Image(asset('img/logo-quik.png'),10,11,21);
                ////       $this->Image(asset('/img/logo-quik.png'), 10, 11, 21);
            } else {
        //        $this->Image(asset($clientReference->imagen_cliente), 10, 11, 21);
            }
        }

        // Linea divisora vertical
//        $this->SetLineWidth(0.1);
//        $this->line(34,12,34,22);
//
//        // Logo de V2M
////        $this->Image(asset('img/v2m.jpg'),38,12,18);

        // Recuadro del titulo
        $this->SetXY(60,5);
        $this->Cell(96,23,"",1,0,'L',0);

        // Titulo Principal
        $this->SetFont('Arial','B',9);
        $this->SetXY(60,13);
        $this->MultiCell(96,4,"COMPARACIÓN DE MÉTODOS Y ESTIMACIÓN DEL SESGO UTILIZANDO MUESTRAS DE PACIENTES",0,'C',0);

        // Recuadro de convenciones del formato
        $this->SetXY(-60,5);
        $this->Cell(55,23,"",1,0,'L',0);


        // Seccion de los valores fijos del formato
        $this->SetTextColor(28, 80, 164);
        $this->SetFont('Arial','B',8);
        $this->SetXY(-60,5);
        $this->Cell(27.5,5.75,"Código:",1,0,'C',0);

        $this->SetXY(-60,10.75);
        $this->Cell(27.5,5.75,"Vigente desde:",1,0,'C',0);

        $this->SetXY(-60,16.5);
        $this->Cell(27.5,5.75,"Versión:",1,0,'C',0);

        $this->SetXY(-60,22.25);
        $this->Cell(27.5,5.75,"Página:",1,0,'C',0);


        // Valores de los codigos fijos del formato
        $this->SetFont('Arial','B',7);
        $this->SetXY(-32.5,5);
        $this->Cell(27.5,5.75,"V2M-EP-09 A2",1,0,'C',0);

        $this->SetXY(-32.5,10.75);
        $this->Cell(27.5,5.75,"25/11/2020",1,0,'C',0);

        $this->SetXY(-32.5,16.5);
        $this->Cell(27.5,5.75,"1",1,0,'C',0);

        $this->SetXY(-32.5,22.25);
        $this->Cell(27.5,5.75, "  ".$this->PageNo() . " de {nb}",1,0,'C',0);

        $this->Ln(12);
        $this->SetX(10);
    }

        function CreateDocument($analitoCorrida,$resultados,$promedios,$resultados_c5,$promedios_c5, $PDFObj){
            $this->AliasNbPages();
            $this->SetAutoPageBreak(true,5);
            $this->AddPage();
            $this->SetTextColor(40, 40, 40);

            // Subtitulo
            $this->Ln(6);
            $this->SetFont('Arial','B',11);
            $this->SetTextColor(28, 80, 164);
            $this->Cell(196,4,"Información general de la comparación",0,0,'C',0);
            $this->Ln(8);

            // Codigo de proyecto
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','B',9.5);
            $this->Cell(36,5,"Código del proyecto:",0,0,'L',0);
            $this->SetFont('Arial','',9.5);
            $this->Cell(50,5, "V2M-EP09-".$analitoCorrida->id_corrida_area ,0,0,'L',0);

            // Metodo de prueba
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','B',9.5);
            $this->Cell(56,5,"Método de prueba",0,0,'L',0);

            // Metodo de comparación
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','B',9.5);
            $this->Cell(56,5,"Método de comparación",0,1,'L',0);


            // Segundo nivel
            // Area
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','B',9.5);
            $this->Cell(11,5,"Área:",0,0,'L',0);
            $this->SetFont('Arial','',9.5);
            $this->Cell(75,5,$analitoCorrida->nom_area,0,0,'L',0);

            // Metodo de prueba
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','',9.5);
            $this->Cell(56,5,$analitoCorrida->nom_analizador_prueba,0,0,'L',0);

            // Metodo de comparación
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','',9.5);
            $this->Cell(56,5,$analitoCorrida->nom_analizador_comparacion,0,1,'L',0);

            // Tercer nivel
            // Area
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','B',9.5);
            $this->Cell(21,5,"Laboratorio:",0,0,'L',0);
            $this->SetFont('Arial','',9.5);
            $this->Cell(65,5,$analitoCorrida->no_laboratorio . " - ". $analitoCorrida->nom_laboratorio,0,0,'L',0);

            // Metodo de prueba
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','',9.5);
            $this->Cell(56,5,$analitoCorrida->nom_metodologia_prueba,0,0,'L',0);

            // Metodo de comparación
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','',9.5);
            $this->Cell(56,5,$analitoCorrida->nom_metodologia_comparacion,0,1,'L',0);


            // Cuarto nivel
            // Area
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','B',9.5);
            $this->Cell(26,5,"Fecha de inicio:",0,0,'L',0);
            $this->SetFont('Arial','',9.5);
            $this->Cell(60,5,$analitoCorrida->fecha_inicio,0,0,'L',0);

            // Metodo de prueba
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','',9.5);
            $this->Cell(56,5,$analitoCorrida->nom_modo_metodologia_prueba,0,0,'L',0);

            // Metodo de comparación
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','',9.5);
            $this->Cell(56,5,$analitoCorrida->nom_modo_metodologia_comparacion,0,1,'L',0);


            // Quinto nivel
            // Nada
            $this->SetTextColor(255, 0, 0);
            $this->SetFont('Arial','B',9.5);
            $this->Cell(26,5,"",0,0,'L',0);
            $this->SetFont('Arial','',9.5);
            $this->Cell(60,5,"",0,0,'L',0);

            // Metodo de prueba
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','',9.5);
            $this->Cell(56,5,$analitoCorrida->nom_unidad_metodologia_prueba,0,0,'L',0);

            // Metodo de comparación
            $this->SetTextColor(0, 0, 0);
            $this->SetFont('Arial','',9.5);
            $this->Cell(56,5,$analitoCorrida->nom_unidad_metodologia_comparacion,0,0,'L',0);


            // Subtitulo
            $this->Ln(10);
            $this->SetFont('Arial','B',11);
            $this->SetTextColor(28, 80, 164);
            $this->Cell(196,4,"Gráficos de correlación",0,0,'C',0);


            // Contenedores de graficos
            $this->SetDrawColor(220,220,220);
            $this->RoundedRect(10, 86, 96.15, 70.15, 1.2);
            $this->RoundedRect(110, 86, 96.15, 70.15, 1.2);
            $this->RoundedRect(10, 160, 96.15, 76.15, 1.2);
            $this->RoundedRect(110, 160, 96.15, 76.15, 1.2);

            $this->SetDrawColor(195,195,195);
            $this->RoundedRect(10, 86, 96, 70, 1.2);
            $this->RoundedRect(110, 86, 96, 70, 1.2);
            $this->RoundedRect(10, 160, 96, 76, 1.2);
            $this->RoundedRect(110, 160, 96, 76, 1.2);


            // Inclusión de los graficos
    //        $this->Image($PDFObj->urlImgB1,13,95,90);
    //        $this->Image($PDFObj->urlImgB2,113,95,90);
    //        $this->Image($PDFObj->urlImgB3,13,174,90);
    //        $this->Image($PDFObj->urlImgB4,113,174,90);


            // Nombres de cada uno de los graficos
            $this->RoundedRect(10, 86, 96, 7.5, 1.2, "12");
            $this->RoundedRect(110, 86, 96, 7.5, 1.2, "12");
            $this->RoundedRect(10, 160, 96, 12, 1.2, "12");
            $this->RoundedRect(110, 160, 96, 12, 1.2, "12");


            // Subtitulo
            $this->SetXY(10,243);
            $this->SetFont('Arial','B',9);
            $this->SetTextColor(65,65,65);
            $this->Cell(196,4,"Observaciones",0,0,'C',0);
            $this->SetDrawColor(150,150,150,150);
            $this->RoundedRect(10, 249, 196, 16, 1.2);


            $this->SetFont('Arial','B',8);
            $this->SetTextColor(100,100,100,100);
            $this->SetXY(10, 88);
            $this->MultiCell(96,4,"  B1. Gráfico de dispersión para la media de las replicas",0,'L',0);
            $this->SetXY(110, 88);
            $this->MultiCell(96,4,"  B2. Gráfico de dispersión para todos los resultados",0,'L',0);
            $this->SetXY(10, 162);
            $this->MultiCell(96,4,"  B3. Gráfica de diferencias entre métodos. En Unidades",0,'L',0);
            $this->SetXY(110, 162);
            $this->MultiCell(96,4,"  B4. Gráfica de diferencias entre métodos. En Porcentaje %",0,'L',0);

            $this->AddPage();

            // Subtitulo
            $this->Ln(3);
            $this->SetFont('Arial','B',11);
            $this->SetTextColor(28, 80, 164);
            $this->Cell(196,4,"Apéndice A y B",0,1,'C',0);

            $wInd = 196 / 14;

            $wItem = $wInd * 1;
            $wMetodoPrueba = $wInd * 3;
            $wMetodoComparativo = $wInd * 3;
            $wValorAbs1 = $wInd * 1;
            $wValorAbs2 = $wInd * 1;
            $wPromedioGeneral = $wInd * 1;
            $wPromedioDiferecias = $wInd * 1;
            $wR1 = $wInd * 1;
            $wR2 = $wInd * 1;


            $this->SetDrawColor(146, 169, 185);
            $this->SetFillColor(191, 208, 222);
            $this->SetFont('Arial','B',5);
            $this->SetTextColor(0,0,0);


            // Primer nivel de enumeración
            $this->SetX(10);
            $this->Ln(3);
            $this->Cell($wInd,2,"1",0,0,'C',0); // It
            $this->Cell($wInd,2,"2",0,0,'C',0); // Codigo paciente
            $this->Cell($wInd,2,"3",0,0,'C',0); // R1
            $this->Cell($wInd,2,"4",0,0,'C',0); // R2
            $this->Cell($wInd,2,"5",0,0,'C',0); // X de repeticiones
            $this->Cell($wInd,2,"6",0,0,'C',0); // R1
            $this->Cell($wInd,2,"7",0,0,'C',0); // R2
            $this->Cell($wInd,2,"8",0,0,'C',0); // X de repeticiones
            $this->Cell($wInd,2,"9",0,0,'C',0); // abs 1
            $this->Cell($wInd,2,"10",0,0,'C',0); // abs 2
            $this->Cell($wInd,2,"11",0,0,'C',0); // promedio general
            $this->Cell($wInd,2,"12",0,0,'C',0); // diferencia
            $this->Cell($wInd,2,"13",0,0,'C',0); // R1
            $this->Cell($wInd,2,"14",0,1,'C',0); // R2

            // Enumeración tabla
            $this->SetXY(8.5, 38);
            $this->SetFont('Arial','B',8);
            $this->SetTextColor(0,0,0);
            $this->Cell(15,4,"Tabla 1.",0,1,'L',0);


            $this->SetDrawColor(146, 169, 185);
            $this->SetFillColor(191, 208, 222);
            $this->SetFont('Arial','B',5);
            $this->SetTextColor(0,0,0);

            // Enunciados primer nivel
            $setX = 10;
            $setY = 47;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wItem,3,"\n\nMuestra\n#\n\n","TLB",'C',0);
            $setX = $setX + $wItem;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wItem,3,"\n\n\n\n\n","TB",'C',0);
            $setX = $setX + $wItem;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wMetodoPrueba,3,"\nMétodo de Prueba (Y)\n\n","T",'C',0);
            $setX = $setX + $wMetodoPrueba;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wMetodoComparativo,3,"\nMétodo Comparativo (X)\n\n","T",'C',0);
            $setX = $setX + $wMetodoComparativo;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wValorAbs1,3,"Valor absoluto diferencia resultados método prueba","TB",'C',0);
            $setX = $setX + $wValorAbs1;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wValorAbs2,3,"Valor absoluto diferencia resultados método comparativo","TB",'C',0);
            $setX = $setX + $wValorAbs2;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wPromedioGeneral,3,"\nPromedio de ambos métodos\n\n","TB",'C',0);
            $setX = $setX + $wPromedioGeneral;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wPromedioDiferecias,3,"Promedio de método de prueba menos método comparativo","TB",'C',0);
            $setX = $setX + $wPromedioDiferecias;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wR1,3,"R1 mét prueba\n-\nmét Comparativo	","TB",'C',0);
            $setX = $setX + $wR1;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wR2,3,"R2 mét prueba\n-\nmét Comparativo","TRB",'C',0);

            // Enunciados segundo nivel
            $setX = 10;
            $setY = 47 + 3 * 3;
            $setX = $setX + $wInd;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wInd,3,"Código paciente\n","B",'C',0);

            $setX = $setX + $wInd;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wInd,3,"R1\n\n","B",'C',0);

            $setX = $setX + $wInd;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wInd,3,"R2\n\n","B",'C',0);

            $setX = $setX + $wInd;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wInd,3,"X de repeticiones\n","B",'C',0);

            $setX = $setX + $wInd;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wInd,3,"R1\n\n","B",'C',0);

            $setX = $setX + $wInd;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wInd,3,"R2\n\n","B",'C',0);

            $setX = $setX + $wInd;
            $this->SetXY($setX,$setY);
            $this->MultiCell($wInd,3,"X de repeticiones\n","B",'C',0);

            $this->SetXY(10, 62);

            foreach($resultados as $index => $resultado) {

                if($index % 2 == 0){
                    $this->SetFillColor($this->gris_oscuro[0], $this->gris_oscuro[1], $this->gris_oscuro[2]);
                } else {
                    $this->SetFillColor($this->gris_claro[0], $this->gris_claro[1], $this->gris_claro[2]);
                }

                $this->SetWidths(array(
                    $wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,
                ));
                $this->SetAligns(array(
                    "C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C",
                ));
                $this->Row(array(
                    $index+1,
                    $resultado->codigo_paciente,
                    round($resultado->resultado_m1c1,2),
                    round($resultado->resultado_m1c2,2),
                    round($resultado->promedio_metodo_prueba,2),
                    round($resultado->resultado_m2c1,2),
                    round($resultado->resultado_m2c2,2),
                    round($resultado->promedio_metodo_comparativo,2),
                    round($resultado->valor_absoluto_metodo_prueba,2),
                    round($resultado->valor_absoluto_metodo_comparativo,2),
                    round($resultado->promedio_ambos_metodos,2),
                    round($resultado->metodo_prueba_menos_metodo_comparacion,2),
                    round($resultado->r1_menos_metodo_comparacion,2),
                    round($resultado->r2_menos_metodo_comparacion,2)
                ));
            }

            // Borde final de la tabla
            $this->Cell($wInd * 13,0,"","T",0,'C',0);

            // Seccion C.1
            $this->AddPage();

            // Subtitulo
            $this->Ln(3);
            $this->SetFont('Arial','B',11);
            $this->SetTextColor(28, 80, 164);
            $wInd = 196 / 12;
            $this->Cell(196,4,"C1. Dentro-Método Duplicados Check (Sección 4.1)",0,1,'C',0);
            $this->Ln(5);

            if(
                isset($resultados[0]->resultado_m1c1) &&
                isset($resultados[0]->resultado_m1c2) &&
                isset($resultados[0]->resultado_m2c1) &&
                isset($resultados[0]->resultado_m2c2)
            ){

                $this->SetDrawColor($this->borde_table[0], $this->borde_table[1], $this->borde_table[2]);
                $this->SetFillColor($this->fondo_table[0], $this->fondo_table[1], $this->fondo_table[2]);
                $this->SetFont('Arial','',6);
                $this->SetTextColor(0,0,0);

                // Primer fila de la tabla
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->resultado_m2c1,2),1,0,'C',0);
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->resultado_m2c2,2),1,0,'C',0);
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->valor_absoluto_metodo_prueba,2),1,0,'C',0);
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->promedio_metodo_prueba,2),1,1,'C',0);


                // Segunda fila de la tabla
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->resultado_m1c1,2),1,0,'C',0);
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->resultado_m1c2,2),1,0,'C',0);
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->indice_metodo_comparacion,2),1,0,'C',0);
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->indice_metodo_prueba,2),1,1,'C',0);


                // Tercera fila de la tabla
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->valor_absoluto_metodo_comparativo,2),1,0,'C',0);
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->promedio_metodo_comparativo,2),1,0,'C',0);


                // Impresión de imágenes
        //        $this->Image(asset('img/formulas/x11.png'),23,47.5,7);
        //        $this->Image(asset('img/formulas/x12.png'),72,47.2,7.6);
        //        $this->Image(asset('img/formulas/form1.png'),112,48,25);
        //        $this->Image(asset('img/formulas/form2.png'),162,47,24);
        //        $this->Image(asset('img/formulas/y11.png'),22,56,9);
        //        $this->Image(asset('img/formulas/y12.png'),71,56,9.6);
        //        $this->Image(asset('img/formulas/form3.png'),115,54.5,20);
        //        $this->Image(asset('img/formulas/form4.png'),164,54.5,20);
        //        $this->Image(asset('img/formulas/form5.png'),15,64,25);
        //        $this->Image(asset('img/formulas/form6.png'),66.5,63,20);
            }


            // Enumeración tabla
            $this->SetXY(8.5, 38);
            $this->SetFont('Arial','B',8);
            $this->SetTextColor(0,0,0);
            $this->Cell(15,4,"Tabla 2.",0,1,'L',0);

            // Primer nivel de enumeración
            $wInd = 196 / 9;
            $this->SetXY(10, 75);
            $this->SetFont('Arial','B',5);
            $this->Cell($wInd,2,"1",0,0,'C',0); // It
            $this->Cell($wInd,2,"2",0,0,'C',0); // Xi1
            $this->Cell($wInd,2,"3",0,0,'C',0); // Xi2
            $this->Cell($wInd,2,"4",0,0,'C',0); // Yi1
            $this->Cell($wInd,2,"5",0,0,'C',0); // Yi2
            $this->Cell($wInd,2,"6",0,0,'C',0); // Dxi
            $this->Cell($wInd,2,"7",0,0,'C',0); // Dyi
            $this->Cell($wInd,2,"8",0,0,'C',0); // Dx'i
            $this->Cell($wInd,2,"9",0,1,'C',0); // Dy'i



            $this->SetDrawColor(146, 169, 185);
            $this->SetFillColor(191, 208, 222);
            $this->SetTextColor(0,0,0);

            // Enunciados primer nivel
            $this->Cell($wInd,6,"Muestra #","TLB",0,'C',0); // It
            $this->Cell($wInd,6,"","TB",0,'C',0); // Xi1
            $this->Cell($wInd,6,"","TB",0,'C',0); // Xi2
            $this->Cell($wInd,6,"","TB",0,'C',0); // Yi1
            $this->Cell($wInd,6,"","TB",0,'C',0); // Yi2
            $this->Cell($wInd,6,"","TB",0,'C',0); // Dxi
            $this->Cell($wInd,6,"","TB",0,'C',0); // Dyi
            $this->Cell($wInd,6,"","TB",0,'C',0); // Dx'i
            $this->Cell($wInd,6,"","TRB",1,'C',0); // Dy'i

            // Impresión de imágenes del encabezado
    //        $this->Image(asset('img/formulas/xi1.png'),40,78,6.5);
    //        $this->Image(asset('img/formulas/xi2.png'),60.8,77.7,8);
    //        $this->Image(asset('img/formulas/yi1.png'),83,78,6.5);
    //        $this->Image(asset('img/formulas/yi2.png'),105.2,77.7,5.5);
    //        $this->Image(asset('img/formulas/dxi.png'),126,78,7.5);
    //        $this->Image(asset('img/formulas/dyi.png'),148.5,78.5,6.1);
    //        $this->Image(asset('img/formulas/dxti.png'),170,78,7.9);
    //        $this->Image(asset('img/formulas/dyti.png'),191.5,78.5,8.3);
            $this->SetFont('Arial','',5);

            foreach($resultados as $index => $resultado) {

                if($index % 2 == 0){
                    $this->SetFillColor($this->gris_oscuro[0], $this->gris_oscuro[1], $this->gris_oscuro[2]);
                } else {
                    $this->SetFillColor($this->gris_claro[0], $this->gris_claro[1], $this->gris_claro[2]);
                }

                $this->SetWidths(array(
                    $wInd, $wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,
                ));
                $this->SetAligns(array(
                    "C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C",
                ));
                $this->Row(array(
                    $index+1,
                    round($resultado->resultado_m2c1,2),
                    round($resultado->resultado_m2c2,2),
                    round($resultado->resultado_m1c1,2),
                    round($resultado->resultado_m1c2,2),
                    round($resultado->valor_absoluto_metodo_comparativo,2),
                    round($resultado->valor_absoluto_metodo_prueba,2),
                    round($resultado->indice_metodo_comparacion,2),
                    round($resultado->indice_metodo_prueba,2)
                ));
            }

            // Borde final de la tabla
            $this->Cell($wInd * 9,0,"","T",0,'C',0);

            $this->Ln(2);

            // Seccion de promedios de la seccion C.1.
            $wInd = 196 / 9;
            $this->SetDrawColor($this->borde_table[0], $this->borde_table[1], $this->borde_table[2]);
            $this->SetFillColor($this->fondo_table[0], $this->fondo_table[1], $this->fondo_table[2]);
            $this->SetFont('Arial','',6);
            $this->SetTextColor(0,0,0);

            // Primer fila de la tabla
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell($wInd,4,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/_dx_.png'),$xAct + 8,$yAct,6.5);
            $this->Cell(($wInd*1),4,round($promedios->avg_diferencias_met_comparacion,2),1,0,'C',0);
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*2),4,"Limite De Control = 4*      ",1,0,'C',1);
    //        $this->Image(asset('img/formulas/_dx_.png'),$xAct + 31,$yAct,6.4);
            $this->Cell(($wInd*1),4,round($promedios->avg_diferencias_met_comparacion_x4,2),1,0,'C',0);
            $this->Cell(($wInd*2),4,"Redondear",1,0,'C',1);
            $this->Cell(($wInd*1),4,ceil($promedios->avg_diferencias_met_comparacion_x4),1,1,'C',0);

            // Segundo nivel
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*1),4,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/_dy_.png'),$xAct + 8,$yAct - 0.2,5.8);
            $this->Cell(($wInd*1),4,round($promedios->avg_diferencias_met_prueba,2),1,0,'C',0);
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*2),4,"Limite De Control = 4*      ",1,0,'C',1);
    //        $this->Image(asset('img/formulas/_dy_.png'),$xAct + 31,$yAct - 0.2,5.6);
            $this->Cell(($wInd*1),4,round($promedios->avg_diferencias_met_prueba_x4,2),1,0,'C',0);
            $this->Cell(($wInd*2),4,"Redondear",1,0,'C',1);
            $this->Cell(($wInd*1),4,ceil($promedios->avg_diferencias_met_prueba_x4),1,1,'C',0);

            // Tercer nivel
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*1),4,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/_dxt_.png'),$xAct + 7.8,$yAct + 0.2,7);
            $this->Cell(($wInd*1),4,round($promedios->avg_indice_met_comparacion,2),1,0,'C',0);
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*2),4,"Limite De Control = 4*      ",1,0,'C',1);
    //        $this->Image(asset('img/formulas/_dxt_.png'),$xAct + 31,$yAct + 0.2,7.1);
            $this->Cell(($wInd*1),4,round($promedios->avg_indice_met_comparacion_x4,2),1,1,'C',0);

            // Cuarto nivel
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*1),4,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/_dyt_.png'),$xAct + 7.8,$yAct + 0.2,6.5);
            $this->Cell(($wInd*1),4,round($promedios->avg_indice_met_prueba,2),1,0,'C',0);
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*2),4,"Limite De Control = 4*      ",1,0,'C',1);
    //        $this->Image(asset('img/formulas/_dyt_.png'),$xAct + 31,$yAct + 0.3,6.6);
            $this->Cell(($wInd*1),4,round($promedios->avg_indice_met_prueba_x4,2),1,1,'C',0);


            // Seccion C.1
            $this->AddPage();

            // Subtitulo
            $this->Ln(3);
            $this->SetFont('Arial','B',11);
            $this->SetTextColor(28, 80, 164);
            $wInd = 196 / 9;
            $this->Cell(196,4,"C2. Prueba para resultados atípicos (Sección 4.4)",0,1,'C',0);
            $this->Ln(5);


            if(
                isset($resultados[0]->resultado_m1c1) &&
                isset($resultados[0]->resultado_m1c2) &&
                isset($resultados[0]->resultado_m2c1) &&
                isset($resultados[0]->resultado_m2c2)
            ){

                $this->SetDrawColor($this->borde_table[0], $this->borde_table[1], $this->borde_table[2]);
                $this->SetFillColor($this->fondo_table[0], $this->fondo_table[1], $this->fondo_table[2]);
                $this->SetFont('Arial','',6);
                $this->SetTextColor(0,0,0);

                // Primer fila de la tabla
                $this->Cell(($wInd*1),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->resultado_m2c1,2),1,0,'C',0);
                $this->Cell(($wInd*1),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->resultado_m2c2,2),1,0,'C',0);
                $this->Cell(($wInd*1),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->valor_absoluto_primeros_resultados,2),1,0,'C',0);
                $this->Cell(($wInd*1),8,"",1,0,'C',1);
                $this->Cell(($wInd*2),8,round($resultados[0]->valor_absoluto_segundos_resultados,2),1,1,'C',0);


                // Segunda fila de la tabla
                $this->Cell(($wInd*1),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->resultado_m1c1,2),1,0,'C',0);
                $this->Cell(($wInd*1),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->resultado_m1c2,2),1,0,'C',0);
                $this->Cell(($wInd*1),8,"",1,0,'C',1);
                $this->Cell(($wInd*1),8,round($resultados[0]->indice_primer_resultado_met_comparacion,2),1,0,'C',0);
                $this->Cell(($wInd*1),8,"",1,0,'C',1);
                $this->Cell(($wInd*2),8,round($resultados[0]->indice_segundo_resultado_met_comparacion,2),1,1,'C',0);


                // Impresión de imágenes
        //        $this->Image(asset('img/formulas/x11.png'),18,47.5,7);
        //        $this->Image(asset('img/formulas/x12.png'),62,47.2,7.6);
        //        $this->Image(asset('img/formulas/form7.png'),97.5,48,21);
        //        $this->Image(asset('img/formulas/form8.png'),141,48,21);
        //        $this->Image(asset('img/formulas/y11.png'),17.5,56,9);
        //        $this->Image(asset('img/formulas/y12.png'),62,56,9.6);
        //        $this->Image(asset('img/formulas/form9.png'),97,55,22);
        //        $this->Image(asset('img/formulas/form10.png'),142,55.5,20);
            }

            // Enumeración tabla
            $this->SetXY(8.5, 38);
            $this->SetFont('Arial','B',8);
            $this->SetTextColor(0,0,0);
            $this->Cell(15,4,"Tabla 3.",0,1,'L',0);


            // Primer nivel de enumeración
            $wInd = 196 / 9;
            $this->SetXY(10, 68);
            $this->SetFont('Arial','B',5);
            $this->Cell($wInd,2,"1",0,0,'C',0);
            $this->Cell($wInd,2,"2",0,0,'C',0);
            $this->Cell($wInd,2,"3",0,0,'C',0);
            $this->Cell($wInd,2,"4",0,0,'C',0);
            $this->Cell($wInd,2,"5",0,0,'C',0);
            $this->Cell($wInd,2,"6",0,0,'C',0);
            $this->Cell($wInd,2,"7",0,0,'C',0);
            $this->Cell($wInd,2,"8",0,0,'C',0);
            $this->Cell($wInd,2,"9",0,1,'C',0);

            $this->SetDrawColor(146, 169, 185);
            $this->SetFillColor(191, 208, 222);
            $this->SetTextColor(0,0,0);

            // Enunciados primer nivel
            $this->Cell($wInd,6,"Muestra #","TLB",0,'C',0); // It
            $this->Cell($wInd,6,"","TB",0,'C',0); // Xi1
            $this->Cell($wInd,6,"","TB",0,'C',0); // Xi2
            $this->Cell($wInd,6,"","TB",0,'C',0); // Yi1
            $this->Cell($wInd,6,"","TB",0,'C',0); // Yi2
            $this->Cell($wInd,6,"","TB",0,'C',0); // Dxi
            $this->Cell($wInd,6,"","TB",0,'C',0); // Dyi
            $this->Cell($wInd,6,"","TB",0,'C',0); // Dx'i
            $this->Cell($wInd,6,"","TRB",1,'C',0); // Dy'i

            // Impresión de imágenes del encabezado
    //        $this->Image(asset('img/formulas/xi1.png'),40,71.5,6.5);
    //        $this->Image(asset('img/formulas/xi2.png'),60.8,71,8);
    //        $this->Image(asset('img/formulas/yi1.png'),83,71,6.5);
    //        $this->Image(asset('img/formulas/yi2.png'),105.2,71,5.5);
    //        $this->Image(asset('img/formulas/ei1.png'),127,71,7);
    //        $this->Image(asset('img/formulas/ei2.png'),148.5,71.5,6.8);
    //        $this->Image(asset('img/formulas/eit1.png'),170,71.5,7.9);
    //        $this->Image(asset('img/formulas/eit2.png'),191.5,71,8.3);

            $this->SetFont('Arial','',5);

            foreach($resultados as $index => $resultado) {

                if($index % 2 == 0){
                    $this->SetFillColor($this->gris_oscuro[0], $this->gris_oscuro[1], $this->gris_oscuro[2]);
                } else {
                    $this->SetFillColor($this->gris_claro[0], $this->gris_claro[1], $this->gris_claro[2]);
                }

                $this->SetWidths(array(
                    $wInd, $wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,
                ));
                $this->SetAligns(array(
                    "C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C",
                ));
                $this->Row(array(
                    $index+1,
                    round($resultado->resultado_m2c1,2),
                    round($resultado->resultado_m2c2,2),
                    round($resultado->resultado_m1c1,2),
                    round($resultado->resultado_m1c2,2),
                    round($resultado->valor_absoluto_primeros_resultados,2),
                    round($resultado->valor_absoluto_segundos_resultados,2),
                    round($resultado->indice_primer_resultado_met_comparacion,2),
                    round($resultado->indice_segundo_resultado_met_comparacion,2)
                ));
            }

            // Borde final de la tabla
            $this->Cell($wInd * 9,0,"","T",0,'C',0);
            $this->Ln(2);

            // Seccion de promedios de la seccion C.1.
            $wInd = 196 / 9;
            $this->SetDrawColor($this->borde_table[0], $this->borde_table[1], $this->borde_table[2]);
            $this->SetFillColor($this->fondo_table[0], $this->fondo_table[1], $this->fondo_table[2]);
            $this->SetFont('Arial','',6);
            $this->SetTextColor(0,0,0);

            // Primer fila de la tabla
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*3),8,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/form11.png'),$xAct + 25,$yAct,18);
            $this->Cell(($wInd*2),8,round($promedios->sumatoria_e_ochentavo,2),1,0,'C',0);
            $this->Cell(($wInd*2),8,"Limite De Control = 4*E",1,0,'C',1);
            $this->Cell(($wInd*2),8,round($promedios->sumatoria_e_ochentavo * 4,2),1,1,'C',0);

            // Segundo nivel
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*3),8,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/form12.png'),$xAct + 25,$yAct+ 1,18);
            $this->Cell(($wInd*2),8,round($promedios->sumatoria_e_prima_ochentavo,2),1,0,'C',0);
            $this->Cell(($wInd*2),8,"Limite De Control = 4*E'",1,0,'C',1);
            $this->Cell(($wInd*2),8,round($promedios->sumatoria_e_prima_ochentavo * 4,2),1,0,'C',0);

            // Seccion C.3 y C.4
            $this->AddPage();

            // Subtitulo
            $this->Ln(3);
            $this->SetFont('Arial','B',11);
            $this->SetTextColor(28, 80, 164);
            $wInd = 196 / 12;
            $this->Cell(196,4,"C3. Rango adecuado Test-Correlación (Sección 4.5)",0,1,'C',0);
            $this->Ln(5);


            $this->SetDrawColor($this->borde_table[0], $this->borde_table[1], $this->borde_table[2]);
            $this->SetFillColor($this->fondo_table[0], $this->fondo_table[1], $this->fondo_table[2]);
            $this->SetFont('Arial','',6);
            $this->SetTextColor(0,0,0);

            // Primer fila de la tabla
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*1),8,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/(_x_).png'),$xAct + 4.5,$yAct + 1.5,8);
            $this->Cell(($wInd*1),8,round($promedios->avg_promedios_met_comparacion,2),1,0,'C',0);
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*1),8,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/sum(_x_i-_x_)^2.png'),$xAct + 1,$yAct + 1,15);
            $this->Cell(($wInd*1),8,round($promedios->sumatoria_met_comp_individual_menos_promedio_comp_cuadrado,2),1,0,'C',0);
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*2),16,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/sum(_x_i-_x_)(_y_i-_y_).png'),$xAct + 5,$yAct + 5,25);
            $this->Cell(($wInd*1),16,round($promedios->sumatoria_mult_entre_xix_yi_y,2),1,0,'C',0);
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*3),16,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/form13.png'),$xAct + 3,$yAct + 3,43);
            $this->Cell(($wInd*2),16,round($promedios->division_r,2),1,1,'C',0);

            // Segunda fila
            $yTemp = $this->GetY();
            $this->SetY($yTemp-8);
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*1),8,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/(_y_).png'),$xAct + 5.2,$yAct + 1,5);
            $this->Cell(($wInd*1),8,round($promedios->avg_promedios_met_prueba,2),1,0,'C',0);
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*1),8,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/sum(_y_i-_y_)^2.png'),$xAct + 2,$yAct + 1,15);
            $this->Cell(($wInd*1),8,round($promedios->sumatoria_met_prueba_individual_menos_promedio_prueba_cuadrado,2),1,1,'C',0);

            // Enumeración tabla
            $this->SetXY(8.5, 38);
            $this->SetFont('Arial','B',8);
            $this->SetTextColor(0,0,0);
            $this->Cell(15,4,"Tabla 4.",0,1,'L',0);

            // Primer nivel de enumeración
            $wInd = 196 / 12;
            $this->SetXY(10, 68);
            $this->SetFont('Arial','B',5);
            $this->Cell($wInd,2,"1",0,0,'C',0);
            $this->Cell($wInd,2,"2",0,0,'C',0);
            $this->Cell($wInd,2,"3",0,0,'C',0);
            $this->Cell($wInd,2,"4",0,0,'C',0);
            $this->Cell($wInd,2,"5",0,0,'C',0);
            $this->Cell($wInd,2,"6",0,0,'C',0);
            $this->Cell($wInd,2,"7",0,0,'C',0);
            $this->Cell($wInd,2,"8",0,0,'C',0);
            $this->Cell($wInd,2,"9",0,0,'C',0);
            $this->Cell($wInd,2,"10",0,0,'C',0);
            $this->Cell($wInd,2,"11",0,0,'C',0);
            $this->Cell($wInd,2,"12",0,1,'C',0);

            $this->SetDrawColor(146, 169, 185);
            $this->SetFillColor(191, 208, 222);
            $this->SetTextColor(0,0,0);

            // Enunciados primer nivel
            $this->Cell($wInd,6,"Muestra #","TLB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TBR",1,'C',0);


            // Impresión de imágenes del encabezado
    //        $this->Image(asset('img/formulas/xi1.png'),31,71.5,6.5);
    //        $this->Image(asset('img/formulas/xi2.png'),46.8,71,8);
    //        $this->Image(asset('img/formulas/yi1.png'),64,71,6.5);
    //        $this->Image(asset('img/formulas/yi2.png'),81.5,71,5.5);
    //        $this->Image(asset('img/formulas/(_xi_).png'),96.5,71,8);
    //        $this->Image(asset('img/formulas/form16.png'),111,71,12);
    //        $this->Image(asset('img/formulas/form17.png'),127,71.2,12);
    //        $this->Image(asset('img/formulas/(_yi_).png'),146,71,7.5);
    //        $this->Image(asset('img/formulas/form18.png'),160.5,71,9.5);
    //        $this->Image(asset('img/formulas/form19.png'),176,71.5,12.2);
    //        $this->Image(asset('img/formulas/form20.png'),189,71.5,16);


            $this->SetFont('Arial','',5);

            foreach($resultados as $index => $resultado) {

                if($index % 2 == 0){
                    $this->SetFillColor($this->gris_oscuro[0], $this->gris_oscuro[1], $this->gris_oscuro[2]);
                } else {
                    $this->SetFillColor($this->gris_claro[0], $this->gris_claro[1], $this->gris_claro[2]);
                }

                $this->SetWidths(array(
                    $wInd, $wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,
                ));
                $this->SetAligns(array(
                    "C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C",
                ));
                $this->Row(array(
                    $index+1,
                    round($resultado->resultado_m2c1,2),
                    round($resultado->resultado_m2c2,2),
                    round($resultado->resultado_m1c1,2),
                    round($resultado->resultado_m1c2,2),
                    round($resultado->promedio_metodo_comparativo,2),
                    round($resultado->met_comp_individual_menos_promedio_comp,2)=>"#C5F4AC",
                    round($resultado->met_comp_individual_menos_promedio_comp_cuadrado,2)=>"#CBEDFD",
                    round($resultado->promedio_metodo_prueba,2),
                    round($resultado->met_prueba_individual_menos_promedio_prueba,2)=>"#F4D9AC",
                    round($resultado->met_prueba_individual_menos_promedio_prueba_cuadrado,2)=>"#F4EDAC",
                    round($resultado->mult_entre_xix_yi_y,2),
                ));
            }

            // Borde final de la tabla
            $this->Cell($wInd * 12,0,"","T",0,'C',0);
            $this->Ln(2);


            // Subtitulo
            $this->Ln(3);
            $this->SetFont('Arial','B',11);
            $this->SetTextColor(28, 80, 164);
            $wInd = 196 / 12;
            $this->MultiCell(196,4,"C4. Estimaciones de los parámetros de regresión (Sección 5.1)\nPendiente (b): El uso de los datos de la página anterior calculado",0,'C',0);
            $this->Ln(5);


            // Seccion de promedios de la seccion C.4.
            $wInd = 196 / 12;
            $this->SetDrawColor($this->borde_table[0], $this->borde_table[1], $this->borde_table[2]);
            $this->SetFillColor($this->fondo_table[0], $this->fondo_table[1], $this->fondo_table[2]);
            $this->SetFont('Arial','',6);
            $this->SetTextColor(0,0,0);
            // Primer fila de la tabla
            $this->Cell(($wInd*1),10,"",0,0,'C',0);
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*2),10,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/form14.png'),$xAct - 1,$yAct + 1.2,35);
            $this->Cell(($wInd*2),10,round($promedios->formula_b,2),1,0,'C',0);
            $this->Cell(($wInd*1),10,"",0,0,'C',0);
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*1),10,"Intercepción (a)",1,0,'C',1);
            $this->Cell(($wInd*2),10,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/form15.png'),$xAct + 20,$yAct + 3,25);
            $this->Cell(($wInd*2),10,round($promedios->intercepcion_a,2),1,0,'C',0);
            $this->Cell(($wInd*1),10,"",0,0,'C',0);

            // Seccion C.5
            $this->AddPage();

            // Subtitulo
            $this->Ln(3);
            $this->SetFont('Arial','B',11);
            $this->SetTextColor(28, 80, 164);
            $wInd = 196 / 18;
            $this->Cell(196,4,"C5. Residuos y error típico de estimación (Sxy) - (Sección 6.1)",0,1,'C',0);
            $this->Ln(5);

            $this->SetDrawColor($this->borde_table[0], $this->borde_table[1], $this->borde_table[2]);
            $this->SetFillColor($this->fondo_table[0], $this->fondo_table[1], $this->fondo_table[2]);
            $this->SetFont('Arial','',6);
            $this->SetTextColor(0,0,0);

            if(
                isset($resultados_c5[0]->resultado_m1c1) &&
                isset($resultados_c5[0]->resultado_m1c2) &&
                isset($resultados_c5[0]->resultado_m2c1) &&
                isset($resultados_c5[0]->resultado_m2c2)
            ){
                // Primer fila de la tabla
                $this->Cell(($wInd*2),8,"Valores pronosticados",1,0,'C',1);
                $xAct = $this->GetX();
                $yAct = $this->GetY();
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
        //        $this->Image(asset('img/formulas/form22.png'),$xAct + 4.5,$yAct + 1.7,16);
                $this->Cell(($wInd*1),8,round($resultados_c5[0]->yai,2),1,0,'C',0);
                $xAct = $this->GetX();
                $yAct = $this->GetY();
                $this->MultiCell(($wInd*2),(8/3),"Suma de\ncuadrados de\nresiduos",1,'C',1);
                $this->SetXY($xAct+($wInd*2), $yAct);
                $xAct = $this->GetX();
                $yAct = $this->GetY();
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
        //        $this->Image(asset('img/formulas/form23.png'),$xAct + 4,$yAct - 0.3,15);
                $this->Cell(($wInd*1),8,round($promedios_c5->sumatoria_residuali_cuadrado,2),1,0,'C',0);
                $xAct = $this->GetX();
                $yAct = $this->GetY();
                $this->MultiCell(($wInd*2),(16/4),"Desviación estándar de muestra de la regresión =",1,'C',1);
                $this->SetXY($xAct+($wInd*2), $yAct);
                $xAct = $this->GetX();
                $yAct = $this->GetY();
                $this->Cell(($wInd*2),16,"",1,0,'C',1);
        //        $this->Image(asset('img/formulas/form23.png'),$xAct + 4,$yAct + 2.7,15);
                $this->Cell(($wInd*1),16,round($promedios_c5->s_y_x,2),1,0,'C',0);
                $xAct = $this->GetX();
                $yAct = $this->GetY();
                $this->MultiCell(($wInd*2),(16/3),"Sesgo en un nivel de decisión \"c\" de 150 = Bc",1,'C',1);
                $this->SetXY($xAct+($wInd*2), $yAct);
                $this->Cell(($wInd*1),16,round($promedios_c5->sesgo150,2),1,1,'C',0);
                $this->SetY($this->GetY()-8);
                $xAct = $this->GetX();
                $yAct = $this->GetY();
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
        //        $this->Image(asset('img/formulas/sum(_x_i-_x_)(_y_i-_y_).png'),$xAct + 0.8,$yAct + 1.02,20);
                $xAct = $this->GetX();
                $yAct = $this->GetY();
                $this->Cell(($wInd*2),8,"",1,0,'C',1);
        //        $this->Image(asset('img/formulas/sum(_x_i-_x_)(_y_i-_y_).png'),$xAct + 0.8,$yAct + 1.02,20);
                $this->Cell(($wInd*1),8,round($resultados_c5[0]->residuali,2),1,0,'C',0);
                $this->Cell(($wInd*4),8,"Grados de libertad = N - 2",1,0,'C',1);
                $this->Cell(($wInd*1),8,sizeof($resultados_c5)-2,1,0,'C',0);
            }

            // Enumeración tabla
            $this->SetXY(8.5, 38);
            $this->SetFont('Arial','B',8);
            $this->SetTextColor(0,0,0);
            $this->Cell(15,4,"Tabla 5.",0,1,'L',0);

            // Primer nivel de enumeración
            $wInd = 196 / 14;
            $this->SetXY(10, 68);
            $this->SetFont('Arial','B',5);
            $this->Cell($wInd,2,"1",0,0,'C',0);
            $this->Cell($wInd,2,"2",0,0,'C',0);
            $this->Cell($wInd,2,"3",0,0,'C',0);
            $this->Cell($wInd,2,"4",0,0,'C',0);
            $this->Cell($wInd,2,"5",0,0,'C',0);
            $this->Cell($wInd,2,"6",0,0,'C',0);
            $this->Cell($wInd,2,"7",0,0,'C',0);
            $this->Cell($wInd,2,"8",0,0,'C',0);
            $this->Cell($wInd,2,"9",0,0,'C',0);
            $this->Cell($wInd,2,"10",0,0,'C',0);
            $this->Cell($wInd,2,"11",0,0,'C',0);
            $this->Cell($wInd,2,"12",0,0,'C',0);
            $this->Cell($wInd,2,"13",0,0,'C',0);
            $this->Cell($wInd,2,"14",0,1,'C',0);
            $this->SetDrawColor(146, 169, 185);
            $this->SetFillColor(191, 208, 222);
            $this->SetTextColor(0,0,0);

            // Enunciados primer nivel
            $this->Cell($wInd,6,"Muestra #","TLB",0,'C',0); // It
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TB",0,'C',0);
            $this->Cell($wInd,6,"","TRB",1,'C',0);

            // Impresión de imágenes del encabezado
    //        $this->Image(asset('img/formulas/xi1.png'),28,71.5,6.5);
    //        $this->Image(asset('img/formulas/xi2.png'),41.8,71,8);
    //        $this->Image(asset('img/formulas/yi1.png'),56.2,71,6.5);
    //        $this->Image(asset('img/formulas/yi2.png'),70.5,71,5.5);
    //        $this->Image(asset('img/formulas/(_xi_).png'),83.5,71.5,8);
    //        $this->Image(asset('img/formulas/form16.png'),96,71.5,12);
    //        $this->Image(asset('img/formulas/form17.png'),110,71.5,12);
    //        $this->Image(asset('img/formulas/(_yi_).png'),125.5,71.2,8);
    //        $this->Image(asset('img/formulas/form18.png'),138,71,10.5);
    //        $this->Image(asset('img/formulas/form19.png'),152,71.4,14);
    //        $this->Image(asset('img/formulas/yai.png'),167.5,71.5,8);
    //        $this->Image(asset('img/formulas/residuali.png'),179,71,12);
    //        $this->Image(asset('img/formulas/form21.png'),194,71,12);
            $this->SetFont('Arial','',5);

            foreach($resultados_c5 as $index => $resultado_c5) {

                if($index % 2 == 0){
                    $this->SetFillColor($this->gris_oscuro[0], $this->gris_oscuro[1], $this->gris_oscuro[2]);
                } else {
                    $this->SetFillColor($this->gris_claro[0], $this->gris_claro[1], $this->gris_claro[2]);
                }

                $this->SetWidths(array(
                    $wInd, $wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd,$wInd
                ));
                $this->SetAligns(array(
                    "C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C"
                ));
                $this->Row(array(
                    $index+1,
                    round($resultado_c5->resultado_m2c1,2),
                    round($resultado_c5->resultado_m2c2,2),
                    round($resultado_c5->resultado_m1c1,2),
                    round($resultado_c5->resultado_m1c2,2),
                    round($resultado_c5->promedio_metodo_comparativo,2),
                    round($resultado_c5->met_comp_individual_menos_promedio_comp,2),
                    round($resultado_c5->met_comp_individual_menos_promedio_comp_cuadrado,2),
                    round($resultado_c5->promedio_metodo_prueba,2),
                    round($resultado_c5->met_prueba_individual_menos_promedio_prueba,2),
                    round($resultado_c5->met_prueba_individual_menos_promedio_prueba_cuadrado,2),
                    round($resultado_c5->yai,2),
                    round($resultado_c5->residuali,2),
                    round($resultado_c5->residuali_cuadrado,2)
                ));
            }

            // Borde final de la tabla
            $this->Cell($wInd * 14,0,"","T",0,'C',0);
            $this->Ln(2);

            // Seccion de promedios de la seccion C.4.
            $wInd = 196 / 14;
            $this->SetDrawColor($this->borde_table[0], $this->borde_table[1], $this->borde_table[2]);
            $this->SetFillColor($this->fondo_table[0], $this->fondo_table[1], $this->fondo_table[2]);
            $this->SetFont('Arial','',5);
            $this->SetTextColor(0,0,0);

            // Primer fila de la tabla
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->MultiCell(($wInd*2),(10/3),"El límite inferior del 95% de intervalo de confianza de la estimación de sesgo",1,'C',1);
            $this->SetXY($xAct+($wInd*2),$yAct);
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*4),10,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/form26.png'),$xAct + 9,$yAct - 0.8,37);
            $this->SetFont('Arial','B',5);
            $this->Cell(($wInd*1),10,round($promedios_c5->bc_menos_2s,2),1,0,'C',0);
            $this->SetFont('Arial','',5);
            $this->Cell(($wInd*1),10,round($promedios_c5->indicador_bc_inf,2),1,0,'C',0);
            $xAct = $this->GetX();
            $yAct = $this->GetY();
            $this->Cell(($wInd*4),10,"",1,0,'C',1);
    //        $this->Image(asset('img/formulas/form27.png'),$xAct + 8.5,$yAct + 0.1,37);
            $this->SetFont('Arial','B',5);
            $this->Cell(($wInd*1),10,round($promedios_c5->bc_menos_2s,2),1,0,'C',0);
            $this->SetFont('Arial','',5);
            $this->Cell(($wInd*1),10,round($promedios_c5->indicador_bc_sup,2),1,0,'C',0);
        }

        function Footer(){
            // Sin nada
        }

    }
