<?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_lengkap'];

// Ambil nama gereja
$q_gereja = $conn->prepare("SELECT nama_gereja FROM gereja WHERE id = ?");
$q_gereja->execute([$gereja_id]);
$nama_gereja = $q_gereja->fetchColumn();

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

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

$saldo = $total_pemasukan - $total_pengeluaran;
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard Bendahara - SIMJEDPW</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>
        .sidebar { background: #1e3a8a; min-height: 100vh; color: white; }
        .sidebar a { color: #cbd5e1; text-decoration: none; display: block; padding: 12px 20px; }
        .sidebar a:hover, .sidebar a.active { background: #1e40af; color: white; }
        .card-summary { border-left: 4px solid; }
        .border-pemasukan { border-left-color: #10b981; }
        .border-pengeluaran { border-left-color: #ef4444; }
        .border-saldo { border-left-color: #3b82f6; }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <!-- Sidebar Khusus Bendahara -->
        <div class="col-md-2 sidebar p-0">
            <div class="p-3 text-center border-bottom">
                <i class="fas fa-church fa-2x"></i>
                <h5 class="mt-2">SIMJEDPW</h5>
                <small>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"><small>KEUANGAN GEREJA</small></div>
            <a href="input_keuangan_bendahara.php"><i class="fas fa-wallet me-2"></i> Input Keuangan</a>
            <a href="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-sm btn-danger 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>Dashboard - <?= htmlspecialchars($nama_gereja) ?></h3>
                <div>
                    <span class="me-3"><?= htmlspecialchars($nama_user) ?></span>
                    <span class="badge bg-success">Bendahara</span>
                </div>
            </div>

            <!-- Ringkasan Bulan Ini -->
            <div class="row mb-4">
                <div class="col-md-4">
                    <div class="card card-summary border-pemasukan shadow-sm">
                        <div class="card-body">
                            <h6 class="text-success">TOTAL PEMASUKAN</h6>
                            <h3>Rp <?= number_format($total_pemasukan,0,',','.') ?></h3>
                            <small>Bulan <?= date('F Y') ?></small>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card card-summary border-pengeluaran shadow-sm">
                        <div class="card-body">
                            <h6 class="text-danger">TOTAL PENGELUARAN</h6>
                            <h3>Rp <?= number_format($total_pengeluaran,0,',','.') ?></h3>
                            <small>Bulan <?= date('F Y') ?></small>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card card-summary border-saldo shadow-sm">
                        <div class="card-body">
                            <h6 class="text-primary">SALDO BULAN INI</h6>
                            <h3>Rp <?= number_format($saldo,0,',','.') ?></h3>
                            <small>Update: <?= date('d/m/Y') ?></small>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Shortcut -->
            <div class="row">
                <div class="col-md-6">
                    <div class="card shadow-sm">
                        <div class="card-header bg-primary text-white">
                            <i class="fas fa-plus-circle me-2"></i> Aksi Cepat
                        </div>
                        <div class="card-body">
                            <a href="input_keuangan_bendahara.php" class="btn btn-success w-100 mb-2">
                                <i class="fas fa-plus me-2"></i> Input Transaksi Baru
                            </a>
                            <a href="laporan_keuangan_bendahara.php?filter=bulan_ini" class="btn btn-info w-100">
                                <i class="fas fa-file-alt me-2"></i> Lihat Laporan Bulan Ini
                            </a>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card shadow-sm">
                        <div class="card-header bg-secondary text-white">
                            <i class="fas fa-info-circle me-2"></i> Informasi Akun
                        </div>
                        <div class="card-body">
                            <table class="table table-sm">
                                <tr><td>Nama</td><td>: <?= htmlspecialchars($nama_user) ?></td></tr>
                                <tr><td>Jabatan</td><td>: Bendahara</td></tr>
                                <tr><td>Gereja</td><td>: <?= htmlspecialchars($nama_gereja) ?></td></tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>