<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace CmBundle\Controller;

use FPDF;

date_default_timezone_set('America/Mexico_City');

/**
 * Description of PHP
 *
 * @author grupo2m
 */
class PDF_ListasAsistencia extends FPDF {

    public $titulo;
    public $posicion;

    function setPosicion($posicion) {
        $this->posicion = $posicion;
    }

    function setTitulo($titulo) {
        $this->titulo = $titulo;
    }

    // Cabecera de página
    function Header() {
        if ($this->posicion == 'V') {
            setlocale(LC_ALL, 'spanish');
            $fecha = date('d-F-Y');
            $this->SetXY(0, 0);
            //Arial italic 8
            $this->SetFont('Arial', 'I', 8);
            //$this->Cell(20, 10, utf8_encode(strftime("%d-%B-%Y")), 0, 1, 'L');
            $this->SetRightMargin(0);
            $this->SetFillColor(78, 148, 210);
            $this->Cell(0, 15, "", 0, 0, 'C', false);

            $this->SetXY(7, 1);
            //if ($this->PageNo() == 1) {
            // Logo
            //$this->Image('images/sep.png', 10, 1, 20);
            //$this->Image('images/escudoJ.png', 180, 1, 25);
            $this->Image('images/encabezadoOK.png', 0, 0, 216);
            // Titulo del reporte
            $this->SetFont('Arial', 'B', 16);
            $this->Cell(0, 20, utf8_decode($this->titulo), 0, 0, 'C');
            $this->SetFont('Arial', 'B', 12);
            //}
            $this->Ln(30);
            $this->SetLeftMargin(10);
            $this->SetRightMargin(10);
        } else if ($this->posicion == 'H') {
            setlocale(LC_ALL, 'spanish');
            $fecha = date('d-F-Y');
            $this->SetXY(0, 0);
            //Arial italic 8
            $this->SetFont('Arial', 'I', 8);
            //$this->Cell(20, 10, utf8_encode(strftime("%d-%B-%Y")), 0, 1, 'L');
            $this->SetRightMargin(0);
            $this->SetFillColor(78, 148, 210);
            $this->Cell(0, 15, "", 0, 0, 'C', false);
            //if ($this->PageNo() == 1) {
            // Logo
            //$this->Image('images/sep.png', 10, 1, 13);
            //$this->Image('images/escudoJ.png', 245, 1, 25);
            //
            //$this->Image('images/escudoJ.png', 245, 1, 10);
            $this->Image('images/encabezadoOK.png', 0, 0, 216);
            // Titulo del Reporte
            $this->SetXY(0, 0);
            $this->SetFont('Arial', 'B', 16);
            $this->SetTextColor(0);
            $this->Cell(0, 16, $this->titulo, 0, 0, 'C');
            //$this->Image('images/logo2.png', 7, 7, 50, 15);
            //}
            $this->Ln(25);
        }
    }

    // Pie de página
    function Footer() {
        if ($this->posicion == 'V') {

            $this->Image('images/pieOK.png', 0, 255, 216);
        } else if ($this->posicion == 'H') {
            $this->Image('images/footerOK.png', 0, 260, 216);
        }


        /* $this->SetXY(0, 201);
          $this->SetRightMargin(0);
          $this->SetFillColor(78, 148, 210);
          $this->Cell(0, 15, "", 0, 0, 'C', true);
         */

        $this->SetLeftMargin(10);
        $this->SetRightMargin(10);
        // Posición: a 1,5 cm del final
        $this->SetY(-15);
        // Arial italic 8
        $this->SetFont('Arial', 'I', 8);
        // Número de página
        //$this->Cell(0, 10, utf8_decode('Página ' . $this->PageNo() . '/{nb}'), 0, 0, 'C');
    }

