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

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

// 已经是代理商则跳转
if ($user['is_agent'] == 1) {
    redirect('?page=agent_dashboard');
}

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $agent_name = trim($_POST['agent_name'] ?? '');
    $contact_phone = trim($_POST['contact_phone'] ?? '');
    $contact_qq = trim($_POST['contact_qq'] ?? '');
    $contact_wechat = trim($_POST['contact_wechat'] ?? '');
    $reason = trim($_POST['reason'] ?? '');
    
    if (empty($agent_name)) {
        $error = '请填写代理名称';
    } else {
        // 检查是否已有待审核申请
        $check = $db->fetchOne("SELECT id FROM agent_applications WHERE user_id = $user_id AND status = 'pending'");
        if ($check) {
            $error = '您已提交过申请，请等待审核';
        } else {
            $agent_name = $db->escape($agent_name);
            $contact_phone = $db->escape($contact_phone);
            $contact_qq = $db->escape($contact_qq);
            $contact_wechat = $db->escape($contact_wechat);
            $reason = $db->escape($reason);
            
            $db->query("INSERT INTO agent_applications (user_id, agent_name, contact_phone, contact_qq, contact_wechat, reason) 
                        VALUES ($user_id, '$agent_name', '$contact_phone', '$contact_qq', '$contact_wechat', '$reason')");
            $success = '申请已提交，请等待管理员审核';
        }
    }
}
?>

<div class="agent-apply-container">
    <div class="page-header">
        <h2>申请成为代理商</h2>
        <p>加入代理商计划，享受更高收益</p>
    </div>
    
    <div class="agent-benefits">
        <h3>代理商权益</h3>
        <div class="benefits-grid">
            <div class="benefit-card">
                <div class="benefit-icon">💰</div>
                <h4>高额佣金</h4>
                <p>最高可获 30% 销售佣金</p>
            </div>
            <div class="benefit-card">
                <div class="benefit-icon">📋</div>
                <h4>成为代理要求</h4>
                <p>在本站预存5元</p>
            </div>
            <div class="benefit-card">
                <div class="benefit-icon">📦</div>
                <h4>独立后台</h4>
                <p>专属代理商管理后台</p>
            </div>
            <div class="benefit-card">
                <div class="benefit-icon">🎯</div>
                <h4>自主定价</h4>
                <p>可自主设置销售价格</p>
            </div>
            <div class="benefit-card">
                <div class="benefit-icon">📊</div>
                <h4>数据统计</h4>
                <p>实时查看销售业绩</p>
            </div>
        </div>
    </div>
    
    <?php if ($success): ?>
        <div class="alert alert-success"><?php echo $success; ?></div>
    <?php endif; ?>
    <?php if ($error): ?>
        <div class="alert alert-error"><?php echo $error; ?></div>
    <?php endif; ?>
    
    <div class="apply-form">
        <form method="POST">
            <div class="form-group">
                <label>代理名称 *</label>
                <input type="text" name="agent_name" required placeholder="您的代理品牌名称">
            </div>
            <div class="form-row">
                <div class="form-group" style="flex:1">
                    <label>联系电话</label>
                    <input type="tel" name="contact_phone" placeholder="手机号码">
                </div>
                <div class="form-group" style="flex:1">
                    <label>QQ号码</label>
                    <input type="text" name="contact_qq" placeholder="QQ号">
                </div>
            </div>
            <div class="form-group">
                <label>微信号</label>
                <input type="text" name="contact_wechat" placeholder="微信号">
            </div>
            <div class="form-group">
                <label>申请理由</label>
                <textarea name="reason" rows="5" placeholder="请简要说明您的申请理由..."></textarea>
            </div>
            <button type="submit" class="btn btn-primary">提交申请</button>
            <a href="?page=dashboard" class="btn btn-secondary">返回</a>
        </form>
    </div>
</div>

<style>
.agent-apply-container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
}
.page-header {
    text-align: center;
    margin-bottom: 30px;
}
.agent-benefits {
    margin-bottom: 30px;
}
.benefits-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-top: 20px;
}
.benefit-card {
    background: var(--bg-primary);
    border-radius: 12px;
    padding: 20px;
    text-align: center;
    border: 1px solid var(--border-color);
}
.benefit-icon {
    font-size: 32px;
    margin-bottom: 10px;
}
.benefit-card h4 {
    margin-bottom: 8px;
}
.benefit-card p {
    font-size: 13px;
    color: var(--text-secondary);
}
.apply-form {
    background: var(--bg-primary);
    border-radius: 16px;
    padding: 30px;
    border: 1px solid var(--border-color);
}
.form-row {
    display: flex;
    gap: 20px;
}
@media (max-width: 768px) {
    .form-row {
        flex-direction: column;
        gap: 0;
    }
}
</style>