import { Categories } from '../../../imports/api/category.js';

Template.catMgmtForm.onCreated(function() {
    this.subscribe("myCategories");
});

Template.catMgmtForm.onRendered(function() {
    Session.set("catNameMiss", false);
    Session.set("catEditMode", false);
});

Template.catMgmtForm.helpers({
    catNameErr: function() {
        return Session.get("catNameMiss");
    },
    catEditMode: function() {
        return Session.get("catEditMode");
    }
});

Template.catMgmtForm.events({
    'click .saveCatMgmt' (event) {
        event.preventDefault();
        let catName = $("#catNameInp").val();
        if (catName == null || catName == "") {
            Session.set("catNameMiss", true);
            return;
        } else {
            Meteor.call('add.category', catName, function(err, result) {
                if (err) {
                    // console.log("    ERROR: Can't add category: " + err);
                } else {
                    // console.log("    SUCCESS adding category.");
                    $("#catNameInp").val("");
                }
            });
        }
    },
    'click .editCatMgmt' (event) {
        event.preventDefault();
        let catName = $("#catNameInp").val();
        let catId = this._id;
        if (catName == null || catName == "") {
            Session.set("catNameMiss", true);
            return;
        } else {
            let catId = Session.get("categoryEditId");
            Meteor.call('edit.category', catId, catName, function(err, result) {
                if (err) {
                    // console.log("    ERROR: Can't edit category: " + err);
                } else {
                    // console.log("    SUCCESS editing category.");
                    $("#catNameInp").val("");
                    Session.set("catEditMode", false);
                }
            });
        }
    },
    'submit #catInputForm' (event) {
        event.preventDefault();
        let catName = $("#catNameInp").val();
        let editMode = Session.get("catEditMode");
        if (catName == null || catName == "") {
            Session.set("catNameMiss", true);
            return;
        } else {
            if (editMode == false) {
                Meteor.call('add.category', catName, function(err, result) {
                    if (err) {
                        // console.log("    ERROR: Can't add category: " + err);
                    } else {
                        // console.log("    SUCCESS adding category.");
                        $("#catNameInp").val("");
                    }
                });
            } else {
                let catId = Session.get("categoryEditId");
                Meteor.call('edit.category', catId, catName, function(err, result) {
                    if (err) {
                        // console.log("    ERROR: Can't edit category: " + err);
                    } else {
                        // console.log("    SUCCESS editing category.");
                        $("#catNameInp").val("");
                        Session.set("catEditMode", false);
                    }
                });
            }
        }
    },
    'click .cancelCatMgmt' (event) {
        event.preventDefault();
        $("#catNameInp").val("");
        Session.set("catEditMode", false);
    }
});