<?php
require_once dirname(dirname(dirname(__FILE__))) . '/includes/init.php';

// 检查是否登录
if (!isset($_SESSION['admin_logged_in']) || !$_SESSION['admin_logged_in']) {
    die('未登录');
}

// 获取POST数据
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$name = isset($_POST['name']) ? sanitize_input($_POST['name']) : '';
$description = isset($_POST['description']) ? sanitize_input($_POST['description']) : '';
$sort_order = isset($_POST['order']) ? intval($_POST['order']) : 0;

// 验证数据
if ($id <= 0) {
    die('无效的ID');
}
if (empty($name)) {
    die('分类名称不能为空');
}

// 更新分类
$stmt = $conn->prepare("UPDATE categories SET name = ?, description = ?, sort_order = ? WHERE id = ?");
$stmt->bind_param("ssii", $name, $description, $sort_order, $id);

if ($stmt->execute()) {
    echo 'success';
} else {
    echo '更新失败：' . $stmt->error;
}

$stmt->close();
$conn->close();
