<?php
session_start();
include 'config/database.php';

// Cek login + role
if(!isset($_SESSION['user_id']) || $_SESSION['role'] != 'bendahara'){
    header("Location: login.php");
    exit;
}

$db = new Database();
$conn = $db->getConnection();

$gereja_id = $_SESSION['gereja_id'];
$nama_user = $_SESSION['nama'];
$nama_gereja = $_SESSION['nama_gereja'];
$foto_user = $_SESSION['foto'] ?: 'default.png';

// Path foto + cek file ada atau tidak
$path_foto = 'assets/img/jemaat/' . $foto_user;
if(!file_exists($path_foto)){
    $path_foto = 'assets/img/jemaat/default.png';
}

// Hitung ringkasan keuangan bulan ini
$bulan_ini = date('Y-m');
$q_pemasukan = $conn->prepare("SELECT COALESCE(SUM(jumlah),0) FROM keuangan WHERE gereja_id = ? AND tipe='Pemasukan' AND DATE_FORMAT(tanggal, '%Y-%m') = ?");
$q_pemasukan->execute([$gereja_id, $bulan_ini]);
$total_pemasukan = $q_pemasukan->fetchColumn();

$q_pengeluaran = $conn->prepare("SELECT COALESCE(SUM(jumlah),0) FROM keuangan WHERE gereja_id = ? AND tipe='Pengeluaran' AND DATE_FORMAT(tanggal, '%Y-%m') = ?");
$q_pengeluaran->execute([$gereja_id, $bulan_ini]);
$total_pengeluaran = $q_pengeluaran->fetchColumn();

$saldo = $total_pemasukan - $total_pengeluaran;

// Hitung total transaksi bulan ini
$q_transaksi = $conn->prepare("SELECT COUNT(*) FROM keuangan WHERE gereja_id = ? AND DATE_FORMAT(tanggal, '%Y-%m') = ?");
$q_transaksi->execute([$gereja_id, $bulan_ini]);
$total_transaksi = $q_transaksi->fetchColumn();

