<?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['update_price'])) {
    $id = intval($_POST['id']);
    $new_price = floatval($_POST['price']);
    
    if ($new_price >= 0) {
        $db->query("UPDATE encrypt_packages SET price = $new_price WHERE id = $id");
        $message = '价格已更新';
    } else {
        $error = '价格不能为负数';
    }
}

// 获取所有套餐
$packages = getEncryptPackages();
?>
<?php include 'header.php'; ?>
<div class="encrypt-container">
    <div style="max-width: 600px; margin: 0 auto;">
        <div class="card" style="background: var(--bg-primary); border-radius: 20px; padding: 30px; border: 1px solid var(--border-color);">
            <h2>套餐价格管理</h2>
            
            <?php if ($message): ?>
                <div class="alert alert-success" style="color: #28a745;"><?php echo $message; ?></div>
            <?php endif; ?>
            <?php if ($error): ?>
                <div class="alert alert-error" style="color: #dc3545;"><?php echo $error; ?></div>
            <?php endif; ?>
            
            <table style="width: 100%; border-collapse: collapse;">
                <thead>
                    <tr style="border-bottom: 1px solid var(--border-color);">
                        <th style="padding: 10px; text-align: left;">套餐名称</th>
                        <th style="padding: 10px; text-align: left;">当前价格</th>
                        <th style="padding: 10px; text-align: left;">新价格</th>
                        <th style="padding: 10px; text-align: left;">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($packages as $package): ?>
                    <form method="POST">
                        <input type="hidden" name="id" value="<?php echo $package['id']; ?>">
                        <tr style="border-bottom: 1px solid var(--border-color);">
                            <td style="padding: 10px;">
                                <?php echo htmlspecialchars($package['name']); ?>
                                <?php if ($package['days'] === null): ?>
                                    <span style="font-size: 11px;">(永久)</span>
                                <?php else: ?>
                                    <span style="font-size: 11px;">(<?php echo $package['days']; ?>天)</span>
                                <?php endif; ?>
                            </td>
                            <td style="padding: 10px;">
                                ¥<?php echo number_format($package['price'], 2); ?>
                            </td>
                            <td style="padding: 10px;">
                                <input type="number" name="price" step="0.5" value="<?php echo $package['price']; ?>" style="width: 80px; padding: 5px;">
                            </td>
                            <td style="padding: 10px;">
                                <button type="submit" name="update_price" class="btn btn-primary" style="padding: 5px 15px;">保存</button>
                            </td>
                        </tr>
                    </form>
                    <?php endforeach; ?>
                </tbody>
            </table>
            
            <div style="margin-top: 30px; padding-top: 20px; border-top: 1px solid var(--border-color);">
                <p><strong>💡 提示：</strong></p>
                <ul style="color: var(--text-secondary);">
                    <li>价格支持小数（如 4.99）</li>
                    <li>修改后立即生效，用户购买时显示新价格</li>
                    <li>已购买的用户不受影响</li>
                </ul>
            </div>
        </div>
    </div>
</div>
<?php include 'footer.php'; ?>