
// ===============
// Setup Dropdowns
// ===============

$(document).ready(function() {

  var $application = $("#APPLICATION_MNEMONIC").val(),
      $sessionStorage = JSON.parse(sessionStorage.getItem($application) || "{}");
  
  // --------------------------------
  // Open previously opened Accordion
  // --------------------------------

  if ((typeof $sessionStorage.reveal !== "undefined") && ($sessionStorage.reveal !== "")) {
  
    var reveal = $sessionStorage.reveal.split("|");

    reveal.forEach((element) => {
      $("#" + element).collapse("show");
    });
    
  }

  // -------------------
  // Save open Accordion
  // -------------------
  
  $("#dropdown-accordion").on("shown.bs.collapse hidden.bs.collapse", function(e) {

    const open = new Array();
    
    $(".collapse.show").each(function() {
      open.push($(this).attr("id"));
    });
    
    $sessionStorage.reveal = open.join("|");
    sessionStorage.setItem($application, JSON.stringify($sessionStorage));
    
  });
  
  // ----------
  // Add Button
  // ----------
  
  $(".btnAdd").on("click", function() {
    
    $sessionStorage.action              = "Dropdowns.addDropdown";
    $sessionStorage.dropdowntype_serial = $(this).data("dropdowntype-serial");
    $sessionStorage.addDropdown         = "Dropdowns.setupDropdowns";
    
    sessionStorage.setItem($application, JSON.stringify($sessionStorage));
    
    formSubmit($sessionStorage);
    
  });
  
  // -----------
  // Done Button
  // -----------
  
  $(".btnDone").on("click", function() {
    
    formSubmit({ action : "" });
    
  });

  // ----
  // Edit
  // ----

  $(".dropdown-table tbody td:not('.dropdown_default, .dropdown_active')").on("click touch", function(e) {

    e.preventDefault();
    
    $sessionStorage.action          = "Dropdowns.editDropdown";
    $sessionStorage.dropdown_serial = $(this).closest("tr").data("dropdown-serial");
    $sessionStorage.editDropdown    = "Dropdowns.setupDropdowns";
    
    sessionStorage.setItem($application, JSON.stringify($sessionStorage));
    
    formSubmit($sessionStorage);
    
  });  

  // ----------------------------
  // Process the Default Checkbox
  // ----------------------------

  $(".dropdown_default").on("click", function(e) {

    // Get the clicked Checkbox's current value.

    var $this   = $(e.target),
        checked = ($this.closest("tr").find("input[name='dropdown_default']").prop("checked"));
    
    // Uncheck all Checkboxes.
    
    $this.closest("tbody").find("input[name='dropdown_default']").each(function() {
      $(this).prop("checked", false);
    });

    // Toggle the selected Checkbox.
    
    $this.closest("tr").find("input[name='dropdown_default']").prop("checked", !checked);
    
    // Update the Dropdowns Table with the chosen Default.
    
    var dropdown_serial       = $(e.target).closest("tr").data("dropdown-serial"),
        dropdown_dropdowntype = $(e.target).closest("tr").data("dropdown-dropdowntype"),
        dropdown_checked      = !checked;

    formPost({action : "Dropdowns.updateDropdownDefault", dropdown_serial : dropdown_serial, dropdown_dropdowntype : dropdown_dropdowntype, dropdown_checked: dropdown_checked});
    
  });

  // ---------------------------
  // Process the Active Checkbox
  // ---------------------------

  $("input[name='dropdown_active']").on("change", function(e) {

    formPost({ action : "Dropdowns.updateDropdownActive", dropdown_serial : $(this).data("dropdown-serial"), dropdown_checked: $(this).prop("checked") });
    
  });
  
});