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

    File:           fac_kenmerkdomein.asp
    Description:    Details van een mld kenmerk
    Parameters:     domein_key
                    mode        show, edit, save

    Context:
    Note:           vervult zowel de _show, _edit als _save mode

*/ %>

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

<%
FCLTHeader.Requires({plugins:["suggest", "jQuery"]})

var domein_key  = getQParamInt("domein_key", -1);
var domein_module  = getQParamInt("domein_module", -1);  // tbv toevoegen
var mode=getQParam("mode", "show");

var authparams = user.checkAutorisation("WEB_PRSSYS");  // lager?

if (domein_key > 0)
{
    sql = "SELECT * FROM fac_kenmerkdomein fkd, fac_usrtab fut"
        + " WHERE fkd.fac_usrtab_key = fut.fac_usrtab_key(+)"
        + "   AND fac_kenmerkdomein_key = " + domein_key;

    oRs = Oracle.Execute(sql);

    var domein =
              { domein_key: domein_key,
                domein_module: oRs("fac_kenmerkdomein_module").value,
                domein_omschrijving: oRs("fac_kenmerkdomein_omschrijving").value,
                domein_objectnaam: oRs("fac_kenmerkdomein_objectnaam").value,
                domein_kolomnaam: oRs("fac_kenmerkdomein_kolomnaam").value,
                domein_kolomtxt: oRs("fac_kenmerkdomein_kolomtxt").value,
                domein_kolomverval: oRs("fac_kenmerkdomein_verval").value,
                domein_xmlnode: oRs("fac_kenmerkdomein_xmlnode").value,
                fac_usrtab_key: oRs("fac_usrtab_key").value,
                fac_usrtab_omschrijving: oRs("fac_usrtab_omschrijving").value
              };

    oRs.Close();
}
else
{
    // Eigen tabellen als initiële waarde invullen
    domein = { domein_objectnaam: "FAC_USRDATA",
               domein_module: getQParam("module", -1),
               domein_kolomnaam: "FAC_USRDATA_KEY",
               domein_kolomtxt: "FAC_USRDATA_OMSCHR"
             };
}
%>

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

    if (mode=="save")
    {
          var fields =  [ { dbs: "fac_kenmerkdomein_omschrijving", typ: "varchar", frm: "omschrijving" },
                          { dbs: "fac_kenmerkdomein_objectnaam", typ: "varchar", frm: "objectnaam" },
                          { dbs: "fac_kenmerkdomein_kolomnaam", typ: "varchar", frm: "kolomnaam" },
                          { dbs: "fac_kenmerkdomein_kolomtxt", typ: "varchar", frm: "kolomtxt" },
                          { dbs: "fac_kenmerkdomein_verval", typ: "varchar", frm: "kolomverval" },
                          { dbs: "fac_kenmerkdomein_module", typ: "varchar", frm: "module" },
                          { dbs: "fac_kenmerkdomein_xmlnode", typ: "varchar", frm: "xmlnode" },
                          { dbs: "fac_usrtab_key", typ: "key", frm: "usrtabkey" }
                        ];

          if (domein_key > 0)
          {
              var sql = buildUpdate("fac_kenmerkdomein", fields) + " fac_kenmerkdomein_key = " + domein_key;
              var err = Oracle.Execute(sql, true);
          }
          else
          {
                fields.push({  dbs: "fac_kenmerkdomein_key",      typ: "key",    seq: "fac_s_fac_kenmerkdomein_key" });
                var facIns = buildInsert("fac_kenmerkdomein", fields);
                var domein_key = facIns.sequences["fac_kenmerkdomein_key"];
                var err = Oracle.Execute(facIns.sql, true);
          }
          if (err.friendlyMsg)
              warning = err.friendlyMsg;
%>    <script>
         $(document).ready(function () {
                FcltMgr.closeDetail(window, { domein_key: <%=domein_key%>,
                                              warning: "<%=safe.jsstring(err.friendlyMsg)%>",
                                              keepForm: <%=err.friendlyMsg?"true":"false"%>
                                             });
         } );
      </script>
<%    Response.End;
    }