    // ###########################################################
    function listaGrupo($g, $alumnos, $numero, $dias, $horario) {
        //$this->Ln(-8);

        $this->SetXY(140, 5);
        $this->SetTextColor(255, 255, 255);
        $this->SetFont('Calibri', 'B', 12);

        $this->SetRightMargin(4);
        $this->MultiCell(0, 5, utf8_decode("LISTA DE ASISTENCIA " . $g->getCveLic()->getNomLic() . " " . $numero . "° CUATRIMESTRE"), 0, 'C', false);
        $this->SetLeftMargin(5);
        $this->SetRightMargin(5);

        $this->SetTextColor(0, 0, 0);
        $this->SetFillColor(112, 173, 71);
        $this->SetDrawColor(169, 208, 142);
        $this->SetFont('Calibri', 'B', 11);
        $this->SetXY(5, 26);
        //$this->Ln();
        $this->Cell(10, 15, utf8_decode("N.C."), 1, 0, 'C', TRUE);
        $this->Cell(80, 15, utf8_decode("NOMBRE DEL ALUMNO"), 1, 0, 'C', TRUE);
        $this->SetFont('Calibri', 'L', 8);


        $largo = 116;
        $anchoCelda = $largo / (count($dias) + 1);
        $residuo = $largo - ($anchoCelda * (count($dias) + 1));
        $tolerancia = $anchoCelda / 2;

        for ($xi = 0; $xi < count($dias); $xi++) {
            $this->Cell($anchoCelda, 15, "", 1, 0, 'C', TRUE);
            $this->TextWithRotation($this->GetX() - $tolerancia, $this->GetY() + 14, utf8_decode($dias[$xi]), 90);
        }
        $this->Cell($residuo, 15, "", 1, 0, 'C', TRUE);
        $this->TextWithRotation($this->GetX() - $tolerancia, $this->GetY() + 12, utf8_decode("TOTAL"), 90);
        $this->Ln();
        $cK = 0;
        $this->SetDrawColor(112, 173, 71);


        $this->SetFillColor(226, 239, 218);


        foreach ($alumnos as $key => $v) {
            $bandera = 1;
            $this->SetFont('Calibri', '', 11);
            $this->Cell(10, 8, utf8_decode($key + 1), 1, 0, 'C', FALSE);
            $this->SetFont('Calibri', '', 11);
            $this->Cell(80, 8, utf8_decode($v->getApePat() . " " . $v->getApeMat() . " " . $v->getNomAlu()), 1, 0, 'L', FALSE);
            $this->SetFont('Calibri', '', 8);
            $indice = 1;
            for ($xi = 0; $xi < count($dias); $xi++) {
                if ($bandera == 1) {
                    //ESTÁ PINTANDO
                    if ($indice <= 4) {
                        //PONTE A PINTAR
                        $this->Cell($anchoCelda, 8, utf8_decode(""), 1, 0, 'C', TRUE);
                        $indice++;
                    } else {
                        $indice = 1;
                        $bandera = 0;
                        $this->Cell($anchoCelda, 8, utf8_decode(""), 1, 0, 'C', FALSE);
                    }
                } else {
                    //NO ESTÁ PINTANDO
                    if ($indice <= 3) {
                        //PONTE A PINTAR
                        $this->Cell($anchoCelda, 8, utf8_decode(""), 1, 0, 'C', FALSE);
                        $indice++;
                    } else {
                        $indice = 1;
                        $bandera = 1;
                        $this->Cell($anchoCelda, 8, utf8_decode(""), 1, 0, 'C', TRUE);
                    }
                }
            }
            $this->Cell($residuo, 8, utf8_decode(""), 1, 0, 'C', TRUE);

            $this->Ln();
            $cK = $key;
        }
        $cK += 1;
        while ($cK <= 19) {
            $bandera = 1;
            $cK = $cK + 1;
            $this->SetFont('Calibri', '', 11);
            $this->Cell(10, 8, utf8_decode($cK), 1, 0, 'C', FALSE);
            $this->SetFont('Calibri', '', 11);
            $this->Cell(80, 8, utf8_decode(""), 1, 0, 'L', FALSE);
            $this->SetFont('Calibri', '', 8);
            $indice = 1;
            for ($xi = 0; $xi < count($dias); $xi++) {
                if ($bandera == 1) {
                    //ESTÁ PINTANDO
                    if ($indice <= 4) {
                        //PONTE A PINTAR
                        $this->Cell($anchoCelda, 8, utf8_decode(""), 1, 0, 'C', TRUE);
                        $indice++;
                    } else {
                        $indice = 1;
                        $bandera = 0;
                        $this->Cell($anchoCelda, 8, utf8_decode(""), 1, 0, 'C', FALSE);
                    }
                } else {
                    //NO ESTÁ PINTANDO
                    if ($indice <= 3) {
                        //PONTE A PINTAR
                        $this->Cell($anchoCelda, 8, utf8_decode(""), 1, 0, 'C', FALSE);
                        $indice++;
                    } else {
                        $indice = 1;
                        $bandera = 1;
                        $this->Cell($anchoCelda, 8, utf8_decode(""), 1, 0, 'C', TRUE);
                    }
                }
            }
            $this->Cell($residuo, 8, utf8_decode(""), 1, 0, 'C', TRUE);

            $this->Ln();
        }
        $this->SetFont('Calibri', 'B', 11);


        $this->Ln();
        $this->Cell(150, 6, utf8_decode(""), 0, 0, 'C', FALSE);
        $this->Cell(23, 6, utf8_decode("∙"), 1, 0, 'C', FALSE);
        $this->SetFont('Calibri', 'L', 11);
        $this->Cell(0, 6, utf8_decode("ASISTENCIA"), 1, 0, 'L', FALSE);
        $this->Ln();
        $this->SetFont('Calibri', 'B', 11);
        $this->Cell(10, 6, utf8_decode(""), 0, 0, 'C');
        $this->Cell(20, 6, utf8_decode("MATERIA:"), 0, 0, 'L');
        $this->SetFont('Calibri', 'L', 11);
        $this->Cell(80, 6, utf8_decode($horario->getCveCur()->getClave() . "-" . $horario->getCveCur()->getNomAsignatura()), 'B', 0, 'L');
        $this->Cell(40, 6, utf8_decode(""), 0, 0, 'L');
        $this->SetFont('Calibri', 'B', 14);
        $this->Cell(23, 6, utf8_decode("/"), 1, 0, 'C', FALSE);
        $this->SetFont('Calibri', 'L', 11);
        $this->Cell(0, 6, utf8_decode("FALTA"), 1, 0, 'L', FALSE);
        $this->Ln();
        $this->SetFont('Calibri', 'B', 14);
        $this->Cell(150, 6, utf8_decode(""), 0, 0, 'C', FALSE);
        $this->Cell(23, 6, utf8_decode("J"), 1, 0, 'C', FALSE);
        $this->SetFont('Calibri', 'L', 11);
        $this->Cell(0, 6, utf8_decode("FALTA JUSTIFICADA"), 1, 0, 'L', FALSE);
        $this->Ln();
        $this->Cell(10, 6, utf8_decode(""), 0, 0, 'C');
        $this->SetFont('Calibri', 'B', 11);
        $this->Cell(20, 6, utf8_decode("DOCENTE:"), 0, 0, 'L');
        $this->SetFont('Calibri', 'L', 11);
        $this->Cell(80, 6, utf8_decode($horario->getCvePro()->getApePat() . " " . $horario->getCvePro()->getApeMat() . " " . $horario->getCvePro()->getNomPro()), 'B', 0, 'L');
        $this->Cell(40, 6, utf8_decode(""), 0, 0, 'L');
        $this->SetFont('Calibri', 'B', 14);
        $this->Cell(23, 6, utf8_decode("R"), 1, 0, 'C', FALSE);
        $this->SetFont('Calibri', 'L', 11);
        $this->Cell(0, 6, utf8_decode("RETARDO"), 1, 0, 'L', FALSE);
    }

