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

    File:               MLD/mld_edit_note.asp
    Description:        Bekijken en/of aanvullen van een notitie behorende bij een melding
                        in een apart blokje
    Parameters:         fronto/backo
    Context:            vanuit melding-detail/overzicht
    Note:               STATUS: functioneel wel ok, moet nog in redesign 5i mee.
                        JGL: In het bijzonder: gewoon in een popupje invullen vanuit show_note?
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="./mld.inc" -->

<%
FCLTHeader.Requires({ plugins: [] });
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
  </head>

<%
  var mld_key = getQParamInt("mld_key");
  var note_key = getQParamInt("note_key", -1);
  var submitting = getQParamInt("submit", 0) == 1;
  var urole = getQParamSafe("urole", "");

  var mld_info = mld.mld_melding_info(mld_key);
  lcl.set_dialect(mld_info.srtdisc_key, "MLD_SRTDISCIPLINE_KEY");
  var mfe = mld.func_enabled_melding (mld_key);
  // Heb ik rechten om notities te wijzigen?
  user.auth_required_or_abort(mfe.canWriteNotes);

  var eigenMelding = user.isCollega(mld_info.melder_key);
  var afgehandeld = (mld_info.mld_status == 1 || mld_info.mld_status == 5 || mld_info.mld_status == 6);

  var kenmerk_key = -1;
  if (mld_key > 0)
  {
    // Is er een bijlage kenmerk?
    var sql = mld.getMldKenmerk_sql(mld_key, {wheres: ["BITAND(mkm.mld_kenmerk_rolcode,1) = 1",
                                                       "(mkm.mld_kenmerk_verplicht IS NULL OR mkm.mld_kenmerk_verplicht=0)",
                                                       "msk.mld_srtkenmerk_kenmerktype = 'M'"]}
                                   );
    var oRs = Oracle.Execute(sql);
    if (!oRs.eof)
    {
      kenmerk_key = oRs("mld_kenmerk_key").Value;
    }
    oRs.Close();
  }

%>

  <script type="text/javascript">

    function checkInput()
    {
      var s = $("#note").val();
      if (s.length > 4000)
      {
        s = s.substring(0, 4000);
        $("#note").val(s);
      }

      return true;
    }

    async function mld_notesubmit()
    {
      // Formulier specifieke checks
      if (!checkInput())
        return false;
      if (!await validateForm("u2"))
        return false;

      $.post($("form[name=u2]")[0].action,
             $("[name=u2]").serialize(),
             FcltCallbackAndThenAlways(mld_submit_callback),
             "json");
      return true; // disabled button ook
    }

    function mld_submit_callback(json)
    {
      if (json.success)
      {
        window.parent.FcltMgr.reload();
        FcltMgr.closeDetail(window, json);
      }
    }

    function mld_notecancel()
    {
      window.parent.FcltMgr.reload();
      FcltMgr.closeDetail(window, { cancel: true } );
    }

    function Upload()
    {
      var url = "<%=protectQS.create("../Shared/BijlagenForm.asp?module=MLD&niveau=M&multi=1&key=" + mld_key + "&kenmerk_key=" + kenmerk_key)%>";
      FcltMgr.openModalDetail(url, L("lcl_appendixes") );
    }

    $(function()
    {
      $('textarea').resize(function () { FcltMgr.resized(window) } );
      $('textarea').autogrow();
      $("#note").focus();
<%
    if (afgehandeld)
    {
%>
      FcltMgr.alert(L('lcl_mld_edit_note_completed'));
<%
    }
%>
    });

  </script>

  <body class="editmode">
  <% SUBFRAME_START(); %>
      <form name="u2" method="post" action="mld_edit_note_save.asp?mld_key=<%=mld_key%>&urole=<%=urole%>&note_key=<%=note_key%>">
