/*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.ravLicenses = (function () {
  "use strict";
  //---------------- BEGIN MODULE SCOPE VARIABLES --------------
  var stateMap = {},
    configModule,
    initModule;
  //----------------- END MODULE SCOPE VARIABLES ---------------

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

  //-------------------- END UTILITY 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; //siteid
    }
  };

  //INITMODULE
  //
  initModule = function ($container) {
    if (typeof $container === "undefined") {
      $container = $("#app-shell-main-content");
    }
    $container.html(Handlebars.templates["app.ravLicenses"]({}));

    //case 3513
    document.title = "Licenses";

    //===================
    //Get licenses
    app.api.get("rvl/list/" + stateMap.id, function (res) {
      if (res.error) {
        $.gevent.publish("app-show-error", res.msg);
      } else {
        var $appList = $("#rf-list");

        $appList.append("<ul >");
        let first = true;
        $.each(res, function (i, obj) {
          let itemClass = "alert alert-success";
          if (!first) {
            itemClass = "alert alert-secondary";
          }
          $appList.append(
            "<li class='my-5'>" +
              "<div class='" +
              itemClass +
              "' role='alert'>"+
              "<h4>"+
              app.utilB.epochToLocalShortDateTime(obj.dtCreated)+
              "</h4>"+
              "<pre>" +
              obj.key +
              "</pre></div>" +
              "</li>"
          );
          first = false;
        });

        $appList.append("</ul>");
      }
    });
    //=========/licenses==============

    app.nav.contextClear(); ///ravLicense/122
    app.nav.contextAddLink("ravLicense/" + stateMap.id+ "/" + stateMap.context.params.cust_id, "New", "plus");
    app.nav.contextAddLink(
      "customerSiteEdit/" + stateMap.id + "/" + stateMap.context.params.cust_id,
      "Site",
      "city"
    );
  };

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