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

    File:           melding_new.asp
    Description:    Kies een stdmelding voor een nieuwe melding
    Parameters:     // impliciet mld_key=-1 voor nieuwe melding
                    // stdm_key bepalen we hier. Als je die hebt spring je maar rechtstreeks naar melding.asp
                    ins_key
                    ruimte_key
                    -
                    rest optioneel

    Note:           We kiezen hier puur alleen de stdmelding. De rest
                    wordt gewoon via melding.asp gedaan

*/ %>

<% REGISTERMENU_Disable = true; %>
<!-- #include file="../shared/common.inc" -->
<!-- #include file="./resultset_table.inc" -->
<!-- #include file="./mobile.inc" -->
<!-- #include file="../mld/mld.inc" -->
<!-- #include file="./iface.inc" -->
<!-- #include file="../shared/suggest/stdmeldingFilter.inc" -->
<!-- #include file="../shared/suggest/disciplinefilter.inc" -->

<%
var mld_key = getQParamInt("mld_key",-1);  // -1 betekent nieuwe
var qrc = getQParamInt("qrc", 0) != 0;
var action = getQParam("action", "");

var transitParam = buildTransitParam(["mld_key", "action", "disc", "srtdisc", "ins_key", "locatie_key", "gebouw_key", "verdieping_key", "ruimte_key"]);

// Verplicht:
var disc_key = getQParamInt("disc", -1);
var srtdisc_key = getQParamInt("srtdisc", -1);

// Optioneel
var ins_key = getQParamInt("ins_key", -1);  // test UWVA:11525 Het kan zijn dat ik vanuit de lijst deze meekrijg om een nieuwe aan te maken

var urole = 'fe'; // in mobile altijd FE
autfunction = (urole=='fe' ? "WEB_MLDUSE" : "WEB_MLDFOF");

if (srtdisc_key > 0)
    lcl.set_dialect(srtdisc_key, "MLD_SRTDISCIPLINE_KEY");

%>
<html>
  <head>
<% FCLTMHeader.Generate({title:  L("lcl_mobile_newmelding") }); %>
    <script type="text/javascript">
        function mld_new(stdm_key)
        {
            window.location.href = "melding.asp?stdm_key=" + stdm_key + "<%=safe.jsstring(transitParam)%>";
        }
    </script>
  </head>
  <body>
    <div>
