<%@language = "javascript" %>
<% /*
     $Revision$
     $Id$

    File:               mld_forward.asp
    Description:        Doorsturen van een opdracht naar een andere vakgroep
    Parameters:         mld_melding_key

    Context:
    Note:               Merk op dat je ook naar vakgroepen mag sturen waar je normaal
                        geen rechten voor hebt!
                        Als S("mld_forward_all") == 0 dan alleen naar dezelfde srtdiscipline

*/ %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/disciplineselector.inc" -->
<!-- #include file="../Shared/stdmeldingselector.inc" -->
<!-- #include file="mld.inc" -->
<%
FCLTHeader.Requires({ plugins: ["jQuery"],
                      js: [] })
%>
<html>
  <head>
<%  FCLTHeader.Generate(); %>
<%
    var mld_key = getQParamInt('mld_key');
    var opmerk = getFParam("opmerk", "");
    var this_mld = mld.func_enabled_melding(mld_key);
    user.auth_required_or_abort(this_mld.canForward || this_mld.canFixMelding);

    var mld_melding = mld.mld_melding_info(mld_key);
    lcl.set_dialect(mld_melding.srtdisc, "MLD_SRTDISCIPLINE_KEY");


    // TODO: Deze gegevens kunnen nu ook opgehaald worden uit mld_melding die bovenin al is opgehaald.
    sql = "SELECT sm.mld_ins_discipline_key"
        + "     , sm.mld_stdmelding_key"
        + "     , mm.mld_melding_opmerking"
        + "     , " + lcl.xsqla('md.ins_discipline_omschrijving', 'md.ins_discipline_key')
        + "     , sd.ins_srtdiscipline_prefix"
        + "     , md.ins_srtdiscipline_key"
        + "  FROM mld_melding mm"
        + "     , mld_stdmelding sm"
        + "     , mld_discipline md"
        + "     , ins_srtdiscipline sd"
        + " WHERE mld_melding_key = " + mld_key
        + "   AND mm.mld_stdmelding_key = sm.mld_stdmelding_key"
        + "   AND sm.mld_ins_discipline_key = md.ins_discipline_key"
        + "   AND md.ins_srtdiscipline_key = sd.ins_srtdiscipline_key";

    oRs = Oracle.Execute(sql);
    mld_disc = oRs("mld_ins_discipline_key").Value;
    srt_disc = oRs("ins_srtdiscipline_key").Value;
    mld_stdm = oRs("mld_stdmelding_key").Value;
    mld_opmr = oRs("mld_melding_opmerking").Value;
    prefix = oRs("ins_srtdiscipline_prefix").Value || "";
    ins_discipline_omschrijving = oRs("ins_discipline_omschrijving").Value;
    oRs.close();
%>

    <script>
      function mld_forward()
      {
        document.activeElement.blur(); // trigger laatste onChanges

        // Standaard checks op verplichte velden, datum formaat, numeriek en float formaat, currency formaat.
        if (!validateForm("u2"))
          return false;

        $.post($("form[name=u2]")[0].action,
               $("[name=u2]").serialize(),
               mld_forward_callback,
               "json");
        return true; // disable button
      }

      function mld_forward_callback(jdata)
      {
          // Moet er voor de melding met een nieuwe stdmelding automatisch een opdracht aangemaakt worden?
          if (jdata.autoorder)
          {
            var data = { mld_key: <%=mld_key%>,
                         message: jdata.message,
                         autoorder: 1
                       };
            <% protectRequest.dataToken("data"); %>
            $.post("mld_edit_opdr_save.asp",
                   data,
                   FcltCallbackAndThenAlways(afsluiten),
                   "json");
          }
          else
            afsluiten(jdata);
      }

      function afsluiten(jdata)
      { // Sluit onszelf.
          FcltMgr.closeDetail(window, { warning: jdata.warning, success: true });
      }

      function mld_cancel()
      {
        FcltMgr.closeDetail(window, { cancel: true } );
      }

      function onChangeVakgroep(vakgroep_key)
      {
        // Melding leegmaken
        sgStdm.setValue(-1, "", true, false, null, true);
        sgStdm.CheckJustOne();
      }

      function process_stdm_info(data, textStatus)
      {
        // Zet de discipline indien niet ingevuld
        if ($("#disc").val() <= 0)
          sgDisc.setValue(data.ins_discipline_key, (data.ins_srtdiscipline_prefix != null ? data.ins_srtdiscipline_prefix + "-" : "") + data.discipline_omschrijving, false /*doonChange*/, true /*checkExist*/);
      }

      function onChangeStdMelding(stdmelding_key)
      {
        if (stdmelding_key > 0)
        {
          $.getJSON("./get_mld_info_ajax.asp",
                    { req_info: "mld_stdmelding",
                      mld_stdmelding_key: stdmelding_key,
                      mld_key: <%=mld_key%>,
                      caller_key: <%=user_key%> },
                      process_stdm_info);
        }
      }
    </script>
  </head>


  <body class="modal" id="mod_mldforward">
    <form action="mld_forward_save.asp?mld_key=<%=mld_key%>" method="post" name="u2">

<%    BLOCK_START("mldForward", this_mld.canForward?L("lcl_forward"):L("lcl_fixmelding") + " " + prefix + mld_key)

      // Vakgroep
      FCLTdisciplineselector("disc",
                             "sgDisc",
                             { label: L("lcl_vakgroup"),
                               disc_key: null,
                               module: "MLD",
                               perslidKey: user_key,
                               autfunctionKey: -1, // alles is toegestaan! authparams.autfunctionkey,
                               srtdisc_key: S("mld_forward_all") == 1? null : srt_disc,
                               filtercode: this_mld.canForwardKeten? "VDK" : "VD",
                               onChange: "onChangeVakgroep",
                               required: true
                             });
      // Discipline
      FCLTstdmeldingselector("stdm",
                             "sgStdm",
                             { label: L("lcl_complain"),
                               perslidKey: user_key,
                               autfunctionKey: -1, // alles is toegestaan! authparams.autfunctionkey,
                               srtdisc_key: S("mld_forward_all") == 1? null : srt_disc,
                               urlAdd: [{urlParam: "disc_key", field: "disc"}],
                               filtercode: this_mld.canForwardKeten? "VDK" : null,
                               onChange: "onChangeStdMelding",
                               stdmelding_key: null, //mld_stdm,
                               required: true
                             });
%>
      <tr><td class="label"><label></label></td>
          <td ><%=safe.html(mld_opmr)%></td>
      </tr>
<%
      RWTEXTAREATR("opmerk",
                   "fldtxt" + ((S("mld_forward_opm_flags") & 1) == 1? " required" : ""),
                   L("lcl_mld_forward_opm"),
                   "",
                   {html: "rows='8' cols='80'"});

      BLOCK_END()
      var buttons =  [ {title: L("lcl_forward"), action:"mld_forward()", singlepress: true, id: "btn_forward_submit" },
                       {title: L("lcl_cancel"),  action:"mld_cancel()" } ];
      CreateButtons(buttons)

      IFACE.FORM_END();
%>
    </form>
  </body>
</html>
