<?php
/*
 * Admin Panel - Edit 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 = $agent->getAgentById($agentId);

if (!$agentData) {
    echo "<script>alert('Agent tidak ditemukan'); window.location='./?hotspot=agent-list&session=$session';</script>";
    exit;
}

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

if (isset($_POST['update_agent'])) {
    $data = [
        'agent_name' => trim($_POST['agent_name']),
        'phone' => trim($_POST['phone']),
        'email' => trim($_POST['email']),
        'level' => $_POST['level'],
        'commission_percent' => floatval($_POST['commission_percent']),
        'status' => $_POST['status'],
        'notes' => trim($_POST['notes'])
    ];
    
    if (!empty($_POST['password'])) {
        $data['password'] = $_POST['password'];
    }
    
    $result = $agent->updateAgent($agentId, $data);
    
    if ($result['success']) {
        $success = 'Data agent berhasil diupdate!';
        $agentData = $agent->getAgentById($agentId);
    } else {
        $error = $result['message'];
    }
}
?>

<style>
.form-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; }
.card-header { margin-bottom: 20px; padding-bottom: 15px; border-bottom: 2px solid #f0f0f0; }
.card-header h3 { margin: 0; color: #333; font-size: 18px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 20px; }
.form-group { margin-bottom: 20px; }
.form-group label { display: block; margin-bottom: 8px; color: #333; font-weight: 500; }
.form-control { width: 100%; padding: 10px 15px; border: 1px solid #e0e0e0; border-radius: 5px; font-size: 14px; }
.form-control:focus { outline: none; border-color: #667eea; box-shadow: 0 0 0 3px rgba(102,126,234,0.1); }
.btn { padding: 12px 30px; border: none; border-radius: 5px; cursor: pointer; font-weight: 500; font-size: 14px; text-decoration: none; display: inline-block; }
.btn-primary { background: #667eea; color: white; }
.btn-secondary { background: #6c757d; color: white; }
.btn-block { width: 100%; }
.alert { padding: 15px 20px; border-radius: 5px; margin-bottom: 20px; }
.alert-danger { background: #fee; color: #c33; border: 1px solid #fcc; }
.alert-success { background: #efe; color: #3c3; border: 1px solid #cfc; }
.info-badge { background: #f0f9ff; color: #1e40af; padding: 5px 15px; border-radius: 15px; font-size: 12px; font-weight: 600; }
@media (max-width: 768px) { .form-row { grid-template-columns: 1fr; } }
</style>

<div class="form-container">
    <div style="margin-bottom: 20px;">
        <h2><i class="fa fa-edit"></i> Edit Agent</h2>
        <p style="color: #666;">Update data agent: <strong><?= $agentData['agent_name']; ?></strong></p>
    </div>

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

    <form method="POST">
        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-user"></i> Data Agent</h3>
            </div>

            <div class="form-group">
                <label>Kode Agent</label>
                <input type="text" class="form-control" value="<?= $agentData['agent_code']; ?>" disabled>
            </div>

            <div class="form-row">
                <div class="form-group">
                    <label>Nama Lengkap</label>
                    <input type="text" name="agent_name" class="form-control" value="<?= $agentData['agent_name']; ?>" required>
                </div>
                <div class="form-group">
                    <label>Nomor WhatsApp</label>
                    <input type="tel" name="phone" class="form-control" value="<?= $agentData['phone']; ?>" required>
                </div>
            </div>

            <div class="form-row">
                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" value="<?= $agentData['email']; ?>">
                </div>
                <div class="form-group">
                    <label>Password Baru</label>
                    <input type="password" name="password" class="form-control" placeholder="Kosongkan jika tidak ingin mengubah">
                </div>
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <h3><i class="fa fa-cog"></i> Pengaturan</h3>
            </div>

            <div class="form-row">
                <div class="form-group">
                    <label>Level Agent</label>
                    <select name="level" class="form-control">
                        <option value="bronze" <?= $agentData['level'] == 'bronze' ? 'selected' : ''; ?>>Bronze</option>
                        <option value="silver" <?= $agentData['level'] == 'silver' ? 'selected' : ''; ?>>Silver</option>
                        <option value="gold" <?= $agentData['level'] == 'gold' ? 'selected' : ''; ?>>Gold</option>
                        <option value="platinum" <?= $agentData['level'] == 'platinum' ? 'selected' : ''; ?>>Platinum</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Komisi (%)</label>
                    <input type="number" name="commission_percent" class="form-control" value="<?= $agentData['commission_percent']; ?>" min="0" max="100" step="0.1">
                </div>
            </div>

            <div class="form-row">
                <div class="form-group">
                    <label>Status</label>
                    <select name="status" class="form-control">
                        <option value="active" <?= $agentData['status'] == 'active' ? 'selected' : ''; ?>>Active</option>
                        <option value="inactive" <?= $agentData['status'] == 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                        <option value="suspended" <?= $agentData['status'] == 'suspended' ? 'selected' : ''; ?>>Suspended</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Saldo Saat Ini</label>
                    <input type="text" class="form-control" value="Rp <?= number_format($agentData['balance'], 0, ',', '.'); ?>" disabled>
                    <small style="color: #666;">Gunakan menu Topup untuk mengubah saldo</small>
                </div>
            </div>

            <div class="form-group">
                <label>Catatan</label>
                <textarea name="notes" class="form-control" rows="3"><?= $agentData['notes']; ?></textarea>
            </div>
        </div>

        <div class="card">
            <button type="submit" name="update_agent" class="btn btn-primary btn-block">
                <i class="fa fa-save"></i> Update Data Agent
            </button>
            <br>
            <a href="./?hotspot=agent-list&session=<?= $session; ?>" class="btn btn-secondary btn-block">
                <i class="fa fa-arrow-left"></i> Kembali
            </a>
        </div>
    </form>
</div>
