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

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

Template.catMgmtTbl.onRendered(function() {
    Meteor.setTimeout(function() {
        $('.tooltipped').tooltip();
    }, 150);
});

Template.catMgmtTbl.helpers({
    cats: function() {
        return Categories.find({});
    }
});

Template.catMgmtTbl.events({
    'click .deleteCategory' (event) {
        event.preventDefault();
        Meteor.call('delete.category', this._id, function(err, result) {
            if (err) {
                console.log("    ERROR deleting category: " + err);
            } else {
                console.log("    SUCCESS deleting the category.");
            }
        });
    },
    'click .editCategory' (event) {
        event.preventDefault();
        Session.set("categoryEditId", this._id);
        Session.set("catEditMode", true);
        let catInfo = Categories.findOne({ _id: this._id });
        $("#catNameInp").val(catInfo.categoryName);
    }
});