import { Products } from '../../../imports/api/products.js';
import { Stores } from '../../../imports/api/stores.js';
import { Categories } from '../../../imports/api/category.js';
import { Locations } from '../../../imports/api/location.js';

Template.prodMgmtForm.onCreated(function() {
    this.subscribe("myProducts");
    this.subscribe("storeInfo");
    this.subscribe("myCategories");
    this.subscribe("myLocations");
});

Template.prodMgmtForm.onRendered(function() {
    Meteor.setTimeout(function() {
        $('select').formSelect();
    }, 200);
    $('select').formSelect();
    $('.modal').modal();
    Session.set("prodEditMode", false);
});

Template.prodMgmtForm.helpers({
    stores: function() {
        return Stores.find({}); 
    },
    prodNameErr: function() {
        return Session.get("prodNameRed");
    },
    category: function() {
        return Categories.find({});
    },
    locations: function() {
        return Locations.find({});
    },
    prodEditMode: function() {
        return Session.get("prodEditMode");
    },
});

Template.prodMgmtForm.events({
    'click .saveProdMgmt' (event) {
        event.preventDefault();
        let name = $("#prodName").val();
        let cat = $("#prodCategory").val();
        let store = $("#prodStore").val();
        let location = $("#prodLocation").val();

        if (cat == null) {
            cat = "";
        }

        if (location == null) {
            location = "";
        }

        if (store == null) {
            store = "";
        }

        if (name == "" || name == null) {
            Session.set("prodNameRed", true);
            return;
        } else {
            Meteor.call('add.product', name, cat, store, location, function(err, result) {
                if (err) {
                    // console.log("     ERROR: can't add product: " + err);
                } else {
                    // console.log("    SUCCESS adding product.");
                    $("#prodName").val("");
                    $("#prodCategory").val("");
                    $("#prodStore").val("");
                    $("#prodLocation").val("");
                    $('select').formSelect();
                }
            });
        }
    },
    'click .editProdMgmt' (event) {
        event.preventDefault();
        let name = $("#prodName").val();
        let cat = $("#prodCategory").val();
        let store = $("#prodStore").val();
        let location = $("#prodLocation").val();
        let prodId = Session.get("prodEditId");

        if (cat == null) {
            cat = "";
        }

        if (location == null) {
            location = "";
        }

        if (store == null) {
            store = "";
        }

        if (name == "" || name == null) {
            Session.set("prodNameRed", true);
            return;
        } else {
            Meteor.call('edit.product', prodId, name, cat, store, location, function(err, result) {
                if (err) {
                    // console.log("     ERROR: can't add product: " + err);
                } else {
                    // console.log("    SUCCESS adding product.");
                    $("#prodName").val("");
                    $("#prodCategory").val("");
                    $("#prodStore").val("");
                    $("#prodLocation").val("");
                    $('select').formSelect();
                }
            });
        }
    },
    'keypress form.prodInputForm' (event) {
        // event.preventDefault();
        if (event.which === 13) {
            let name = $("#prodName").val();
            let cat = $("#prodCategory").val();
            let store = $("#prodStore").val();
            let location = $("#prodLocation").val();
            let prodId = Session.get("prodEditId");
            let prodEditMode = Session.get("prodEditMode");
            console.log("    ----    got the submit event for products.");
            if (cat == null) {
                cat = "";
            }

            if (location == null) {
                location = "";
            }

            if (store == null) {
                store = "";
            }

            if (name == "" || name == null) {
                Session.set("prodNameRed", true);
                return;
            } else {
                if (prodEditMode == true) {
                    Meteor.call('edit.product', prodId, name, cat, store, location, function(err, result) {
                        if (err) {
                            // console.log("     ERROR: can't add product: " + err);
                        } else {
                            // console.log("    SUCCESS adding product.");
                            $("#prodName").val("");
                            $("#prodCategory").val("");
                            $("#prodStore").val("");
                            $("#prodLocation").val("");
                            $('select').formSelect();
                        }
                    });
                } else {
                    Meteor.call('add.product', name, cat, store, location, function(err, result) {
                        if (err) {
                            // console.log("     ERROR: can't add product: " + err);
                        } else {
                            // console.log("    SUCCESS adding product.");
                            $("#prodName").val("");
                            $("#prodCategory").val("");
                            $("#prodStore").val("");
                            $("#prodLocation").val("");
                            $('select').formSelect();
                        }
                    });
                }
            }
        }
    },
    'click .cancelProdMgmt' (event) {
        event.preventDefault();
        $("#prodName").val("");
        $("#prodCategory").val("");
        $("#prodStore").val("");
        $("#prodLocation").val("");
        $('select').formSelect();
    },
    'change #prodStore' (event) {
        event.preventDefault();
        let val = $("#prodStore").val();
        
        if (val == "addNewStore") {
            $("#prodStore").val("");
            // open a modal to enter store information.
            $('#modalStore').modal('open');
        }
    },
    'change #prodLocation' (event) {
        event.preventDefault();
        let val = $("#prodLocation").val();
        
        if (val == "addNewLocation") {
            $("#prodLocation").val("");
            // open a modal to enter store information.
            $('#modalLocation').modal('open');
        }
    },
    'change #prodCategory' (event) {
        event.preventDefault();
        let val = $("#prodCategory").val();
        
        if (val == "addNewCat") {
            $("#prodCategory").val("");
            // open a modal to enter store information.
            $('#modalCategory').modal('open');
        }
    },
    'click .modal-close' (event) {
        $('select').formSelect();
    }
});