<?php
$ruta = "../";
require_once "../vendor/autoload.php";
require_once "../class/c_login.php";
    
$user = Login::get_user();

$user->print_to_log('Genera excel de asistencias');

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

//crea imagen 
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('La Salle');
$drawing->setDescription('La Salle');
$drawing->setPath('../imagenes/logo.png'); // put your path and image here
$drawing->setCoordinates('A1');
$drawing->setHeight(100);
$drawing->setOffsetX(10);
//agrega imagen
$drawing->setWorksheet($spreadsheet->getActiveSheet());


// In POST
/** Array
 * * nombre
 * * clave
 * * id
 * * total
 * * asistencias
 * * faltas
 * * justificaciones
 * * retardos
 */

$retardo = query("SELECT COALESCE(FS_HAS_RETARDO(:facultad), FALSE) AS retardo", [':facultad' => $user->facultad['facultad_id']])['retardo'];
extract($_POST);

$row = 6;

$sheet->setCellValue("A$row", 'Clave');
$sheet->setCellValue("B$row", 'Profesor');
$sheet->setCellValue("C$row", 'Asistencias');
$sheet->setCellValue("D$row", 'Faltas');
$sheet->setCellValue("E$row", 'Justificaciones');
$sheet->setCellValue("F$row", 'Retardos');
$sheet->setCellValue("G$row", 'Total');

// $row++;
$col = 0;
# die(print_r($asistencias, true));
foreach (json_decode($asistencias, true) as $profesor) {
    $row++;
    $sheet->setCellValue("A$row", $profesor['profesor_clave']);
    $sheet->setCellValue("B$row", $profesor['profesor_nombre']);
    $sheet->setCellValue("C$row", $profesor['asistencias']);
    $sheet->setCellValue("D$row", $profesor['faltas']);
    $sheet->setCellValue("E$row", $profesor['justificaciones']);
    $sheet->setCellValue("F$row", $profesor['retardos']);
    $sheet->setCellValue("G$row", $profesor['total']);
}

# Style
$sheet->getStyle("A6:G$row")->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
$sheet->getStyle("A6:G$row")->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$sheet->getStyle("A6:G$row")->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$sheet->getStyle("A6:G$row")->getAlignment()->setWrapText(true);
$sheet->getStyle("A6:G$row")->getFont()->setSize(12);
$sheet->getStyle("A6:G$row")->getFont()->setName('Indivisa Sans');
# Autosize columns
foreach (range('A', 'G') as $column) {
    $sheet->getColumnDimension($column)->setAutoSize(true);
}
# filters in the column
$sheet->setAutoFilter("A6:G6");

if (!$retardo) # hide column
    $sheet->getColumnDimension('F')->setVisible(false);

#$writer = new Xlsx($spreadsheet);
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
# $writer->save('asistencias.xlsx');

// download
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="asistencias.xlsx"');
header('Cache-Control: max-age=0');

// cache expires in 60 seconds (1 minute)
header('Expires: mon 26 jul 1997 05:00:00 gmt');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');

$writer->save('php://output');
