/*
     $Revision$
     $Id$

     File:        mld_show_melding.js
*/
function mld_print()
{
   FcltMgr.windowopen('./mld_opdr_xml.asp?mld_key=' + mld_key);
}

function mld_delen()
{
    FcltMgr.openModalDetail('./mld_opdr_xml.asp?share=1&mld_key=' + mld_key);
}

function mld_timereg()
{
  var datefrom = new Date();
  datefrom.setDate(datefrom.getDate() - datefrom.getDay() + 1); // this week's monday
  var url = "appl/mld/opdr_edit_uren.asp?mld_key=" + mld_key + "&datefrom=" + datefrom.valueOf();
  FcltMgr.openDetail(url, L("lcl_mld_ord_weeklijst"));
}

function mld_change(urole)
{
  if (FcltMgr.startEdit(window))
    window.location.href = "mld_edit_melding.asp?urole=" + urole + "&mld_key=" + mld_key + "&act_key=" + act_key + "&embedded=1";
}

// Reload zonder verynew=1. Geen parent.FcltMgr.reload() omdat dan verynew=1 wel meegegeven wordt.
function mld_melding_reload(mld_key)
{
  parent.location.href = "mld_melding.asp?urole=" + urole + "&mld_key=" + mld_key + "&embedded=1";
}

function fn_mld_melding_reload(mld_key)
{
  return function () { mld_melding_reload(mld_key) };
}

function fn_mld_melding_forwarded(mld_key)
{
  return function () { mld_melding_forwarded(mld_key) };
}

function mld_melding_forwarded(mld_key)
{
  var data = { req_info: "show_after_fwd"};
             //  mld_key: mld_key  deze staat al in proteced_mld_info_ajax
  $.getJSON(protected_mld_info_ajax,
            data,
            mld_reload_or_close);
}

function mld_melding_ingestemd() {
  var data = { req_info: "show_after_instem"};
  $.getJSON(protected_mld_info_ajax,
            data,
            mld_reload_or_close);
}

function mld_melding_rejected() {
  var data = { req_info: "show_after_reject"};
  $.getJSON(protected_mld_info_ajax,
            data,
            mld_reload_or_close);
}

function mld_reload_or_close(json, textStatus)
{
  if (json)
  {
    if (json.show)
      mld_melding_reload(mld_key);
    else
    {
      FcltMgr.topmanager().window.$.toast({ text: json.toaster, icon: "success", position : 'top-center', loaderBg: '#fff'});
      parent.FcltMgr.closeDetail(parent.window, {close: true});
    }
  }
}

function mld_accept(doublecheck, autoConfirm)
{
  // Indien verynew (uitgezonderd: setting doublecheck = alleen bij accepteren (2))
  // dan is doublecheck al uitgevoerd en de eventuele pupup met soortgelijke meldingen al geweest.
  if (!verynew || doublecheck == 2)
    $.get("../mld/mld_doublecheck.asp"
           + "?mld_key=" + mld_key
           + "&urole=" + urole
           + "&autoConfirm=" + (autoConfirm ? 1 : 0)
           + "&reloadmld=1",
           doublecheckCallback,
           "json");
  else
    doAccept( { mld_key     : mld_key,
                prefix      : mldPrefix,
                autoConfirm : autoConfirm
              },
              function(data)
              {
                if (data.edit)
                  window.location.href = "mld_edit_melding.asp?urole=" + urole + "&mld_key=" + data.key + "&embedded=1";
                else
                {
                  FcltMgr.closeDetail(window, {close: true});
                  mld_melding_reload(mld_key);
                }
              }
             );
}

function mld_forward(urole, fwdType, behandelteam_key)
{
  if (emptyRequiredFields)
  {
    FcltMgr.alert(L("lcl_shared_validator_missing"));
    // Ga naar de edit mode.
    if (FcltMgr.startEdit(window))
      window.location.href = "mld_edit_melding.asp?urole=" + urole + "&mld_key=" + mld_key + "&embedded=1";
  }
  else
    FcltMgr.openModalDetail("../mld/mld_forward.asp?mld_key=" + mld_key
                                                + "&urole=" + urole
                                                + "&fwdType=" + fwdType
                                                + (behandelteam_key > 0? "&behandelteam_key=" + behandelteam_key : ""),
                            (fwdType == 1? L("lcl_mld_forward_disc") : L("lcl_mld_forward_team")),
                            { callback: fn_mld_melding_forwarded(mld_key) });
}

function mld_fix_sla()
{
  FcltMgr.openModalDetail("../mld/mld_fix_sla.asp?mld_key=" + mld_key + "&urole=" + urole, L("lcl_complain"), { callback: fn_mld_melding_reload(mld_key) });
}

