<?php
/**
 * Export SHU - Preview & Download
 * Supports: PDF, Word, Excel
 */

require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/config/auth.php';
requireAdmin();

$db = getDB();

// Selected year
$tahun = intval($_GET['tahun'] ?? date('Y'));

// Get SHU data
$stmt = $db->prepare("SELECT * FROM shu WHERE tahun = ?");
$stmt->execute([$tahun]);
$shu = $stmt->fetch();

if (!$shu) {
    die("Data SHU untuk tahun $tahun tidak ditemukan. Silakan hitung SHU terlebih dahulu.");
}

// Get SHU per anggota
$stmtAnggota = $db->prepare("
    SELECT sa.*, a.nama, a.no_anggota
    FROM shu_anggota sa
    JOIN anggota a ON sa.anggota_id = a.id
    WHERE sa.shu_id = ?
    ORDER BY total_shu DESC
");
$stmtAnggota->execute([$shu['id']]);
$shuAnggota = $stmtAnggota->fetchAll();

// Handle export action
$action = isset($_GET['action']) ? $_GET['action'] : 'preview';
$format = isset($_GET['format']) ? $_GET['format'] : 'pdf';

if ($action === 'download') {
    if ($format === 'pdf') {
        exportPDF();
    } elseif ($format === 'word') {
        exportWord();
    } elseif ($format === 'excel') {
        exportExcel();
    }
    exit;
}

function exportPDF() {
    global $shu, $shuAnggota, $tahun;
    
    header('Content-Type: text/html; charset=utf-8');
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Laporan SHU <?= $tahun ?></title>
        <style>
            @page { size: A4 landscape; margin: 15mm; }
            body { font-family: Arial, sans-serif; margin: 0; padding: 15px; color: #333; font-size: 10px; }
            .header { text-align: center; margin-bottom: 20px; border-bottom: 3px solid #dc2626; padding-bottom: 15px; }
            .header h1 { margin: 0; color: #b91c1c; font-size: 20px; }
            .header h2 { margin: 8px 0 5px; color: #374151; font-size: 16px; }
            .section { margin-top: 20px; background: #f9fafb; padding: 15px; border-radius: 8px; }
            .section h3 { margin-top: 0; color: #1e40af; }
            table { width: 100%; border-collapse: collapse; margin-top: 10px; }
            table th { background: #dc2626; color: white; padding: 8px; text-align: left; font-size: 10px; }
            table td { padding: 6px 8px; border-bottom: 1px solid #e5e7eb; font-size: 10px; }
            table tr:nth-child(even) { background: #f9fafb; }
            .signature { margin-top: 40px; display: flex; justify-content: space-between; }
            .signature-box { text-align: center; width: 200px; }
            .signature-box .line { margin-top: 70px; border-top: 2px solid #333; padding-top: 5px; }
            @media print {
                body { padding: 0; }
                .no-print { display: none !important; }
            }
        </style>
    </head>
    <body>
        <div class="no-print" style="background: #fee2e2; padding: 12px; text-align: center; margin-bottom: 15px; border-radius: 8px;">
            <button onclick="window.print()" style="padding: 8px 16px; background: #dc2626; color: white; border: none; border-radius: 5px; cursor: pointer; margin: 0 5px;">🖨️ Print</button>
            <a href="?action=download&format=pdf" style="padding: 8px 16px; background: #059669; color: white; text-decoration: none; border-radius: 5px; margin: 0 5px;">💾 PDF</a>
            <a href="?format=word" style="padding: 8px 16px; background: #2563eb; color: white; text-decoration: none; border-radius: 5px; margin: 0 5px;">📝 Word</a>
            <a href="?format=excel" style="padding: 8px 16px; background: #10b981; color: white; text-decoration: none; border-radius: 5px; margin: 0 5px;">📊 Excel</a>
            <a href="shu.php?tahun=<?= $tahun ?>" style="padding: 8px 16px; background: #6b7280; color: white; text-decoration: none; border-radius: 5px; margin: 0 5px;">← Kembali</a>
        </div>
        
        <div class="header">
            <h1>KOPERASI TSUROYA</h1>
            <h2>LAPORAN SISA HASIL USAHA (SHU)</h2>
            <p>Tahun <?= $tahun ?></p>
        </div>
        
        <div class="section">
            <h3>Ringkasan SHU</h3>
            <table>
                <tr><td style="width: 250px; font-weight: 600;">Total Pendapatan (Jasa Pinjaman)</td><td style="text-align: right; font-weight: 600;">Rp <?= number_format($shu['total_pendapatan'], 0, ',', '.') ?></td></tr>
                <tr><td style="font-weight: 600;">Total Beban Operasional</td><td style="text-align: right; font-weight: 600;">Rp <?= number_format($shu['total_beban'], 0, ',', '.') ?></td></tr>
                <tr style="background: #dbeafe;"><td style="font-weight: 700; font-size: 12px;">SHU BERSIH</td><td style="text-align: right; font-weight: 700; font-size: 12px; color: #1e40af;">Rp <?= number_format($shu['shu_bersih'], 0, ',', '.') ?></td></tr>
            </table>
        </div>
        
        <div class="section">
            <h3>Distribusi SHU</h3>
            <table>
                <tr><th>Komponen</th><th style="text-align: right;">Persentase</th><th style="text-align: right;">Nominal</th></tr>
                <tr><td>Dana Cadangan</td><td style="text-align: right;"><?= $shu['persen_cadangan'] ?>%</td><td style="text-align: right;">Rp <?= number_format($shu['dana_cadangan'], 0, ',', '.') ?></td></tr>
                <tr><td>Dana Jasa Simpanan</td><td style="text-align: right;"><?= $shu['persen_jasa_simpanan'] ?>%</td><td style="text-align: right;">Rp <?= number_format($shu['dana_jasa_simpanan'], 0, ',', '.') ?></td></tr>
                <tr><td>Dana Jasa Pinjaman</td><td style="text-align: right;"><?= $shu['persen_jasa_pinjaman'] ?>%</td><td style="text-align: right;">Rp <?= number_format($shu['dana_jasa_pinjaman'], 0, ',', '.') ?></td></tr>
                <tr><td>Dana Pengurus</td><td style="text-align: right;"><?= $shu['persen_pengurus'] ?>%</td><td style="text-align: right;">Rp <?= number_format($shu['dana_pengurus'], 0, ',', '.') ?></td></tr>
                <tr><td>Dana Pendidikan</td><td style="text-align: right;"><?= $shu['persen_pendidikan'] ?>%</td><td style="text-align: right;">Rp <?= number_format($shu['dana_pendidikan'], 0, ',', '.') ?></td></tr>
                <tr><td>Dana Sosial</td><td style="text-align: right;"><?= $shu['persen_sosial'] ?>%</td><td style="text-align: right;">Rp <?= number_format($shu['dana_sosial'], 0, ',', '.') ?></td></tr>
                <tr><td>Dana Pembangunan</td><td style="text-align: right;"><?= $shu['persen_pembangunan'] ?>%</td><td style="text-align: right;">Rp <?= number_format($shu['dana_pembangunan'], 0, ',', '.') ?></td></tr>
                <tr style="background: #dbeafe;"><td style="font-weight: 700;">TOTAL</td><td style="text-align: right; font-weight: 700;">100%</td><td style="text-align: right; font-weight: 700;">Rp <?= number_format($shu['shu_bersih'], 0, ',', '.') ?></td></tr>
            </table>
        </div>
        
        <div class="section">
            <h3>Pembagian SHU per Anggota</h3>
            <table>
                <tr><th>No</th><th>No. Anggota</th><th>Nama</th><th style="text-align: right;">Jasa Simpanan</th><th style="text-align: right;">Jasa Pinjaman</th><th style="text-align: right;">Total SHU</th></tr>
                <?php $no = 1; foreach ($shuAnggota as $sa): ?>
                <tr>
                    <td style="text-align: center;"><?= $no++ ?></td>
                    <td><?= htmlspecialchars($sa['no_anggota']) ?></td>
                    <td><?= htmlspecialchars($sa['nama']) ?></td>
                    <td style="text-align: right;">Rp <?= number_format($sa['shu_jasa_simpanan'], 0, ',', '.') ?></td>
                    <td style="text-align: right;">Rp <?= number_format($sa['shu_jasa_pinjaman'], 0, ',', '.') ?></td>
                    <td style="text-align: right; font-weight: 600;">Rp <?= number_format($sa['total_shu'], 0, ',', '.') ?></td>
                </tr>
                <?php endforeach; ?>
            </table>
        </div>
        
        <div class="signature">
            <div class="signature-box">
                <p>Mengetahui,</p>
                <p>Ketua Koperasi</p>
                <div class="line">(_________________)</div>
            </div>
            <div class="signature-box">
                <p>Bendahara</p>
                <div class="line">(_________________)</div>
            </div>
        </div>
    </body>
    </html>
    <?php
    exit;
}

function exportWord() {
    global $shu, $shuAnggota, $tahun;
    
    header('Content-Type: application/msword');
    header('Content-Disposition: attachment; filename="Laporan_SHU_' . $tahun . '.doc"');
    ?>
    <html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'>
    <head>
        <meta charset="utf-8">
        <title>Laporan SHU <?= $tahun ?></title>
        <style>
            body { font-family: Arial, sans-serif; font-size: 10pt; }
            .header { text-align: center; margin-bottom: 15px; }
            .header h1 { margin: 0; font-size: 16pt; }
            .header h2 { margin: 8px 0 5px; font-size: 14pt; }
            table { width: 100%; border-collapse: collapse; margin-top: 10px; }
            th { background-color: #dc2626; color: white; padding: 6px; text-align: left; font-size: 10pt; }
            td { padding: 6px; border: 1px solid #ddd; font-size: 10pt; }
        </style>
    </head>
    <body>
        <div class="header">
            <h1>KOPERASI TSUROYA</h1>
            <h2>LAPORAN SISA HASIL USAHA (SHU) - Tahun <?= $tahun ?></h2>
        </div>
        
        <table style="width: 50%; margin-bottom: 20px;">
            <tr><td style="width: 200px;"><b>Total Pendapatan</b></td><td>Rp <?= number_format($shu['total_pendapatan'], 0, ',', '.') ?></td></tr>
            <tr><td><b>Total Beban</b></td><td>Rp <?= number_format($shu['total_beban'], 0, ',', '.') ?></td></tr>
            <tr><td><b>SHU Bersih</b></td><td style="font-weight: bold; color: #1e40af;">Rp <?= number_format($shu['shu_bersih'], 0, ',', '.') ?></td></tr>
        </table>
        
        <table>
            <tr><th>Komponen</th><th>Persentase</th><th>Nominal</th></tr>
            <tr><td>Dana Cadangan</td><td style="text-align: right;"><?= $shu['persen_cadangan'] ?>%</td><td style="text-align: right;"><?= number_format($shu['dana_cadangan'], 0, ',', '.') ?></td></tr>
            <tr><td>Dana Jasa Simpanan</td><td style="text-align: right;"><?= $shu['persen_jasa_simpanan'] ?>%</td><td style="text-align: right;"><?= number_format($shu['dana_jasa_simpanan'], 0, ',', '.') ?></td></tr>
            <tr><td>Dana Jasa Pinjaman</td><td style="text-align: right;"><?= $shu['persen_jasa_pinjaman'] ?>%</td><td style="text-align: right;"><?= number_format($shu['dana_jasa_pinjaman'], 0, ',', '.') ?></td></tr>
            <tr><td>Dana Pengurus</td><td style="text-align: right;"><?= $shu['persen_pengurus'] ?>%</td><td style="text-align: right;"><?= number_format($shu['dana_pengurus'], 0, ',', '.') ?></td></tr>
            <tr><td>Dana Pendidikan</td><td style="text-align: right;"><?= $shu['persen_pendidikan'] ?>%</td><td style="text-align: right;"><?= number_format($shu['dana_pendidikan'], 0, ',', '.') ?></td></tr>
            <tr><td>Dana Sosial</td><td style="text-align: right;"><?= $shu['persen_sosial'] ?>%</td><td style="text-align: right;"><?= number_format($shu['dana_sosial'], 0, ',', '.') ?></td></tr>
            <tr><td>Dana Pembangunan</td><td style="text-align: right;"><?= $shu['persen_pembangunan'] ?>%</td><td style="text-align: right;"><?= number_format($shu['dana_pembangunan'], 0, ',', '.') ?></td></tr>
        </table>
        
        <h3>Pembagian SHU per Anggota</h3>
        <table>
            <tr><th>No</th><th>No. Anggota</th><th>Nama</th><th style="text-align: right;">Jasa Simpanan</th><th style="text-align: right;">Jasa Pinjaman</th><th style="text-align: right;">Total SHU</th></tr>
            <?php $no = 1; foreach ($shuAnggota as $sa): ?>
            <tr>
                <td style="text-align: center;"><?= $no++ ?></td>
                <td><?= htmlspecialchars($sa['no_anggota']) ?></td>
                <td><?= htmlspecialchars($sa['nama']) ?></td>
                <td style="text-align: right;"><?= number_format($sa['shu_jasa_simpanan'], 0, ',', '.') ?></td>
                <td style="text-align: right;"><?= number_format($sa['shu_jasa_pinjaman'], 0, ',', '.') ?></td>
                <td style="text-align: right; font-weight: bold;"><?= number_format($sa['total_shu'], 0, ',', '.') ?></td>
            </tr>
            <?php endforeach; ?>
        </table>
    </body>
    </html>
    <?php
    exit;
}

function exportExcel() {
    global $shu, $shuAnggota, $tahun;
    
    $filename = "Laporan_SHU_" . $tahun;
    $tmpfile = tempnam(sys_get_temp_dir(), 'xlsx_');
    
    $zip = new ZipArchive();
    if ($zip->open($tmpfile, ZipArchive::CREATE | ZipArchive::OVERWRITE) === false) {
        die('Gagal membuat file Excel');
    }
    
    $sheetData = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<cols>
<col min="1" max="1" width="25" customWidth="1"/>
<col min="2" max="2" width="20" customWidth="1"/>
</cols><sheetData>';

    $sheetData .= '<row r="1"><c r="A1" t="str"><v>KOPERASI TSUROYYA</v></c></row>';
    $sheetData .= '<row r="2"><c r="A2" t="str"><v>LAPORAN SISA HASIL USAHA (SHU) - Tahun ' . $tahun . '</v></c></row>';
    $sheetData .= '<row r="3"/>';
    
    $sheetData .= '<row r="4"><c r="A4" t="str"><v>Total Pendapatan</v></c><c r="B4"><v>' . $shu['total_pendapatan'] . '</v></c></row>';
    $sheetData .= '<row r="5"><c r="A5" t="str"><v>Total Beban</v></c><c r="B5"><v>' . $shu['total_beban'] . '</v></c></row>';
    $sheetData .= '<row r="6"><c r="A6" t="str"><v>SHU BERSIH</v></c><c r="B6"><v>' . $shu['shu_bersih'] . '</v></c></row>';
    $sheetData .= '<row r="7"/>';
    
    $sheetData .= '<row r="8"><c r="A8" t="str"><v>DISTRIBUSI SHU</v></c></row>';
    $headers = ['Komponen', 'Persentase', 'Nominal'];
    $sheetData .= '<row r="9">';
    foreach ($headers as $i => $header) {
        $col = chr(65 + $i);
        $sheetData .= '<c r="' . $col . '9" t="str"><v>' . $header . '</v></c>';
    }
    $sheetData .= '</row>';
    
    $distribusi = [
        ['Dana Cadangan', $shu['persen_cadangan'], $shu['dana_cadangan']],
        ['Dana Jasa Simpanan', $shu['persen_jasa_simpanan'], $shu['dana_jasa_simpanan']],
        ['Dana Jasa Pinjaman', $shu['persen_jasa_pinjaman'], $shu['dana_jasa_pinjaman']],
        ['Dana Pengurus', $shu['persen_pengurus'], $shu['dana_pengurus']],
        ['Dana Pendidikan', $shu['persen_pendidikan'], $shu['dana_pendidikan']],
        ['Dana Sosial', $shu['persen_sosial'], $shu['dana_sosial']],
        ['Dana Pembangunan', $shu['persen_pembangunan'], $shu['dana_pembangunan']]
    ];
    
    $rowNum = 10;
    foreach ($distribusi as $d) {
        $sheetData .= '<row r="' . $rowNum . '">';
        $sheetData .= '<c r="A' . $rowNum . '" t="str"><v>' . $d[0] . '</v></c>';
        $sheetData .= '<c r="B' . $rowNum . '"><v>' . $d[1] . '</v></c>';
        $sheetData .= '<c r="C' . $rowNum . '"><v>' . $d[2] . '</v></c>';
        $sheetData .= '</row>';
        $rowNum++;
    }
    
    $rowNum += 2;
    $sheetData .= '<row r="' . $rowNum . '"><c r="A' . $rowNum . '" t="str"><v>PEMBAGIAN SHU PER ANGGOTA</v></c></row>';
    $rowNum++;
    
    $headers = ['No', 'No. Anggota', 'Nama', 'Jasa Simpanan', 'Jasa Pinjaman', 'Total SHU'];
    $sheetData .= '<row r="' . $rowNum . '">';
    foreach ($headers as $i => $header) {
        $col = chr(65 + $i);
        $sheetData .= '<c r="' . $col . $rowNum . '" t="str"><v>' . $header . '</v></c>';
    }
    $sheetData .= '</row>';
    $rowNum++;
    
    $no = 1;
    foreach ($shuAnggota as $sa) {
        $sheetData .= '<row r="' . $rowNum . '">';
        $sheetData .= '<c r="A' . $rowNum . '"><v>' . $no . '</v></c>';
        $sheetData .= '<c r="B' . $rowNum . '" t="str"><v>' . htmlspecialchars($sa['no_anggota']) . '</v></c>';
        $sheetData .= '<c r="C' . $rowNum . '" t="str"><v>' . htmlspecialchars($sa['nama']) . '</v></c>';
        $sheetData .= '<c r="D' . $rowNum . '"><v>' . $sa['shu_jasa_simpanan'] . '</v></c>';
        $sheetData .= '<c r="E' . $rowNum . '"><v>' . $sa['shu_jasa_pinjaman'] . '</v></c>';
        $sheetData .= '<c r="F' . $rowNum . '"><v>' . $sa['total_shu'] . '</v></c>';
        $sheetData .= '</row>';
        $rowNum++;
        $no++;
    }
    
    $sheetData .= '</sheetData></worksheet>';
    $zip->addFromString('xl/worksheets/sheet1.xml', $sheetData);
    
    $contentTypes = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>
</Types>';
    $zip->addFromString('[Content_Types].xml', $contentTypes);
    
    $rels = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
</Relationships>';
    $zip->addFromString('_rels/.rels', $rels);
    
    $workbookRels = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
</Relationships>';
    $zip->addFromString('xl/_rels/workbook.xml.rels', $workbookRels);
    
    $workbook = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<sheets><sheet name="SHU" sheetId="1" r:id="rId1"/></sheets>
</workbook>';
    $zip->addFromString('xl/workbook.xml', $workbook);
    
    $styles = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<fonts count="1"><font><sz val="11"/><name val="Calibri"/></font></fonts>
<fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills>
<borders count="1"><border><left/><right/><top/><bottom/></border></borders>
<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>
<cellXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/></cellXfs>
</styleSheet>';
    $zip->addFromString('xl/styles.xml', $styles);
    
    $zip->close();
    
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment; filename="' . $filename . '.xlsx"');
    header('Content-Length: ' . filesize($tmpfile));
    header('Cache-Control: no-cache');
    
    readfile($tmpfile);
    unlink($tmpfile);
    exit;
}

include __DIR__ . '/includes/header.php';
?>

<style>
    .export-actions { display: flex; gap: 10px; margin: 20px 0; flex-wrap: wrap; }
    .export-btn { padding: 12px 24px; border: none; border-radius: 8px; cursor: pointer; font-weight: 600; text-decoration: none; display: inline-flex; align-items: center; gap: 8px; transition: all 0.3s; }
    .export-btn:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.15); }
    .btn-pdf { background: #dc2626; color: white; }
    .btn-word { background: #2563eb; color: white; }
    .btn-excel { background: #10b981; color: white; }
    .btn-back { background: #6b7280; color: white; }
    .preview-frame { width: 100%; height: 600px; border: 2px solid #e5e7eb; border-radius: 8px; margin-top: 20px; }
    .info-card { background: white; padding: 20px; border-radius: 8px; margin-bottom: 20px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
</style>

<div class="info-card">
    <h2 style="margin-top: 0;">📄 Export Preview - Laporan SHU <?= $tahun ?></h2>
    <p><strong>SHU Bersih:</strong> Rp <?= number_format($shu['shu_bersih'], 0, ',', '.') ?></p>
    <p><strong>Jumlah Anggota Penerima:</strong> <?= count($shuAnggota) ?> anggota</p>
</div>

<div class="export-actions">
    <a href="?format=pdf&action=preview" class="export-btn btn-pdf">📕 Preview PDF</a>
    <a href="?format=word&action=preview" class="export-btn btn-word">📘 Preview Word</a>
    <a href="?format=excel&action=preview" class="export-btn btn-excel">📗 Preview Excel</a>
    <a href="shu.php?tahun=<?= $tahun ?>" class="export-btn btn-back">← Kembali</a>
</div>

<?php
if ($format === 'pdf' && $action === 'preview'):
?>
    <iframe src="?format=pdf&action=download" class="preview-frame"></iframe>
<?php elseif ($format === 'word' && $action === 'preview'): ?>
    <div class="info-card">
        <h3>📘 Preview Word</h3>
        <p>Klik tombol di bawah untuk melihat atau download file Word:</p>
        <a href="?format=word&action=download" class="export-btn btn-word" target="_blank">📝 Open / Download Word</a>
    </div>
<?php elseif ($format === 'excel' && $action === 'preview'): ?>
    <div class="info-card">
        <h3>📗 Preview Excel</h3>
        <p>Klik tombol di bawah untuk melihat atau download file Excel:</p>
        <a href="?format=excel&action=download" class="export-btn btn-excel" target="_blank">📊 Open / Download Excel</a>
    </div>
<?php endif; ?>
