<?php
require_once '../../config/config.php';

if (!isset($_POST['ids'])) {
    echo 'missing ids';
    exit;
}

$ids = explode(',', $_POST['ids']);
$ids = array_map('intval', $ids);

// 获取第一个链接的分类ID
$conn = db_connect();
$first_id = $ids[0];
$category_query = $conn->query("SELECT category_id FROM links WHERE id = $first_id");
$category_result = $category_query->fetch_assoc();
$category_id = $category_result['category_id'];

// 保存排序
$order_file = '../../data/link_order.json';
$order_data = [];
if (file_exists($order_file)) {
    $order_data = json_decode(file_get_contents($order_file), true) ?? [];
}

$order_data[$category_id] = $ids;
if (file_put_contents($order_file, json_encode($order_data))) {
    echo 'success';
} else {
    echo '保存排序失败';
}

$conn->close();