<%
  var header_title = ((srtdisc_key < 0) ? L("lcl_mobile_selectmeldingtype") : L("lcl_mobile_selectmelding") );

  PAGE_START({id: "page-1-"+mld_key});
  HEADER({title: header_title, back:!qrc, home: !qrc, datatheme: (urole=='fe' ? jQFrontEndTheme : null) });
  CONTENT_START();

    function fncolSrtLink(oRs)
    {
        var nurl = "melding_new.asp?srtdisc=" + oRs("ins_srtdiscipline_key").Value + transitParam;
        return nurl;
    };
    function fncolLink(oRs)
    {
        var nurl = "javascript:mld_new("+oRs("mld_stdmelding_key").Value+")";
        return nurl;
    };

    if (srtdisc_key < 0) // Eerst srtdiscipline kiezen
    {
        var filt_params = { ins_key: ins_key };
        if (action == "forward")
        {
            var this_mld = mld.func_enabled_melding(mld_key);
            var filtcode = (this_mld.canForwardKeten? "VDK" : "VD");
            var filtClause = getFiltClauseDiscipline(filtcode, { module: "MLD" } );
            filt_params.filter_from = filtClause.from;
            filt_params.filter_where = filtClause.where;
        }
        var mySRTsql = mld.srtdisc_sql(autfunction, filt_params);

        var sql = "SELECT MIN(ins_srtdiscipline_key) ins_srtdiscipline_key"
                + "     , COUNT(DISTINCT ins_srtdiscipline_key) aantal"
                + "  FROM (" +  mySRTsql + ")";
        var oRs = Oracle.Execute(sql);
        if (oRs("aantal").Value == 1)
            srtdisc_key = oRs("ins_srtdiscipline_key").Value;
        oRs.Close();
    }

    if (srtdisc_key < 0) // Dan écht eerst srtdiscipline kiezen
    {
        var rst = new ResultsetTable({ sql: mySRTsql
                                     , keyColumn: "ins_srtdiscipline_key"
                                     , linkColumn: fncolSrtLink
                                     , headerColumn: "ins_srtdiscipline_omschrijving"
                                     , ID: "factable"
                                     , showAll: true
                                     , noSearch: true
                                     , emptySetString: L("lcl_empty")
                                     });
    }
    else
    {
        var sql = "SELECT fac_functie_key"
                + "  FROM fac_functie"
                + " WHERE fac_functie_code=" + safe.quoted_sql(autfunction);
        var oRs = Oracle.Execute(sql);
        var auth_key = -1;
        if (!oRs.eof)
          auth_key = oRs("fac_functie_key").value;
        oRs.Close();

        var filt_params = { disc_key: disc_key
                          , srtdisc_key: srtdisc_key
                          , ins_key: ins_key
                          , perslidKey: user_key // altijd voor jezelf via mobile
                          , autfunctionKey: auth_key
                          };
        var filtcode = 'FE';
        if (action == "forward")
        {
            var this_mld = mld.func_enabled_melding(mld_key);
            filtcode = (this_mld.canForwardKeten? "VDK" : null);
        }
        var filtClause = getFiltClauseStdmelding(filtcode, filt_params);

        mySTDsql = "SELECT mld_stdmelding_key"
                 + ","+    lcl.xsqla('sm.mld_stdmelding_omschrijving','sm.mld_stdmelding_key')
                 + ","+    lcl.xsqla('mld_stdmelding_hint', 'sm.mld_stdmelding_key')
                 + ","+    lcl.xsqla('di.ins_discipline_omschrijving', 'di.ins_discipline_key')
                 + "  FROM mld_stdmelding sm"
                 + "     , mld_v_aanwezigdiscipline di"
                 + "     , ins_srtdiscipline sd"
                 + filtClause.from
                 + " WHERE sm.mld_ins_discipline_key = di.ins_discipline_key"
                 + "   AND di.ins_srtdiscipline_key = sd.ins_srtdiscipline_key"
                 + "   AND sm.mld_stdmelding_verwijder IS NULL"
                 + "   AND (sm.mld_stdmelding_vervaldatum IS NULL OR"
                 + "        sm.mld_stdmelding_vervaldatum > SYSDATE)"
                 + filtClause.where
        // Selector mode vakgroep/melding voor de frontend. {0(=default)=vakgroep en melding (beide eerst te kiezen), 1=vakgroep readonly, 2=alleen melding}
        if (S("mld_selector_mode")==0) {
            mySTDsql += " ORDER BY UPPER("+lcl.xsql('di.ins_discipline_omschrijving', 'di.ins_discipline_key')+"), UPPER("+lcl.xsql('sm.mld_stdmelding_omschrijving','sm.mld_stdmelding_key')+")";
        } else {
            mySTDsql += " ORDER BY UPPER("+lcl.xsql('sm.mld_stdmelding_omschrijving','sm.mld_stdmelding_key')+")";
        }

        var rst = new ResultsetTable({ sql: mySTDsql
                                     ,  keyColumn: "mld_stdmelding_key"
                                     ,  linkColumn: fncolLink
                                     ,  dividerColumn: S("mld_selector_mode")==0?"ins_discipline_omschrijving":null  // groepeer op vakgroep
                                     ,  headerColumn: "mld_stdmelding_omschrijving" //fncolHeader
                                     ,  extraFilterColumn: "mld_stdmelding_hint"
                                     ,  asideColumn: S("mld_selector_mode")==1?"ins_discipline_omschrijving":null
                                     ,  ID: "factable"
                                     ,  showAll: true
                                     ,  emptySetString: L("lcl_empty")
                                     });
    }
    var cnt = rst.processResultset();
    if (cnt == 0)
    {
       %><script>alert(L("lcl_mobile_no_stdmelding"));window.history.back(1)</script><%
    }
  CONTENT_END();
  FOOTER();
  PAGE_END()
%>
    </div>
  </body>
</html>
