<?php
// No session_start() needed - already started in index.php
// No auth check needed - already checked in index.php

include_once('./include/db_config.php');
include_once('./lib/Agent.class.php');

$agent = new Agent();
$agentId = $_GET['agent_id'] ?? 0;
$agentData = $agentId ? $agent->getAgentById($agentId) : null;
$transactions = $agentId ? $agent->getTransactions($agentId, 100) : [];
$agents = $agent->getAllAgents();
?>

<style>
.trans-container { padding: 20px; }
.card { background: white; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); padding: 25px; margin-bottom: 20px; }
.form-control { width: 100%; padding: 10px; border: 1px solid #e0e0e0; border-radius: 5px; }
table { width: 100%; border-collapse: collapse; }
table th { background: #f8f9fa; padding: 12px; text-align: left; font-weight: 600; border-bottom: 2px solid #e0e0e0; }
table td { padding: 12px; border-bottom: 1px solid #f0f0f0; }
.badge { padding: 4px 12px; border-radius: 12px; font-size: 12px; font-weight: 600; }
.badge-topup { background: #d1fae5; color: #065f46; }
.badge-generate { background: #fee2e2; color: #991b1b; }
</style>

<div class="trans-container">
    <h2><i class="fa fa-history"></i> Transaksi Agent</h2>
    
    <div class="card">
        <form method="GET">
            <input type="hidden" name="hotspot" value="agent-transactions">
            <input type="hidden" name="session" value="<?= $session; ?>">
            <select name="agent_id" class="form-control" onchange="this.form.submit()">
                <option value="">-- Pilih Agent --</option>
                <?php foreach ($agents as $agt): ?>
                <option value="<?= $agt['id']; ?>" <?= $agentId == $agt['id'] ? 'selected' : ''; ?>>
                    <?= $agt['agent_name']; ?> (<?= $agt['agent_code']; ?>)
                </option>
                <?php endforeach; ?>
            </select>
        </form>
    </div>

    <?php if ($agentData && !empty($transactions)): ?>
    <div class="card">
        <h3><?= $agentData['agent_name']; ?> - Saldo: Rp <?= number_format($agentData['balance'], 0, ',', '.'); ?></h3>
        <table>
            <thead>
                <tr>
                    <th>Tanggal</th>
                    <th>Tipe</th>
                    <th>Jumlah</th>
                    <th>Saldo Sebelum</th>
                    <th>Saldo Sesudah</th>
                    <th>Keterangan</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($transactions as $trx): ?>
                <tr>
                    <td><?= date('d M Y H:i', strtotime($trx['created_at'])); ?></td>
                    <td><span class="badge badge-<?= $trx['transaction_type']; ?>"><?= ucfirst($trx['transaction_type']); ?></span></td>
                    <td style="font-weight: bold; color: <?= $trx['transaction_type'] == 'topup' ? '#10b981' : '#ef4444'; ?>">
                        <?= $trx['transaction_type'] == 'topup' ? '+' : '-'; ?>Rp <?= number_format($trx['amount'], 0, ',', '.'); ?>
                    </td>
                    <td>Rp <?= number_format($trx['balance_before'], 0, ',', '.'); ?></td>
                    <td>Rp <?= number_format($trx['balance_after'], 0, ',', '.'); ?></td>
                    <td><?= $trx['description'] ?: ($trx['profile_name'] . ' - ' . $trx['voucher_username']); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>
