<?php
define("ADMIN", true);
require_once("../../common/lib.php");
require_once("../../common/define.php");


 ini_set('display_errors', 1);

require('../../fpdf/fpdf.php'); // Chemin vers la librairie FPDF


// Récupération des données
$stmt = $db->prepare("SELECT * FROM pm_resume_journee ORDER BY date_jour DESC, ligne ASC");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Création du PDF
$pdf = new FPDF('L', 'mm', 'A4');
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$pdf->Cell(0,10, utf8_decode('Résumé de Production'), 0, 1, 'C');
$pdf->Ln(3);

// En-tête du tableau
$pdf->SetFont('Arial','B',10);
$headers = ['Date', 'Ligne', 'Début', 'Fin', 'Cadence', 'Qté Carton', 'Temps Palettage', 'Temps Arrêt', '% Arrêt'];
foreach ($headers as $col) {
    $pdf->Cell(35,7, utf8_decode($col), 1);
}
$pdf->Ln();

// Contenu du tableau
$pdf->SetFont('Arial','',9);
foreach($rows as $r){
    $pdf->Cell(35,6, utf8_decode($r['date_jour']),1);
    $pdf->Cell(35,6, utf8_decode($r['ligne']),1);
    $pdf->Cell(35,6, utf8_decode($r['demarage']),1);
    $pdf->Cell(35,6, utf8_decode($r['last_declaration']),1);
    $pdf->Cell(35,6, utf8_decode($r['cadence_sachets']),1);
    $pdf->Cell(35,6, utf8_decode($r['qte_carton']),1);
    $pdf->Cell(35,6, utf8_decode($r['temps_pallette_std']),1);
    $pdf->Cell(35,6, utf8_decode($r['temps_arret']),1);
    $pdf->Cell(35,6, utf8_decode($r['pourcentage_arret']),1);
    $pdf->Ln();
}

// Sortie du fichier
$pdf->Output('I', 'resume_journee.pdf');
?>