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

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

    Context:
    Note:           vervult zowel de _show, _edit als _save mode
                    Voorheen facmgtVB\mld_kenmerkrecord.asp

*/ %>

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

<%
FCLTHeader.Requires({js: ["jquery-ui.js"]});

var mode=getQParam("mode", "show");
var kenmerk_key  = getQParamInt("kenmerk_key", -1);
var kenmerk_niveau = getQParam("niveau", "-1");

var authparams = user.checkAutorisation("WEB_MLDMGT");

if (kenmerk_key > 0)
{
    sql = "SELECT * FROM mld_kenmerk"
        + " WHERE mld_kenmerk_key = " + kenmerk_key;

    oRs = Oracle.Execute(sql);

    var kenmerk =
              { kenmerk_key: kenmerk_key,
                srtkenmerk_key: oRs("mld_srtkenmerk_key").Value,
                stdmelding_key: oRs("mld_stdmelding_key").Value,
                kenmerk_niveau: oRs("mld_kenmerk_niveau").Value,
                kenmerk_verplicht: oRs("mld_kenmerk_verplicht").Value? oRs("mld_kenmerk_verplicht").Value : -1,
                kenmerk_groep: oRs("mld_kenmerk_groep").Value,
                kenmerk_verplicht_status: oRs("mld_kenmerk_verplicht_status").Value? oRs("mld_kenmerk_verplicht_status").Value : -1,
                kenmerk_toonbaar: oRs("mld_kenmerk_toonbaar").Value,
                kenmerk_volgnummer: oRs("mld_kenmerk_volgnummer").Value,
                kenmerk_regexp: oRs("mld_kenmerk_regexp").Value,
                kenmerk_hint: oRs("mld_kenmerk_hint").Value,
                kenmerk_uniek: oRs("mld_kenmerk_uniek").Value,
                kenmerk_default: oRs("mld_kenmerk_default").Value,
                kenmerk_verwijder: oRs("mld_kenmerk_verwijder").Value,
                typeopdr_key: oRs("mld_typeopdr_key").Value,
                kenmerk_hint: oRs("mld_kenmerk_hint").Value
            }
    if (kenmerk.kenmerk_niveau == "D")
        kenmerk.disc_key = kenmerk.stdmelding_key;
    else if (kenmerk.kenmerk_niveau == "T")
        kenmerk.srtdisc_key = kenmerk.stdmelding_key;

    oRs.Close();
}
else
{
    kenmerk = { kenmerk_niveau: kenmerk_niveau,  // defaults
                srtdisc_key: getQParamInt("srtdisc_key", -1),
                stdmelding_key: getQParamInt("stdm", -1),
                disc_key: getQParamInt("disc_key", -1)   };
}

var kenmerktypeMkey = [];
sql = "SELECT mld_srtkenmerk_key"
    + "  FROM mld_srtkenmerk "
    + " WHERE mld_srtkenmerk_kenmerktype='M'";
oRs = Oracle.Execute(sql);
while (!oRs.eof)
{
    kenmerktypeMkey.push(oRs("mld_srtkenmerk_key").value);
    oRs.MoveNext();
}
oRs.Close();
%>

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

    if (kenmerk_key == -1)
    {
        if (kenmerk_niveau == "-1") // dwing keuze
        {
%><script>
        var niveau = "";
        function dialogClose()
        {
          if (niveau != "")
          {
            var addurl = "mld_kenmerk.asp?mode=edit&niveau=" + niveau;
            addurl += "<%=buildTransitParam(["srtdisc_key", "disc_key", "stdm"])%>";
            window.location.href = addurl;
          }
        }
        $(document).ready(function()
                        {
                          var params = { title: "<%=L("lcl_mld_choose_srt")%>",
                                         modal: true,
                                         width : 300,
                                         resizable : false,
                                         close : dialogClose
                                       };
                          FcltMgr.fixActiveelementError();
                          $('div#dmodal').dialog(params).dialog('open');
                        });
        </script>
    </head>
    <body>
        <div id="dmodal" style="display:none">
<%
            buttons = [{ title:  L("lcl_complain"),         action: "niveau='S';$('div#dmodal').dialog('close').remove();"},
                       { title:  L("lcl_mld_vakgroep"),     action: "niveau='D';$('div#dmodal').dialog('close').remove();"},
                       { title:  L("lcl_mld_vakgroeptype"), action: "niveau='T';$('div#dmodal').dialog('close').remove();"}
                      ];
            CreateButtons(buttons, { vertical: 1, isDialog: true } )
%>      </div>
    </body>
<%    Response.End;
        }
    }

