<?php
// bendahara/donasi/index.php
$pageTitle = 'Verifikasi Donasi';
require_once '../../includes/config.php';
require_once '../../includes/database.php';
require_once '../../includes/functions.php';
require_once '../../includes/auth.php';

// Mulai session jika belum dimulai
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// Cek apakah user sudah login dan role-nya bendahara
if (!isset($_SESSION['user_id']) || !isset($_SESSION['role']) || $_SESSION['role'] != 'bendahara') {
    // Redirect ke halaman login jika belum login atau bukan bendahara
    setFlash('login_required', 'Anda harus login sebagai bendahara untuk mengakses halaman ini', 'danger');
    header('Location: ' . SITE_URL . '/auth/login.php');
    exit;
}

// Koneksi database
$db = dbConnect();

// Proses filter dan search
$status_filter = isset($_GET['status']) ? $_GET['status'] : '';
$search = isset($_GET['search']) ? $_GET['search'] : '';
$date_start = isset($_GET['date_start']) ? $_GET['date_start'] : '';
$date_end = isset($_GET['date_end']) ? $_GET['date_end'] : '';

// Query dasar
$query_base = "SELECT d.*, k.judul as kampanye_judul, u.nama_lengkap as donatur_nama 
                FROM donasi d 
                JOIN kampanye k ON d.kampanye_id = k.id 
                JOIN users u ON d.user_id = u.id 
                WHERE 1=1";
$params = [];

// Tambahkan filter
if (!empty($status_filter)) {
    $query_base .= " AND d.status = :status";
    $params[':status'] = $status_filter;
}

if (!empty($search)) {
    $query_base .= " AND (k.judul LIKE :search OR u.nama_lengkap LIKE :search OR u.email LIKE :search)";
    $params[':search'] = "%$search%";
}

if (!empty($date_start)) {
    $query_base .= " AND d.created_at >= :date_start";
    $params[':date_start'] = $date_start . ' 00:00:00';
}

if (!empty($date_end)) {
    $query_base .= " AND d.created_at <= :date_end";
    $params[':date_end'] = $date_end . ' 23:59:59';
}

// Hitung total untuk pagination
$query_count = "SELECT COUNT(*) as total FROM ($query_base) as t";
$db->query($query_count);
foreach ($params as $param => $value) {
    $db->bind($param, $value);
}
$total_rows = $db->single()->total;

// Pagination
$per_page = 10;
$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$total_pages = ceil($total_rows / $per_page);
$offset = ($current_page - 1) * $per_page;

// Query final dengan order dan limit
$query_final = $query_base . " ORDER BY d.created_at DESC LIMIT :offset, :per_page";
$db->query($query_final);
foreach ($params as $param => $value) {
    $db->bind($param, $value);
}
$db->bind(':offset', $offset, PDO::PARAM_INT);
$db->bind(':per_page', $per_page, PDO::PARAM_INT);
$donasi_list = $db->resultSet();

// Include header
require_once '../../includes/header.php';
?>

