<?php
include '../includes/auth.php';
include '../includes/functions.php';

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'Metode request tidak valid']);
    exit;
}

$data = json_decode(file_get_contents('php://input'), true);
$eventId = (int)$data['eventId'] ?? 0;
$urutan = $data['urutan'] ?? [];

try {
    $conn->begin_transaction();
    
    foreach ($urutan as $item) {
        $id_foto = (int)$item['id_foto'];
        $urutan = (int)$item['urutan'];
        
        $stmt = $conn->prepare("UPDATE dafdir_event_foto SET urutan = ? WHERE id_foto = ? AND id_event = ?");
        $stmt->bind_param("iii", $urutan, $id_foto, $eventId);
        $stmt->execute();
    }
    
    $conn->commit();
    echo json_encode(['success' => true]);
} catch (Exception $e) {
    $conn->rollback();
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
?>