<?php
require_once __DIR__ . '/../../includes/auth.php';
requireLogin();

require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../../includes/layout.php';

$stmt = $pdo->prepare("
    SELECT *
    FROM settlement_runs
    WHERE company_id = ?
    ORDER BY created_at DESC
");

$stmt->execute([$_SESSION['company_id']]);
$runs = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="container">

<h2>Settlement History</h2>

<?php if(isset($_GET['saved'])): ?>
    <div style="color:green;font-weight:bold;">
        ✅ Settlement saved successfully
    </div>
<?php endif; ?>

<div class="table-wrap">
<table>
<thead>
<tr>
    <th>ID</th>
    <th>File</th>
    <th>Total Records</th>
    <th>Total Amount</th>
    <th>Date</th>
    <th>Action</th>
</tr>
</thead>

<tbody>

<?php foreach($runs as $run): ?>
<tr>
    <td>#<?= $run['id'] ?></td>
    <td><?= htmlspecialchars($run['file_name'] ?? '') ?></td>
    <td><?= $run['total_records'] ?></td>
    <td><strong>R <?= number_format($run['total_amount'],2) ?></strong></td>
    <td><?= date('Y-m-d H:i', strtotime($run['created_at'])) ?></td>
    <td>
        <a href="view_run.php?id=<?= $run['id'] ?>" class="btn">
            View
        </a>
    </td>
</tr>
<?php endforeach; ?>

</tbody>
</table>
</div>

</div>

<?php require_once __DIR__ . '/../../includes/layout_footer.php'; ?>