import { TaskItems } from '../../imports/api/tasks.js';

Template.myTasksForm.onCreated(function() {
    
});

Template.myTasksForm.onRendered(function() {  
    $('.datepicker').datepicker();
    Session.set("hideComplete", false);
    Session.set("onlyToday", false);
});

Template.myTasksForm.helpers({

});

Template.myTasksForm.events({
    'click .addNewTask' (event) {
        event.preventDefault();
        let taskName = $("#myTaskName").val();
        let taskDate = $("#myTaskDate").val();
        let actDate = new Date(taskDate);

        if (taskName == null || taskName == "" || taskDate == "" || taskDate == null) {
            taskEntryErr = true;
            showSnackbar("Task Name and Date are Required!", "red");
            return;
        } else {
            Meteor.call("add.task", taskName, "self", "selfId", taskDate, actDate, function(err, result) {
                if (err) {
                    console.log("    ERROR adding task for self: " + err);
                    showSnackbar("ERROR adding task for self!", "red");
                } else {
                    console.log("    SUCCESS adding task for self.");
                    $("#myTaskName").val("");
                    $("#myTaskDate").val("");
                }
            });
        }
    },
    'click #hideMyCompletedTasks' (event) {
        let hide = $("#hideMyCompletedTasks").prop('checked');
        if (hide == true) {
            Session.set("hideComplete", true);
        } else {
            Session.set("hideComplete", false);
        }
    },
    'click #onlyTodaysTasks' (event) {
        let onlyToday = $("#onlyTodaysTasks").prop('checked');
        if (onlyToday == true) {
            Session.set("onlyToday", true);
        } else {
            Session.set("onlyToday", false);
        }

    }
});