<?php
session_start();
require_once '../config.php';
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'petugas') {
    header("Location: ../login.php");
    exit;
}

$nama_petugas = $_SESSION['nama'] ?? 'Petugas';

// Ambil Bulan dan Tahun saat ini
$bulan_ini = (int)date('m');
$tahun_ini = (int)date('Y');

// Hitung Statistik
// 1. Total Pelanggan Aktif
$total_pelanggan = (int)$pdo->query("SELECT COUNT(*) FROM pelanggan WHERE status='aktif'")->fetchColumn();

// 2. Total Tagihan Bulan Ini (Rp)
$stmt_tagihan = $pdo->prepare("SELECT COALESCE(SUM(total), 0) FROM tagihan WHERE bulan = ? AND tahun = ?");
$stmt_tagihan->execute([$bulan_ini, $tahun_ini]);
$total_tagihan_bulan_ini = (float)$stmt_tagihan->fetchColumn();

// 3. Jumlah Pelanggan Sudah Lunas Bulan Ini
$stmt_lunas = $pdo->prepare("SELECT COUNT(*) FROM tagihan WHERE status = 'lunas' AND bulan = ? AND tahun = ?");
$stmt_lunas->execute([$bulan_ini, $tahun_ini]);
$total_lunas = (int)$stmt_lunas->fetchColumn();

// 4. Jumlah Pelanggan Belum Bayar Bulan Ini
$stmt_belum = $pdo->prepare("SELECT COUNT(*) FROM tagihan WHERE status = 'belum_lunas' AND bulan = ? AND tahun = ?");
$stmt_belum->execute([$bulan_ini, $tahun_ini]);
$total_belum = (int)$stmt_belum->fetchColumn();
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Dashboard Petugas - SPAM</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>
body { background-color: #f4f7fe; font-family: 'Segoe UI', sans-serif; }
.header-section {
    background: linear-gradient(135deg, #3A7CA5 0%, #2C5F7D 100%);
    color: white;
    padding: 30px 20px 40px;
    border-bottom-left-radius: 40px;
    border-bottom-right-radius: 40px;
    margin-bottom: -20px;
}
.header-content h5 { font-weight: 300; opacity: 0.9; margin-bottom: 2px; font-size: 0.95rem; }
.header-content h2 { font-weight: 800; font-size: 1.6rem; margin-bottom: 5px; }
.petugas-tag {
    display: inline-block;
    background: rgba(255,255,255,0.2);
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 0.75rem;
    backdrop-filter: blur(5px);
}
.menu-container { padding: 0 15px; }
.menu-card {
    position: relative;
    border: none;
    border-radius: 22px;
    text-decoration: none;
    color: #333;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 25px 10px;
    background: white;
    box-shadow: 0 8px 15px rgba(0,0,0,0.04);
    transition: all 0.2s ease;
    height: 100%;
}
.menu-card:active { transform: scale(0.95); }
.count-badge {
    position: absolute;
    top: 12px;
    right: 12px;
    background: #f8f9fa;
    color: #3A7CA5;
    font-weight: 800;
    font-size: 0.8rem;
    min-width: 30px;
    height: 25px;
    padding: 0 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    border: 1px solid #eee;
}
.menu-icon { font-size: 2.5rem; margin-bottom: 12px; }
.menu-label { font-size: 1rem; font-weight: 700; color: #444; text-align: center;}
.bg-pelanggan { color: #3A7CA5; }
.bg-catat { color: #2ECC71; }
.bg-bayar { color: #F1C40F; }
.bg-tagihan { color: #E67E22; }
.btn-logout {
    display: inline-flex;
    align-items: center;
    padding: 8px 25px;
    border-radius: 30px;
    font-size: 0.85rem;
    font-weight: 700;
    margin: 25px 0 10px;
    border: 2px solid #e74c3c;
    color: #e74c3c;
    text-decoration: none;
}
.app-footer { font-size: 0.65rem; color: #aaa; letter-spacing: 0.5px; padding-bottom: 20px; }

/* Stats Cards */
.stats-row { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 20px; }
.stat-card {
    background: white;
    border-radius: 16px;
    padding: 15px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.03);
    text-align: center;
}
.stat-value { font-size: 1.2rem; font-weight: 800; color: #3A7CA5; line-height: 1.2; word-break: break-word;}
.stat-label { font-size: 0.75rem; color: #7a8190; margin-top: 4px; }
</style>
</head>
<body>

<div class="header-section">
    <div class="header-content">
        <h5>Selamat bertugas,</h5>
        <h2><?= htmlspecialchars($nama_petugas) ?></h2>
        <div class="petugas-tag">
            <i class="fa fa-user-check me-1"></i> Petugas Penarik Iuran
        </div>
    </div>
</div>

<div class="menu-container">
    <!-- Statistik Bulan Ini -->
    <div class="stats-row">
        <div class="stat-card">
            <div class="stat-value"><?= $total_pelanggan ?></div>
            <div class="stat-label">Total Pelanggan</div>
        </div>
        <div class="stat-card">
            <div class="stat-value"><?= format_rupiah($total_tagihan_bulan_ini) ?></div>
            <div class="stat-label">Tagihan Bulan Ini</div>
        </div>
        <div class="stat-card">
            <div class="stat-value" style="color:#2ECC71;"><?= $total_lunas ?></div>
            <div class="stat-label">Sudah Lunas</div>
        </div>
        <div class="stat-card">
            <div class="stat-value" style="color:#E67E22;"><?= $total_belum ?></div>
            <div class="stat-label">Belum Bayar</div>
        </div>
    </div>

    <!-- Menu Utama -->
    <div class="row g-3">
        <div class="col-6">
            <a href="catat_meteran.php" class="menu-card">
                <i class="fa-solid fa-gauge-high menu-icon bg-catat"></i>
                <span class="menu-label">Catat Meteran</span>
            </a>
        </div>
        <div class="col-6">
            <a href="terima_pembayaran.php" class="menu-card">
                <i class="fa-solid fa-hand-holding-dollar menu-icon bg-bayar"></i>
                <span class="menu-label">Terima Bayar</span>
            </a>
        </div>
        <div class="col-12">
            <a href="daftar_tagihan.php" class="menu-card flex-row py-3">
                <i class="fa-solid fa-file-invoice menu-icon bg-tagihan me-3 mb-0" style="font-size: 2rem;"></i>
                <span class="menu-label">Daftar Tagihan Pelanggan</span>
            </a>
        </div>
    </div>
    
    <div class="text-center">
        <a href="../logout.php" class="btn-logout" onclick="return confirm('Yakin ingin keluar?')">
            <i class="fa-solid fa-arrow-right-from-bracket me-2"></i> KELUAR
        </a>
        <div class="app-footer">SPAM Desa Gemuruh v.1.0</div>
    </div>
</div>

</body>
</html>