<?php
/**
 * PHPExcel
 *
 * Copyright (c) 2006 - 2015 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
 * @version    ##VERSION##, ##DATE##
 */

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

if (PHP_SAPI == 'cli')
	die('This example should only be run from a Web Browser');

/** Include PHPExcel */
require_once dirname(__FILE__) . '../../lib/PHPExcel/Classes/PHPExcel.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

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

	// Create a first sheet, representing sales data
	$objPHPExcel->setActiveSheetIndex(0);
	$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Id formulario'); // Agrega valor a una celda
	$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Fecha de registro');
	$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Tipo de asesoría');
	$objPHPExcel->getActiveSheet()->setCellValue('D1', 'Observación asesoría');
	$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Observación capacitación');
	$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Acuerdo próxima visita');
	$objPHPExcel->getActiveSheet()->setCellValue('G1', 'Usuario atiende');
	$objPHPExcel->getActiveSheet()->setCellValue('H1', 'Usuario atendido');
	$objPHPExcel->getActiveSheet()->setCellValue('A2', $_GET['id_formulario_plantilla']);

	$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(18); // Alto por defecto de filas
	$objPHPExcel->getActiveSheet()->getRowDimension(3)->setRowHeight(32); // Alto a una fila especifica

	// Centra texto en celdas fucionadas
	$style = array(
	    'alignment' => array(
	            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
	    )
	);
	$objPHPExcel->getActiveSheet()->getStyle("A1:H1")->applyFromArray($style); // Centra texto
	// $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setSize(16); // Tamaño de letra
	// $objPHPExcel->getActiveSheet()->getRowDimension(3)->setRowHeight(32); // Alto a una fila especifica

	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true); // Ajusta celda al texto

	// Rename sheet
	$objPHPExcel->getActiveSheet()->setTitle('atención'); // Nombre de la primera hoja

	// Create a new worksheet, after the default sheet
	$objPHPExcel->createSheet(); // Creando nueva hoja

	// Add some data to the second sheet, resembling some different data types
	$objPHPExcel->setActiveSheetIndex(1);
	$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Id atención');
	$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Tipo de capacitación');
	$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Tema');
	$objPHPExcel->getActiveSheet()->setCellValue('D1', 'Genero');
	$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Numero de personas');
	$objPHPExcel->getActiveSheet()->setCellValue('A2', $_GET['id_formulario_plantilla']);

	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true); // Ajusta celda al texto

	$objPHPExcel->getActiveSheet()->getStyle("A1:E1")->applyFromArray($style); // Centra texto

	// Rename 2nd sheet
	$objPHPExcel->getActiveSheet()->setTitle('capacitación');

	// Redirect output to a client’s web browser (Excel5)
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment;filename="atencion productores.xls"');
	header('Cache-Control: max-age=0');
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	$objWriter->save('php://output');

