<?php
    session_start();

    include("../scripts/connection.php");
    include("../scripts/functions.php");
    $user_data = check_admin($con);

    if(isset($_GET['table'])) {
        $table = $_GET['table'];
    }

    header('Content-type: text/csv');
    header('Content-Disposition: attachment; filename="table.csv" ');

function download_table($stmt) {
    $output = fopen('php://output', 'w');
    $header = true;
    while ($row = $stmt->fetch()) {
        if ($header) {
            fputcsv($output, array_keys($row));
            $header = false;
        }
        fputcsv($output, $row);
    }
    fclose($output);
}

    if($table == "total-score") {
        $total_score = get_total_score($con);
        download_table($total_score);
    } else {
        $station = get_station($con, $table);
        download_table($station);
    }
?>