<?php
session_start();
error_reporting(0);

// Check if logged in
if (!isset($_SESSION['agent_id'])) {
    header("Location: index.php");
    exit();
}

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

$agent = new Agent();
$agentId = $_SESSION['agent_id'];
$agentData = $agent->getAgentById($agentId);

// Get all transactions
$transactions = $agent->getTransactions($agentId, 100);

// Handle logout
if (isset($_GET['logout'])) {
    session_destroy();
    header("Location: index.php");
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Transaction History - MikhMon</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="agent.css">
</head>
<body>
    <div class="header">
        <h1><i class="fa fa-history"></i> Transaction History</h1>
        <nav>
            <a href="dashboard.php"><i class="fa fa-dashboard"></i> Dashboard</a>
            <a href="?logout=1"><i class="fa fa-sign-out"></i> Logout</a>
        </nav>
    </div>
    
    <div class="container">
        <div class="card">
            <div class="card-header">
                <h2>Transaction History</h2>
                <div>Current Balance: <strong>Rp <?= number_format($agentData['balance'], 0, ',', '.'); ?></strong></div>
            </div>
            
            <?php if (!empty($transactions)): ?>
            <div style="overflow-x: auto;">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Date & Time</th>
                            <th>Type</th>
                            <th>Amount</th>
                            <th>Balance Before</th>
                            <th>Balance After</th>
                            <th>Description</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 else: ?>
            <p>No transactions found.</p>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>