<?php
    require('../fpdf/fpdf.php');
    include '../prijava/connect.php';

    $month = date("m", strtotime("first day of previous month"));
    $month_first_day = date("d", strtotime("first day of previous month"));
    $month_last_day = date("d", strtotime("last day of previous month"));
    $year = date("Y", strtotime("first day of previous month"));
    date_default_timezone_set('Europe/Zagreb');

    $random_val = rand(1000,9999);
    $n = 3;
    $random_hash = bin2hex(random_bytes($n));

    $sum_matko = 0;
    $sum_milan = 0;
    $sum_nikolina = 0;
    $sum_jelena = 0;
    $sum_mario = 0;
    $sum_ivica = 0;
    $sum_ivan = 0;
    $sum_filip = 0;
    $sum_petar = 0;
    $sum_josip = 0;
    $sum_sanja = 0;
    $sum_ira = 0;
    $sum_marko = 0;
    $sum_antonela = 0;
    $sum = 0;

    $query = "SELECT * FROM orders WHERE YEAR(date) = $year AND MONTH(date) = $month AND completed = '1'";
    $result = mysqli_query($dbc, $query);
    while($row = mysqli_fetch_array($result)) {
        if($row['user'] == 'Matko') {
            $sum_matko += 1;
            $sum += 1;
        } else if ($row['user'] == 'Milan') {
            $sum_milan += 1;
            $sum += 1;
        } else if ($row['user'] == 'Nikolina') {
            $sum_nikolina += 1;
            $sum += 1;
        } else if ($row['user'] == 'Jelena') {
            $sum_jelena += 1;
            $sum += 1;
        } else if ($row['user'] == 'Mario') {
            $sum_mario += 1;
            $sum += 1;
        } else if ($row['user'] == 'Ivica') {
            $sum_ivica += 1;
            $sum += 1;
        } else if ($row['user'] == 'Filip') {
            $sum_filip += 1;
            $sum += 1;
        } else if ($row['user'] == 'Ivan') {
            $sum_ivan += 1;
            $sum += 1;
        } else if ($row['user'] == 'Petar') {
            $sum_petar += 1;
            $sum += 1;
        } else if ($row['user'] == 'Josip') {
            $sum_josip += 1;
            $sum += 1;
        } else if ($row['user'] == 'Sanja') {
            $sum_sanja += 1;
            $sum += 1;
        } else if ($row['user'] == 'Ira') {
            $sum_ira += 1;
            $sum += 1;
        } else if ($row['user'] == 'Marko') {
            $sum_marko += 1;
            $sum += 1;
        } else if ($row['user'] == 'Antonela') {
            $sum_antonela += 1;
            $sum += 1;
        }
    }
    
    class PDF extends FPDF {
        // Page header
        function Header() {
            $this->Image('../assets/krikstudiologo.png',8,8,33);
            $this->SetFont('Helvetica','B',20);
            $this->Cell(80);
            $this->Ln(24);
        }
    
        // Page footer
        function Footer() {
            $this->SetY(-12);
            $this->SetFont('Helvetica','',8);
            $now = date("d.m.Y. | H:i:s");
            $this->Cell(0,10,'Generirano: '.$now.'',0,0,'C');
        }
    }

    $pdf = new PDF();

    $pdf->AliasNbPages();
    $pdf->AddPage();
    $pdf->SetFont('Helvetica','',14);
    $str = "Statistika narudžbi za razdoblje:";
    $str = iconv('UTF-8', 'windows-1252', $str);
    $pdf->Cell(0, 20, $str, 0, 1);
    $pdf->SetFont('Helvetica','',16);
    $pdf->Cell(0, 0, 'od '. $month_first_day .'.'. $month . '.'. $year .'. do '. $month_last_day .'.'. $month .'.'. $year .'.', 0, 1);
    $pdf->SetFont('Helvetica','',12);

    $counter = 1;

    $pdf->Cell(0, 10, '', 0, 1);

    $pdf->Cell(10,8,$counter,1,0,'L',false);
    $pdf->Cell(40,8,' Matko',1,0,'L',false);   // empty cell with left,top, and right borders
    $pdf->Cell(15,8,' '.$sum_matko,1,0,'L',false);
    $pdf->Ln(8);
    $counter = $counter + 1;

    $pdf->Cell(10,8,$counter,1,0,'L',false);
    $pdf->Cell(40,8,' Milan',1,0,'L',false);   // empty cell with left,top, and right borders
    $pdf->Cell(15,8,' '.$sum_milan,1,0,'L',false);
    $pdf->Ln(8);
    $counter = $counter + 1;
    
    $pdf->Cell(10,8,$counter,1,0,'L',false);
    $pdf->Cell(40,8,' Nikolina',1,0,'L',false);   // empty cell with left,top, and right borders
    $pdf->Cell(15,8,' '.$sum_nikolina,1,0,'L',false);
    $pdf->Ln(8);
    $counter = $counter + 1;
    
    if($sum_jelena > 0) {
        $pdf->Cell(10,8,$counter,1,0,'L',false);
        $pdf->Cell(40,8,' Jelena',1,0,'L',false);   // empty cell with left,top, and right borders
        $pdf->Cell(15,8,' '.$sum_jelena,1,0,'L',false);
        $pdf->Ln(8);
        $counter = $counter + 1;
    }
    
    $pdf->Cell(10,8,$counter,1,0,'L',false);
    $pdf->Cell(40,8,' Ivica',1,0,'L',false);   // empty cell with left,top, and right borders
    $pdf->Cell(15,8,' '.$sum_ivica,1,0,'L',false);
    $pdf->Ln(8);
    $counter = $counter + 1;
    
    $pdf->Cell(10,8,$counter,1,0,'L',false);
    $pdf->Cell(40,8,' Ira',1,0,'L',false);   // empty cell with left,top, and right borders
    $pdf->Cell(15,8,' '.$sum_ira,1,0,'L',false);
    $pdf->Ln(8);
    $counter = $counter + 1;
    
    $pdf->Cell(10,8,$counter,1,0,'L',false);
    $pdf->Cell(40,8,' Mario',1,0,'L',false);   // empty cell with left,top, and right borders
    $pdf->Cell(15,8,' '.$sum_mario,1,0,'L',false);
    $pdf->Ln(8);
    $counter = $counter + 1;
    
    $pdf->Cell(10,8,$counter,1,0,'L',false);
    $pdf->Cell(40,8,' Marko',1,0,'L',false);   // empty cell with left,top, and right borders
    $pdf->Cell(15,8,' '.$sum_marko,1,0,'L',false);
    $pdf->Ln(8);
    $counter = $counter + 1;
    
    $pdf->Cell(10,8,$counter,1,0,'L',false);
    $pdf->Cell(40,8,' Antonela',1,0,'L',false);   // empty cell with left,top, and right borders
    $pdf->Cell(15,8,' '.$sum_antonela,1,0,'L',false);
    $pdf->Ln(8);
    $counter = $counter + 1;
    
    $pdf->Cell(10,8,$counter,1,0,'L',false);
    $pdf->Cell(40,8,' Josip',1,0,'L',false);   // empty cell with left,top, and right borders
    $pdf->Cell(15,8,' '.$sum_josip,1,0,'L',false);
    $pdf->Ln(8);
    $counter = $counter + 1;
    
    if($sum_sanja > 0) {
        $pdf->Cell(10,8,$counter,1,0,'L',false);
        $pdf->Cell(40,8,' Sanja',1,0,'L',false);   // empty cell with left,top, and right borders
        $pdf->Cell(15,8,' '.$sum_sanja,1,0,'L',false);
        $pdf->Ln(8);
    }

    $pdf->Cell(0, 16, '', 0, 1);
    $pdf->Cell(40,8,' UKUPNO: ',1,0,'L',false);   // empty cell with left,top, and right borders
    $pdf->Cell(20,8,' '.$sum,1,0,'L',false);

    $filename = "narudzbe_".$month."_".$year.".pdf";
    $pdf->Output('I', $filename);
?>
