<?php
// download_template_xlsx.php

// !!! PENTING: ASUMSI AUTOLOAD TELAH DI-INCLUDE SEBELUMNYA !!!
// require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

// Buat Spreadsheet baru
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// Tulis Header
$sheet->setCellValue('A1', 'Nama');
$sheet->setCellValue('B1', 'NIK');
$sheet->setCellValue('C1', 'E-mail');
$sheet->setCellValue('D1', 'No. HP');

// Tulis Contoh Data
$sheet->setCellValue('A2', 'Budi Santoso');
$sheet->setCellValue('B2', '3573010101900001');
$sheet->setCellValue('C2', 'budi.s@example.com');
$sheet->setCellValue('D2', '08123456789');

$sheet->setCellValue('A3', 'Rina Kusuma');
$sheet->setCellValue('B3', '3573020202950002');
$sheet->setCellValue('C3', 'rina.k@example.com');
$sheet->setCellValue('D3', '08556789012');

// Opsional: Format kolom NIK sebagai Teks agar nol di depan tidak hilang
$sheet->getStyle('B2:B100')->getNumberFormat()->setFormatCode('@');


// Siapkan file writer
$writer = new Xlsx($spreadsheet);

$filename = "Template_Import_Mitra_" . date('Ymd') . ".xlsx";

// Set header HTTP untuk mengunduh file
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Cache-Control: max-age=0');
header('Expires: GMT');
header('Pragma: public');

// Kirim output ke browser
$writer->save('php://output');
exit;
?>