/*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.subnotify = (function () {

    'use strict';
    //---------------- BEGIN MODULE SCOPE VARIABLES --------------
    var
        stateMap = {},
        configModule,
        initModule,
        onSend;
    //----------------- END MODULE SCOPE VARIABLES ---------------

    //------------------- BEGIN UTILITY METHODS ------------------
    //-------------------- END UTILITY METHODS -------------------

    //------------------- BEGIN EVENT HANDLERS -------------------
    /////////////////////////
    //SEND (create draft) of renewal warning notification email
    //
    onSend = function (event) {
        event.preventDefault();

        //Gather purchase id's
        var s = $(this).data("purchaseidlist");

        if(s==undefined || s==null){
            return false;
        }
        
        //Convert string of comma separate numeric values to numeric array
        var ids = new Array();
        if (typeof s == 'string') {
            ids = s.split(",");
            var arrayLength = ids.length;
            for (var i = 0; i < arrayLength; i++) {
                ids[i] = parseInt(ids[i], 10);
            }
        } else {
            //it's a number
            ids[0] = s;
        }



        //post to a route that will make the draft emails then tag the purchase as notified
        app.api.create('subscription/sendnotify', ids, function (res) {
            if (res.error) {
                $.gevent.publish('app-show-error',res.msg);
            } else {
                page('#!/subnotify');
            }
        });


        return false; //prevent default
    };



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

    //------------------- BEGIN PUBLIC METHODS -------------------
    //CONFIGMODULE
    //
    configModule = function (context) {
        stateMap.context = context.context;
    };

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

        $container.html(Handlebars.templates['app.subnotify']({}));

        //fetch data
        app.api.get('subscription/notifylist', function (res) {
            if (res.error) {
                $.gevent.publish('app-show-error',res.msg);
            } else {

                //get the list ul
                var $appList = $('#rf-list');
                $appList.empty();
                var displayedItems = 0;
                $.each(res, function (i, obj) {
                    $appList.append("<li>" +
                        app.utilB.genListColumnNoLink(obj.customer + ' -> ') +
                        '&nbsp;' +
                        app.utilB.genListColumnNoLink(obj.purchasenames) +
                        '&nbsp;' +
                        app.utilB.genListColumnNoLink("<span class='RFSEND mdi mdi-cube-send btn btn-sm btn-outline-primary' data-purchaseidlist='" + obj.purchaseidlist.toString() + "'>Send</span>") +
                        "</li>");

                        displayedItems++;
                });

                if(displayedItems==0){
                    $appList.append('<li>NO RENEWALS IMMINENT - ' + moment().format('YYYY-MM-DD LT') + '</li>');
                }


                //event handler for buttons added dynamically
                $('.RFSEND').bind('click', onSend);

            }
        });

         //Context menu           
        app.nav.contextClear();       
        app.nav.contextAddLink("subscription/", "Subscriptions", "basket");
       
    };


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