<?php
require_once dirname(__DIR__, 2) . '/config.php';

$user_id = $_SESSION['user_id'];
$user = currentUser();

// 检查是否为代理商
if ($user['is_agent'] != 1) {
    redirect('?page=dashboard');
}

// 统计数据
$total_orders = $db->fetchOne("SELECT COUNT(*) as cnt FROM agent_orders WHERE agent_id = $user_id")['cnt'] ?? 0;
$total_sales = $db->fetchOne("SELECT SUM(amount) as total FROM agent_orders WHERE agent_id = $user_id AND status = 'paid'")['total'] ?? 0;
$total_commission = $user['agent_total_commission'] ?? 0;
$available_commission = $user['agent_total_commission'] - $user['agent_withdrawn'];

// 最近订单
$recent_orders = $db->fetchAll("
    SELECT ao.*, a.name as app_name, u.username as customer_name
    FROM agent_orders ao
    JOIN apps a ON ao.app_id = a.id
    JOIN users u ON ao.customer_id = u.id
    WHERE ao.agent_id = $user_id
    ORDER BY ao.id DESC LIMIT 10
");

// 可销售应用
$my_apps = $db->fetchAll("
    SELECT aa.*, a.name, a.description
    FROM agent_apps aa
    JOIN apps a ON aa.app_id = a.id
    WHERE aa.agent_id = $user_id AND aa.status = 1
");
?>

<div class="agent-dashboard">
    <div class="page-header">
        <h2>代理商控制台</h2>
        <p>欢迎回来，<?php echo htmlspecialchars($user['username']); ?></p>
    </div>
    
    <div class="stats-grid">
        <div class="stat-card">
            <h3>总订单数</h3>
            <div class="stat-value"><?php echo $total_orders; ?></div>
        </div>
        <div class="stat-card">
            <h3>总销售额</h3>
            <div class="stat-value">¥<?php echo number_format($total_sales, 2); ?></div>
        </div>
        <div class="stat-card">
            <h3>累计佣金</h3>
            <div class="stat-value">¥<?php echo number_format($total_commission, 2); ?></div>
        </div>
        <div class="stat-card">
            <h3>可提现佣金</h3>
            <div class="stat-value">¥<?php echo number_format($available_commission, 2); ?></div>
            <p><a href="?page=agent_withdraw" class="btn-primary btn-sm">申请提现</a></p>
        </div>
    </div>
    
    <!-- 我的应用 -->
    <div class="section-box">
        <div class="section-header">
            <h3>我的应用</h3>
            <a href="?page=agent_apps" class="btn-primary btn-sm">管理应用</a>
        </div>
        <div class="apps-grid">
            <?php if (empty($my_apps)): ?>
                <p style="color: var(--text-muted);">暂无应用，请联系管理员开通</p>
            <?php else: ?>
                <?php foreach ($my_apps as $app): ?>
                    <div class="app-card">
                        <h4><?php echo htmlspecialchars($app['name']); ?></h4>
                        <p><?php echo htmlspecialchars($app['description'] ?: '暂无描述'); ?></p>
                        <div class="app-price">售价: ¥<?php echo number_format($app['agent_price'], 2); ?></div>
                        <div class="app-commission">佣金比例: <?php echo $app['commission_rate']; ?>%</div>
                        <a href="?page=agent_sell&app_id=<?php echo $app['app_id']; ?>" class="btn-primary btn-sm">推广链接</a>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>
    
    <!-- 最近订单 -->
    <div class="section-box">
        <div class="section-header">
            <h3>最近订单</h3>
            <a href="?page=agent_orders" class="btn-primary btn-sm">查看全部</a>
        </div>
        <div class="table-container">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>订单号</th>
                        <th>客户</th>
                        <th>商品</th>
                        <th>金额</th>
                        <th>佣金</th>
                        <th>状态</th>
                        <th>时间</th>
                    </thead>
                <tbody>
                    <?php if (empty($recent_orders)): ?>
                        <tr><td colspan="7" style="text-align: center;">暂无订单</td></tr>
                    <?php else: ?>
                        <?php foreach ($recent_orders as $order): ?>
                            <tr>
                                <td><?php echo $order['order_no']; ?> 
                                <td><?php echo htmlspecialchars($order['customer_name']); ?> 
                                <td><?php echo htmlspecialchars($order['app_name']); ?> 
                                <td>¥<?php echo number_format($order['amount'], 2); ?> 
                                <td>¥<?php echo number_format($order['agent_commission'], 2); ?> 
                                <td><span class="status-badge status-<?php echo $order['status']; ?>"><?php echo $order['status'] == 'paid' ? '已支付' : '待支付'; ?></span> 
                                <td><?php echo $order['created_at']; ?> 
                             </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<style>
.agent-dashboard .stats-grid {
    margin-bottom: 30px;
}
.section-box {
    background: var(--bg-primary);
    border-radius: var(--radius-md);
    padding: 20px;
    margin-bottom: 20px;
    border: 1px solid var(--border-color);
}
.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--border-color);
}
.apps-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 15px;
}
.app-card {
    background: var(--bg-secondary);
    border-radius: 12px;
    padding: 15px;
}
.app-card h4 {
    margin-bottom: 8px;
}
.app-price {
    font-size: 18px;
    font-weight: bold;
    color: var(--primary-color);
    margin: 10px 0;
}
.app-commission {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 10px;
}
.btn-sm {
    padding: 5px 12px;
    font-size: 12px;
}
</style>