<?php

require_once("../class/dB.class.php");
require_once("../tcpdf/tcpdf.php");

$YesNo = array("nein","ja");
 
$mydb= new DB_MySQL('zeltlager','zeltlager','Z3ltlager3363','localhost');

$sql = "SELECT * FROM VerwaltungZelte ORDER BY  VerwaltungZelte.zeltnummer = 0, VerwaltungZelte.zeltnummer ";
$mydb->query($sql);
while ($row = $mydb->fetchRow())
{
    $zelte_nr_array[] = $row['zeltnummer']; // Inside while loop
    $zelte_namen_array[] = $row['zelt'];
}       
 
// Extend the TCPDF class to create custom Header and Footer
class MYPDFZelteinteilung extends TCPDF {

    //Page header
    public function Header()
    {
         //aktuelle Jahreszahl auslesen
        $mydb= new DB_MySQL('zeltlager','zeltlager','Z3ltlager3363','localhost');
         
        $mydb->query("SELECT * FROM VerwaltungLagerplatz ORDER BY jahr DESC limit 1");
        $row = $mydb->fetchRow();
        
        
        $this->SetFont('helvetica', 'B', 10);
        $this->Cell(170, 10, 'Evang. Gemeindejugend Memmingen - '. $row['platz'], 0, false, 'C', 0, '', 0, false, 'C', 'B');
        
       
        // Logo
        $image_file = "../images/gemeindejugend_logo.jpg";
        $this->Image($image_file, 15, 5, '20', '', 'JPG', '', 'M', true, 600, '', false, false, 0, false, false, false);
        
        $mydb->disconnect();
    }

    // Page footer
    public function Footer() 
    {
       
        // Position at 10 mm from bottom
        $this->SetY(-10);
        // Set font
        $this->SetFont('helvetica', '', 8);
        // Status zeile
        $datum  = date("d.m.Y", time());
        $this->Cell(0, 10, 'created: ' . $datum . "  -   www.zeltlageranmeldung.de                                          Seite ".$this->PageNo(). " von " . $this->getAliasNbPages(), 0, false, 'L', 0, '', 0, false, 'T', 'M');
    }
}

// create new PDF document
$pdf = new MYPDFZelteinteilung(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator('www.fischerkoenig.de');
$pdf->SetAuthor('Team der Zeltlageranmeldung');
$pdf->SetTitle('Zeltlageranmeldung');
$pdf->SetSubject('Listen fürs Zeltlager');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH,'Zeltlageranmeldung', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
 
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
//$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetMargins(PDF_MARGIN_LEFT, 0, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
//$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->SetAutoPageBreak(TRUE, 10);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set default font subsetting mode
$pdf->setFontSubsetting(true);

//Datenbankabfrage
$mydb->query("SELECT * FROM AnmeldungAdressen as a LEFT JOIN AnmeldungVerwaltung as v on a.uid = v.uid WHERE v.gruppe = 3 ORDER BY a.nachname COLLATE latin1_german2_ci ASC, a.vorname COLLATE latin1_german2_ci ASC ");
$NumberOfEntries =  $mydb->count();

$style= <<<EOF
<style>
    
    table {
        
    }
    td {
        height:28px;
        line-height:2px;
        text-align:left;
        border-bottom: 1px solid black;
    }
    
    .lfdnrfield{
        text-align:left;
        width: 50px;
    }
    
    .namefield{
        text-align:left;
        width: 280px;

    }
    
    .field{
        text-align:center;
        width: 100px;
    }
    
    th {
        text-align:center;
        border-bottom: 2px solid black;
    }
  
</style>
EOF;

 

foreach( $zelte_nr_array as $index => $zeltnr) 
 {
    $sql = "SELECT a.nachname, a.vorname, z.mitarbeiter, g.gruppe, g.gruppe_txt, y.schwimmen,(YEAR(CURRENT_DATE) - YEAR(p.geburtstag)) - (RIGHT(CURRENT_DATE,5) < RIGHT(p.geburtstag,5)) AS age  FROM AnmeldungVerwaltung t LEFT JOIN AnmeldungAdressen a ON t.uid = a.uid LEFT JOIN AnmeldungZeltwunsch z ON t.uid = z.uid JOIN AnmeldungStatus s ON t.uid = s.uid JOIN VerwaltungGruppe g ON g.gruppe = t.gruppe JOIN AnmeldungZusatzangaben as y on t.uid = y.uid JOIN AnmeldungPerson as p on t.uid = p.uid WHERE t.zeltnummer = $zeltnr AND s.bestaetigt = 1 ORDER by z.mitarbeiter DESC , a.nachname COLLATE latin1_german2_ci, a.vorname COLLATE latin1_german2_ci";
    
    $mydb->query($sql);
    $amount = $mydb->count(); 
   
    
    if($amount > 0)
    {
        $lfdnr = 0;
         
        $pdf->AddPage();
      
        $pdf->SetFont('helvetica', 'B', 17);
        $pdf->Cell(190, 40, 'Zelteinteilung - Zelt '. $zelte_namen_array[$index], '', 0, 'C', false, '', 0, false, 'T', 'M');
       
        
        $pdf->SetFont('helvetica', '', 11);
        $tbl = $style . "<table>";
        
        $tbl .= "<thead><tr><th class=\"lfdnrfield\">&nbsp;</th><th class=\"namefield\">Name</th><th class=\"field\">Alter</th><th class=\"field\">Schwimmen</th><th class=\"field\">Gruppe</th></tr></thead><tbody>";
       
        while($row = $mydb->fetchRow())
        {
            $lfdnr++;
            $schimmid = $row["schwimmen"];
            if ($row["gruppe"] == 3)
            {
                $tbl .= "<tr><td class=\"lfdnrfield\">$lfdnr</td><td class=\"namefield\"><b>$row[nachname], $row[vorname]</b></td><td class=\"field\">$row[age]</td><td class=\"field\">$YesNo[$schimmid]</td><td class=\"field\">$row[gruppe_txt]</td></tr>";  
            }
            else
            {
                $tbl .= "<tr><td class=\"lfdnrfield\">$lfdnr</td><td class=\"namefield\">$row[nachname], $row[vorname]</td><td class=\"field\">$row[age]</td><td class=\"field\">$YesNo[$schimmid]</td><td class=\"field\">$row[gruppe_txt]</td></tr>";  
            }
        }
        
       
        $tbl .= "</tbody></table>";
        //$pdf->writeHTML($tbl, true, false, false, false, '');
        $pdf->writeHTMLCell	(0,0,15,30, $tbl, 0, 0, false, true,'',true);		
    }
 }


        
$filepath = '../pdf_listen/Liste-Zelteinteilung.pdf';
$pdf->Output($filepath, 'F');


$mydb->disconnect();
?>
