<?php
/**
 * admin/download-csv.php
 * Export register as CSV.
 */

require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

requireLogin();

$stmt = $pdo->query("SELECT membership_id, full_name, gender, phone, kindred, DATE_FORMAT(date_joined, '%Y-%m-%d') as registered
                      FROM members ORDER BY created_at DESC");
$rows = $stmt->fetchAll();

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="register_' . date('Y-m-d') . '.csv"');

$out = fopen('php://output', 'w');
fputcsv($out, ['Membership ID', 'Full Name', 'Gender', 'Phone', 'Kindred', 'Date Joined']);
foreach ($rows as $r) {
    fputcsv($out, $r);
}
fclose($out);
exit;