elseif(isset($_GET['id_formulario_plantilla']) && $_GET['id_formulario_plantilla']==2):

		// Create a first sheet, representing sales data
	$objPHPExcel->setActiveSheetIndex(0);
	$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Id asistencia');
	$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Fecha de registro');
	$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Tipo de atención');
	$objPHPExcel->getActiveSheet()->setCellValue('D1', 'País');
	$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Departamento');
	$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Municipio');
	$objPHPExcel->getActiveSheet()->setCellValue('G1', 'Vereda');
	$objPHPExcel->getActiveSheet()->setCellValue('H1', 'Nombre usuario atiende');
	$objPHPExcel->getActiveSheet()->setCellValue('I1', 'Cargo usuario atiende');
	$objPHPExcel->getActiveSheet()->setCellValue('J1', 'Nombre usuario atendido');
	$objPHPExcel->getActiveSheet()->setCellValue('K1', 'Nombre de organización');
	$objPHPExcel->getActiveSheet()->setCellValue('L1', 'Tipo de poblavión');
	$objPHPExcel->getActiveSheet()->setCellValue('M1', 'Número de identificación');
	$objPHPExcel->getActiveSheet()->setCellValue('N1', 'Teléfono');
	$objPHPExcel->getActiveSheet()->setCellValue('O1', 'Correo');
	$objPHPExcel->getActiveSheet()->setCellValue('P1', 'Lineas productivas');
	$objPHPExcel->getActiveSheet()->setCellValue('Q1', 'Observación');
	$objPHPExcel->getActiveSheet()->setCellValue('A2', $_GET['id_formulario_plantilla']);

	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('N')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('O')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('P')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setAutoSize(true); // Ajusta celda al texto

	// Centra texto en celdas fucionadas
	$style = array(
	    'alignment' => array(
	            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
	    )
	);
	// $objPHPExcel->getActiveSheet()->getStyle("A3")->applyFromArray($style); // Centra texto
	$objPHPExcel->getActiveSheet()->getStyle("A1:P1")->applyFromArray($style); // Centra texto

	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('N')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('O')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('P')->setAutoSize(true); // Ajusta celda al texto

	// Rename sheet
	$objPHPExcel->getActiveSheet()->setTitle('asistencia'); // Nombre de la primera hoja


	// Hoja 2
	// Create a new worksheet, after the default sheet
	$objPHPExcel->createSheet(); // Creando nueva hoja

	// Add some data to the second sheet, resembling some different data types
	$objPHPExcel->setActiveSheetIndex(1);
	$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Id asistencia');
	$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Cultivo');
	$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Edad cultivo');
	$objPHPExcel->getActiveSheet()->setCellValue('D1', 'Total sembrado');
	$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Total en producción');
	$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Producción');
	$objPHPExcel->getActiveSheet()->setCellValue('G1', 'Rendimiento');
	$objPHPExcel->getActiveSheet()->setCellValue('H1', 'Costos producción');
	$objPHPExcel->getActiveSheet()->setCellValue('I1', 'Canal de comercialización');
	$objPHPExcel->getActiveSheet()->setCellValue('A2', $_GET['id_formulario_plantilla']);


	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true); // Ajusta celda al texto

	$objPHPExcel->getActiveSheet()->getStyle("A1:I1")->applyFromArray($style); // Centra texto

	// Rename 2nd sheet
	$objPHPExcel->getActiveSheet()->setTitle('agricola');

	// Hoja 3
	// Create a new worksheet, after the default sheet
	$objPHPExcel->createSheet(); // Creando nueva hoja

	// Add some data to the second sheet, resembling some different data types
	$objPHPExcel->setActiveSheetIndex(2);
	$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Id asistencia');
	$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Especie');
	$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Raza');
	$objPHPExcel->getActiveSheet()->setCellValue('D1', 'Edad');
	$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Producción predio');
	$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Costos producción');
	$objPHPExcel->getActiveSheet()->setCellValue('G1', 'Canal de comercialización');
	$objPHPExcel->getActiveSheet()->setCellValue('A2', $_GET['id_formulario_plantilla']);

	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true); // Ajusta celda al texto

	$objPHPExcel->getActiveSheet()->getStyle("A1:G1")->applyFromArray($style); // Centra texto

	// Rename 2nd sheet
	$objPHPExcel->getActiveSheet()->setTitle('Pecuaria');

	// Hoja 4
	// Create a new worksheet, after the default sheet
	$objPHPExcel->createSheet(); // Creando nueva hoja

	// Add some data to the second sheet, resembling some different data types
	$objPHPExcel->setActiveSheetIndex(3);
	$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Id asistencia');
	$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Nombre de producto');
	$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Dosis recomendada');
	$objPHPExcel->getActiveSheet()->setCellValue('D1', 'Método de aplicación');
	$objPHPExcel->getActiveSheet()->setCellValue('A2', $_GET['id_formulario_plantilla']);

	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true); // Ajusta celda al texto
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true); // Ajusta celda al texto

	$objPHPExcel->getActiveSheet()->getStyle("A1:D1")->applyFromArray($style); // Centra texto

	// Rename 2nd sheet
	$objPHPExcel->getActiveSheet()->setTitle('Recomendaciones');

	// Redirect output to a client’s web browser (Excel5)
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment;filename="asistencia productores.xls"');
	header('Cache-Control: max-age=0');
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	$objWriter->save('php://output');

endif;	

