<?php
require_once '../config/db.php';
require_once '../includes/auth.php';
require_once '../includes/functions.php';
requireAdminPage();

$page_title = 'Reports';
$active_nav = 'reports';

$month = intval($_GET['month'] ?? date('n'));
$year  = intval($_GET['year']  ?? date('Y'));

$monthly_collections = getMonthlyCollections($conn, $month, $year);
$daily_collections   = getDailyCollections($conn);
$active_loans        = getActiveLoans($conn);
$overdue_loans       = getOverdueLoans($conn);
$paid_loans          = getPaidLoans($conn);
$total_borrowers     = getTotalBorrowers($conn);

// Per-day breakdown for current month
$daily_breakdown = fetchAll($conn,
    "SELECT payment_date, SUM(amount) as total, COUNT(*) as count
     FROM payments
     WHERE MONTH(payment_date) = ? AND YEAR(payment_date) = ?
     GROUP BY payment_date ORDER BY payment_date DESC",
    [$month, $year], "ii"
);

$months = ['January','February','March','April','May','June',
           'July','August','September','October','November','December'];

include 'header.php';
?>

<!-- Month picker -->
<div class="card mb-3">
    <div class="card-body">
        <form method="GET" style="display:flex;gap:8px;align-items:flex-end;">
            <div style="flex:1;">
                <label class="form-label" style="font-size:12px;">Month</label>
                <select class="form-select" name="month">
                    <?php foreach ($months as $i => $m): ?>
                        <option value="<?php echo $i+1; ?>" <?php echo $month === $i+1 ? 'selected':''; ?>><?php echo $m; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div style="flex:1;">
                <label class="form-label" style="font-size:12px;">Year</label>
                <select class="form-select" name="year">
                    <?php for ($y = date('Y'); $y >= date('Y')-5; $y--): ?>
                        <option value="<?php echo $y; ?>" <?php echo $year === $y ? 'selected':''; ?>><?php echo $y; ?></option>
                    <?php endfor; ?>
                </select>
            </div>
            <button type="submit" class="btn btn-primary btn-sm" style="height:44px;padding:0 14px;">Go</button>
        </form>
    </div>
</div>

<!-- Summary stats -->
<div class="section-title"><?php echo $months[$month-1] . ' ' . $year; ?> Summary</div>
<div class="stat-grid mb-3">
    <div class="stat-card green">
        <div class="stat-label">Collections</div>
        <div class="stat-num" style="font-size:20px;">₱<?php echo number_format($monthly_collections, 2); ?></div>
        <div class="stat-sub">This month</div>
    </div>
    <div class="stat-card blue">
        <div class="stat-label">Active Loans</div>
        <div class="stat-num"><?php echo $active_loans; ?></div>
        <div class="stat-sub">Ongoing</div>
    </div>
    <div class="stat-card red">
        <div class="stat-label">Overdue</div>
        <div class="stat-num"><?php echo $overdue_loans; ?></div>
        <div class="stat-sub">Needs action</div>
    </div>
    <div class="stat-card orange">
        <div class="stat-label">Today</div>
        <div class="stat-num" style="font-size:20px;">₱<?php echo number_format($daily_collections, 2); ?></div>
        <div class="stat-sub">Collected</div>
    </div>
</div>

<!-- Per-day breakdown -->
<div class="section-title">Daily Breakdown</div>
<div class="card">
    <?php if (!empty($daily_breakdown)): ?>
        <?php foreach ($daily_breakdown as $row): ?>
            <div class="list-row">
                <div class="row-info">
                    <div class="row-name"><?php echo date('l', strtotime($row['payment_date'])); ?></div>
                    <div class="row-sub"><?php echo date('M d, Y', strtotime($row['payment_date'])); ?> · <?php echo $row['count']; ?> payment<?php echo $row['count'] > 1 ? 's':''; ?></div>
                </div>
                <div class="row-right">
                    <div class="row-amount" style="color:var(--success);">₱<?php echo number_format($row['total'], 2); ?></div>
                </div>
            </div>
        <?php endforeach; ?>
    <?php else: ?>
        <div class="empty-state"><i class="fas fa-chart-bar"></i><p>No payments this month</p></div>
    <?php endif; ?>
</div>

<?php include 'footer.php'; ?>
