<?php
require_once 'config.php';

// 错误处理
error_reporting(E_ALL);
ini_set('display_errors', 1);

// 检查并添加缺少的字段
function checkAndAddColumns($conn) {
    $checks = [
        "apps LIKE 'virtual_downloads'" => "ALTER TABLE apps ADD COLUMN virtual_downloads INT NOT NULL DEFAULT 0 AFTER download_count",
        "apps LIKE 'download_people'" => "ALTER TABLE apps ADD COLUMN download_people INT NOT NULL DEFAULT 0 AFTER download_count",
        "apps LIKE 'is_sticky'" => "ALTER TABLE apps ADD COLUMN is_sticky TINYINT NOT NULL DEFAULT 0 AFTER virtual_downloads",
        "apps LIKE 'sticky_time'" => "ALTER TABLE apps ADD COLUMN sticky_time DATETIME NULL DEFAULT NULL AFTER is_sticky",
        "apps LIKE 'sort_order'" => "ALTER TABLE apps ADD COLUMN sort_order INT NOT NULL DEFAULT 0",
        "apps LIKE 'is_official'" => "ALTER TABLE apps ADD COLUMN is_official TINYINT NOT NULL DEFAULT 0", 
        "categories LIKE 'created_at'" => "ALTER TABLE categories ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP",
    ];
    
    foreach($checks as $check => $sql) {
        $res = mysqli_query($conn, "SHOW COLUMNS FROM $check");
        if (mysqli_num_rows($res) == 0) {
            mysqli_query($conn, $sql);
        }
    }
}

checkAndAddColumns($conn);

// --- 处理逻辑 ---

// 1. 系统设置保存
if (isset($_POST['save_settings'])) {
    $auto_approve = isset($_POST['auto_approve']) ? 1 : 0;
    // 确保表存在
    mysqli_query($conn, "CREATE TABLE IF NOT EXISTS system_settings (
        id INT AUTO_INCREMENT PRIMARY KEY,
        setting_key VARCHAR(50) NOT NULL,
        setting_value TINYINT(1) NOT NULL DEFAULT 0
    )");
    
    // 检查是否有这个设置项，没有则插入
    $check = mysqli_query($conn, "SELECT id FROM system_settings WHERE setting_key='auto_approve'");
    if(mysqli_num_rows($check) == 0) {
        mysqli_query($conn, "INSERT INTO system_settings (setting_key, setting_value) VALUES ('auto_approve', $auto_approve)");
    } else {
        mysqli_query($conn, "UPDATE system_settings SET setting_value = $auto_approve WHERE setting_key = 'auto_approve'");
    }
    echo "<script>alert('设置已保存'); location.href='admin.php?tab=settings';</script>";
}

// 2. 置顶开关
if (isset($_GET['toggle_sticky'])) {
    $id = (int)$_GET['toggle_sticky'];
    $current = mysqli_query($conn, "SELECT is_sticky FROM apps WHERE id = $id");
    if ($current && $row = mysqli_fetch_assoc($current)) {
        $new_sticky = $row['is_sticky'] ? 0 : 1;
        $sticky_time = $new_sticky ? date('Y-m-d H:i:s') : 'NULL';
        mysqli_query($conn, "UPDATE apps SET is_sticky = $new_sticky, sticky_time = " . ($new_sticky ? "'$sticky_time'" : "NULL") . " WHERE id = $id");
    }
    header("Location: admin.php?tab=single");
    exit;
}

// 3. 删除应用
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $result = mysqli_query($conn, "SELECT image FROM apps WHERE id = $id");
    if ($result && $row = mysqli_fetch_assoc($result)) {
        if (file_exists($row['image'])) unlink($row['image']);
    }
    mysqli_query($conn, "DELETE FROM apps WHERE id = $id");
    header("Location: sh.php?tab=1"); 
    exit;
}

// 4. 删除分类
if (isset($_GET['delete_category'])) {
    $id = (int)$_GET['delete_category'];
    mysqli_query($conn, "DELETE FROM categories WHERE id = $id");
    header("Location: admin.php?tab=category");
    exit;
}

