<?php 

    require_once "../../controlador/gestion.formulario.controlador.php";
    require_once "../../modelo/gestion.formulario.modelo.php";
    require_once "../../vista/lib/PHPExcel/Classes/PHPExcel/IOFactory.php";

    $objFormularioControlado = new ControladorGestonFormulario();

    if(isset($_GET['id_formulario']) && isset($_GET['id_formulario'])){

        if(isset($_GET['id_formulario']) && $_GET['id_formulario']==1):

            $conultaultimoidformulario = ModeloConexion::consultaSimple("SELECT MAX(id) AS ultimoid FROM sie_formulario_asistencia");  
            
        
        elseif(isset($_GET['id_formulario']) && $_GET['id_formulario']==2):
        
            $conultaultimoidformulario = ModeloConexion::consultaSimple("SELECT MAX(id) AS ultimoid FROM sie_formulario_atencion");  

        endif;    

        $conultaultimoidformulario = $conultaultimoidformulario->fetch();
        $consecutivo = $conultaultimoidformulario['ultimoid']+1;

        $objPHPExcel = new PHPExcel();

        $objReader = PHPEXCEL_IOFactory::createReader('Excel2007');

        $objPHPExcel = $objReader->load('../../docs/formularios_excel/plantillas/'.$_GET['filename']);

        $objPHPExcel->setActiveSheetIndex(0);

        $objPHPExcel->getActiveSheet()->SetCellValue('A2', $consecutivo);

        $objWriter = PHPEXCEL_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        
        unlink('../../docs/formularios_excel/plantillas/'.$_GET['filename']);

        $objWriter->save("../../docs/formularios_excel/plantillas/".$_GET['filename']);

        header("location: ../../docs/formularios_excel/plantillas/".$_GET['filename']);

    }