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

     File:           fac_edit_menu.asp
     Description:
     Parameters:     menu_key

*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="fac.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/flexfileselector.inc" -->
<%
FCLTHeader.Requires({plugins:["jQuery"]})

var menu_key = getQParamInt("menu_key", -1);
var menuitem_key = getQParamInt("menuitem_key", -1);
var after_key = getQParamInt("after_key", -1); // voor nieuwe

var autfunction = "WEB_PRSSYS";
var authparams = user.checkAutorisation(autfunction);

if (menu_key != -1 && menuitem_key != -1)
{
  var sql = "SELECT mi.fac_menuitems_key"
          + "     , mi.fac_menuitems_groep"
          + "     , mi.fac_menuitems_image"
          + "     , mi.fac_menuitems_label"
          + "     , mi.fac_menuitems_oms"
          + "     , mi.fac_menuitems_module"
          + "     , mi.fac_menuitems_url"
          + "     , mi.fac_menuitems_srtdisc"
          + "     , mi.fac_menuitems_disc"
          + "     , mi.fac_functie_key"
          + "     , mi.fac_menuitems_rw"
          + "     , mi.fac_menuitems_default"
          + "     , m.fac_menu_key"
          + "     , m.fac_menu_volgnr"
          + "     , m.fac_menuitems_key"
          + "     , m.fac_menu_info"
          + "     , m.fac_menu_altlabel"
          + "     , m.fac_menu_alturl"
          + "     , m.fac_menu_altgroep"
          + "     , m.fac_menu_image"
          + "     , m.fac_functie_key altfunctiekey"
          + "     , m.ins_srtdiscipline_key"
          + "     , m.ins_discipline_key"
          + "     , f.fac_functie_code"
          + "     , f.fac_functie_omschrijving"
          + "     , m.fac_menu_level"
          + "     , m.fac_menu_portal"
          + "  FROM fac_menu m"
          + "     , fac_menuitems mi"
          + "     , fac_functie f"
          + " WHERE m.fac_menuitems_key = mi.fac_menuitems_key"
          + "   AND mi.fac_functie_key = f.fac_functie_key"
          + "   AND m.fac_menu_key = " + menu_key;
  var oRs = Oracle.Execute(sql);
}
else if (menuitem_key != -1)
{
  var sql = "SELECT mi.fac_menuitems_key"
          + "     , mi.fac_menuitems_groep"
          + "     , mi.fac_menuitems_image"
          + "     , mi.fac_menuitems_label"
          + "     , mi.fac_menuitems_oms"
          + "     , mi.fac_menuitems_module"
          + "     , mi.fac_menuitems_url"
          + "     , mi.fac_menuitems_srtdisc"
          + "     , mi.fac_menuitems_disc"
          + "     , mi.fac_functie_key"
          + "     , mi.fac_menuitems_rw"
          + "     , mi.fac_menuitems_default"
          + "     , f.fac_functie_code"
          + "     , f.fac_functie_omschrijving"
          + "  FROM fac_menuitems mi"
          + "     , fac_functie f"
          + " WHERE mi.fac_functie_key = f.fac_functie_key"
          + "   AND mi.fac_menuitems_key = " + menuitem_key;

  var oRs = Oracle.Execute(sql);
} else if (menu_key != -1)
{
  var sql = "SELECT m.fac_menu_key"
          + "     , m.fac_menu_volgnr"
          + "     , m.fac_menuitems_key"
          + "     , m.fac_menu_info"
          + "     , m.fac_menu_altlabel"
          + "     , m.fac_menu_alturl"
          + "     , m.fac_menu_altgroep"
          + "     , m.fac_menu_image"
          + "     , m.ins_srtdiscipline_key"
          + "     , m.ins_discipline_key"
          + "     , m.fac_functie_key altfunctiekey"
          + "     , m.fac_menu_level"
          + "     , m.fac_menu_portal"
          + "  FROM fac_menu m"
          + " WHERE m.fac_menu_key = " + menu_key;

  var oRs = Oracle.Execute(sql);
}

