<?php
include 'config.php';
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="Salary_Master_' . date('Y-m-d') . '.csv"');

$output = fopen('php://output', 'w');
fputcsv($output, ['Emp Code','Name','Department','Gross Salary','Basic','DA','HRA','Medical','Conveyance','EPF','ESI','Travel','2W Maintenance','TDS','Advance','Net Salary','Email']);

$res = $conn->query("SELECT * FROM salary_master ORDER BY department, name");
while ($row = $res->fetch_assoc()) {
    fputcsv($output, [
        $row['emp_code'],
        $row['name'],
        $row['department'],
        $row['gross_salary'],
        $row['basic'],
        $row['da'],
        $row['hra'],
        $row['medical'],
        $row['conveyance'],
        $row['epf'],
        $row['esi'],
        $row['travel_allowance'],
        $row['tw_maintenance'],
        $row['tds'],
        $row['advance'],
        $row['net_salary'],
        $row['email']
    ]);
}