/*jslint         browser : true, continue : true,
  devel  : true, indent  : 2,    maxerr   : 50,
  newcap : true, nomen   : true, plusplus : true,
  regexp : true, sloppy  : true, vars     : false,
  white  : true
*/

/*global $, app */

app.vendorNotification = (function () {
    "use strict";
    //---------------- BEGIN MODULE SCOPE VARIABLES --------------
    var stateMap = {},
        onSave,
        onDelete,
        configModule,
        initModule;
    //----------------- END MODULE SCOPE VARIABLES ---------------

    //------------------- BEGIN EVENT HANDLERS -------------------

    //ONSAVE
    //
    onSave = function (event) {
        event.preventDefault();
        $.gevent.publish("app-clear-error");

        alert("STUB: ONSAVE");
        // //get form data
        // var formData = $("form").serializeArray({
        //     checkboxesAsBools: true
        // });

        // var submitData = app.utilB.objectifyFormDataArray(formData);

        // //is this a new record?
        // if (stateMap.id != "new") {
        //     //put id into the form data
        //     submitData.id = stateMap.id;

        //     app.api.update("vendor-notifications", submitData, function (res) {
        //         if (res.error) {
        //             $.gevent.publish("app-show-error", res.msg);
        //         }
        //     });
        // }
        return false; //prevent default?
    };

    //ONDELETE
    //
    onDelete = function (event) {
        event.preventDefault();
        $.gevent.publish("app-clear-error");
        alert("STUB: onDelete");
        // var r = confirm("Are you sure you want to delete this record?");
        // if (r == true) {
        //     //Delete
        //     app.api.remove("vendor-notifications/" + stateMap.id, function (res) {
        //         if (res.error) {
        //             $.gevent.publish("app-show-error", res.msg);
        //         } else {
        //             //deleted, return to  list
        //             page("#!/vendorNotifications");
        //             return false;
        //         }
        //     });
        // } else {
        //     return false;
        // }
        return false; //prevent default?
    };

    //-------------------- END EVENT HANDLERS --------------------

    //------------------- BEGIN PUBLIC METHODS -------------------

    //CONFIGMODULE
    //
    configModule = function (context) {
        stateMap.context = context.context;
        if (stateMap.context.params.id) {
            stateMap.id = stateMap.context.params.id;
        }
    };

    //INITMODULE
    //
    initModule = function ($container) {
        if (typeof $container === "undefined") {
            $container = $("#app-shell-main-content");
        }

        $container.html(Handlebars.templates["app.vendorNotification"]({}));

        ////app.nav.setContextTitle("Case");

        //Context menu
        app.nav.contextClear();
        app.nav.contextAddLink(
            "vendorNotifications/",
            "Notifications",
            "bell"
        );

        //case 3513 (ironically I can't see it yet)
        document.title = "Vendor notification " + stateMap.id;
        //fetch existing record
        app.api.get("vendor-notifications/" + stateMap.id, function (res) {
            if (res.error) {
                $.gevent.publish("app-show-error", res.msg);
            } else {
                //set the caseid header //<span class="badge badge-secondary">New</span>
                $("#notificationid").html(stateMap.id);
                $("#dtcreated").html(
                    "created " +
                        app.utilB.epochToLocalShortDateTime(res.dtCreated)
                );

                if (res.processed) {
                    $("#processed").html("Processed");
                    $("#dtprocessed").html(
                        "processed " +
                            app.utilB.epochToLocalShortDateTime(res.dtProcessed)
                    );
                } else {
                    $("#processed").html("NOT PROCESSED");
                }

                $("#vendor").html(res.vendor);
                $("#data").html(res.data);

                //fill out form
                app.utilB.formData(res);

                //================
            }
        });
    };

    // RETURN PUBLIC METHODS
    //
    return {
        configModule: configModule,
        initModule: initModule
    };
    //------------------- END PUBLIC METHODS ---------------------
})();