// 5. 编辑分类
if (isset($_POST['edit_category'])) {
    $id = (int)$_POST['category_id'];
    $name = mysqli_real_escape_string($conn, $_POST['category_name']);
    
    // 检查新名字是否和其他分类重复
    $check_sql = "SELECT id FROM categories WHERE name='$name' AND id != $id";
    $res = mysqli_query($conn, $check_sql);
    if(mysqli_num_rows($res) > 0) {
        echo "<script>alert('分类名称已存在，无法修改'); history.back();</script>";
    } else {
        mysqli_query($conn, "UPDATE categories SET name='$name' WHERE id=$id");
        header("Location: admin.php?tab=category");
    }
    exit;
}

// 6. 分类排序
if (isset($_POST['save_category_order'])) {
    foreach($_POST['cat_order'] as $id => $new_order) {
        $id = (int)$id; $new_order = (int)$new_order;
        mysqli_query($conn, "UPDATE categories SET sort_order=$new_order WHERE id=$id");
    }
    $res = mysqli_query($conn, "SELECT id FROM categories ORDER BY sort_order ASC, id ASC");
    $index = 1;
    while($row = mysqli_fetch_assoc($res)) {
        mysqli_query($conn, "UPDATE categories SET sort_order = $index WHERE id = " . $row['id']);
        $index++;
    }
    header("Location: admin.php?tab=category");
    exit;
}

// 7. 编辑应用
if (isset($_POST['edit'])) {
    $id = (int)$_POST['id'];
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $download_url = mysqli_real_escape_string($conn, $_POST['download_url']);
    $category = mysqli_real_escape_string($conn, $_POST['category']);
    $virtual_downloads = (int)($_POST['virtual_downloads'] ?? 0);
    $download_count = (int)($_POST['download_count'] ?? 0);
    $download_people = (int)($_POST['download_people'] ?? 0);
    $sort_order = (int)($_POST['sort_order'] ?? 0);
    $coin_price = (int)($_POST['coin_price'] ?? 0);
    $disk_code = mysqli_real_escape_string($conn, $_POST['disk_code'] ?? '');
    
    $is_sticky = isset($_POST['is_sticky']) ? 1 : 0;
    $is_official = isset($_POST['is_official']) ? 1 : 0;
    $sticky_time = $is_sticky ? date('Y-m-d H:i:s') : 'NULL';

    $sql = "UPDATE apps SET 
            name='$name', 
            download_url='$download_url', 
            category='$category', 
            virtual_downloads='$virtual_downloads',
            download_count='$download_count',
            download_people='$download_people',
            sort_order='$sort_order',
            coin_price='$coin_price',
            disk_code='$disk_code',
            is_sticky='$is_sticky',
            is_official='$is_official',
            sticky_time=" . ($is_sticky ? "'$sticky_time'" : "NULL");
    
    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
        $upload_dir = "uploads/";
        if (!is_dir($upload_dir)) mkdir($upload_dir, 0777, true);
        $pic_name = uniqid() . "_" . basename($_FILES['image']['name']);
        $dst = $upload_dir . $pic_name;
        if (move_uploaded_file($_FILES['image']['tmp_name'], $dst)) {
            $old = mysqli_query($conn, "SELECT image FROM apps WHERE id = $id");
            if ($old && $old_row = mysqli_fetch_assoc($old)) {
                if (file_exists($old_row['image'])) unlink($old_row['image']);
            }
            $sql .= ", image='$dst'";
        }
    }
    mysqli_query($conn, $sql . " WHERE id = $id");
    header("Location: admin.php?tab=single&edit=" . $id); 
    exit;
}

