<?php
require_once __DIR__ . '/layout.php';
$db = db();

$title = __('mining_management');
ob_start();
$records = $db->query("SELECT r.*, u.username, u.uid FROM staking_records r JOIN users u ON r.user_id = u.id ORDER BY r.created_at DESC")->fetchAll();
?>
<div class="d-flex justify-content-between align-items-center mb-4">
    <div class="d-flex align-items-center gap-3">
        <a href="index.php" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left"></i> <?= __('back') ?></a>
        <h4 class="mb-0"><?= __('mining_management') ?></h4>
    </div>
</div>
<div class="table-container">
    <table class="table table-hover">
        <thead>
            <tr class="small text-muted">
                <th>编号</th>
                <th><?= __('users') ?></th>
                <th><?= __('mining_pool') ?></th>
                <th><?= __('amount') ?></th>
                <th><?= __('total_profit') ?></th>
                <th><?= __('est_apy') ?></th>
                <th><?= __('cycle') ?> / <?= __('last_updated') ?></th>
                <th><?= __('status') ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($records as $r): ?>
            <tr>
                <td><?= $r['id'] ?></td>
                <td><?= htmlspecialchars($r['username']) ?><br><code class="small"><?= $r['uid'] ?></code></td>
                <td><?= htmlspecialchars($r['plan_name']) ?> (<?= $r['symbol'] ?>)</td>
                <td><strong><?= number_format($r['amount'], 4) ?></strong></td>
                <td class="text-success fw-bold">+<?= number_format($r['total_profit'], 6) ?></td>
                <td><?= number_format($r['daily_profit'] * 365, 1) ?>%</td>
                <td>
                    <span class="small"><?= $r['period'] ?> <?= __('day') ?></span><br>
                    <small class="text-muted"><?= $r['last_settle_time'] ?: $r['created_at'] ?></small>
                </td>
                <td>
                    <?php if ($r['status'] === 'running'): ?>
                        <span class="badge bg-primary"><?= __('running') ?></span>
                    <?php else: ?>
                        <span class="badge bg-secondary"><?= __('completed') ?></span>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php
$content = ob_get_clean();
renderAdminPage($content, $title);
?>
