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

    File:               edit_note.asp
    Description:        Toon het popupschermpje om een PRJ opmerking te plaatsen te laten kopieren.
    Parameters:
        note_key        existing prj_scenario_note_key of leeg voor een nieuw aan te maken. Dan worden
                        de volgende twee parameters gebruikt:
        scenario_key    scenario_key
        floor           alg_verdieping_key (eentje die in het project zit)
    Context:            Verhuizingen, wijzig aantekening
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../CAD/FGII.inc" -->
<%
  var note_key = getQParamInt("note_key", -1);      // note key
  var scenario_key = getQParamInt("scenario_key");  // scenario key
  var floor_key = getQParamInt("vKey");             // verdieping key
  var note_nr = -1;                                 // note nr (volgnummer)
  var doUpdate = getFParamInt("doUpdate", 0) == 1;  // Moet ik bijwerken? (dan autoclose)
  var doDelete = getFParamInt("doDelete", 0) == 1;  // Moet ik verwijderen? (dan autoclose)

  var authparamsBOF = user.checkAutorisation("WEB_PRJBOF"); // om te bewerken

%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
  </head>
<%
  if (doDelete)
  {
    protectRequest.validateToken();
    sql = "DELETE FROM prj_scenario_note"
        + " WHERE prj_scenario_note_key = " + note_key;
    Oracle.Execute(sql);
    %><script type="text/javascript">FcltMgr.closeDetail(window);</script><%
    Response.End();
  }
  if (doUpdate)
  {
    protectRequest.validateToken();
    var note_nr = getFParamInt("note_nr");
    var note_descr = getFParam("note_descr", "");
    var perslid_key = getFParamInt("perslid_key");

    if (note_key!=-1)
    {
      // Create the update query
      sql = "UPDATE prj_scenario_note"
          + "   SET prj_scenario_note_omschrijving = " + safe.quoted_sql(note_descr)
          + "     , prj_scenario_note_aanmaak = SYSDATE"  // mutatiedatum is relevanter dan ooit aangemaakt
          + " WHERE prj_scenario_note_key = " + note_key;
    }
    else
    {    // Create the insert query
      sql = "INSERT INTO prj_scenario_note"
          + " (prj_scenario_key"
          + ", prj_scenario_note_nr"
          + ", prj_scenario_note_omschrijving"
          + ", alg_verdieping_key"
          + ", prs_perslid_key)"
          + " VALUES (" + scenario_key
          + ", " + note_nr
          + ", " + safe.quoted_sql(note_descr)
          + ", " + floor_key
          + ", " + perslid_key + ")";
    }
    Oracle.Execute(sql);

    %><script type="text/javascript">FcltMgr.closeDetail(window, {success: true});</script><%
    Response.End();
  }
%>
  <body class="fclt-modal" id="mod_pnotes">
    <% MODAL_START(); %>
    <form name=u2 method=post>
<%

      if ((isNaN(scenario_key) || scenario_key==-1) && (isNaN(note_key) || note_key==-1))
      { %>
        <p><%=L("lcl_internal_error")%></p>
<%
      }
      else
      {
        var sql = "";
        if (note_key !=-1)
        {
          sql = "SELECT n.prj_scenario_note_key"
              + ", n.prj_scenario_note_omschrijving"
              + ", n.prj_scenario_note_nr"
              + ", " + S("prs_pers_string") + " naam"
              + ", p.prs_perslid_key"
              + " FROM prj_scenario_note n"
              + "    , prs_perslid p"
              + " WHERE n.prs_perslid_key = p.prs_perslid_key"
              + "   AND n.prj_scenario_note_key = " + note_key;
          oRs = Oracle.Execute(sql);

          note_key = oRs("prj_scenario_note_key").Value;
          note_descr = oRs("prj_scenario_note_omschrijving").Value;
          note_nr = oRs("prj_scenario_note_nr").Value;
          perslid_name = oRs("naam").Value;
          perslid_key = oRs("prs_perslid_key").Value;
        }
        else
        { // nieuwe aantekening. Bepaal volgnummer binnen gehele scenario_key!=0
          sql = "SELECT MAX(n.prj_scenario_note_nr)"
              + " FROM prj_scenario_note n"
              + " WHERE n.prj_scenario_key = " + scenario_key;
          oRs = Oracle.Execute(sql);

          if (oRs.eof)
            note_nr = 1;
          else
            note_nr = oRs(0).value + 1;

          note_descr = "";
          perslid_key = user_key;
        }
        oRs.close();

%>
        <script type="text/javascript">
          function doSubmit()
          {
            if (document.forms.u2.note_descr.value != "")
              document.u2.submit();
            else
              FcltMgr.alert(L("lcl_prj_no_description_note"));
          }

          function dofnDelete()
          {
            FcltMgr.confirm(L("lcl_R_U_sure"), function() {
              document.u2.doUpdate.value = 0;
              document.forms.u2.doDelete.value = 1;
              document.forms.u2.submit();
            });
          }
        </script>
<%
        MODAL_BLOCK_START("mldReject", note_key > 0 ? L("lcl_prj_note_edit") : L("lcl_prj_note_new"), { icon: (note_key > 0 ? "fa-fclt-edit" : "fa-plus") });
%>
            <tr>
              <!-- Note number -->
              <td class='label'><label for="note_nr"><%=L("lcl_prj_note_nr")%></label></td>
              <td><%=note_nr%></td><input type='hidden' name='note_nr' id='note_nr' value='<%=safe.htmlattr(note_nr)%>'>
            </tr>

            <tr>
              <!-- Persoon -->
              <td class='label'><label for="perslid_key"><%=L("lcl_name")%>:</label></td>
              <td><%=safe.html(user.naam())%><input type='hidden' name='perslid_key' id='perslid_key' value='<%=perslid_key%>'></td>
            </tr>

<%
            // Note omschrijving -->
            RWTEXTAREATR("note_descr",
                         "fldtxt",
                         L("lcl_prj_note"),
                         note_descr,
                         {html: "rows='5'", maxlength: 2000});
                         %>
        <input type="hidden" name="note_key" id="note_key" value="<%=note_key%>">
        <input type="hidden" name="scenario_key" id="scenario_key" value="<%=scenario_key%>">
        <input type="hidden" name="vKey" id="floor" value="<%=floor_key%>">
        <input type="hidden" name="doUpdate" id="doUpdate" value="1">
        <input type="hidden" name="doDelete" id="doDelete" value="0">
        <%
        MODAL_BLOCK_END();

            buttons = [{ title: L("lcl_submit"), icon: "fa-fclt-save", action: "doSubmit()", importance: 1}]
            if (note_key!=-1)
            {
                 buttons.push({title: L("lcl_delete"), icon: "fa-trash-alt", action: "dofnDelete()", importance: 1 });
            }
            buttons.push({ title: L("lcl_cancel"), icon: "fa-fclt-cancel", action: "FcltMgr.closeDetail(window, {refresh: false})", importance: 3 });
            SIMPLE_BLOCK_START();
              CreateButtons(buttons);
            SIMPLE_BLOCK_END();
%>


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