<script>
    import {getContext} from "svelte";
    import Manager from "./Manager.svelte";
    import FilePreview from "./FilePreview.svelte"
    import Form from "./form/Form.svelte";
    import axios from "axios";

    const channel = getContext("channel");

    export let schema;
    export let record;
    export let graph = [];
    export let recordHistory;
    export let isCreateMode;
    // export let isWritable = false;
    // export let users;
    $: validationErrors = null;
    $: errorMessage = null;

    let form;

    function save(e) {
        e.preventDefault();
        let status = e.detail.status
        console.log("SAVE: Attempt");
        validationErrors = null;
        return new Promise(function (resolve, reject) {

            // remove trashed edges
            let replaceEdges = graph
                .map((queryRecord) => queryRecord.edge)
                .filter((edge) => !edge._isTrashed && edge.source === record.id);

            axios
                .post(channel.lucentUrl + "/records", {
                    schemaName: record.schema,
                    updateEdges: true,
                    id: record.id,
                    data: record.data,
                    edges: replaceEdges,
                    status: status,
                    isCreateMode: isCreateMode,
                })
                .then(function (response) {
                    console.log("SAVE: SAVED");

                    if (isCreateMode) {
                        window.location.href = channel.lucentUrl + "/records/" + record.id;
                    } else {
                        record = response.data.record ?? null;
                        if (!record) {
                            // means trashed
                            window.location = channel.lucentUrl;
                            return;
                        }
                        graph = [...response.data.graph];
                        form.setOriginalData();
                    }

                    resolve(null);
                })
                .catch(function (error) {
                    // setOriginalContent();
                    if (error.response) {
                        if (typeof error.response.data.error === "string") {
                            errorMessage = error.response.data.error;
                        } else {
                            validationErrors = error.response.data.error;
                            console.log(validationErrors)
                        }
                    }
                    resolve(null);

                });
        });
    }
</script>


<div class="wrapper-normal transparent">
    <Manager managerRecords={recordHistory} {graph}/>

    <FilePreview {record} {schema}/>

    <div class=" mt-4" style="margin-bottom:150px">
            <Form
                    bind:this={form}
                    data={record.data}
                    status={record.status}
                    bind:graph
                    {schema}
                    {record}
                    {isCreateMode}
                    {errorMessage}
                    {validationErrors}
                    on:save={save}
            />
<!--            <Graph {graph} {record}/>-->
        <!--            <Info {record} {graph} {users} {schema}/>-->
    </div>
</div>