// Ambil 10 transaksi terakhir
$q_list = $conn->prepare("SELECT * FROM keuangan WHERE gereja_id = ? ORDER BY tanggal DESC, id DESC LIMIT 10");
$q_list->execute([$gereja_id]);
$transaksi_terbaru = $q_list->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard Bendahara - SIAMAN</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --hijau-muda: #d1fae5;
            --hijau-border: #10b981;
            --ungu-muda: #ede9fe;
            --ungu-border: #8b5cf6;
            --kuning-muda: #fef3c7;
            --kuning-border: #f59e0b;
            --biru-muda: #dbeafe;
            --biru-border: #3b82f6;
        }
        body { background-color: #f8fafc; }
        .sidebar { 
            background: linear-gradient(180deg, #1e3a8a 0%, #1e40af 100%); 
            min-height: 100vh; 
            color: white; 
        }
        .sidebar a { 
            color: #cbd5e1; 
            text-decoration: none; 
            display: block; 
            padding: 12px 20px; 
            transition: all 0.3s;
        }
        .sidebar a:hover, .sidebar a.active { 
            background: rgba(255,255,255,0.1); 
            color: white; 
            border-left: 3px solid #fff;
        }
        .card-stat { 
            border: none; 
            border-radius: 12px; 
            box-shadow: 0 1px 3px rgba(0,0,0,0.05);
            transition: transform 0.2s;
        }
        .card-stat:hover { transform: translateY(-3px); }
        .card-pemasukan { background: var(--hijau-muda); border-left: 4px solid var(--hijau-border); }
        .card-pengeluaran { background: var(--ungu-muda); border-left: 4px solid var(--ungu-border); }
        .card-saldo { background: var(--kuning-muda); border-left: 4px solid var(--kuning-border); }
        .card-transaksi { background: var(--biru-muda); border-left: 4px solid var(--biru-border); }
        .table-hover tbody tr:hover { background-color: #f1f5f9; }
        .badge-pemasukan { background: var(--hijau-border); }
        .badge-pengeluaran { background: var(--ungu-border); }
        .img-profile { width: 40px; height: 40px; object-fit: cover; }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <!-- Sidebar -->
        <div class="col-md-2 sidebar p-0">
            <div class="p-3 text-center border-bottom border-light border-opacity-25">
                <i class="fas fa-church fa-2x mb-2"></i>
                <h5 class="mb-0">SIAMAN</h5>
                <small class="text-light text-opacity-75">BENDAHARA</small>
            </div>
            <a href="dashboard_bendahara.php" class="active"><i class="fas fa-tachometer-alt me-2"></i> Dashboard</a>
            <div class="px-3 mt-3 mb-2 text-light text-opacity-50"><small>KEUANGAN GEREJA</small></div>
            <a href="pages/input_keuangan_bendahara.php"><i class="fas fa-wallet me-2"></i> Input Keuangan</a>
            <a href="pages/laporan_keuangan_bendahara.php"><i class="fas fa-file-invoice-dollar me-2"></i> Laporan Keuangan</a>
            <div class="px-3 mt-4">
                <a href="logout.php" class="btn btn-danger btn-sm w-100"><i class="fas fa-sign-out-alt me-2"></i> Logout</a>
            </div>
        </div>

        <!-- Konten -->
        <div class="col-md-10 p-4">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h3 class="fw-light">Dashboard - <?= htmlspecialchars($nama_gereja) ?></h3>
                <div class="d-flex align-items-center">
                    <img src="<?= htmlspecialchars($path_foto) ?>" 
                         onerror="this.src='assets/img/jemaat/default.png'"
                         class="img-profile rounded-circle me-2" 
                         alt="Foto"
                         style="border: 2px solid #0d6efd;">
                    <div>
                        <span class="me-2 text-dark fw-bold"><?= htmlspecialchars($nama_user) ?></span>
                        <span class="badge bg-primary">Bendahara</span>
                    </div>
                </div>
            </div>

            <!-- 4 Card Statistik -->
            <div class="row mb-4">
                <div class="col-xl-3 col-md-6 mb-3">
                    <div class="card card-stat card-pemasukan">
                        <div class="card-body">
                            <div class="d-flex justify-content-between">
                                <div>
                                    <h6 class="text-success mb-1">TOTAL PEMASUKAN</h6>
                                    <h3 class="mb-0">Rp <?= number_format($total_pemasukan,0,',','.') ?></h3>
                                    <small class="text-muted">Bulan <?= date('F Y') ?></small>
                                </div>
                                <div class="align-self-center">
                                    <i class="fas fa-arrow-down fa-2x text-success opacity-50"></i>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-xl-3 col-md-6 mb-3">
                    <div class="card card-stat card-pengeluaran">
                        <div class="card-body">
                            <div class="d-flex justify-content-between">
                                <div>
                                    <h6 style="color: var(--ungu-border)" class="mb-1">TOTAL PENGELUARAN</h6>
                                    <h3 class="mb-0">Rp <?= number_format($total_pengeluaran,0,',','.') ?></h3>
                                    <small class="text-muted">Bulan <?= date('F Y') ?></small>
                                </div>
                                <div class="align-self-center">
                                    <i class="fas fa-arrow-up fa-2x opacity-50" style="color: var(--ungu-border)"></i>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-xl-3 col-md-6 mb-3">
                    <div class="card card-stat card-saldo">
                        <div class="card-body">
                            <div class="d-flex justify-content-between">
                                <div>
                                    <h6 style="color: var(--kuning-border)" class="mb-1">SALDO BULAN INI</h6>
                                    <h3 class="mb-0">Rp <?= number_format($saldo,0,',','.') ?></h3>
                                    <small class="text-muted">Update: <?= date('d/m/Y') ?></small>
                                </div>
                                <div class="align-self-center">
                                    <i class="fas fa-wallet fa-2x opacity-50" style="color: var(--kuning-border)"></i>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-xl-3 col-md-6 mb-3">
                    <div class="card card-stat card-transaksi">
                        <div class="card-body">
                            <div class="d-flex justify-content-between">
                                <div>
                                    <h6 style="color: var(--biru-border)" class="mb-1">TOTAL TRANSAKSI</h6>
                                    <h3 class="mb-0"><?= $total_transaksi ?></h3>
                                    <small class="text-muted">Bulan <?= date('F Y') ?></small>
                                </div>
                                <div class="align-self-center">
                                    <i class="fas fa-receipt fa-2x opacity-50" style="color: var(--biru-border)"></i>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Tabel Transaksi Terbaru -->
            <div class="row">
                <div class="col-12">
                    <div class="card shadow-sm border-0">
                        <div class="card-header bg-white py-3">
                            <div class="d-flex justify-content-between align-items-center">
                                <h5 class="mb-0"><i class="fas fa-history me-2 text-primary"></i>10 Transaksi Terakhir</h5>
                                <a href="pages/laporan_keuangan_bendahara.php" class="btn btn-sm btn-primary">
                                    Lihat Semua <i class="fas fa-arrow-right ms-1"></i>
                                </a>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-hover align-middle">
                                    <thead class="table-light">
                                        <tr>
                                            <th>Tanggal</th>
                                            <th>Tipe</th>
                                            <th>Jenis</th>
                                            <th>Keterangan</th>
                                            <th class="text-end">Jumlah</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if(empty($transaksi_terbaru)): ?>
                                            <tr><td colspan="5" class="text-center text-muted py-4">Belum ada transaksi bulan ini</td></tr>
                                        <?php else: ?>
                                            <?php foreach($transaksi_terbaru as $trx): ?>
                                            <tr>
                                                <td><?= date('d/m/Y', strtotime($trx['tanggal'])) ?></td>
                                                <td>
                                                    <?php if($trx['tipe'] == 'Pemasukan'): ?>
                                                        <span class="badge badge-pemasukan">Pemasukan</span>
                                                    <?php else: ?>
                                                        <span class="badge badge-pengeluaran">Pengeluaran</span>
                                                    <?php endif; ?>
                                                </td>
                                                <td><?= htmlspecialchars($trx['jenis']) ?></td>
                                                <td><?= htmlspecialchars($trx['keterangan']) ?></td>
                                                <td class="text-end fw-bold <?= $trx['tipe'] == 'Pemasukan' ? 'text-success' : 'text-danger' ?>">
                                                    Rp <?= number_format($trx['jumlah'],0,',','.') ?>
                                                </td>
                                            </tr>
                                            <?php endforeach; ?>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>