<div class="container-fluid">
    <div class="row">
        <!-- Sidebar -->
        <nav id="sidebar" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
            <div class="position-sticky pt-3">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/bendahara/dashboard.php">
                            <i class="fas fa-tachometer-alt me-2"></i>
                            Dashboard
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="<?= SITE_URL ?>/bendahara/donasi/index.php">
                            <i class="fas fa-donate me-2"></i>
                            Verifikasi Donasi
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/bendahara/laporan/index.php">
                            <i class="fas fa-chart-bar me-2"></i>
                            Laporan Keuangan
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/bendahara/kampanye/index.php">
                            <i class="fas fa-hand-holding-heart me-2"></i>
                            Data Kampanye
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/bendahara/profil/index.php">
                            <i class="fas fa-user-circle me-2"></i>
                            Profil Saya
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/auth/logout.php">
                            <i class="fas fa-sign-out-alt me-2"></i>
                            Logout
                        </a>
                    </li>
                </ul>
                
                <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
                    <span>Pintasan</span>
                </h6>
                <ul class="nav flex-column mb-2">
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>" target="_blank">
                            <i class="fas fa-external-link-alt me-2"></i>
                            Lihat Website
                        </a>
                    </li>
                </ul>
            </div>
        </nav>

        <!-- Content Area -->
        <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
            <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                <h1 class="h2">Verifikasi Donasi</h1>
                <div class="btn-toolbar mb-2 mb-md-0">
                    <div class="btn-group me-2">
                        <a href="<?= SITE_URL ?>/bendahara/laporan/generate.php" class="btn btn-sm btn-outline-secondary">
                            <i class="fas fa-file-export me-1"></i> Export Laporan
                        </a>
                    </div>
                </div>
            </div>
            
            <?php flash(); ?>
            
            <!-- Filter & Search -->
            <div class="card mb-4">
                <div class="card-body">
                    <form action="" method="get" class="row g-3">
                        <div class="col-md-3">
                            <label for="status" class="form-label">Status</label>
                            <select class="form-select" id="status" name="status">
                                <option value="">Semua Status</option>
                                <option value="menunggu" <?= $status_filter == 'menunggu' ? 'selected' : '' ?>>Menunggu</option>
                                <option value="diverifikasi" <?= $status_filter == 'diverifikasi' ? 'selected' : '' ?>>Diverifikasi</option>
                                <option value="ditolak" <?= $status_filter == 'ditolak' ? 'selected' : '' ?>>Ditolak</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label for="date_start" class="form-label">Tanggal Mulai</label>
                            <input type="date" class="form-control" id="date_start" name="date_start" value="<?= $date_start ?>">
                        </div>
                        <div class="col-md-3">
                            <label for="date_end" class="form-label">Tanggal Akhir</label>
                            <input type="date" class="form-control" id="date_end" name="date_end" value="<?= $date_end ?>">
                        </div>
                        
                        <div class="col-12">
                            <button type="submit" class="btn btn-primary">Filter</button>
                            <a href="<?= SITE_URL ?>/bendahara/donasi/index.php" class="btn btn-secondary">Reset</a>
                        </div>
                    </form>
                </div>
            </div>
            
            <!-- Daftar Donasi -->
            <div class="card">
                <div class="card-body">
                    <?php if (empty($donasi_list)): ?>
                        <div class="alert alert-info">
                            Tidak ada data donasi yang ditemukan.
                        </div>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-hover align-middle">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Donatur</th>
                                        <th>Kampanye</th>
                                        <th>Jumlah</th>
                                        <th>Tanggal</th>
                                        <th>Status</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $no = $offset + 1; ?>
                                    <?php foreach ($donasi_list as $donasi): ?>
                                        <tr>
                                            <td><?= $no++ ?></td>
                                            <td>
                                                <?php if ($donasi->is_anonim): ?>
                                                    <span class="text-muted">Anonim</span>
                                                <?php else: ?>
                                                    <?= $donasi->donatur_nama ?>
                                                <?php endif; ?>
                                            </td>
                                            <td><?= $donasi->kampanye_judul ?></td>
                                            <td><?= rupiah($donasi->jumlah) ?></td>
                                            <td><?= date('d/m/Y H:i', strtotime($donasi->created_at)) ?></td>
                                            <td>
                                                <?php if ($donasi->status == 'menunggu'): ?>
                                                    <span class="badge bg-warning text-dark">Menunggu</span>
                                                <?php elseif ($donasi->status == 'diverifikasi'): ?>
                                                    <span class="badge bg-success">Diverifikasi</span>
                                                <?php else: ?>
                                                    <span class="badge bg-danger">Ditolak</span>
                                                <?php endif; ?>
                                            </td>
                                            <td>
                                                <a href="<?= SITE_URL ?>/bendahara/donasi/detail.php?id=<?= $donasi->id ?>" class="btn btn-sm btn-info">
                                                    <i class="fas fa-eye"></i> Detail
                                                </a>
                                                
                                                <?php if ($donasi->status == 'menunggu'): ?>
                                                    <div class="btn-group">
                                                        <button type="button" class="btn btn-sm btn-success" onclick="verifikasiDonasi(<?= $donasi->id ?>)">
                                                            <i class="fas fa-check"></i> Verifikasi
                                                        </button>
                                                        <button type="button" class="btn btn-sm btn-danger" onclick="tolakDonasi(<?= $donasi->id ?>)">
                                                            <i class="fas fa-times"></i> Tolak
                                                        </button>
                                                    </div>
                                                <?php endif; ?>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                        
                        <!-- Pagination -->
                        <?php if ($total_pages > 1): ?>
                            <nav aria-label="Page navigation" class="mt-4">
                                <ul class="pagination justify-content-center">
                                    <?php if ($current_page > 1): ?>
                                        <li class="page-item">
                                            <a class="page-link" href="?page=<?= $current_page - 1 ?>&status=<?= $status_filter ?>&search=<?= $search ?>&date_start=<?= $date_start ?>&date_end=<?= $date_end ?>">
                                                &laquo; Sebelumnya
                                            </a>
                                        </li>
                                    <?php else: ?>
                                        <li class="page-item disabled">
                                            <span class="page-link">&laquo; Sebelumnya</span>
                                        </li>
                                    <?php endif; ?>
                                    
                                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                                        <li class="page-item <?= $i == $current_page ? 'active' : '' ?>">
                                            <a class="page-link" href="?page=<?= $i ?>&status=<?= $status_filter ?>&search=<?= $search ?>&date_start=<?= $date_start ?>&date_end=<?= $date_end ?>">
                                                <?= $i ?>
                                            </a>
                                        </li>
                                    <?php endfor; ?>
                                    
                                    <?php if ($current_page < $total_pages): ?>
                                        <li class="page-item">
                                            <a class="page-link" href="?page=<?= $current_page + 1 ?>&status=<?= $status_filter ?>&search=<?= $search ?>&date_start=<?= $date_start ?>&date_end=<?= $date_end ?>">
                                                Selanjutnya &raquo;
                                            </a>
                                        </li>
                                    <?php else: ?>
                                        <li class="page-item disabled">
                                            <span class="page-link">Selanjutnya &raquo;</span>
                                        </li>
                                    <?php endif; ?>
                                </ul>
                            </nav>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
            </div>
        </main>
    </div>