// 8. 添加应用
if (isset($_POST['submit'])) {
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $download_url = mysqli_real_escape_string($conn, $_POST['download_url']);
    $category = mysqli_real_escape_string($conn, $_POST['category']);
    $coin_price = (int)($_POST['coin_price'] ?? 0);
    $disk_code = mysqli_real_escape_string($conn, $_POST['disk_code'] ?? '');
    
    // 识别网盘类型
    $disk_type = '其他';
    if (strpos($download_url, 'baidu.com') !== false) $disk_type = '百度网盘';
    elseif (strpos($download_url, 'quark.cn') !== false) $disk_type = '夸克网盘';
    elseif (strpos($download_url, 'xunlei.com') !== false) $disk_type = '迅雷网盘';
    elseif (strpos($download_url, 'lanzou') !== false) $disk_type = '蓝奏云';
    elseif (strpos($download_url, 'aliyundrive.com') !== false || strpos($download_url, 'alipan.com') !== false) $disk_type = '阿里云盘';
    elseif (strpos($download_url, 'weiyun.com') !== false) $disk_type = '腾讯微云';
    elseif (strpos($download_url, 'yun.10086.cn') !== false) $disk_type = '移动云盘';
    elseif (strpos($download_url, '123pan.com') !== false) $disk_type = '123云盘';
    
    $upload_dir = "uploads/";
    if (!is_dir($upload_dir)) mkdir($upload_dir, 0777, true);
    
    $image_path = "";
    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
        $pic_name = uniqid() . "_" . basename($_FILES['image']['name']);
        $dst = $upload_dir . $pic_name;
        if (move_uploaded_file($_FILES['image']['tmp_name'], $dst)) {
            $image_path = $dst;
        }
    }
    
    $max_order = mysqli_query($conn, "SELECT MAX(sort_order) as mo FROM apps");
    $next_order = (mysqli_fetch_assoc($max_order)['mo'] ?? 0) + 1;

    $sql = "INSERT INTO apps (name, image, download_url, disk_type, disk_code, category, created_at, sort_order, coin_price) 
            VALUES ('$name', '$image_path', '$download_url', '$disk_type', '$disk_code', '$category', NOW(), $next_order, $coin_price)";
            
    if (mysqli_query($conn, $sql)) {
        echo "<script>alert('添加成功'); location.href='admin.php?tab=single';</script>";
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}

// 9. 添加分类 (修复重复报错问题)
if (isset($_POST['add_category'])) {
    $name = mysqli_real_escape_string($conn, $_POST['category_name']);
    
    // 【核心修复】先检查是否存在同名分类
    $check = mysqli_query($conn, "SELECT id FROM categories WHERE name = '$name'");
    if(mysqli_num_rows($check) > 0) {
        echo "<script>alert('添加失败：分类名称 [{$name}] 已存在！'); history.back();</script>";
    } else {
        mysqli_query($conn, "INSERT INTO categories (name) VALUES ('$name')");
        header("Location: admin.php?tab=category");
    }
    exit;
}

// 获取标签
$tab = isset($_GET['tab']) ? $_GET['tab'] : 'single';

// 获取分类列表
$categories = mysqli_query($conn, "SELECT * FROM categories ORDER BY sort_order, name");
$categories_array = [];
if ($categories) while ($c = mysqli_fetch_assoc($categories)) $categories_array[] = $c;

// 获取应用列表
$apps = mysqli_query($conn, "SELECT a.*, c.name as cat_name FROM apps a LEFT JOIN categories c ON a.category = c.name ORDER BY a.is_sticky DESC, a.sort_order ASC, a.id DESC");