function mld_reject()
{
  if (mld_allow_parentmelding)
    $.get("../mld/mld_doublecheck.asp"
          + "?mld_key=" + mld_key
          + "&urole=" + urole
          + "&reloadmld=1"
          + "&reject=1",
           doublecheckCallback,
           "json");
  else
  {
    url = "../mld/mld_reject.asp?urole=" + urole + "&mld_key=" + mld_key
    FcltMgr.openModalDetail(url,
                            L("lcl_mld_complain") + ' ' + mldPrefix + mld_key,
                            { callback: FcltCallbackAndThen(mld_melding_rejected) }
                           );
  }
}

function mld_laatgaan()
{
  // Noot: de status is na invoeren default op 2, dus hoef ik eigenlijk niets te doen.
  FcltMgr.closeDetail(window, {close: true});
}

function mld_doorzet()
{
  var data = { mld_key: mld_key };
  protectRequest.dataToken(data);
  $.post("mld_doorzet.asp",
         data,
         FcltCallbackAndThen(fn_mld_melding_reload(mld_key)),
         "json");
}

function mld_terugzet()
{
  var data = { mld_key: mld_key
             , terug: 1
             };
  protectRequest.dataToken(data);
  $.post("mld_doorzet.asp",
         data,
         FcltCallbackAndThen(fn_mld_melding_reload(mld_key)),
         "json");
}

// Verynew is de frontoffice mode van meldingen
function mld_close(verynew)
{
  var url = "../mld/mld_close_confirm.asp?mld_key=" + mld_key + "&verynew=" + (verynew? 1 : 0);
  FcltMgr.openModalDetail(url,
                          L("lcl_mld_final_close"),
                          {
                            callback: function(data)
                                      {
                                        if (data.edit)
                                          window.location.href = "mld_edit_melding.asp?urole=" + urole + "&mld_key=" + data.key + "&embedded=1";
                                        else
                                        {
                                          FcltMgr.closeDetail(window, {close: true});
                                          mld_melding_reload(mld_key);
                                        }
                                      }
                          });
}

function mld_reopen(verynew)
{
  var url = "../mld/mld_reopen.asp?mld_key=" + mld_key;
  FcltMgr.openModalDetail(url,
                          L("lcl_mld_reopen"),
                          {
                            callback: function(data)
                                      {
                                        FcltMgr.closeDetail(window, {close: true});
                                        mld_melding_reload(mld_key);
                                      }
                          });
}

function mld_unlink()
{
  FcltMgr.confirm(L("lcl_mld_unlink"), function() {
      var data = {
          mld_key: mld_key,
          parent_key: -1
      }
      protectRequest.dataToken(data);
      $.post("mld_link.asp",
          data,
          FcltCallbackAndThen(fn_mld_melding_reload(mld_key)),
          "json");
  });
}

function preSaveLink($dialog)
{ // Ik moet eerst controleren of de melding al is "Afgemeld". Dan moet er eerst een melding gegeven worden.
  var mldnum = $dialog.find("#mldnum").val();
  var data = { req_info: "mld_status"}; // variabele protected_mld_info_ajax bevat de url + mld_key
  $.getJSON(protected_mld_info_ajax,
            data,
            function (json) { saveLink(json, mldnum) });
}

function saveLink(json, mldnum)
{
  if (json)
  {
    var label = "";

    // Bij Afgewezen(1), Afgemeld(5) of  Administratief voltooid(6) krijg je een melding.
    if (json.mld_status == 1)
        label = L("lcl_mld_will_be_rejected");
    else if (json.mld_status == 5 || json.mld_status == 6)
        label = L("lcl_mld_will_be_closed");

		var autoConf = label == ""; // no label, no confirm prompt

    FcltMgr.confirm(label, { autoconfirm: autoConf }, function() {

        if (!mldnum)
            return;

        var data = {
            mld_key: mld_key,
            parent_key: mldnum
        };
        protectRequest.dataToken(data);
        $.post("mld_link.asp",
            data,
            FcltCallbackAndThen(fn_mld_melding_reload(mld_key)),
            "json");
    });
  }
  else
    FcltMgr.alert(L("lcl_linkedparent_notfound"));
}

function createLink()
{
  if (!evt) var evt = window.event; // PNBR#24239 evt is hier de globale var uit iface.js/iface.button.click
                                    // Lelijk maar werkend

  if (mld_allow_parentmelding)
  {
    params = {oktekst: L("lcl_mld_koppelen"),
              title: L("lcl_mld_koppelen"),
              ishtmlsafe: true,
              fncallback: preSaveLink,
              buttons: [{ text: L("lcl_cancel"), icon: "fal fa-fw fa-undo", class: "alertCancel" }]
    };
    var page_content = L("lcl_mld_link_to") + ': <input type="text" id="mldnum" class="fld" placeholder="12345">';
    FcltMgr.mydialog(page_content, params);
  }
}

