<?php
include 'koneksi.php';
include 'auth.php';         // Mengecek login
include 'akses_ditolak.php'; // Tambahkan ini
// Fungsi untuk mengunduh data sebagai XLS
function downloadXLS($query, $filename)
{
    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=$filename.xls");

    $result = mysqli_query($GLOBALS['koneksi'], $query);
    $columns = mysqli_fetch_fields($result);

    echo "<table border='1'><tr>";
    foreach ($columns as $col) {
        echo "<th>{$col->name}</th>";
    }
    echo "</tr>";

    while ($row = mysqli_fetch_assoc($result)) {
        echo "<tr>";
        foreach ($row as $data) {
            echo "<td>{$data}</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
    exit;
}

// Tangani request download via POST
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['download'])) {
    $table = $_POST['download'];
    switch ($table) {
        case 'absen':
            downloadXLS("SELECT * FROM absen", "data_absen");
            break;
        case 'absensi':
            downloadXLS("SELECT * FROM absensi", "data_absensi");
            break;
        case 'tabungan':
            downloadXLS("SELECT * FROM tabungan", "data_tabungan");
            break;
        case 'user':
            downloadXLS("SELECT * FROM user", "data_user");
            break;
        case 'orders':
            downloadXLS("SELECT * FROM orders", "data_orders");
            break;
    }
}
?>

<!DOCTYPE html>
<html lang="id">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <link rel="stylesheet" href="css/fa/all.css">
    <link rel="stylesheet" href="css/admin.css">
    <link rel="stylesheet" href="css/navbar.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
</head>

<style>
    button {
        border: 1px solid #ddd;
        border-radius: 8px;
        background: linear-gradient(to top, #f5a300, #f5d216, #f5a300);
        color: black;
        cursor: pointer;
        font-weight: bold;
        text-align: center;
        margin-bottom: 10px;
        height: 80px;
    }

    button:hover {
        transform: scale(1.05);
    }
</style>

<body>
    <h4 style="margin-top:50px;">DOWNLOAD DATA</h4>

    <form method="POST" style="margin-top:30px;">
        <button class="button" name="download" value="absen">Download Daily Stay</button>
        <button class="button" name="download" value="absensi">Download Absensi</button>
        <button class="button" name="download" value="tabungan">Download Report Tabungan</button>
        <button class="button" name="download" value="user">Download Daftar Member</button>
        <button class="button" name="download" value="orders">Download Report Orderan</button>
    </form>

    <h6>File yang di Download berupa file Fxcel, dengan extensi .xls</h6>
    <?php include 'navbar4.php'; ?>


</body>

</html>