<?php
if ( !$user['superAdmin'] ) {
    http_response_code(403);
    echo json_encode('You do not have permission to order public project attachments');
    exit;
}

if ( !$publicProject = $db->row("SELECT * FROM public_project WHERE id = ?", $_POST['id']) ) {
    http_response_code(400);
    echo json_encode('Invalid public project ID');
    exit;
}

// $order = json_decode($_POST['order']);
$orders = $_POST['order'];
foreach ( $orders as $order ) {
    if ( !$publicProjectAttachment = $db->row("SELECT * FROM public_project_attachment WHERE id = ? AND publicProjectId = ?", $order['id'], $_POST['id']) ) {
        http_response_code(400);
        echo json_encode('Invalid public project attachment ID');
        exit;
    }
    $db->update('public_project_attachment', ['sort' => $order['order']], ['id' => $order['id'], 'publicProjectId' => $_POST['id']]);
}


// if ( !$publicProjectAttachment = $db->row("SELECT * FROM public_project_attachment WHERE id = ?", $_POST['id']) ) {
//     http_response_code(400);
//     echo json_encode('Invalid public project attachment ID');
//     exit;
// } 
// $txt .= print_r($publicProjectAttachment, true);

// $db->delete('public_project_attachment', ['id' => $_POST['id']]);

// $txt .= "done\n\n\n\n";
// $file = fopen(PATH_LOGS.'/btb.delete-public-project-image.log', "a");
// fwrite($file, $txt);
// fclose($file);

http_response_code(200);
echo json_encode('Public project attachments ordered');