/*
 * Copyright 2020 Netsyms Technologies.
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

function openEventAddScreen(machineid) {
    app.dialog.preloader("Loading...");
    apirequest(
            "geteventtypes",
            {},
            function (resp) {
                app.dialog.close();

                catoptions = [

                ];

                for (const catlabel in resp) {
                    var options = [];
                    for (const optionval in resp[catlabel]) {
                        options.push({
                            value: optionval,
                            label: resp[catlabel][optionval]
                        });
                    }
                    catoptions.push({
                        label: catlabel,
                        options: options
                    });
                }

                console.log(catoptions);

                router.navigate("/addevent", {
                    context: {
                        machineid: machineid,
                        fields: [
                            {
                                name: "event",
                                label: "Event",
                                type: "select",
                                catoptions: catoptions,
                                value: "",
                                tabindex: 0
                            },
                            {
                                name: "date",
                                label: "Date/Time",
                                type: "datetime",
                                value: "",
                                tabindex: 1
                            },
                            {
                                name: "privatenotes",
                                label: "Private Notes",
                                type: "textarea",
                                value: "",
                                tabindex: 2
                            },
                            {
                                name: "publicnotes",
                                label: "Public Notes",
                                type: "textarea",
                                value: "",
                                tabindex: 3
                            }
                        ]
                    }
                });
            },
            function (xhr) {
                app.dialog.close();
                var error = $.parseJSON(xhr.responseText);
                if (error && typeof error.msg != 'undefined') {
                    app.dialog.alert(error.msg, "Error");
                } else {
                    app.dialog.alert("A server or network error occurred.", "Error");
                }
            },
            getStorage("username"),
            getStorage("password")
            );
}

function addEvent(machineid) {
    app.dialog.preloader("Adding event...");

    var eventdata = {
        id: machineid
    };
    $("#eventform .eventfield").each(function (i, o) {
        if ($(this).parent().is(".smart-select")) {
            eventdata[$(this).attr("name")] = app.smartSelect.get('#smartselect-' + $(this).attr("name")).getValue();
        } else {
            eventdata[$(this).attr("name")] = $(this).val();
        }
    });

    apirequest(
            "addevent",
            eventdata,
            function (resp) {
                app.dialog.close();
                if (resp.status == "ERROR") {
                    app.dialog.alert(resp.msg, "Error");
                } else {
                    app.toast.create({
                        text: 'Event added!',
                        closeTimeout: 2000,
                    }).open();
                    router.back();
                }
            },
            function (xhr) {
                app.dialog.close();
                var error = $.parseJSON(xhr.responseText);
                if (error && typeof error.msg != 'undefined') {
                    app.dialog.alert(error.msg, "Error");
                } else {
                    app.dialog.alert("A server or network error occurred.", "Error");
                }
            });
}