// 获取当前自动审核设置
$auto_approve_val = 0;
$res_set = mysqli_query($conn, "SELECT setting_value FROM system_settings WHERE setting_key='auto_approve'");
if($res_set && $row_set = mysqli_fetch_assoc($res_set)) {
    $auto_approve_val = $row_set['setting_value'];
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WC软件后台管理</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #f0f2f5; padding: 20px; font-size: 14px; }
        .container { max-width: 1200px; margin: 0 auto; }
        
        .tabs { display: flex; gap: 10px; margin-bottom: 20px; flex-wrap: wrap; }
        .tab-btn { padding: 10px 20px; background: #fff; border: none; border-radius: 8px; cursor: pointer; font-size: 15px; text-decoration: none; color: #333; box-shadow: 0 2px 4px rgba(0,0,0,0.05); }
        .tab-btn.active { background: #007bff; color: white; }
        
        .card { background: #fff; border-radius: 12px; padding: 20px; margin-bottom: 20px; box-shadow: 0 2px 8px rgba(0,0,0,0.05); }
        
        .form-group { margin-bottom: 15px; }
        .form-group label { display: block; font-size: 13px; color: #666; margin-bottom: 5px; font-weight: bold; }
        input, select, textarea { width: 100%; height: 40px; border: 1px solid #ddd; border-radius: 6px; padding: 0 10px; font-size: 14px; background: #fff; }
        textarea { height: auto; padding: 10px; }
        
        .btn { padding: 8px 15px; background: #007bff; color: white; border: none; border-radius: 6px; cursor: pointer; font-size: 14px; text-decoration: none; display: inline-block; margin: 2px; }
        .btn-danger { background: #dc3545; }
        .btn-success { background: #28a745; }
        .btn-secondary { background: #6c757d; }
        .btn-warning { background: #ffc107; color:#333; }
        
        table { width: 100%; border-collapse: collapse; margin-top: 15px; }
        th, td { padding: 12px; text-align: left; border-bottom: 1px solid #eee; }
        th { background: #f8f9fa; font-weight: bold; white-space: nowrap; }
        img { width: 50px; height: 50px; border-radius: 6px; object-fit: cover; }
        
        .checkbox-label { display: flex; align-items: center; gap: 10px; cursor: pointer; }
        .checkbox-label input { width: 18px; height: 18px; margin: 0; }
        
        /* 开关样式 */
        .switch-container { display: flex; align-items: center; gap: 15px; padding: 15px; background: #f8f9fa; border-radius: 8px; margin-bottom: 20px; }
        .switch { position: relative; display: inline-block; width: 50px; height: 26px; }
        .switch input { opacity: 0; width: 0; height: 0; }
        .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; transition: .4s; border-radius: 26px; }
        .slider:before { position: absolute; content: ""; height: 20px; width: 20px; left: 3px; bottom: 3px; background-color: white; transition: .4s; border-radius: 50%; }
        input:checked + .slider { background-color: #28a745; }
        input:checked + .slider:before { transform: translateX(24px); }
    </style>
</head>
<body>
<div class="container">
    <div class="tabs">
        <a href="?tab=single" class="tab-btn <?= $tab == 'single' ? 'active' : '' ?>">应用管理</a>
        <a href="?tab=category" class="tab-btn <?= $tab == 'category' ? 'active' : '' ?>">分类管理</a>
        <a href="?tab=settings" class="tab-btn <?= $tab == 'settings' ? 'active' : '' ?>" style="background: #17a2b8; color: white;">系统设置</a>
        <a href="sh.php" class="tab-btn" style="background: #ffc107; color: #333;">投稿审核</a>
    </div>

    <!-- 系统设置标签页 -->
    <?php if($tab == 'settings'): ?>
    <div class="card">
        <h3 style="margin-bottom: 20px;">系统设置</h3>
        <form method="post">
            <div class="switch-container">
                <label class="switch">
                    <input type="checkbox" name="auto_approve" value="1" <?= $auto_approve_val ? 'checked' : '' ?>>
                    <span class="slider"></span>
                </label>
                <div>
                    <div style="font-weight: bold; font-size: 15px;">自动审核通过</div>
                    <div style="font-size: 12px; color: #666;">开启后，用户投稿的内容将直接通过审核，不再进入待审列表。</div>
                </div>
            </div>
            <button type="submit" name="save_settings" class="btn btn-success">保存设置</button>
        </form>
    </div>
    <?php endif; ?>

    <!-- 应用管理标签页 -->
    <?php if($tab == 'single'): ?>
    <div class="card">
        <h3 style="margin-bottom: 15px;">添加新应用</h3>
        <form method="post" enctype="multipart/form-data">
             <div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 15px;">
                <div class="form-group">
                    <label>软件名称</label>
                    <input type="text" name="name" required>
                </div>
                <div class="form-group">
                    <label>所属分类</label>
                    <select name="category" required>
                        <?php foreach($categories_array as $cat): ?>
                        <option value="<?= htmlspecialchars($cat['name']) ?>"><?= htmlspecialchars($cat['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label>下载价格 (小马币)</label>
                    <input type="number" name="coin_price" value="0" min="0">
                </div>
            </div>
            
            <div class="form-group">
                <label>下载链接</label>
                <input type="url" name="download_url" required placeholder="http://...">
            </div>
            
            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
                 <div class="form-group">
                    <label>提取码 (可选)</label>
                    <input type="text" name="disk_code" placeholder="如: 1234">
                </div>
                <div class="form-group">
                    <label>软件图标</label>
                    <input type="file" name="image" accept="image/*">
                </div>
            </div>
            
            <button type="submit" name="submit" class="btn btn-success">添加应用</button>
        </form>
    </div>

    <div class="card">
        <h3 style="margin-bottom: 15px;">应用列表</h3>
        <div style="overflow-x:auto;">
            <table>
                <thead>
                    <tr>
                        <th>图标</th>
                        <th>名称</th>
                        <th>分类</th>
                        <th>价格</th>
                        <th>下载</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while($app = mysqli_fetch_assoc($apps)): ?>
                    <tr>
                        <td><img src="<?= htmlspecialchars($app['image']) ?>" onerror="this.src='https://via.placeholder.com/50'"></td>
                        <td><?= htmlspecialchars($app['name']) ?></td>
                        <td><?= htmlspecialchars($app['cat_name']) ?></td>
                        <td><?= $app['coin_price'] ?>币</td>
                        <td style="font-size:12px; line-height:1.4;">
                            点击: <?= $app['download_count'] ?><br>
                            虚拟: <?= $app['virtual_downloads'] ?>
                        </td>
                        <td>
                            <a href="?tab=single&edit=<?= $app['id'] ?>" class="btn">编辑</a>
                            <a href="?delete=<?= $app['id'] ?>" class="btn btn-danger" onclick="return confirm('确定删除?')">删除</a>
                            <a href="?toggle_sticky=<?= $app['id'] ?>" class="btn <?= $app['is_sticky'] ? 'btn-warning' : 'btn-secondary' ?>">
                                <?= $app['is_sticky'] ? '取消置顶' : '置顶' ?>
                            </a>
                        </td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
    </div>

    <!-- 编辑弹窗 -->
    <?php if(isset($_GET['edit'])): ?>
    <?php
    $edit_id = (int)$_GET['edit'];
    $edit_res = mysqli_query($conn, "SELECT * FROM apps WHERE id = $edit_id");
    $edit_app = mysqli_fetch_assoc($edit_res);
    ?>
    <div class="card" style="border: 2px solid #007bff;">
        <h3 style="margin-bottom: 15px;">编辑: <?= htmlspecialchars($edit_app['name']) ?></h3>
        <form method="post" enctype="multipart/form-data">
            <input type="hidden" name="id" value="<?= $edit_app['id'] ?>">
            
            <div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 15px;">
                <div class="form-group">
                    <label>软件名称</label>
                    <input type="text" name="name" value="<?= htmlspecialchars($edit_app['name']) ?>" required>
                </div>
                <div class="form-group">
                    <label>所属分类</label>
                    <select name="category" required>
                        <?php foreach($categories_array as $cat): ?>
                        <option value="<?= htmlspecialchars($cat['name']) ?>" <?= $edit_app['category'] == $cat['name'] ? 'selected' : '' ?>><?= htmlspecialchars($cat['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label>下载价格</label>
                    <input type="number" name="coin_price" value="<?= $edit_app['coin_price'] ?>">
                </div>
            </div>

            <div class="form-group">
                <label>下载链接</label>
                <input type="url" name="download_url" value="<?= htmlspecialchars($edit_app['download_url']) ?>" required>
            </div>
            
            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
                <div class="form-group">
                    <label>提取码</label>
                    <input type="text" name="disk_code" value="<?= htmlspecialchars($edit_app['disk_code']) ?>">
                </div>
                <div class="form-group">
                    <label>网盘类型</label>
                    <input type="text" value="<?= htmlspecialchars($edit_app['disk_type']) ?>" disabled style="background:#eee;">
                </div>
            </div>

            <!-- 数据统计 -->
            <div style="background:#f8f9fa; padding:15px; border-radius:8px; margin-bottom:15px;">
                <div style="display:flex; gap:20px; flex-wrap:wrap;">
                    <div class="form-group" style="flex:1; min-width:100px;">
                        <label>点击次数</label>
                        <input type="number" name="download_count" value="<?= $edit_app['download_count'] ?>">
                    </div>
                    <div class="form-group" style="flex:1; min-width:100px;">
                        <label>下载人数</label>
                        <input type="number" name="download_people" value="<?= $edit_app['download_people'] ?>">
                    </div>
                    <div class="form-group" style="flex:1; min-width:100px;">
                        <label>虚拟下载</label>
                        <input type="number" name="virtual_downloads" value="<?= $edit_app['virtual_downloads'] ?>">
                    </div>
                    <div class="form-group" style="flex:1; min-width:100px;">
                        <label>排序权重</label>
                        <input type="number" name="sort_order" value="<?= $edit_app['sort_order'] ?>">
                    </div>
                </div>
            </div>

            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
                 <div class="form-group">
                    <label>软件图标</label>
                    <input type="file" name="image" accept="image/*">
                </div>
                <div class="form-group">
                    <label>特殊属性</label>
                    <div style="display:flex; gap:15px; height:40px; align-items:center;">
                        <label class="checkbox-label"><input type="checkbox" name="is_sticky" value="1" <?= $edit_app['is_sticky'] ? 'checked' : '' ?>> 置顶</label>
                        <label class="checkbox-label"><input type="checkbox" name="is_official" value="1" <?= $edit_app['is_official'] ? 'checked' : '' ?>> 官方</label>
                    </div>
                </div>
            </div>
            
            <div style="margin-top: 15px;">
                <button type="submit" name="edit" class="btn btn-success">保存修改</button>
                <a href="?tab=single" class="btn btn-secondary">取消</a>
            </div>
        </form>
    </div>
    <?php endif; ?>
    <?php endif; ?>

    <!-- 分类管理标签页 -->
    <?php if($tab == 'category'): ?>
    <div class="card">
        <h3 style="margin-bottom: 15px;">添加分类</h3>
        <form method="post" style="display:flex; gap:10px;">
            <input type="text" name="category_name" placeholder="输入分类名称" required style="flex:1;">
            <button type="submit" name="add_category" class="btn btn-success">添加</button>
        </form>
    </div>

    <div class="card">
        <h3 style="margin-bottom: 15px;">分类列表 (可拖动排序或修改序号)</h3>
        <form method="post">
        <table>
            <thead>
                <tr>
                    <th>序号</th>
                    <th>名称</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($categories_array as $cat): ?>
                <tr>
                    <td><input type="number" name="cat_order[<?= $cat['id'] ?>]" value="<?= $cat['sort_order'] ?>" style="width:60px; text-align:center;"></td>
                    <td>
                        <input type="text" name="cat_name_edit" value="<?= htmlspecialchars($cat['name']) ?>" form="cat_edit_form_<?= $cat['id'] ?>">
                    </td>
                    <td>
                        <button type="submit" name="save_category_order" class="btn" style="padding:5px 10px;">保存排序</button>
                        <form id="cat_edit_form_<?= $cat['id'] ?>" method="post" style="display:inline;">
                            <input type="hidden" name="category_id" value="<?= $cat['id'] ?>">
                            <button type="submit" name="edit_category" class="btn btn-success" style="padding:5px 10px;">改名</button>
                        </form>
                        <a href="?delete_category=<?= $cat['id'] ?>" class="btn btn-danger" style="padding:5px 10px;" onclick="return confirm('确定删除?')">删除</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        </form>
    </div>
    <?php endif; ?>
</div>
</body>
</html>
