/*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.purchases = (function () {
    'use strict';
    //---------------- BEGIN MODULE SCOPE VARIABLES --------------
    var

        configMap = {
            //main_html: '',

            settable_map: {}
        },
        stateMap = {
            $append_target: null
        },
        onSubmit, loadList, configModule, initModule;
    //----------------- END MODULE SCOPE VARIABLES ---------------

    //------------------- BEGIN UTILITY METHODS ------------------
    loadList = function () {
        //----
        //fetch                   
        app.api.get('site/' + stateMap.id + '/purchases', function (res) {
            if (res.error) {
                $.gevent.publish('app-show-error',res.msg);
            } else {
                //get the list ul
                var $appList = $('#rf-list');
                $appList.empty();
                $.each(res, function (i, obj) {

                    if (obj.cancelDate) {
                        $appList.append("<li class='rfc-list-item-inactive'><a href=\"#!/purchaseEdit/" + obj.id + "/" + stateMap.id + "\">" +
                            // app.utilB.genListColumn(app.utilB.epochToShortDate(obj.purchaseDate)) +
                            // '&nbsp;' +
                            "<span class='text-muted'>"+
                            app.utilB.genListColumn(obj.name) +
                            "&nbsp;cancelled&nbsp;" + 
                            app.utilB.genListColumn(app.utilB.epochToShortDate(obj.cancelDate)) +
                            "</span>"+
                            "</a></li>");
                    } else {
                        $appList.append("<li><a href=\"#!/purchaseEdit/" + obj.id + "/" + stateMap.id + "\">" +
                            // app.utilB.genListColumn(app.utilB.epochToShortDate(obj.purchaseDate)) +
                            // '&nbsp;' +
                            app.utilB.genListColumn(obj.name) +
                            "&nbsp;expires&nbsp;" + 
                            app.utilB.genListColumn(app.utilB.epochToShortDate(obj.expireDate)) +
                            "</a></li>");

                            //
                    }




                });
            }
        });
        //------
    }
    //-------------------- END UTILITY METHODS -------------------

    //--------------------- BEGIN DOM METHODS --------------------




    // Begin private DOM methods 



    // End private DOM methods 
    //---------------------- END DOM METHODS ---------------------

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




    //-------------------- 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;
        }
    };

    // Begin public method /initModule/
    // Example    : app.customer.initModule( $('#div_id') );
    // Purpose    : directs the module to being offering features
    // Arguments  : $container - container to use
    // Action     : Provides interface
    // Returns    : none
    // Throws     : none
    //
    initModule = function ($container) {
        if (typeof $container === 'undefined') {
            $container = $('#app-shell-main-content');
        }
        stateMap.sortOrder = 'd';
        $container.html(Handlebars.templates['app.purchases']({}));

        if (!stateMap.id) {
            throw ('app.purchases.js::initModule - There is no stateMap.id!');
        }

        app.api.get('site/' + stateMap.id, function (res) {
            if (res.error) {
                $.gevent.publish('app-show-error',res.msg);
            } else {

                //Context menu
                app.nav.contextClear();
                app.nav.contextAddLink('purchaseEdit/new/' + stateMap.id, "New", "plus");
                app.nav.contextAddLink("customerEdit/" + res.customerId, "Customer", "account");
                app.nav.contextAddLink("customerSiteEdit/" + stateMap.id + '/' + res.customerId, "Site", "city");
                if (stateMap.id) {
                    loadList();
                }
            }
        });
    };
    // End public method /initModule/



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