<?php
if (isset($_POST['chartfile'])) {
    $uploadDir = '../../../resources/charts/';
    $responses = [];

    if (!is_dir($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }

    foreach ($_POST['chartfile'] as $key => $base64Image) {
        $fileName = 'chart' . $key . '.ch';
        $filePath = $uploadDir . $fileName;

        if (file_put_contents($filePath, $base64Image)) {
            $responses[] = "File uploaded successfully";
        } else {
            $responses[] = "Failed to save file";
        }
    }

    echo json_encode($responses);
} else {
    echo json_encode(["error" => "No images uploaded."]);
}