<%
        var buttons = [];
        if (kenmerk_key > 0){
          buttons.push({title: L("lcl_appendixes"), icon: "fa-folder-open", action: "Upload()", id: "bupl_mld_note" });
        }

        buttons.push({title: L("lcl_submit"), icon: "fa-fclt-save", action:"mld_notesubmit()" });
        buttons.push({title: L("lcl_cancel"), icon: "fa-fclt-cancel", action:"mld_notecancel()" });

        IFRAMER_HEADER(L("lcl_mld_frame_notes"), buttons);
          BLOCK_START("mld_note", L("lcl_mld_note"), { icon: "fa-clipboard-list-check" });
            // initialisatie voor nieuwe note: note_key == -1
            var note_oms = "";
            var note_zichtbaar = (mld_info.fenotes & 1);
            if (note_key > -1)
            { // Bestaande note: gegevens uit record gebruiken.
              sql = "SELECT mld_melding_note_omschrijving"
                  + "     , mld_melding_note_flag"
                  + "  FROM mld_melding_note"
                  + " WHERE mld_melding_note_key = " + note_key;
              var oRs = Oracle.Execute(sql);
              note_oms = oRs("mld_melding_note_omschrijving").value;
              note_zichtbaar = oRs("mld_melding_note_flag").value == 1;
            }

            // Bron van verwarring maar: als het een eigen melding is dan ben je zelf de FE-user van die melding
            // Dan is het heel onlogisch als je het vinkje zou kunnen uitzetten. Disablen we dan ook effectief.
            if (eigenMelding)
              note_zichtbaar = true;

            RWTEXTAREATR("note", "fldtxt required", L("lcl_mld_note_new"), note_oms, {html: "rows='4' cols='80'"});

            if (mld_info.fenotes & 2 && !mfe.haveOnlyFErights)
            {
              if (note_key == -1 && !eigenMelding)
                RWCHECKBOXTR("zichtbaarFE", "fldcheck", "<nobr>" + L("lcl_mld_zichtbaar_FE") + "</nobr>", note_zichtbaar, {html: ""});
              else
                ROCHECKBOXTR("fldmldzichtbaarFE", "<nobr>" + L("lcl_mld_zichtbaar_FE") + "</nobr>", note_zichtbaar, {html: "" });
            }
          BLOCK_END();
        IFACE.FORM_END();
%>
      </form>
      <%
 SUBFRAME_END(); %>
      <div id="result">
<%
    function fnZichtbaar(oRs)
    {
        var html = "";
        if (oRs("mld_melding_note_flag").Value == 1)
            html = "<span title='" + safe.htmlattr(L("lcl_mld_zichtbaar_FE")) + "'>*</span>";

        return html;
    }
      // Zelfde query in mld_show_note.asp
      sql = "SELECT n.mld_melding_note_aanmaak"
          + "     , n.mld_melding_note_key"
          + "     , pf.prs_perslid_naam_full"
          + "     , n.mld_melding_note_flag"
          + "     , n.mld_melding_note_omschrijving"
          + "  FROM mld_melding_note n, prs_v_perslid_fullnames_all pf"
          + " WHERE n.prs_perslid_key = pf.prs_perslid_key (+)"
          + "   AND n.mld_melding_key = " + mld_key
          + (note_key > -1? " AND n.mld_melding_note_key <> "+note_key : "")
          + (mfe.haveOnlyFErights? " AND mld_melding_note_flag = 1" : "")
          + " ORDER BY n.mld_melding_note_aanmaak "
          + (S("mld_note_sort_ascending") == 0 ? "DESC " : "");

      // Toon de overige notes boven het invulveld.
      var rst = new ResultsetTable({ sql: sql,
                                     ID: "notestable",
                                     keyColumn: "mld_melding_note_key",
                                     noPrint: true,
                                     emptySetString : ""
                                   });

      rst.addColumn(new Column({caption: L("lcl_date_time"), content: "mld_melding_note_aanmaak", datatype: "datetime"}));
      rst.addColumn(new Column({caption: L("lcl_mld_changedby"), content: "prs_perslid_naam_full"}));
      if (!mfe.haveOnlyFErights)
          rst.addColumn(new Column({caption: "*", title: L("lcl_mld_zichtbaar_FE"), content: fnZichtbaar}));
      rst.addColumn(new Column({caption: L("lcl_mld_note"), content: "mld_melding_note_omschrijving"}));

      var cnt = rst.processResultset();
      %></div>
  </body>
</html>
<% ASPPAGE_END(); %>
