<div>
  <button type="button" class="btn btn-light" id="download_db">
      <svg xmlns="http://www.w3.org/2000/svg"  viewBox="0 0 48 48" width="48px" height="48px"><path fill="#4CAF50" d="M41,10H25v28h16c0.553,0,1-0.447,1-1V11C42,10.447,41.553,10,41,10z"/><path fill="#FFF" d="M32 15H39V18H32zM32 25H39V28H32zM32 30H39V33H32zM32 20H39V23H32zM25 15H30V18H25zM25 25H30V28H25zM25 30H30V33H25zM25 20H30V23H25z"/><path fill="#2E7D32" d="M27 42L6 38 6 10 27 6z"/><path fill="#FFF" d="M19.129,31l-2.411-4.561c-0.092-0.171-0.186-0.483-0.284-0.938h-0.037c-0.046,0.215-0.154,0.541-0.324,0.979L13.652,31H9.895l4.462-7.001L10.274,17h3.837l2.001,4.196c0.156,0.331,0.296,0.725,0.42,1.179h0.04c0.078-0.271,0.224-0.68,0.439-1.22L19.237,17h3.515l-4.199,6.939l4.316,7.059h-3.74V31z"/></svg>
  </button>
</div>


<script>
  function getFechaHoraFormato() {
    const now = new Date();
    const pad = (n) => n.toString().padStart(2, '0');
    const dia = pad(now.getDate());
    const mes = pad(now.getMonth() + 1);
    const anio = now.getFullYear();
    const hora = pad(now.getHours());
    const minuto = pad(now.getMinutes());
    return `${dia}.${mes}.${anio}_${hora}_${minuto}`;
  }

  async function download_db() {
    let response = await fetch('/user/download-db', {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      },
      credentials: 'include'
    });

    let responseData = await response.json();
    let data = responseData.data;
    let headers = ['ID', 'Fecha', 'Hora', 'Nombre', 'Apellido', 'Email', 'Estado', 'Teléfono', 'No. Empleados', 'Rol Contacto', 'Sector', 'Requerimiento', 'Status'];

    // Insertar los headers al inicio
    data.unshift(headers);

    // Crear el libro y hoja
    let ws = XLSX.utils.aoa_to_sheet(data); // aoa = array of arrays
    let wb = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, `db_contactos_${getFechaHoraFormato()}`);

    // Descargar el archivo
    XLSX.writeFile(wb, `db_contactos_${getFechaHoraFormato()}.xlsx`);
  }

  let btn = document.getElementById("download_db");
  btn.addEventListener("click", download_db);
</script>