%>

    <script type="text/javascript" >
      function gotoshow(data)
      {
        window.location.href = "mld_kenmerk.asp?mode=show&kenmerk_key=" + data.kenmerk_key;
      }
      function mld_submit()
      {
          if (!validateForm("u2"))
              return false;
          kmtypeM = "<%=kenmerktypeMkey%>".split(",");
          for (var i=0;i<kmtypeM.length;i++)
          {
            if (($("#srtkenmerk_key").val() == kmtypeM[i]) && ($("#verplicht_status").val() != -1))
            {
              var invalid_kenmerk = $("#srtkenmerk_key :selected").text(); // van type "M"
              alert(L("lcl_mld_kenmerktype_fail1").format(invalid_kenmerk, L("lcl_mld_req_status")));
              return false;
            }
          }
          $.post($("form[name=u2]")[0].action, $("[name=u2]").serialize(), FcltCallbackAndThen(gotoshow), "json");
          return true;
      }

      function mld_edit()
      {
          window.location.href = "mld_kenmerk.asp?mode=edit&kenmerk_key=<%=kenmerk_key%>";
      }
      function mld_delete()
      {
          if (confirm("<%=safe.jsstring(L("lcl_R_U_sure"))%>"))
              $.post("mld_kenmerk_save.asp?mode=delete&kenmerk_key=<%=kenmerk_key%>", {}, FcltCallbackClose, "json");
      }
      function mld_cancel()
      {
          window.location.href = "mld_kenmerk.asp?mode=show&kenmerk_key=<%=kenmerk_key%>";
      }

      function mldClose(params)
      {
          <% if (kenmerk_key == -1)
             { %>
              if (params.cancel)
              {
                  FcltMgr.closeDetail(window, params );
                  return true;
              }
          <% } else { // Bestaand kenmerk bewerkt, switch naar show-mode
            %>
              if (params.close)
              {
                FcltMgr.closeDetail(window, { close: true})
              }
          <% } %>
          if (!params.keepForm)
              window.location.href = "mld_kenmerk.asp?mode=show&kenmerk_key=" + params.kenmerk_key;
      }
      jQuery(document).ready(function()
                 {
                   $('textarea').resize(function () { FcltMgr.resized(window) } );
                   $('textarea').autogrow();
                 });
    </script>
  </head>
  <body id="<%=mode=="edit"?"editbody":"showbody"%>">
<%  var buttons = [];

    if (mode=="show")
        var buttons =  [ {title: L("lcl_change"), action:"mld_edit()",   icon: "wijzigen.png" },
                         {title: L("lcl_delete"), action:"mld_delete()", icon: "delete.png" } ];
    else
        var buttons =  [ {title: L("lcl_submit"), action:"mld_submit()", icon: "opslaan.png" },
                         {title: L("lcl_cancel"), action:"mld_cancel()", icon: "undo.png" } ];
    IFRAMER_HEADER(L("lcl_kenmerk_frame"), buttons);

if (mode=="edit")
{
    myFIELD = RWFIELDTR;
    myTEXTAREA = RWTEXTAREATR;
    myCHECKBOX = RWCHECKBOXTR;
}
else
{
    myFIELD = function (a,b,c,d,e) { ROFIELDTR(b,c,d,e) };
    myTEXTAREA = function (a,b,c,d,e) { ROTEXTAREATR(b,c,d,e) };
    myCHECKBOX = function (a,b,c,d,e) { ROCHECKBOXTR(b,c,d,e) };
}