if (menuitem_key != -1)
{
  var fac_mikey      = oRs('fac_menuitems_key').value;
  var fac_milbl      = oRs('fac_menuitems_label').value;
  var fac_misrtdisc  = oRs('fac_menuitems_srtdisc').value;
  var fac_midisc     = oRs('fac_menuitems_disc').value;
  var fac_migrp      = fncolGroep(oRs("fac_menuitems_groep").value);
  var fac_mimage     = oRs('fac_menuitems_image').value;
  var fac_mimod      = oRs('fac_menuitems_module').value;
  var fac_mioms      = oRs('fac_menuitems_oms').value;
  var fac_func       = oRs('fac_functie_omschrijving').value + " (" + oRs('fac_functie_code').value + ")";
  var fac_funckey    = oRs('fac_functie_key').value;
  var fac_mirw       = oRs('fac_menuitems_rw').value;
  var fac_miurl      = oRs('fac_menuitems_url').value;
  var fac_miimage    = oRs("fac_menuitems_image").value;

  var ml = fac.localstring(fac_milbl);
  if (fac_misrtdisc == 1)
    ml += " (+ " + L("lcl_vakgroup_type") + ")";
  if (fac_midisc == 1)
    ml += " (+ " + L("lcl_discipline") + ")";
}

if (menu_key != -1)
{
  var fac_maltlbl    = oRs('fac_menu_altlabel').value;
  var fac_maltgrp    = oRs('fac_menu_altgroep').value;
  var fac_mimage     = oRs('fac_menu_image').value;
  var fac_minfo      = oRs('fac_menu_info').value;
  var fac_altfunc    = oRs('altfunctiekey').value;
  var fac_mvolgnr    = oRs('fac_menu_volgnr').value;
  var fac_malturl    = oRs('fac_menu_alturl').value;
  var fac_mdisckey   = oRs('ins_discipline_key').value
  var fac_msrtdisckey= oRs('ins_srtdiscipline_key').value
  var fac_mlevel     = oRs("fac_menu_level").value;
  var fac_mportal    = oRs("fac_menu_portal").value;
}

if (menu_key != -1 || menuitem_key != -1)
  oRs.Close();
else // defaults bepalen
{
    if (after_key > 0)
    {
      var sql = "SELECT m.fac_menu_volgnr"
              + "     , COALESCE(m.fac_menu_altgroep,mi.fac_menuitems_groep) groep"
              + "  FROM fac_menu m,"
              + "       fac_menuitems mi"
              + " WHERE m.fac_menuitems_key = mi.fac_menuitems_key(+)"
              + "   AND m.fac_menu_key = " + after_key;
      var oRs = Oracle.Execute(sql);
      var fac_maltgrp    = oRs('groep').value;
      var fac_mvolgnr    = oRs('fac_menu_volgnr').value + 1;
      oRs.Close();
    }
}

function fncolGroep(oRsvalue)
{
    var lgrtext = "";
    switch (oRsvalue) {
        case 0 : lgrtext = L("lcl_menu_pers") ; break;
        case 1 : lgrtext = L("lcl_menu_prof") ;  break;
        case 2 : lgrtext = L("lcl_menu_config") ;  break;
        case 3 : lgrtext = L("lcl_menu_mobile") ;  break;
    }
    return lgrtext;
}
%>
<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script type="text/javascript" >
      function doSubmit()
      {
        document.activeElement.blur(); // trigger laatste onChanges

        // Standaard checks op verplichte velden, datum formaat, numeriek en float formaat, currency formaat.
        if (!validateForm("u2"))
          return false

        document.forms.u2.submit();
      }

      function doCancel()
      {
        FcltMgr.closeDetail(window, { cancel: true } );
      }

      function toggle_disc_srtdisc()
      {
        if ($("#altfunctie")[0].options[$("#altfunctie")[0].selectedIndex].disc == 0)
        {
          $("#disckey")[0].disabled = true;
          $("#disckey").val(-1);
        }
        else
          $("#disckey")[0].disabled = false;

        if ($("#altfunctie")[0].options[$("#altfunctie")[0].selectedIndex].srtdisc == 0)
        {
          $("#srtdisckey")[0].disabled = true;
          $("#srtdisckey").val(-1);
        }
        else
          $("#srtdisckey")[0].disabled = false;

        if ($("#disckey").val() == -1 && $("#srtdisckey").val() == -1)
          $("#alturl")[0].readOnly = false;
        else // Catalogus of vakgroeptype heeft een waarde. Url aanpassen.
          fill_url();
      }

      function fill_url()
      {
        var altfunc = $("#altfunctie :selected")[0];
        if ((altfunc.getAttribute("disc") != 0 && $("#disckey").val() > 0) ||
            (altfunc.getAttribute("srtdisc") != 0 && $("#srtdisckey").val() > 0))
        {
          var urole = ""
          switch (altfunc.getAttribute("functiecode"))
          {
            case "WEB_BESUSE":
            case "WEB_MLDUSE":
            {
              urole = "fe"
              break;
            }
            case "WEB_BESFOF":
            case "WEB_MLDFOF":
            {
              urole = "fo"
              break;
            }
          }

          if ($("#disckey").val() > 0)
            $("#alturl").val("appl/bes/bes_bestelling.asp?urole=" + urole);
          else if ($("#srtdisckey").val() > 0)
            $("#alturl").val("appl/mld/mld_melding.asp?urole=" + urole);
          $("#alturl")[0].readOnly = true;
        }
        else
          $("#alturl")[0].readOnly = false;
      }

      function edit_image()
      {
        var url = "<%=protectQS.create("../../appl/shared/BijlagenForm.asp?module=MENU&multi=1")%>";
        FcltMgr.openModalDetail(url, L("lcl_mrk_upload_foto") );
      }

    </script>
  </head>

  <body id="editbody">
