/*
     $Revision$
     $Id$

     File:        notes_m.js
*/

function noteOptionToggled(type)
{
  var $this = $(this);
  var $wrapper = $this.closest(".note-option-group-m");
  var subject_role = $wrapper.attr("data-toggle-subject-role");
  var state = $this.prop("checked");
  var actiecode = $this.attr("actiecode");
  var module = $this.attr("module");
  var mld_note_hidden_action2FE = $this.attr("mld_note_hidden_action2FE");

  /*  Types:
      1  = Zichtbaarheid
      2  = Notificeren
      4  = Melders van kinder-meldingen notificeren
      8  = Reactie vragen / Actie !
  */
  var _toggle = (type, show) => {
    var $checkbox = $wrapper.find("[data-toggle-type=" + type + "]");
    if (!show) // Dan vullen we de default in
    {
        var defVal = $checkbox.attr("checked") == "checked";
        $checkbox.prop("checked", defVal).checkboxradio("refresh");
    }
    $checkbox.closest(".ui-checkbox").toggle(show);
  }

  switch (type) {
      case 1: _toggle(2, state);
              _toggle(4, state);
              if (mld_note_hidden_action2FE == 0) {
                _toggle(8, state);
              }
              if (subject_role == 1) {
                var $checkbox_ext = $("[data-toggle-subject-role=0]").find("[data-toggle-type=2]");
                if (!state) {
                  $checkbox_ext.prop("checked", false).checkboxradio("refresh");
                }
                $checkbox_ext.closest(".ui-checkbox").toggle(state);
              }
              break;
      case 2: if (!state) {
                $wrapper.find("[data-toggle-type=8]").prop("checked", false).checkboxradio("refresh");
              }
      case 4: break;
      case 8: if (module !== "MLD" || actiecode & 1) {
                if (state) { // Zet notificeren aan als je
                  $wrapper.find("[data-toggle-type=2], [data-toggle-type='4']").prop("checked", true).checkboxradio("refresh");
                }
              }
              break;
      default: console.error("UNSUPPORTED_NOTE_OPTION_TOGGLE_TYPE");
  }
}

$(_ => {
  load_note_draft();
  if (draft_notes_enabled) {
    $("#note").on("input", e => {
      draft_note(window.module, window.module_key, $("#note").val());
    });
  }
})