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

    File:         prs_edit_afdeling.asp
    Description:  edit afdeling gegevens.
    Parameters:   afd_key, afd_niveau


    Context:
    Note:

*/ %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/adresselector.inc" -->
<!-- #include file="../Shared/kostenplaatsselector.inc" -->
<!-- #include file="../Shared/afdelingselector.inc" -->
<!-- #include file="../Shared/persoonselector.inc" -->
<!-- #include file="prs_flexkenmerk.inc" -->

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

   var afd_key  = getQParamInt("afd_key", -1);
   var afd_niveau = getQParamInt("afd_niveau", -1);

   var autfunction = "WEB_PRSMAN";
   var authparams = user.checkAutorisation(autfunction); //   var authparams = PRS.checkAutorisation(); TODO

if (afd_key > -1)
{
    sql = "SELECT prs_afdeling_key "
        + "     , prs_afdeling_naam "
        + "     , prs_afdeling_omschrijving "
        + "     , prs_bedrijf_key "
        + "     , prs_afdeling_parentkey "
        + "     , mld_adres_key "
        + "     , prs_kostenplaats_key "
        + "     , prs_perslid_key_verantw "
        + "  FROM prs_afdeling "
        + " WHERE prs_afdeling_key =" + afd_key

    var oRs = Oracle.Execute(sql);
    if (oRs.Eof)
        shared.record_not_found("<!--"+afd_key+"-->");

    var afd_key     = oRs("prs_afdeling_key").value;
    var afd_naam    = oRs("prs_afdeling_naam").value;
    var afd_oms     = oRs("prs_afdeling_omschrijving").value;
    var bdr_key     = oRs("prs_bedrijf_key").value;
    var afd_parkey  = oRs("prs_afdeling_parentkey").value;
    var mld_adres   = oRs("mld_adres_key").value;
    var kstpl_key   = oRs("prs_kostenplaats_key").value;
    var verantw_key = oRs("prs_perslid_key_verantw").value;

    oRs.Close();
}
%>

<html>
<head>
<%
   FCLTHeader.Generate();
%>
<script>
    async function prs_submit()
    {
        document.activeElement.blur(); // trigger laatste onChanges
        if (!await validateForm("u2"))
            return false;
        document.forms.u2.submit();
    }
    function prs_cancel()
    {
        FcltMgr.closeDetail(window, { cancel: true } );
    }
</script>
</head>

<body class="editmode">
<%
  SUBFRAME_START();
  var buttons =  [ {title: L("lcl_submit"), action:"prs_submit()", icon: "fa-fclt-save" },
                   {title: L("lcl_cancel"), action:"prs_cancel()", icon: "fa-fclt-cancel" } ];
  IFRAMER_HEADER(L("lcl_prs_afdeling_frame"), buttons);
%>
<form name="u2"
      action="prs_edit_afdeling_save.asp?afd_key=<%=afd_key%>"
      method="post"
      target="hidFrameSubmit"
      onSubmit="prs_submit();">
<%
BLOCK_START("prsAfd", L("lcl_dep_name_level"+afd_niveau), {icon: "fa-users"});

  if (afd_niveau == 1) {
    sql = "SELECT prs_bedrijf_key,"
        + "       prs_bedrijf_naam"
        + "  FROM prs_bedrijf"
        + " WHERE prs_bedrijf_verwijder IS NULL AND prs_bedrijf_intern = 1"
        + (authparams.PRSwritelevel >= 0 ? " AND prs_bedrijf_key = " + user.afdeling().prs_bedrijf_key() : "")
        + " ORDER BY prs_bedrijf_naam_upper";

    FCLTselector("bdr_key", sql,
                {  label: L("lcl_prs_dept_company"),
                   initKey: bdr_key,
                   emptyOption: "",
                   readonly: false,
                   required: true });
    }
  else if (afd_niveau > 1)
    {
    FCLTafdelingselector("afd_parkey",
                         "afd_parkey",
                        { departmentKey: afd_parkey,
                          label: L("lcl_dep_name_level" + (afd_niveau-1)), // Nu Afdeling, was L("lcl_BES_organisation") => Organisatie
                          autlevel: authparams.PRSreadlevel, // Was voorheen niet aanwezig
                          filtercode:String(afd_niveau - 1),
                          readonly: false,
                          required: true });
     }

  RWFIELDTR("afd_naam", "fld", L("lcl_prs_dept_name"), afd_naam, {required: true, maxlength: 15});
  RWFIELDTR("afd_oms", "fld", L("lcl_prs_dept_desc"), afd_oms, {required: true, maxlength: 60});

  FCLTkostenplaatsselector( "kstpl_key",
                            "kstpl_key",
                            user_key,
                            { label: L("lcl_prs_dept_kosten"),
                              kostenplaatsKey: kstpl_key,
                              filtercode: "A" // Laat alle kostenplaatsen zien, negeer mandatering.
                            });

  FCLTpersoonselector("prs_verantwkey",
                      "prs_verantwkey",
                      { label: L("lcl_prs_dept_verantw"),
                        perslidKey: verantw_key,
                        autlevel: authparams.PRSreadlevel
                        });

  FCLTadresselector("mld_adres", "sgAdres",
                  {
                     label: L("lcl_delivery_address"),
                     adresKey:  mld_adres,
                     filtercode: "A",
                     emptyOption: ""
                  }) ;

BLOCK_END();
%><div id='prsAFlex' class='prsAFlex'></div><%
  generateFlexKenmerkCode ({
     link_key       : afd_key,
     niveau         : "A",
     reado          : false
});
IFACE.FORM_END();
%>
</form>
<iframe src="../Shared/empty.html" name="hidFrameSubmit" style="display:none"></iframe>
<% SUBFRAME_END(); %>
</body>
</html>
<% ASPPAGE_END(); %>
