<?php
/**
 * Export Konsumsi PDF (Printable View)
 * Koperasi Management System
 */

require_once __DIR__ . '/../config/database.php';
$db = getDB();

$search = $_GET['search'] ?? '';
$selectedMonth = isset($_GET['month']) ? (int)$_GET['month'] : (int)date('n');
$selectedYear = isset($_GET['year']) ? (int)$_GET['year'] : (int)date('Y');

$where = "WHERE MONTH(t.tanggal) = :month AND YEAR(t.tanggal) = :year";
$queryParams = [
    ':month' => $selectedMonth,
    ':year' => $selectedYear
];

if ($search) {
    $where .= " AND (a.nama LIKE :search OR a.no_anggota LIKE :search OR t.nama_barang LIKE :search)";
    $queryParams[':search'] = "%$search%";
}

$stmt = $db->prepare("
    SELECT t.*, a.nama, a.no_anggota 
    FROM konsumsi_transaksi t 
    JOIN anggota a ON t.anggota_id = a.id 
    $where
    ORDER BY t.tanggal ASC, t.id ASC
");
$stmt->execute($queryParams);
$data = $stmt->fetchAll();

$months = [
    1 => 'Januari', 2 => 'Februari', 3 => 'Maret', 4 => 'April',
    5 => 'Mei', 6 => 'Juni', 7 => 'Juli', 8 => 'Agustus',
    9 => 'September', 10 => 'Oktober', 11 => 'November', 12 => 'Desember'
];

$totalHargaBeli = 0;
$totalPPN = 0;
$totalHargaJual = 0;
$totalJumlahBelanja = 0;

foreach ($data as $r) {
    $totalHargaBeli += $r['harga_beli'];
    $totalPPN += ($r['harga_beli'] * ($r['ppn_persen'] / 100));
    $totalHargaJual += $r['harga_jual'];
    $totalJumlahBelanja += $r['jumlah_belanja'];
}

include __DIR__ . '/../includes/header.php'; // Include header for styling and functions
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rekap Konsumsi - Koperasi Management System</title>
    <link rel="stylesheet" href="<?= ASSETS_URL ?>/css/style.css">
    <link rel="stylesheet" href="<?= ASSETS_URL ?>/css/premium.css">
    <style>
        body { font-family: 'Inter', sans-serif; margin: 0; padding: 0; background-color: #f8f9fa; color: #333; }
        .container { width: 90%; margin: 20px auto; background-color: #fff; padding: 30px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
        .header { text-align: center; margin-bottom: 30px; }
        .header h1 { margin: 0; font-size: 2em; color: var(--primary-800); }
        .header p { margin: 5px 0 0; font-size: 1em; color: #666; }
        .table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
        .table th, .table td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        .table th { background-color: var(--primary-100); color: var(--primary-800); font-weight: bold; }
        .table tr:nth-child(even) { background-color: #f2f2f2; }
        .text-right { text-align: right; }
        .text-center { text-align: center; }
        .total-row { background-color: var(--primary-50) !important; font-weight: bold; }
        .footer { margin-top: 30px; text-align: right; }
        .signature { margin-top: 50px; }
        .signature p { margin: 0; }
        @media print {
            body { background-color: #fff; }
            .container { box-shadow: none; border: none; }
            .header h1 { color: #333; }
            .table th { background-color: #e9ecef !important; color: #333 !important; -webkit-print-color-adjust: exact; }
            .total-row { background-color: #e9ecef !important; -webkit-print-color-adjust: exact; }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>REKAP KONSUMSI</h1>
            <p>Koperasi Management System</p>
            <p>Periode: <?= $months[$selectedMonth] ?> <?= $selectedYear ?></p>
            <?php if($search): ?>
            <p>Pencarian: <strong><?= htmlspecialchars($search) ?></strong></p>
            <?php endif; ?>
        </div>

        <table class="table">
            <thead>
                <tr>
                    <th width="5%">No</th>
                    <th width="10%">Tanggal</th>
                    <th width="15%">Nama Anggota</th>
                    <th width="10%">No Anggota</th>
                    <th width="20%">Nama Barang</th>
                    <th width="10%">Harga Beli (Rp)</th>
                    <th width="5%">PPN (%)</th>
                    <th width="10%">Harga Jual (Rp)</th>
                    <th width="10%">Jumlah Belanja (Rp)</th>
                    <th width="15%">Keterangan</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($data)): ?>
                    <tr><td colspan="10" class="text-center">Tidak ada data konsumsi</td></tr>
                <?php else: ?>
                    <?php foreach ($data as $i => $r): ?>
                        <tr>
                            <td class="text-center"><?= $i + 1 ?></td>
                            <td class="text-center"><?= tglIndo(date('Y-m-d', strtotime($r['tanggal']))) ?></td>
                            <td><?= htmlspecialchars($r['nama']) ?></td>
                            <td><?= htmlspecialchars($r['no_anggota']) ?></td>
                            <td><?= htmlspecialchars($r['nama_barang']) ?></td>
                            <td class="text-right"><?= number_format($r['harga_beli'], 0, ',', '.') ?></td>
                            <td class="text-right"><?= number_format($r['ppn_persen'], 0, ',', '.') ?></td>
                            <td class="text-right"><?= number_format($r['harga_jual'], 0, ',', '.') ?></td>
                            <td class="text-right"><?= number_format($r['jumlah_belanja'], 0, ',', '.') ?></td>
                            <td><?= htmlspecialchars($r['keterangan']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
            <tfoot>
                <tr class="total-row">
                    <td colspan="5" class="text-right">TOTAL</td>
                    <td class="text-right"><?= number_format($totalHargaBeli, 0, ',', '.') ?></td>
                    <td class="text-right"><?= number_format($totalPPN, 0, ',', '.') ?></td>
                    <td class="text-right"><?= number_format($totalHargaJual, 0, ',', '.') ?></td>
                    <td class="text-right"><?= number_format($totalJumlahBelanja, 0, ',', '.') ?></td>
                    <td></td>
                </tr>
            </tfoot>
        </table>

        <div class="footer">
            <p>Bogor, <?= tglIndo(date('Y-m-d')) ?></p>
            <div class="signature">
                <p>Bendahara,</p>
                <br><br><br>
                <p>(________________________)</p>
            </div>
        </div>
    </div>
</body>
</html>
<?php include __DIR__ . '/../includes/footer.php'; // Include footer for closing tags and scripts ?>