<%
    var buttons = [];
    buttons.push({ title: "lcl_photos", action: "edit_image()", icon: "folder.png" });
    buttons.push({title: L("lcl_submit"),  icon: "opslaan.png", action: "doSubmit()" });
    buttons.push({title: L("lcl_cancel"), icon: "undo.png", action: "doCancel()" });

    IFRAMER_HEADER(L("lcl_fac_menuoptie"), buttons);
%>
    <div id="edit">
      <form name=u2
            method="post"
            action="fac_edit_menu_save.asp?menu_key=<%=menu_key%>&menuitem_key=<%=menuitem_key%>"
            target="hidFrameSubmit"
            onSubmit="doSubmit();">
<%
        BLOCK_START("menuStd", L("lcl_menu_menuitemblock"));

        if (menuitem_key == -1 && menu_key == -1)
        {
            sql = "SELECT fac_menuitems_key, "
                + "       DECODE(fac_menuitems_groep "
                          + ", 0, "+ safe.quoted_sql(L("lcl_menu_pers"))
                          + ", 1, "+ safe.quoted_sql(L("lcl_menu_prof"))
                          + ", 2, "+ safe.quoted_sql(L("lcl_menu_config"))
                          + ", 3, "+ safe.quoted_sql(L("lcl_menu_mobile"))+" )||': '||"
                + "       COALESCE(fac_locale_xsl_tekst, ' ('||fac_menuitems_label || ')'),"
                + "       fac_menuitems_label"
                + "  FROM fac_menuitems, fac_locale_xsl xsl"
                + " WHERE xsl.fac_locale_xsl_label(+) = fac_menuitems_label"
                + "   AND xsl.fac_locale_xsl_lang(+) = " + safe.quoted_sql(user.lang())
                + " ORDER BY 2 ";

            FCLTselector("fac_mikey", sql,{  label: L("lcl_fac_menuoptie"),
                           initKey: fac_mikey,
                           titleCol: "fac_menuitems_label",
                           emptyOption: ""});
        }
        else
        {
            ROFIELDTR("fld", L("lcl_fac_menuoptie"), ml, {suppressEmpty: true});
        }
        ROFIELDTR("fld", L("lcl_menugroep"), fac_migrp, {suppressEmpty: true} );
        ROFIELDTR("fld", L("lcl_menu_module"), fac_mimod, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_menuinfo"), fac_mioms, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_fac_menusysautorisatie"), fac_func, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_menu_fac_rw"), fac_mirw, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_menu_fac_url"), fac_miurl, {suppressEmpty: true});
        ROFIELDTR("fld", L("lcl_image"), fac_miimage, {suppressEmpty: true});
        BLOCK_END();

        BLOCK_START("menuCust", L("lcl_menu_menublock"));
        RWFIELDTR("altlabel", "fld", L("lcl_fac_menuoptie"), fac_maltlbl, { required: (menuitem_key == -1 && menu_key > 0), maxlength: 30 });

        sql = "SELECT 0, " + safe.quoted_sql(L("lcl_menu_pers"))   + " FROM DUAL UNION ALL "
            + "SELECT 1, " + safe.quoted_sql(L("lcl_menu_prof"))   + " FROM DUAL UNION ALL "
            + "SELECT 2, " + safe.quoted_sql(L("lcl_menu_config")) + " FROM DUAL UNION ALL "
            + "SELECT 3, " + safe.quoted_sql(L("lcl_menu_mobile")) + " FROM DUAL";
        FCLTselector("altgroep",
                     sql,
                     { label: L("lcl_menugroep"),
                       initKey: fac_maltgrp,
                       emptyOption: "",
                       required: (menuitem_key == -1)
                       });

        RWFIELDTR("volgnr", "fld", L("lcl_menuvolgnr"), fac_mvolgnr, { required: true });
        RWFIELDTR("menuinfo", "fld", L("lcl_menuinfo"), fac_minfo, { maxlength: 1024 });

        // Bij de submit alleen dan meenemen, anders wissen
        // kijk of er een (andere) standaard menuopties is voor deze functie
        // met explodeerbare disc/srtdisc
        // TODO: Moet dit geen eigenschap worden van de tabel fac_functie?
        sql = "SELECT f.fac_functie_key"
            + "     , f.fac_functie_code || ': ' || f.fac_functie_omschrijving"
            + "     , MAX(mi.fac_menuitems_disc) disc"
            + "     , MAX(mi.fac_menuitems_srtdisc) srtdisc"
            + "     , f.fac_functie_code functiecode"
            + "  FROM fac_functie f"
            + "     , fac_menuitems mi"
            + " WHERE f.fac_functie_key = mi.fac_functie_key(+)"
            + (menuitem_key != -1
              ? " AND f.fac_functie_key <> " + fac_funckey
              : "")
         if (!user.has("WEB_FACTAB"))
         {
           sql += " AND fac_functie_code  NOT IN ('WEB_FACFAC', 'WEB_FACTAB')"
         }
         sql +=
             " GROUP BY f.fac_functie_key"
            + "        , f.fac_functie_code"
            + "        , f.fac_functie_omschrijving"
            + " ORDER BY 2";

        FCLTselector("altfunctie",
                     sql,
                     { label: L("lcl_fac_menuautorisatie"),
                       initKey: fac_altfunc? fac_altfunc : -1,
                       onChange: "toggle_disc_srtdisc()",
                       extraParamValue: "disc, srtdisc, functiecode",
                       extraParamValueInit: "0, 0",
                       emptyOption: ""});

        // Bepaal of de fac_menuitems_disc en fac_menuitems_srtdisc gezet zijn voor bovenstaande altfunctie
        // Dit houdt geen rekening met Mobile [*1] en is te bot zo.
        var disc = 0;
        var srtdisc = 0;
        if (fac_altfunc)
        {
          sql = "SELECT MAX(fac_menuitems_disc) disc"
              + "     , MAX(fac_menuitems_srtdisc) srtdisc"
              + "  FROM fac_menuitems"
              + " WHERE fac_functie_key = " + fac_altfunc;
          var oRs = Oracle.Execute(sql);
          disc = oRs("disc").value;
          srtdisc = oRs("srtdisc").value;
        }

        sql = "SELECT ins_discipline_key"
            + ", " + lcl.xsqla("ins_discipline_omschrijving", "ins_discipline_key", "ins_discipline_omschrijving")
            + " FROM bes_v_aanwezigdiscipline ORDER BY 2";
        FCLTselector("disckey",
                     sql,
                     { label: L("lcl_bes_discipline"),
                       initKey: fac_mdisckey,
                       onChange: "fill_url()",
                       emptyOption: "",
                       disable: disc == 0
                     });

        sql = "SELECT ins_srtdiscipline_key"
            + ", " + lcl.xsqla("ins_srtdiscipline_omschrijving", "ins_srtdiscipline_key", "ins_srtdiscipline_omschrijving")
            + " FROM ins_v_aanwezigsrtdiscipline ORDER BY 2";
        FCLTselector("srtdisckey",
                     sql,
                     { label: L("lcl_mld_vakgroeptype"),
                       initKey: fac_msrtdisckey,
                       onChange: "fill_url()",
                       emptyOption: "",
                       disable: srtdisc == 0
                     });

        RWCHECKBOXTR("level", "fldcheck", L("lcl_menulevel"), fac_mlevel);
        RWCHECKBOXTR("portal", "fldcheck", L("lcl_menuportal"), fac_mportal);
        // de readonly hieronder houdt geen rekening met Mobile [*1]
        var urlparams = { };
        if (srtdisc == 1 || disc == 1)
            urlparams = { readonly: 1 };
        RWFIELDTR("alturl", "fld", L("lcl_menu_fac_url"), fac_malturl, urlparams);
        FCLTflexfileselector("image",
               "sgFile",
               {
                 filename: fac_mimage,
                 label: L("lcl_image"),
                 flexmodule: "MENU"
               });
        BLOCK_END();
IFACE.FORM_END();
%>
      </form>
      <iframe src="../Shared/empty.html" name="hidFrameSubmit" style="display:none"></iframe>
    </div>
  </body>
</html>
