<?php
include '../../includes/header.php';
checkLogin();

$db = getDB()->getConnection();

// الحصول على الشهر والسنة الحاليين
$current_month = date('n');
$current_year  = date('Y');

// جلب المشتركين الذين دفعوا هذا الشهر
$paid_sql = "
    SELECT s.id, s.name, s.phone, s.generator_id, p.amount_paid, p.payment_date
    FROM subscribers s
    JOIN payments p ON s.id = p.subscriber_id
    WHERE p.billing_month = ? AND p.billing_year = ?
    ORDER BY s.name ASC
";
$paid_subscribers = $db->prepare($paid_sql);
$paid_subscribers->execute([$current_month, $current_year]);
$paid_list = $paid_subscribers->fetchAll();

// جلب المشتركين الذين لم يدفعوا
$unpaid_sql = "
    SELECT s.id, s.name, s.phone, s.generator_id
    FROM subscribers s
    WHERE s.id NOT IN (
        SELECT subscriber_id FROM payments
        WHERE billing_month = ? AND billing_year = ?
    )
    ORDER BY s.name ASC
";
$unpaid_subscribers = $db->prepare($unpaid_sql);
$unpaid_subscribers->execute([$current_month, $current_year]);
$unpaid_list = $unpaid_subscribers->fetchAll();
?>

<div class="container mt-4">

    <h2 class="mb-4">تقارير دفعات شهر <?= $current_month ?> / <?= $current_year ?></h2>

    <div class="card mb-4">
        <div class="card-header bg-success text-white">
            المشتركين الذين دفعوا هذا الشهر
        </div>
        <div class="card-body">
            <?php if (count($paid_list) > 0): ?>
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>الاسم</th>
                            <th>الهاتف</th>
                            <th>المولدة</th>
                            <th>المبلغ</th>
                            <th>تاريخ الدفع</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($paid_list as $p): ?>
                        <tr>
                            <td><?= htmlspecialchars($p['name']) ?></td>
                            <td><?= htmlspecialchars($p['phone']) ?></td>
                            <td><?= htmlspecialchars($p['generator_id']) ?></td>
                            <td><?= number_format($p['amount_paid'], 3) ?></td>
                            <td><?= htmlspecialchars($p['payment_date']) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <p class="text-danger">لا يوجد أي مشترك دفع هذا الشهر.</p>
            <?php endif; ?>
        </div>
    </div>

    <div class="card mb-4">
        <div class="card-header bg-danger text-white">
            المشتركين الذين لم يدفعوا هذا الشهر
        </div>
        <div class="card-body">
            <?php if (count($unpaid_list) > 0): ?>
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>الاسم</th>
                            <th>الهاتف</th>
                            <th>المولدة</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($unpaid_list as $u): ?>
                        <tr>
                            <td><?= htmlspecialchars($u['name']) ?></td>
                            <td><?= htmlspecialchars($u['phone']) ?></td>
                            <td><?= htmlspecialchars($u['generator_id']) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <p class="text-success">كل المشتركين دفعوا هذا الشهر 🎉</p>
            <?php endif; ?>
        </div>
    </div>

</div>

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