<?php
/* POST: order = JSON-Array von page_ids in neuer Reihenfolge, token */
require_once __DIR__.'/_inc.php';

global $db, $user;
if (!$user->hasRight('bericht', 'write')) bericht_ajax_fail('Permission denied', 403);

$order_raw = $_POST['order'] ?? '';
$ids = json_decode($order_raw, true);
if (!is_array($ids)) bericht_ajax_fail('Ungültige Reihenfolge');

$db->begin();
foreach ($ids as $pos => $pageid) {
    $pageid = (int) $pageid;
    if ($pageid <= 0) continue;
    if (!$db->query("UPDATE ".$db->prefix()."bericht_page SET page_order = ".((int) ($pos + 1))." WHERE rowid = ".$pageid)) {
        $db->rollback();
        bericht_ajax_fail($db->lasterror());
    }
}
$db->commit();

bericht_ajax_ok();
