<?php
require_once __DIR__ . '/../config/auth.php';
require_once __DIR__ . '/../includes/functions.php';
requirePermission('report_view');

$pdo = getDB();
$suppliers = $pdo->query("SELECT * FROM suppliers WHERE current_balance > 0 ORDER BY current_balance DESC")->fetchAll();
$total_due = array_sum(array_column($suppliers, 'current_balance'));

include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<div class="content-wrapper">
    <div class="container-fluid">
        <h4 class="page-title"><i class="fas fa-truck me-2"></i>Supplier Due Report</h4>
        <div class="row mb-3">
            <div class="col-md-4"><div class="stat-card bg-danger-gradient"><div class="stat-value"><?= formatCurrency($total_due) ?></div><div class="stat-label">Total Payable</div></div></div>
        </div>
        <div class="card">
            <div class="card-body">
                <div class="table-responsive">
                    <table id="supplierDueTable" class="table table-hover datatable">
                        <thead><tr><th>Company</th><th>Contact</th><th>Phone</th><th>Balance Due</th></tr></thead>
                        <tbody>
                            <?php foreach ($suppliers as $s): ?>
                            <tr>
                                <td><?= htmlspecialchars($s['company_name']) ?></td>
                                <td><?= htmlspecialchars($s['contact_person'] ?? '-') ?></td>
                                <td><?= htmlspecialchars($s['phone']) ?></td>
                                <td class="text-danger fw-bold"><?= formatCurrency($s['current_balance']) ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
