/*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.licenseView = (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');
        
        var isFetched=$('#fetched').prop('checked')

       // var submitData = { isFetched: isFetched };
        app.api.putAction('license/fetched/' + stateMap.id + "/" + isFetched,  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');
        var r = confirm("Are you sure you want to delete this record?");
        if (r == true) {
            app.api.remove('license/' + stateMap.id, function (res) {
                if (res.error) {
                    $.gevent.publish('app-show-error', res.msg);
                } else {
                    page('#!/licenses');
                    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.licenseView']({}));

        document.title = 'License ';

        //fetch existing record
        app.api.get('license/' + stateMap.id, function (res) {
            if (res.error) {
                $.gevent.publish('app-show-error', res.msg);
            } else {
                //fill out form
                app.utilB.formData(res);
            }
        });
        //Context menu           
        app.nav.contextClear();
        app.nav.contextAddLink("licenses/", "List", "");



        // bind actions      
        $('#btn-save').bind('click', onSave);
        $('#btn-delete').bind('click', onDelete);
    };


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