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

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

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

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

    onGenerate = function (event) {

        event.preventDefault();
        $.gevent.publish('app-clear-error');
        //get form data
        var formData = $("form").serializeArray({
            checkboxesAsBools: true
        });

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

        //Now convert this specially for this method into a clean object to send with only what is required
        var submitData = {};
        submitData.products = [];
        var l = formData.length;
        for (var i = 0; i < l; i++) {
            var fname = formData[i].name;
            var fvalue = formData[i].value;

            if (fname == 'ckIncidental') {
                submitData.ckIncidental =  (fvalue == 'true');
            } else if (fname == 'ckNoContact') {
                submitData.ckNoContact = (fvalue == 'true');
            } else if (fname == 'csvdata') {
                ;//do nothing
            } else {
                //it's a product key
                if (fvalue == 'true') {
                    submitData.products.push(fname);
                }
            }
        }


        app.api.create('report/emailsforproductcodes',submitData, function (res) {
            if (res.error) {
                $.gevent.publish('app-show-error',res.msg);
            } else {
                $('#csvdata').val(res);
                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.reportDataProdEmail']({}));

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

                $.each(res, function (i, obj) {

                    var pcode = obj.productCode;
                    var badCode = (!pcode);

                    //-----------                       
                    $('<input />', {
                        type: 'checkbox',
                        id: 'id' + i,
                        name: obj.productCode,
                        disabled: badCode
                    }).appendTo("#cbdiv");

                    $('#cbdiv').append('<label for="' + 'id' + i + '">&nbsp;' + (badCode ? '[NO PRODUCT CODE ENTERED]' : obj.productCode) + '&nbsp;' + obj.name + '</label>');
                    $('#cbdiv').append('<br/>');

                    //--------------
                });
            }
        });

        //set title 
        var title="Email addresses by product code";
        //app.nav.setContextTitle(title);

         //Context menu           
        app.nav.contextClear();
        app.nav.contextAddButton('btn-generate', 'Build list', 'build', onGenerate);       
        app.nav.contextAddLink("reportData/", "Reports", "book-open-variant");

    };


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