%>

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

      function f_edit()
      {
          window.location.href = "fac_kenmerkdomein.asp?mode=edit&domein_key=<%=domein_key%>";
      }

      function f_delete()
      {
          if (confirm(L("lcl_R_U_sure")))
          {
            var data = { key: <%=domein_key%>,
                         level: "KD"
                       };
            <% protectRequest.dataToken("data"); %>
            $.post("fac_delete.asp",
                   data,
                   FcltCallbackClose,
                   "json");
          }
      }

      function f_cancel()
      {
        if (<%=domein_key%> > 0)
          window.location.href = "fac_kenmerkdomein.asp?mode=show&domein_key=<%=domein_key%>";
        else
          FcltMgr.closeDetail(window, { cancel: true });
      }
      function f_close(params)
      {
          <% if (domein_key == -1)
             { %>
              if (params.cancel)
              {
                  FcltMgr.closeDetail(window, params );
                  return true;
              }
              FcltMgr.setTitle("<%=safe.jsstring(domein.domein_omschrijving)%>", {hot: false});
          <% } else { // Bestaande melding bewerkt, switch naar show-mode
%>
              if (params.close)
              {
                FcltMgr.closeDetail(window, { close: true})
              }
          <% } %>
          if (!params.keepForm)
              window.location.href = "fac_kenmerkdomein.asp?mode=show&domein_key=" + params.domein_key;
      }
    </script>
  </head>
  <body id="<%=mode=="edit"?"editbody":"showbody"%>">
<%  var buttons = [];

    if (mode=="show")
        var buttons =  [ {title: L("lcl_change"), action:"f_edit()",   icon: "wijzigen.png" },
                         {title: L("lcl_delete"), action:"f_delete()", icon: "delete.png" } ];
    else
        var buttons =  [ {title: L("lcl_submit"), action:"f_submit()", icon: "opslaan.png" },
                         {title: L("lcl_cancel"), action:"f_cancel()", icon: "undo.png" } ];
    IFRAMER_HEADER(L("lcl_fac_kenmerkdomein_frame"), buttons);

if (mode=="edit")
    myRWFIELD = RWFIELDTR;
else
    myRWFIELD = function (a,b,c,d,e) { ROFIELDTR(b,c,d,e) }; // in showmode zijn er veel readonly

%>
    <div id="show">
      <form name="u2"
            action="fac_kenmerkdomein.asp?mode=save&domein_key=<%=domein_key%>"
            method="post"
            target="hidFrameSubmit"
            onSubmit="f_submit()">
<%      BLOCK_START("fkd", L("lcl_kenmerkdomein_gegevens"));
        myRWFIELD("omschrijving", "fld", L("lcl_kenmerkdomein_omschrijving"), domein.domein_omschrijving, { required: true } );
        if (mode == "edit")
        {
          sql = " SELECT fac_module_name"
              + "      , fac_module_name || ' - ' || fac_module_remark"
              + " FROM fac_module"
              + " WHERE fac_module_extern = 1"
              + " AND EXISTS (SELECT *"
              + "               FROM fac_v_webgebruiker g"
              + "                  , fac_functie f"
              + "              WHERE f.fac_functie_key = g.fac_functie_key"
              + "                AND prs_perslid_key = " + user_key
              + "                AND (fac_functie_code = 'WEB_PRSSYS'"
              + "                     OR fac_functie_code =    'WEB_' || fac_module_name || 'MSU'))"
              + " ORDER BY fac_module_name ASC";

          FCLTselector("module",
                       sql,
                       {label: L("lcl_fac_module"),
                        initKey: domein.domein_module,
                        emptyOption: "",
                        required: true
                       });

          sql = "SELECT fac_usrtab_key"
              + "     , " + lcl.xsqla("fac_usrtab_naam", "fac_usrtab_key")
              + "  FROM fac_usrtab"
              + " WHERE fac_usrtab_verwijder IS NULL"
              + " ORDER BY UPPER(" + lcl.xsql("fac_usrtab_naam", "fac_usrtab_key") + ")";

          FCLTselector("usrtabkey",
                       sql,
                       {label: L("lcl_usrtab_omschrijving"),
                        initKey: domein.fac_usrtab_key,
                        emptyOption: "",
                        required: false
                       });
        }
        else
          myRWFIELD("usrtabkey", "fld", L("lcl_usrtab_omschrijving"), domein.fac_usrtab_omschrijving);

        myRWFIELD("objectnaam", "fld", L("lcl_kenmerkdomein_objectnaam"), domein.domein_objectnaam);
        myRWFIELD("kolomnaam", "fld", L("lcl_kenmerkdomein_kolomnaam"), domein.domein_kolomnaam);
        myRWFIELD("kolomtxt", "fld", L("lcl_kenmerkdomein_kolomtxt"), domein.domein_kolomtxt);
        myRWFIELD("kolomverval", "fld", L("lcl_kenmerkdomein_verval"), domein.domein_kolomverval);
        myRWFIELD("xmlnode", "fld", L("lcl_kenmerkdomein_xmlnode"), domein.domein_xmlnode);
        BLOCK_END();
IFACE.FORM_END();
%>
      </form>
    </div>
    <iframe src="../Shared/empty.html"
            name="hidFrameSubmit"
            FcltClose="f_close"
            style="display:none"></iframe>
  </body>
</html>
