import { Locations } from '../../../imports/api/location.js';

Template.locMgmtForm.onCreated(function() {
    this.subscribe("myLocations");
});

Template.locMgmtForm.onRendered(function() {
    Session.set("locNameMiss", false);
    Session.set("locEditMode", false);
});

Template.locMgmtForm.helpers({
    locNameErr: function() {
        return Session.get("locNameMiss");
    },
    locEditMode: function() {
        return Session.get("locEditMode");
    }
});

Template.locMgmtForm.events({
    'click .saveLocMgmt' (event) {
        event.preventDefault();
        let locName = $("#locNameInp").val();
        if (locName == null || locName == "") {
            Session.set("locNameMiss", true);
            return;
        } else {
            Meteor.call('add.location', locName, function(err, result) {
                if (err) {
                    // console.log("    ERROR: Can't add category: " + err);
                } else {
                    // console.log("    SUCCESS adding category.");
                    $("#locNameInp").val("");
                }
            });
        }
    },
    'click .editLocMgmt' (event) {
        event.preventDefault();
        let locName = $("#locNameInp").val();
        let locEditMode = Session.get("locEditMode");
        let locId = Session.get("locEditId");
        if (locName == null || locName == "") {
            Session.set("locNameMiss", true);
            return;
        } else {
            Meteor.call('edit.location', locId, locName, function(err, result) {
                if (err) {
                    // console.log("    ERROR: Can't edit category: " + err);
                } else {
                    // console.log("    SUCCESS editing category.");
                    $("#locNameInp").val("");
                    Session.set("locEditMode", false);
                }
            });
        }
    },
    'submit #locInputForm' (event) {
        event.preventDefault();
        let locName = $("#locNameInp").val();
        let locEditMode = Session.get("locEditMode");
        let locId = Session.get("locEditId");
        if (locName == null || locName == "") {
            Session.set("locNameMiss", true);
            return;
        } else {
            if (locEditMode == false) {
                Meteor.call('add.location', locName, function(err, result) {
                    if (err) {
                        // console.log("    ERROR: Can't add category: " + err);
                    } else {
                        // console.log("    SUCCESS adding category.");
                        $("#locNameInp").val("");
                    }
                });
            } else {
                Meteor.call('edit.location', locId, locName, function(err, result) {
                    if (err) {
                        // console.log("    ERROR: Can't edit category: " + err);
                    } else {
                        // console.log("    SUCCESS editing category.");
                        $("#locNameInp").val("");
                        Session.set("locEditMode", false);
                    }
                });
            }
        }
    },
    'click .cancelLocMgmt' (event) {
        event.preventDefault();
        $("#locNameInp").val("");
        Session.set("locEditMode", false);
    }
});