</div>

<!-- Modal Verifikasi -->
<div class="modal fade" id="verifikasiModal" tabindex="-1" aria-labelledby="verifikasiModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-success text-white">
                <h5 class="modal-title" id="verifikasiModalLabel">Verifikasi Donasi</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="<?= SITE_URL ?>/bendahara/donasi/proses.php" method="post">
                <div class="modal-body">
                    <p>Anda yakin ingin memverifikasi donasi ini?</p>
                    <p>Pastikan dana sudah masuk ke rekening yayasan.</p>
                    
                    <div class="mb-3">
                        <label for="keterangan" class="form-label">Keterangan (opsional)</label>
                        <textarea class="form-control" id="keterangan" name="keterangan" rows="3"></textarea>
                    </div>
                    
                    <input type="hidden" name="donasi_id" id="verifikasi_donasi_id">
                    <input type="hidden" name="action" value="verifikasi">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-success">Verifikasi Donasi</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Modal Tolak -->
<div class="modal fade" id="tolakModal" tabindex="-1" aria-labelledby="tolakModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title" id="tolakModalLabel">Tolak Donasi</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="<?= SITE_URL ?>/bendahara/donasi/proses.php" method="post">
                <div class="modal-body">
                    <p>Anda yakin ingin menolak donasi ini?</p>
                    
                    <div class="mb-3">
                        <label for="alasan_tolak" class="form-label">Alasan Penolakan <span class="text-danger">*</span></label>
                        <textarea class="form-control" id="alasan_tolak" name="keterangan" rows="3" required></textarea>
                    </div>
                    
                    <input type="hidden" name="donasi_id" id="tolak_donasi_id">
                    <input type="hidden" name="action" value="tolak">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-danger">Tolak Donasi</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    // Fungsi untuk verifikasi donasi
    function verifikasiDonasi(id) {
        document.getElementById('verifikasi_donasi_id').value = id;
        var verifikasiModal = new bootstrap.Modal(document.getElementById('verifikasiModal'));
        verifikasiModal.show();
    }
    
    // Fungsi untuk tolak donasi
    function tolakDonasi(id) {
        document.getElementById('tolak_donasi_id').value = id;
        var tolakModal = new bootstrap.Modal(document.getElementById('tolakModal'));
        tolakModal.show();
    }
</script>

<?php require_once '../../includes/footer.php'; ?>