<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


		$finish_timeb = time();
		$texto_finishb= date("d/m/y", $finish_timeb);
		require_once 'PHPExcel/Classes/PHPExcel.php';
		$filename = 'Reporte_Mensajes_'.$texto_finishb; //your file name


include_once '../dbconnect.php';

//print first employee table

$sql = "SELECT * FROM comentarios WHERE id_usuario != '7704' AND id_usuario !='10216728766392800'";
$rs_result = $mysqli->query($sql)  or die("Error en".$sql);

$objPHPExcel = new PHPExcel();
/*********************Add column headings START**********************/
		$objPHPExcel->setActiveSheetIndex(0)

					->setCellValue('A1', 'FECHA')

					->setCellValue('B1', 'TIPO')
					->setCellValue('C1', 'COMENTARIO')
					->setCellValue('D1', 'EMAIL USUARIO')
					->setCellValue('E1', 'NOMBRE USUARIO');
		/*********************Add column headings END**********************/


$row = 2;
while($subrow = mysqli_fetch_assoc($rs_result))
{
$uid = $subrow["id_usuario"];


	$sql2 = "SELECT * FROM registros WHERE id_registro = $uid OR facebook_id = '$uid'";
	$rs_result2 = $mysqli->query($sql2)  or die("Error en".$sql2);

	while($subrow2 = mysqli_fetch_assoc($rs_result2))
	{

		$email = $subrow2["correo"];
		$nombre = $subrow2["nombre"] . " ". $subrow2["apellido"];
		$nombre = strtolower($nombre);
		$nombre = ucwords($nombre);
		$telefono = $subrow2["telefono"];

	}

 		$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $subrow['fecha']);
 		$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $subrow['tipo']);
		$objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $subrow['comentario']);
		$objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $email);
    $objPHPExcel->getActiveSheet()->setCellValue('E'.$row, $nombre);




    $row++;
}
/*********************Autoresize column width depending upon contents START**********************/
        foreach(range('A','E') as $columnID) {
			$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
		}
		/*********************Autoresize column width depending upon contents END***********************/

		$objPHPExcel->getActiveSheet()->getStyle('A1:E1')->getFont()->setBold(true); //Make heading font bold

		/*********************Add color to heading START**********************/
		$styleArray = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => 'F0F0F0'),
        'size'  => 10,
        'name'  => 'Century Gothic',

    ),
	'fill'  => array(
    'type' => PHPExcel_Style_Fill::FILL_SOLID,
    'color' => array('rgb' => 'DD1B2B')
	),
	'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )

	);
		$objPHPExcel->getActiveSheet()
					->getStyle('A1:E1')->applyFromArray($styleArray);
		/*********************Add color to heading END***********************/

		$objPHPExcel->getActiveSheet()->setTitle('Comentarios y Vipstorias'); //give title to sheet
		$objPHPExcel->setActiveSheetIndex(0);
		header('Content-Type: application/vnd.ms-excel');
		header("Content-Disposition: attachment;Filename=$filename.xls");
		header('Cache-Control: max-age=0');
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
		exit;
?>
