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

    File:               cnt/cnt_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.
TODO: mld-labels ver-cnt-en
*/ %>

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

<%
FCLTHeader.Requires({plugins: [],
                          js: []})

var cnt_key = getQParamInt("cnt_key");
var note_key = getQParamInt("note_key", -1);
var submitting = getQParamInt("submit", 0) == 1;

var this_cnt = cnt.func_enabled_contract(cnt_key); // Wat heb ik zoal aan rechten op dit contract?
user.auth_required_or_abort(this_cnt.canWriteNotes);
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
  <script>
    jQuery(document).ready(function()
           {
             $('textarea').resize(function () { FcltMgr.resized(window) } );
             $('textarea').autogrow();
           });
  </script>
  </head>

<%
  if (submitting)
  {
    // TODO: Autorisatie check  if (func_enabled["WEB_MLDBOF"] || func_enabled["WEB_MLDBO3"]) {
    // ook: cnt_bo_logboek_mode != 1

    var note = getFParam("note");
    if (note != "")
    {
        var fields =  [ { dbs: "cnt_contract_key",               typ: "key",     val: cnt_key },
                        { dbs: "prs_perslid_key",                typ: "key",     val: user_key },
                        { dbs: "cnt_contract_note_omschrijving", typ: "varchar", frm: "note", len: 4000 }
                      ];
        if (note_key == -1)
        {
          var noteIns = buildInsert("cnt_contract_note", fields);
          Oracle.Execute(noteIns.sql);
        }
        else
        {
          var sql = buildUpdate("cnt_contract_note", fields)
                  + " cnt_contract_note_key = " + note_key
                  + " AND prs_perslid_key = " + user_key; // Voorkom dat er een notitie van een ander aangepast wordt.
          Oracle.Execute(sql);
        }
    }
%>
    <script type="text/javascript">
      FcltMgr.closeDetail(window, {refresh: true} );
    </script>
<%
    Response.End;
  }
%>

  <script type="text/javascript">
    function cnt_notesubmit()
    { //TODO CheckLengte
      if (!validateForm("u2"))
        return false;
      document.forms.u2.submit();
      return true;
    }

    function cnt_notecancel()
    {
      FcltMgr.closeDetail(window, { cancel: true } );
    }
  </script>

  <body id="editbody">
    <div id="edit">
      <form name=u2 method=post action="cnt_edit_note.asp?submit=1&cnt_key=<%=cnt_key%>&note_key=<%=note_key%>">
<%
        var buttons =  [ {title: L("lcl_submit"), icon: "opslaan.png", action: "cnt_notesubmit()"},
                         {title: L("lcl_cancel"), icon: "undo.png", action: "cnt_notecancel()"}];
        IFRAMER_HEADER(L("lcl_cnt_frame_notes"), buttons);
          BLOCK_START("cnt_note", L("lcl_cnt_note"));
            var note_oms = "";
            if (note_key > -1)
            {
              sql = "SELECT cnt_contract_note_omschrijving"
                  + "  FROM cnt_contract_note"
                  + " WHERE cnt_contract_note_key = " + note_key;
              var oRs = Oracle.Execute(sql);
              note_oms = oRs("cnt_contract_note_omschrijving").value;
            }
            RWTEXTAREATR("note", "fldtxt required", L("lcl_cnt_note_new"), note_oms, {html: "rows='4' cols='80'"});
          BLOCK_END();
        IFACE.FORM_END();
%>
      </form>
    </div>

    <div id="result">
<%
      sql = "SELECT n.cnt_contract_note_aanmaak"
          + "     , n.cnt_contract_note_key"
          + "     , pf.prs_perslid_naam_full"
          + "     , n.cnt_contract_note_omschrijving"
          + "  FROM cnt_contract_note n, prs_v_perslid_fullnames_all pf"
          + " WHERE n.prs_perslid_key = pf.prs_perslid_key (+)"
          + "   AND n.cnt_contract_key = " + cnt_key
          + (note_key > -1? " AND n.cnt_contract_note_key <> "+note_key : "")
          + " ORDER BY n.cnt_contract_note_aanmaak"
          + (S("mld_note_sort_ascending") == 0
            ? " DESC "
            : "");

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

      rst.addColumn(new Column({caption: L("lcl_date_time"), content: "cnt_contract_note_aanmaak", datatype: "datetime"}));
      rst.addColumn(new Column({caption: L("lcl_mld_changedby"), content: "prs_perslid_naam_full"}));
      rst.addColumn(new Column({caption: L("lcl_mld_note"), content: "cnt_contract_note_omschrijving"}));

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