<?php
require_once 'config.php';
checkLogin();

header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=directorio_contactos_" . date('Y-m-d') . ".xls");
header("Pragma: no-cache");
header("Expires: 0");

$stmt = $pdo->query("SELECT * FROM contactos");
$contactos = $stmt->fetchAll();

echo "<table border='1'>";
echo "<tr>
        <th style='background-color: #003366; color: white;'>Nombre y Apellido</th>
        <th style='background-color: #003366; color: white;'>Empresa</th>
        <th style='background-color: #003366; color: white;'>Pais</th>
        <th style='background-color: #003366; color: white;'>Ciudad</th>
        <th style='background-color: #003366; color: white;'>Telefono 1</th>
        <th style='background-color: #003366; color: white;'>Telefono 2</th>
        <th style='background-color: #003366; color: white;'>Correo</th>
        <th style='background-color: #003366; color: white;'>Cumpleaños</th>
      </tr>";

foreach ($contactos as $c) {
    echo "<tr>";
    echo "<td>" . htmlspecialchars($c['nombre_apellido']) . "</td>";
    echo "<td>" . htmlspecialchars($c['empresa']) . "</td>";
    echo "<td>" . htmlspecialchars($c['pais']) . "</td>";
    echo "<td>" . htmlspecialchars($c['ciudad']) . "</td>";
    echo "<td>" . htmlspecialchars($c['telefono1']) . "</td>";
    echo "<td>" . htmlspecialchars($c['telefono2']) . "</td>";
    echo "<td>" . htmlspecialchars($c['correo']) . "</td>";
    echo "<td>" . htmlspecialchars($c['fecha_cumpleanos']) . "</td>";
    echo "</tr>";
}
echo "</table>";
?>
