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

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

$search = $_GET['search'] ?? '';
$where = $search ? "WHERE keterangan LIKE ?" : "";
$params = $search ? ["%$search%"] : [];

$allData = $db->query("SELECT * FROM provisi ORDER BY tanggal ASC, id ASC")->fetchAll();
$saldoMap = [];
$tempSaldo = 0;
foreach ($allData as $row) {
    if ($row['tipe'] === 'masuk') $tempSaldo += $row['jumlah'];
    else $tempSaldo -= $row['jumlah'];
    $saldoMap[$row['id']] = $tempSaldo;
}

$stmt = $db->prepare("SELECT * FROM provisi $where ORDER BY tanggal ASC, id ASC");
$stmt->execute($params);
$data = $stmt->fetchAll();

$totalMasuk = 0;
$totalKeluar = 0;
foreach ($data as $r) {
    if ($r['tipe'] === 'masuk') $totalMasuk += $r['jumlah'];
    else $totalKeluar += $r['jumlah'];
}

function tglIndo($tanggal){
    $bulan = array (
        1 =>   'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'
    );
    $pecahkan = explode('-', $tanggal);
    return $pecahkan[2] . ' ' . $bulan[ (int)$pecahkan[1] ] . ' ' . $pecahkan[0];
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rekap Manajemen Provisi - PDF</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            font-size: 14px;
            color: #333;
            margin: 0;
            padding: 0;
            background: #f5f5f5;
        }
        .toolbar {
            background: #fff;
            padding: 15px 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        .toolbar-title {
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }
        .toolbar-actions {
            display: flex;
            gap: 10px;
        }
        .btn {
            padding: 8px 15px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            text-decoration: none;
            color: #fff;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        .btn-back { background: #6c757d; }
        .btn-back:hover { background: #5a6268; }
        .btn-print { background: #007bff; }
        .btn-print:hover { background: #0056b3; }
        .btn-download { background: #28a745; }
        .btn-download:hover { background: #218838; }
        .page-container {
            background: #fff;
            width: 210mm;
            min-height: 297mm;
            margin: 0 auto 50px auto;
            padding: 20mm;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            box-sizing: border-box;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
            border-bottom: 2px solid #000;
            padding-bottom: 10px;
        }
        .header h1 {
            margin: 0 0 10px 0;
            font-size: 24px;
        }
        .header p {
            margin: 0;
            font-size: 16px;
        }
        .table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 30px;
        }
        .table th, .table td {
            border: 1px solid #000;
            padding: 8px 12px;
            text-align: left;
        }
        .table th {
            background-color: #f2f2f2;
            text-align: center;
            font-weight: bold;
        }
        .text-right {
            text-align: right !important;
        }
        .text-center {
            text-align: center !important;
        }
        .summary {
            width: 50%;
            margin-left: auto;
            border-collapse: collapse;
        }
        .summary th, .summary td {
            border: 1px solid #000;
            padding: 8px 12px;
            text-align: right;
        }
        .summary th {
            background-color: #f2f2f2;
            text-align: left;
        }
        .footer-ttd {
            margin-top: 50px;
            width: 100%;
            display: flex;
            justify-content: flex-end;
        }
        .ttd-box {
            width: 200px;
            text-align: center;
        }
        .ttd-box .date {
            margin-bottom: 70px;
        }
        .ttd-box .name {
            font-weight: bold;
            text-decoration: underline;
        }
        @media print {
            body { background: #fff; padding: 0; }
            .toolbar { display: none; }
            .page-container {
                width: auto;
                min-height: auto;
                margin: 0;
                padding: 0;
                box-shadow: none;
            }
            @page { margin: 1.5cm; }
        }
    </style>
</head>
<body>
    <div class="toolbar">
        <div class="toolbar-title">Preview PDF: Rekap Provisi</div>
        <div class="toolbar-actions">
            <a href="../provisi.php" class="btn btn-back">⬅ Kembali</a>
            <button class="btn btn-print" onclick="window.print()">🖨️ Cetak PDF</button>
            <a href="provisi_export_excel.php<?= $search ? '?search='.urlencode($search) : '' ?>" class="btn btn-download">⬇ Download Excel</a>
        </div>
    </div>
    
    <div class="page-container">
    
    <div class="header">
        <h1>REKAP MANAJEMEN PROVISI</h1>
        <p>Koperasi Management System</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="15%">Tanggal</th>
                <th width="35%">Keterangan</th>
                <th width="15%">Masuk (Rp)</th>
                <th width="15%">Keluar (Rp)</th>
                <th width="15%">Saldo (Rp)</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($data)): ?>
                <tr><td colspan="6" class="text-center">Tidak ada data provisi</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['keterangan']) ?></td>
                        <td class="text-right"><?= ($r['tipe'] === 'masuk') ? number_format($r['jumlah'], 0, ',', '.') : '-' ?></td>
                        <td class="text-right"><?= ($r['tipe'] === 'keluar') ? number_format($r['jumlah'], 0, ',', '.') : '-' ?></td>
                        <td class="text-right"><?= number_format($saldoMap[$r['id']], 0, ',', '.') ?></td>
                    </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>

    <table class="summary">
        <tr>
            <th>Total Masuk</th>
            <td><?= number_format($totalMasuk, 0, ',', '.') ?></td>
        </tr>
        <tr>
            <th>Total Keluar</th>
            <td><?= number_format($totalKeluar, 0, ',', '.') ?></td>
        </tr>
        <tr>
            <th>Saldo Akhir</th>
            <td style="font-weight:bold; <?= ($totalMasuk - $totalKeluar) >= 0 ? "color:green;" : "color:red;" ?>">
                <?= number_format($totalMasuk - $totalKeluar, 0, ',', '.') ?>
            </td>
        </tr>
    </table>

    <div class="footer-ttd">
        <div class="ttd-box">
            <div class="date">Bogor, <?= tglIndo(date('Y-m-d')) ?></div>
            <div class="name">Bendahara</div>
        </div>
    </div>
    </div>
</body>
</html>
