<?php
/*
 * Admin Panel - Topup Saldo Agent
 */

// 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;

$error = '';
$success = '';

if (isset($_POST['topup'])) {
    $agentId = $_POST['agent_id'];
    $amount = floatval($_POST['amount']);
    $description = trim($_POST['description']);
    
    if ($amount <= 0) {
        $error = 'Jumlah topup harus lebih dari 0!';
    } else {
        $result = $agent->topupBalance($agentId, $amount, $description, $_SESSION['mikhmon']);
        
        if ($result['success']) {
            $success = 'Topup berhasil! Saldo sebelum: Rp ' . number_format($result['balance_before'], 0, ',', '.') . 
                      ', Saldo sekarang: Rp ' . number_format($result['balance_after'], 0, ',', '.');
            $agentData = $agent->getAgentById($agentId);
        } else {
            $error = $result['message'];
        }
    }
}

$agents = $agent->getAllAgents('active');
?>

<style>
.topup-container { max-width: 800px; margin: 0 auto; padding: 20px; }
.card { background: white; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); padding: 25px; margin-bottom: 20px; }
.balance-card { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; text-align: center; border-radius: 15px; margin-bottom: 20px; }
.balance-card h3 { margin: 0 0 10px 0; font-size: 16px; opacity: 0.9; }
.balance-card .amount { font-size: 36px; font-weight: bold; margin: 10px 0; }
.form-group { margin-bottom: 20px; }
.form-group label { display: block; margin-bottom: 8px; color: #333; font-weight: 500; }
.form-control { width: 100%; padding: 12px 15px; border: 1px solid #e0e0e0; border-radius: 5px; font-size: 14px; }
.btn { padding: 12px 30px; border: none; border-radius: 5px; cursor: pointer; font-weight: 500; font-size: 14px; }
.btn-primary { background: #667eea; color: white; width: 100%; }
.btn-secondary { background: #6c757d; color: white; width: 100%; text-decoration: none; display: block; text-align: center; }
.alert { padding: 15px 20px; border-radius: 5px; margin-bottom: 20px; }
.alert-danger { background: #fee; color: #c33; }
.alert-success { background: #efe; color: #3c3; }
.quick-amounts { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-top: 10px; }
.quick-amount { padding: 10px; background: #f8f9fa; border: 2px solid #e0e0e0; border-radius: 5px; text-align: center; cursor: pointer; font-weight: 600; }
.quick-amount:hover { background: #667eea; color: white; border-color: #667eea; }
</style>

<div class="topup-container">
    <h2><i class="fa fa-money"></i> Topup Saldo Agent</h2>
    <p style="color: #666; margin-bottom: 20px;">Tambahkan saldo untuk agent</p>

    <?php if ($error): ?>
    <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?= $error; ?></div>
    <?php endif; ?>

    <?php if ($success): ?>
    <div class="alert alert-success"><i class="fa fa-check-circle"></i> <?= $success; ?></div>
    <?php endif; ?>

    <?php if ($agentData): ?>
    <div class="balance-card">
        <h3><?= $agentData['agent_name']; ?> (<?= $agentData['agent_code']; ?>)</h3>
        <div class="amount">Rp <?= number_format($agentData['balance'], 0, ',', '.'); ?></div>
        <p style="opacity: 0.9;">Saldo Saat Ini</p>
    </div>
    <?php endif; ?>

    <div class="card">
        <?php if ($agentData): ?>
        <form method="POST" id="topupForm">
            <input type="hidden" name="agent_id" value="<?= $agentId; ?>">
            
            <div class="form-group">
                <label>Agent</label>
                <select class="form-control" onchange="window.location.href='./?hotspot=agent-topup&agent_id=' + this.value + '&session=<?= $session; ?>'">
                    <option value="">-- Pilih Agent --</option>
                    <?php foreach ($agents as $agt): ?>
                    <option value="<?= $agt['id']; ?>" <?= $agentId == $agt['id'] ? 'selected' : ''; ?>>
                        <?= $agt['agent_name']; ?> (<?= $agt['agent_code']; ?>) - Saldo: Rp <?= number_format($agt['balance'], 0, ',', '.'); ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            
            <div class="form-group">
                <label>Jumlah Topup</label>
                <input type="number" name="amount" id="amount" class="form-control" placeholder="50000" min="1000" step="1000" required>
                <div class="quick-amounts">
                    <div class="quick-amount" onclick="setAmount(50000)">50K</div>
                    <div class="quick-amount" onclick="setAmount(100000)">100K</div>
                    <div class="quick-amount" onclick="setAmount(500000)">500K</div>
                    <div class="quick-amount" onclick="setAmount(1000000)">1JT</div>
                </div>
            </div>

            <div class="form-group">
                <label>Keterangan</label>
                <textarea name="description" class="form-control" rows="3" placeholder="Topup saldo via transfer BCA"></textarea>
            </div>

            <button type="submit" name="topup" class="btn btn-primary">
                <i class="fa fa-check"></i> Proses Topup
            </button>
            <br><br>
            <a href="./?hotspot=agent-list&session=<?= $session; ?>" class="btn-secondary">
                <i class="fa fa-arrow-left"></i> Kembali
            </a>
        </form>
        <?php else: ?>
        <div class="form-group">
            <label>Pilih Agent</label>
            <select class="form-control" onchange="window.location.href='./?hotspot=agent-topup&agent_id=' + this.value + '&session=<?= $session; ?>'">
                <option value="">-- Pilih Agent --</option>
                <?php foreach ($agents as $agt): ?>
                <option value="<?= $agt['id']; ?>">
                    <?= $agt['agent_name']; ?> (<?= $agt['agent_code']; ?>) - Saldo: Rp <?= number_format($agt['balance'], 0, ',', '.'); ?>
                </option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php endif; ?>
    </div>
</div>

<script>
function setAmount(amount) {
    document.getElementById('amount').value = amount;
}
</script>
