<?php
require_once 'config.php';

$user = getEncryptUser();
if (!$user || $user['role'] !== 'admin') {
    header('Location: /encrypt/index.php');
    exit;
}

$message = '';
$error = '';

// 添加优惠码
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_coupon'])) {
    $code = strtoupper(trim($_POST['code']));
    $name = trim($_POST['name']);
    $type = $_POST['type'];
    $value = floatval($_POST['value']);
    $min_amount = floatval($_POST['min_amount']);
    $usage_limit = intval($_POST['usage_limit']);
    $expire_days = intval($_POST['expire_days']);
    
    $expire_date = $expire_days > 0 ? date('Y-m-d H:i:s', strtotime("+$expire_days days")) : 'NULL';
    
    $db->query("INSERT INTO encrypt_coupons (code, name, type, value, min_amount, usage_limit, expire_date) 
                VALUES ('$code', '$name', '$type', $value, $min_amount, $usage_limit, " . ($expire_date === 'NULL' ? 'NULL' : "'$expire_date'") . ")");
    $message = '优惠码添加成功';
}

// 删除优惠码
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $db->query("DELETE FROM encrypt_coupons WHERE id = $id");
    $message = '优惠码已删除';
}

$coupons = $db->fetchAll("SELECT * FROM encrypt_coupons ORDER BY id DESC");
?>
<?php include 'header.php'; ?>
<div class="encrypt-container">
    <div style="max-width: 1000px; margin: 0 auto;">
        <div class="card">
            <h2>🎟️ 优惠码管理</h2>
            
            <?php if ($message): ?>
                <div class="alert alert-success">✅ <?php echo $message; ?></div>
            <?php endif; ?>
            
            <!-- 添加优惠码表单 -->
            <form method="POST" style="margin-bottom: 30px; padding: 20px; background: var(--bg-secondary); border-radius: 16px;">
                <h3>添加新优惠码</h3>
                <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 15px;">
                    <input type="text" name="code" placeholder="优惠码 (如: SUMMER50)" required style="padding: 10px; border-radius: 8px; border: 1px solid var(--border);">
                    <input type="text" name="name" placeholder="名称 (如: 夏季特惠)" style="padding: 10px; border-radius: 8px; border: 1px solid var(--border);">
                    <select name="type" style="padding: 10px; border-radius: 8px; border: 1px solid var(--border);">
                        <option value="fixed">固定金额</option>
                        <option value="percent">百分比 (%)</option>
                    </select>
                    <input type="number" name="value" step="0.01" placeholder="优惠金额/百分比" required style="padding: 10px; border-radius: 8px; border: 1px solid var(--border);">
                    <input type="number" name="min_amount" step="0.01" placeholder="最低消费" value="0" style="padding: 10px; border-radius: 8px; border: 1px solid var(--border);">
                    <input type="number" name="usage_limit" placeholder="可使用次数" value="1" style="padding: 10px; border-radius: 8px; border: 1px solid var(--border);">
                    <select name="expire_days" style="padding: 10px; border-radius: 8px; border: 1px solid var(--border);">
                        <option value="7">7天过期</option>
                        <option value="30">30天过期</option>
                        <option value="90">90天过期</option>
                        <option value="0">永不过期</option>
                    </select>
                </div>
                <button type="submit" name="add_coupon" class="btn btn-primary" style="margin-top: 15px;">➕ 添加优惠码</button>
            </form>
            
            <!-- 优惠码列表 -->
            <h3>现有优惠码</h3>
            <div style="overflow-x: auto;">
                <table class="data-table" style="width: 100%;">
                    <thead>
                        <tr><th>优惠码</th><th>名称</th><th>类型</th><th>优惠值</th><th>最低消费</th><th>已用/总次数</th><th>过期时间</th><th>状态</th><th>操作</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($coupons as $c): ?>
                        <tr>
                            <td><strong><?php echo $c['code']; ?></strong></td>
                            <td><?php echo $c['name']; ?></td>
                            <td><?php echo $c['type'] == 'fixed' ? '💰 固定金额' : '📊 百分比'; ?></td>
                            <td><?php echo $c['type'] == 'fixed' ? '¥'.$c['value'] : $c['value'].'%'; ?></td>
                            <td><?php echo $c['min_amount'] > 0 ? '¥'.$c['min_amount'] : '无'; ?></td>
                            <td><?php echo $c['used_count']; ?>/<?php echo $c['usage_limit'] ?: '∞'; ?></td>
                            <td><?php echo $c['expire_date'] ? date('Y-m-d', strtotime($c['expire_date'])) : '永久'; ?></td>
                            <td><?php echo $c['status'] ? '✅ 启用' : '❌ 禁用'; ?></td>
                            <td><a href="?delete=<?php echo $c['id']; ?>" onclick="return confirm('确定删除？')" class="btn btn-danger btn-sm">删除</a></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?php include 'footer.php'; ?>