function mld_herplan()
{
  var url = "../mld/mld_reschedule.asp?mld_key=" + mld_key;
  FcltMgr.openModalDetail(url,
                          L("lcl_reschedule"),
                          {
                            callback: function(data)
                                      {
                                        FcltMgr.closeDetail(window, {close: true});
                                        mld_melding_reload(mld_key);
                                      }
                          });
}

function mld_publish() {
    var url = "appl/msg/fac_nieuws.asp?nieuws_key=-1&mld_key=" + mld_key;
    FcltMgr.openDetail(url, L("lcl_add"));
}

function mld_orders(doublecheck)
{
  // Maakt -direct na invoer melding- een nieuwe order aan in huidige tab (let op: autoswitch naar bo-rechten?)
  // Moeten we nu ook eerst wel even accepteren!
  if (!verynew || doublecheck == 2)
  {
    $.get("../mld/mld_doublecheck.asp"
          + "?mld_key=" + mld_key
          + "&urole=" + urole
          + "&reloadmld=2", // Voeg aan een nieuwe melding ook gelijk een opdracht toe.
           doublecheckCallback,
           "json");
  }
  else
  {
    var data = { mld_key: mld_key };
    protectRequest.dataToken(data);
    $.post("mld_accept_save.asp",
           data,
           FcltCallbackAndThen(function ()
           {
             FcltMgr.setTitle(L("lcl_mld_new_order"));
             FcltMgr.closeDetail(window, { redirect: "../mld/mld_opdr.asp?urole=bo&mld_key=" + mld_key });
           }),
           "json");
  }
}

function mld_bestel()
{
  // Moeten we nu ook eerst wel even accepteren!
  var data = { mld_key: mld_key };
  protectRequest.dataToken(data);
  $.post("mld_accept_save.asp",
         data,
         FcltCallbackAndThen(function ()
         {
           FcltMgr.setTitle(L("lcl_mld_new_bestelling"));
           FcltMgr.closeDetail(window, { redirect: "../bes/bes_bestelling.asp?urole=fo&mld_key=" + mld_key });
         }),
         "json");
}

function mld_copy()
{
   FcltMgr.openDetail("appl/mld/mld_melding.asp?urole=" + urole + "&mld_key=" + mld_key + "&mld_copy=1",  L("lcl_mld_copy") + " " + L("lcl_shared_complain").toLowerCase());
}

function mld_instem()
{
  FcltMgr.confirm(L("lcl_mld_ask_instem").format(mldPrefix + mld_key), function() {
      var data = {
          mld_key: mld_key
      };
      protectRequest.dataToken(data);
      $.post("mld_instemmen.asp",
          data,
          FcltCallbackAndThen(mld_melding_ingestemd),
          "json");
  });
}
function mld_afrond()
{
  FcltMgr.confirm(L("lcl_mld_ask_afrond").format(mldPrefix + mld_key), function() {
      var data = {
          mld_key: mld_key
      };
      protectRequest.dataToken(data);
      $.post("mld_afronden.asp",
          data,
          FcltCallbackAndThenAlways(mld_afrond_callback),
          "json");
  });
}

function mld_afrond_callback(json)
{
  if (json.success)
    mld_melding_reload(/* global */ mld_key);
  else if (json.error == 1) // 1 = verplicht kenmerk ontbreekt
    mld_change(/* global */ urole);
}

function mld_verwerk()
{
  FcltMgr.confirm(L("lcl_mld_ask_verwerk").format(mldPrefix + mld_key), function() {
      var data = {
          mld_key: mld_key
      };
      protectRequest.dataToken(data);
      $.post("mld_verwerken.asp",
          data,
          FcltCallbackAndThen(fn_mld_melding_reload(mld_key)),
          "json");
  });
}

function mld_add2faq()
{
   FcltMgr.openModalDetail("../mld/mld_2faq.asp?mld_key=" + mld_key + "&urole=" + urole,
                                            //+ "&faq_question=" + escape(data.question)
                                            //+ "&faq_answer=" + escape(data.answer)
                                            //+ "&faq_perslid_key=" + escape(data.author),
                           L("lcl_mld_addtofaq"));
}

function mld_cancelworkflow()
{
    var url = "../mld/mld_cancelworkflow.asp?mld_key="+ mld_key;
    FcltMgr.openModalDetail(url,
                          L("lcl_mld_cancel_workflow"),
                          {
                            callback: function()
                                {
                                    FcltMgr.closeDetail(window, {close: true});
                                    mld_melding_reload(mld_key);
                                }
                          });

}