    function TextWithDirection($x, $y, $txt, $direction = 'R') {
        if ($direction == 'R')
            $s = sprintf('BT %.2F %.2F %.2F %.2F %.2F %.2F Tm (%s) Tj ET', 1, 0, 0, 1, $x * $this->k, ($this->h - $y) * $this->k, $this->_escape($txt));
        elseif ($direction == 'L')
            $s = sprintf('BT %.2F %.2F %.2F %.2F %.2F %.2F Tm (%s) Tj ET', -1, 0, 0, -1, $x * $this->k, ($this->h - $y) * $this->k, $this->_escape($txt));
        elseif ($direction == 'U')
            $s = sprintf('BT %.2F %.2F %.2F %.2F %.2F %.2F Tm (%s) Tj ET', 0, 1, -1, 0, $x * $this->k, ($this->h - $y) * $this->k, $this->_escape($txt));
        elseif ($direction == 'D')
            $s = sprintf('BT %.2F %.2F %.2F %.2F %.2F %.2F Tm (%s) Tj ET', 0, -1, 1, 0, $x * $this->k, ($this->h - $y) * $this->k, $this->_escape($txt));
        else
            $s = sprintf('BT %.2F %.2F Td (%s) Tj ET', $x * $this->k, ($this->h - $y) * $this->k, $this->_escape($txt));
        if ($this->ColorFlag)
            $s = 'q ' . $this->TextColor . ' ' . $s . ' Q';
        $this->_out($s);
    }

    function TextWithRotation($x, $y, $txt, $txt_angle, $font_angle = 0) {
        $font_angle += 90 + $txt_angle;
        $txt_angle *= M_PI / 180;
        $font_angle *= M_PI / 180;

        $txt_dx = cos($txt_angle);
        $txt_dy = sin($txt_angle);
        $font_dx = cos($font_angle);
        $font_dy = sin($font_angle);

        $s = sprintf('BT %.2F %.2F %.2F %.2F %.2F %.2F Tm (%s) Tj ET', $txt_dx, $txt_dy, $font_dx, $font_dy, $x * $this->k, ($this->h - $y) * $this->k, $this->_escape($txt));
        if ($this->ColorFlag)
            $s = 'q ' . $this->TextColor . ' ' . $s . ' Q';
        $this->_out($s);
    }

}
