import { Products } from '../../../imports/api/products.js';

Template.prodMgmtTbl.onCreated(function() {
    this.subscribe("myProducts");
});

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

Template.prodMgmtTbl.helpers({
    products: function() {
        return Products.find({});
    },
});

Template.prodMgmtTbl.events({
    'click .deleteProduct' (event) {
        event.preventDefault();
        Meteor.call('delete.product', this._id, function(err, result) {
            if (err) {
                console.log("    ERROR deleting product: " + err);
            } else {
                console.log("    SUCCESS deleting the product.");
            }
        });
    },
    'click .editProduct' (event) {
        event.preventDefault();
        Session.set("prodEditMode", true);
        Session.set("prodEditId", this._id);
        let prodInfo = Products.findOne({ _id: this._id });
        $("#prodName").val(prodInfo.prodName);
        $("#prodCategory").val(prodInfo.prodCategory);
        $("#prodLocation").val(prodInfo.prodLocation);
        $("#prodStore").val(prodInfo.prodStore);
        $('select').formSelect();
    }
});