%>
    <div id="show">
      <form name="u2"
            action="mld_kenmerk_save.asp?mode=save&kenmerk_key=<%=kenmerk_key%>"
            method="post"
            target="hidFrameSubmit"
            onSubmit="mld_submit()">
      <input type='hidden' name='niveau' value='<%=safe.htmlattr(kenmerk.kenmerk_niveau)%>'>
<%      BLOCK_START("mldKenmerk", L("lcl_mld_kenmerk"));

        ROFIELDTR("fld", L("lcl_key"), kenmerk.kenmerk_key);
        var sql = "SELECT mld_srtkenmerk_key, "
                + "       mld_srtkenmerk_omschrijving  ||' ('|| mld_srtkenmerk_kenmerktype || ')'"
                + "  FROM mld_srtkenmerk "
                + " WHERE mld_srtkenmerk_verwijder IS NULL "
                + " ORDER by mld_srtkenmerk_omschrijving ASC"
        FCLTselector("srtkenmerk_key", sql,
                    { label: L("lcl_mld_srtkenmerk_omschrijving"),
                      initKey: kenmerk.srtkenmerk_key,
                      emptyOption: "",
                      readonly: (mode=="show"),
                      required: true
                    });

        ROFIELDTR("fld", L("lcl_niveau"), { 'S' : L("lcl_complain"),'D' : L("lcl_mld_vakgroep"),'T' : L("lcl_mld_vakgroeptype") }[kenmerk.kenmerk_niveau]);
        if (kenmerk.kenmerk_niveau == "T")
        {
            sql = "SELECT b.ins_srtdiscipline_key, "
                + "       b.ins_srtdiscipline_omschrijving"
                + "  FROM ins_srtdiscipline b"
                + " WHERE 1=1"
                + " ORDER BY ins_srtdiscipline_omschrijving";
            FCLTselector("stdmelding_key",
                         sql,
                         { initKey: kenmerk.srtdisc_key,
                           label: L("lcl_mld_vakgroeptype"),
                           required: true,
                           emptyOption: "",
                           readonly: (mode=='show')
                         });
        }
        else if (kenmerk.kenmerk_niveau == "D")
        {
            // Vakgroep
            FCLTdisciplineselector("stdmelding_key", // misbruik stdmelding_key
                                   "sgDisc",
                                   { label: L("lcl_mld_vakgroep"),
                                     disc_key: kenmerk.disc_key,
                                     module: "MLD",
                                     perslidKey: user_key,
                                     autfunctionKey: -1, // alles is toegestaan! authparams.autfunctionkey,
                                     srtdisc_key: kenmerk.srtdisc_key,
                                     urlAdd: [{urlParam: "srtdisc_key", field: "srtdisc_key"}],
                                     required: (mode=='edit'),
                                     readonly: (mode=='show')
                                   });
        }
        else if (kenmerk.kenmerk_niveau == "S")
        {
            // Melding
            FCLTstdmeldingselector("stdmelding_key", // misbruik stdmelding_key
                                   "sgStdm",
                                   { label: L("lcl_complain"),
                                     perslidKey: user_key,
                                     stdmelding_key: kenmerk.stdmelding_key,
                                     autfunctionKey: -1, // alles is toegestaan! authparams.autfunctionkey,
                                     srtdisc_key: kenmerk.srtdisc_key,
                                     required: true,
                                     urlAdd: [{urlParam: "disc_key", field: "disc_key"}],
                                     readonly: (mode=='show')
                                   });
        }
        myFIELD("volgnummer", "fldshort", L("lcl_mld_kenmerk_volgnummer"), kenmerk.kenmerk_volgnummer, {required: true});
        sql = "SELECT         0, '' FROM DUAL"
            + " UNION SELECT  1, " + safe.quoted_sql(L("lcl_mld_kenmerk_groep") + " 1") + " FROM DUAL"
            + " UNION SELECT  2, " + safe.quoted_sql(L("lcl_mld_kenmerk_groep") + " 2") + " FROM DUAL"
            + " UNION SELECT  3, " + safe.quoted_sql(L("lcl_mld_kenmerk_groep") + " 3") + " FROM DUAL"
            + " UNION SELECT  4, " + safe.quoted_sql(L("lcl_mld_kenmerk_groep") + " 4") + " FROM DUAL"
            + " UNION SELECT  5, " + safe.quoted_sql(L("lcl_mld_kenmerk_groep") + " 5") + " FROM DUAL"
            + " UNION SELECT  6, " + safe.quoted_sql(L("lcl_mld_kenmerk_groep") + " 6") + " FROM DUAL"
            + " UNION SELECT  7, " + safe.quoted_sql(L("lcl_mld_kenmerk_groep") + " 7") + " FROM DUAL"
            + " UNION SELECT  8, " + safe.quoted_sql(L("lcl_mld_kenmerk_groep") + " 8") + " FROM DUAL"
            + " UNION SELECT  9, " + safe.quoted_sql(L("lcl_mld_kenmerk_groep") + " 9") + " FROM DUAL"
            + " ORDER BY 1";
        FCLTselector("groep",
                     sql,
                     { initKey: kenmerk.kenmerk_groep,
                       label: L("lcl_mld_kenmerk_groep"),
                       readonly: (mode=='show')
                     });

        sql = "SELECT -1, " + safe.quoted_sql(L("lcl_mld_niet_verplicht")) + " FROM DUAL"
            + " UNION SELECT  1, " + safe.quoted_sql(L("lcl_mld_altijd_verplicht")) + " FROM DUAL"
            + " UNION SELECT  2, " + safe.quoted_sql(L("lcl_mld_groepsverplichting")) + " FROM DUAL"
            + " ORDER BY 1";
        FCLTselector("verplicht",
                     sql,
                     { initKey: kenmerk.kenmerk_verplicht,
                       label: L("lcl_mld_kenmerk_verplicht"),
                       readonly: (mode=='show')
                     });
        sql = "SELECT -1, '' FROM DUAL"
            //+ " UNION SELECT 0, " + safe.quoted_sql(L("lcl_mld_pending"))      + " FROM DUAL"
            + " UNION SELECT 1, " + safe.quoted_sql(L("lcl_mld_afgewezen"))    + " FROM DUAL"
            //+ " UNION SELECT 2, " + safe.quoted_sql(L("lcl_mld_ingevoerd"))    + " FROM DUAL"
            //+ " UNION SELECT 3, " + safe.quoted_sql(L("lcl_mld_ingezien"))     + " FROM DUAL"
            //+ " UNION SELECT 4, " + safe.quoted_sql(L("lcl_mld_geaccepteerd")) + " FROM DUAL"
            + " UNION SELECT 5, " + safe.quoted_sql(L("lcl_mld_afgemeld"))     + " FROM DUAL"
            //+ " UNION SELECT 6, " + safe.quoted_sql(L("lcl_mld_verwerkt"))     + " FROM DUAL"
            //+ " UNION SELECT 7, " + safe.quoted_sql(L("lcl_mld_uitgegeven"))   + " FROM DUAL"
            + " ORDER BY 1";
        FCLTselector("verplicht_status",
                     sql,
                     { initKey: kenmerk.kenmerk_verplicht_status,
                       label:  L("lcl_mld_req_status"),
                       readonly: (mode=='show')
                     });

        myCHECKBOX("toonbaar", "fldcheck", L("lcl_mld_kenmerk_readonly"), kenmerk.kenmerk_toonbaar);
        myFIELD("regexp", "fldtxt", L("lcl_regexp"), kenmerk.kenmerk_regexp);
        myTEXTAREA("hint", "fldtxt", L("lcl_hint"), kenmerk.kenmerk_hint,
                   {translate: { fld: "mld_kenmerk_hint", key : kenmerk_key }}
                  );
        myTEXTAREA("kenmerk_default",
                   "fldtxt",
                   L("lcl_default"),
                   kenmerk.kenmerk_default,
                   {translate: { fld: "mld_kenmerk_default", key : kenmerk_key }}
                   );
        BLOCK_END();

IFACE.FORM_END();
%>
      </form>
    </div>
    <iframe src="../Shared/empty.html"
            name="hidFrameSubmit"
            FcltClose="mldClose"
            style="display:none"></iframe>
  </body>
</html>
