<?php
/**
 * Backup transfers as CSV for a given date range
 */

require_once __DIR__ . '/../../includes/init.php';
requireLogin();

$fromDate = sanitize($_GET['from_date'] ?? '');
$toDate = sanitize($_GET['to_date'] ?? '');

if (!$fromDate || !$toDate) {
    $today = new DateTimeImmutable();
    $fromDate = $today->modify('first day of this month')->format('Y-m-d');
    $toDate = $today->modify('last day of this month')->format('Y-m-d');
}

try {
    $db = getDB();

    $sql = "
        SELECT
            t.transfer_id,
            t.employee_id,
            t.employee_category,
            t.from_site_id,
            t.from_location,
            t.to_site_id,
            t.to_location,
            t.transfer_date,
            t.effective_branch,
            t.effective_attendance,
            t.esi_applicability,
            t.transfer_form_filled,
            t.approved_by_district_head,
            t.transfer_reason,
            t.transfer_status,
            t.created_at,
            t.approved_at
        FROM employee_transfers t
        WHERE t.transfer_date BETWEEN :from_date AND :to_date
        ORDER BY t.transfer_date DESC, t.transfer_id DESC
    ";

    $stmt = $db->prepare($sql);
    $stmt->execute([
        ':from_date' => $fromDate,
        ':to_date' => $toDate,
    ]);
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // CSV headers
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename="employee_transfers_' . $fromDate . '_to_' . $toDate . '.csv"');

    $output = fopen('php://output', 'w');

    // Header row
    fputcsv($output, array_keys([
        'transfer_id' => '',
        'employee_id' => '',
        'employee_category' => '',
        'from_site_id' => '',
        'from_location' => '',
        'to_site_id' => '',
        'to_location' => '',
        'transfer_date' => '',
        'effective_branch' => '',
        'effective_attendance' => '',
        'esi_applicability' => '',
        'transfer_form_filled' => '',
        'approved_by_district_head' => '',
        'transfer_reason' => '',
        'transfer_status' => '',
        'created_at' => '',
        'approved_at' => '',
    ]));

    foreach ($rows as $row) {
        fputcsv($output, [
            $row['transfer_id'],
            $row['employee_id'],
            $row['employee_category'],
            $row['from_site_id'],
            $row['from_location'],
            $row['to_site_id'],
            $row['to_location'],
            $row['transfer_date'],
            $row['effective_branch'],
            $row['effective_attendance'],
            $row['esi_applicability'],
            $row['transfer_form_filled'],
            $row['approved_by_district_head'],
            $row['transfer_reason'],
            $row['transfer_status'],
            $row['created_at'],
            $row['approved_at'],
        ]);
    }

    fclose($output);
    exit;

} catch (Exception $e) {
    error_log('Transfer backup error: ' . $e->getMessage());
    redirect(BASE_URL . '/modules/transfer/report.php', 'Error generating backup.', 'error');
}


