<?php
session_start(); // Start the session

// Check if the user is logged in
if (!isset($_SESSION['username'])) {
    // If not logged in, redirect to the login page
    header("Location: login.php");
    exit();
}

// Koneksi ke database
$servername = "localhost"; // Ganti jika perlu
$username = "root"; // Ganti jika perlu
$password = ""; // Ganti jika perlu
$dbname = "bprkredit"; // Nama database yang telah dibuat

// Membuat koneksi
$conn = new mysqli($servername, $username, $password, $dbname);

// Cek koneksi
if ($conn->connect_error) {
    die("Koneksi gagal: " . $conn->connect_error);
}

// Ambil data pengajuan dari database
$sql = "SELECT * FROM pengajuan ORDER BY id DESC";
$result = $conn->query($sql);

// Set header untuk file Excel
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="riwayat_pengajuan.xls"');

// Output header kolom
echo "ID\tNama\tNo. KTP\tAlamat\tKode Pos\tNo. Telp\tNominal\tUpload\tStatus\n";

// Output data
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo $row['id'] . "\t" .
             $row['nama'] . "\t" .
             $row['ktp'] . "\t" .
             $row['alamat'] . "\t" .
             $row['kodepos'] . "\t" .
             $row['telp'] . "\t" .
             $row['nominal'] . "\t" .
             $row['upload'] . "\t" .
             $row['status'] . "\n";
    }
} else {
    echo "Tidak ada pengajuan yang ditemukan.\n";
}

// Menutup koneksi
$conn->close();
?>