<?php
    chdir('../'); // Change directory to root
    
    // Start output buffering before any HTML output
    ob_start();
    session_start();
    
    // Include dependencies
    require_once("public/connection.php");
    require_once("public/functions.php");

	// Set headers to download file
	header('Content-Type: text/csv; charset=utf-8');
	header('Content-Disposition: attachment; filename=staff_payroll.csv');

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

	// Add column headers
	fputcsv($output, ["ID", "Staff Name", "Salary", "Surcharge", "Bonus", "Allowance", "Tax", "Pension", "Net Salary", "Date"]);

	// Fetch and write data
	$query = "SELECT * FROM staff_payroll";
	$result = $connection->query($query);

	while ($row = $result->fetch_assoc()) {
		fputcsv($output, $row);
	}

	fclose($output);
	$connection->close();
?>

