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

    File:               SuggestStdmelding.asp
    Description:        Suggereer (standaard)meldteksten
    Parameters:         k             De eerste paar letters
                        filtcode beperk de lijst met getoonde locaties TO BE DEFINED
                        perslidKey  : verplicht  Voor wie is de melding
                        autfunctionKey: verplicht fac_function_key of -1 als alles toegestaan

    Globals:
    Context:
    Note:
*/ %>

<%
JSON_Result = true;
%>

<!-- #include file="../../Shared/common.inc" -->
<!-- #include file="./suggest.inc" -->
<!-- #include file="./stdmeldingFilter.inc" -->

<%
var chars = getQParam("k", "");
var autfunctionKey = getQParamInt("autfunctionKey");
var bAll = getQParamInt("SuggestAll", 0) == 1;
var srtdisc_key = getQParamInt("srtdisc_key", -1);
var disc_key = getQParamInt("disc_key", -1);
var ins_key = getQParamInt("ins_key", -1);
var extracode = getQParam("extracode", "");
var perslidKey = getQParamInt("perslidKey", user_key); // Voor wie is de melding
var show_expired = (getQParamInt("show_expired", 0) == 1); // Vervallen stdmeldingen ook tonen
var filtcode = getQParam("filtcode", "");

chars = chars.replace(/\*/g,"%");

var filt_params = { disc_key: disc_key,
                    srtdisc_key: srtdisc_key,
                    ins_key: ins_key,
                    perslidKey: perslidKey,
                    autfunctionKey: autfunctionKey,
                    show_expired: show_expired
                  };

var filtClause = getFiltClauseStdmelding(filtcode, filt_params);
var extraInf = getExtraInfStdmelding(extracode);

// TODO: Voor Frontend er uit filter waar s1.mld_stdmelding_notfrontend = 0?
sql = "SELECT sm.mld_stdmelding_key"
    + "      , " + lcl.xsqla('mld_stdmelding_omschrijving', 'sm.mld_stdmelding_key')
    + "      , " + lcl.xsqla('mld_stdmelding_hint', 'sm.mld_stdmelding_key')
    + extraInf
    + "      , CASE WHEN sd.ins_srtdiscipline_prefix IS NULL THEN '' ELSE sd.ins_srtdiscipline_prefix || '-' END "
    + "        || " + lcl.xsqla('ins_discipline_omschrijving', 'di.ins_discipline_key')
    + "   FROM mld_stdmelding sm"
    + "      , mld_v_aanwezigdiscipline di"
    + "      , ins_srtdiscipline sd"
    + filtClause.from
    + "  WHERE (UPPER(" + lcl.xsql('mld_stdmelding_omschrijving', 'sm.mld_stdmelding_key') + ") LIKE " + safe.quoted_sql_wild("%" + chars + "%")
    + "          OR UPPER("+ lcl.xsql('mld_stdmelding_hint', 'sm.mld_stdmelding_key') + ") LIKE " + safe.quoted_sql_wild("%" + chars + "%")
    + "          OR UPPER("+ lcl.xsql('ins_discipline_omschrijving', 'di.ins_discipline_key') + ") LIKE " + safe.quoted_sql_wild("%" + chars + "%") +")"
    + "    AND sm.mld_ins_discipline_key = di.ins_discipline_key"
    + "    AND mld_stdmelding_verwijder is NULL"
    + "    AND di.ins_srtdiscipline_key = sd.ins_srtdiscipline_key"
    + filtClause.where
    + " ORDER BY sm.mld_stdmelding_volgnr, mld_stdmelding_omschrijving";

WriteResult2(sql, { bAll:          bAll,
                   fldName:       "mld_stdmelding_omschrijving",
                   fldKey:        "mld_stdmelding_key",
                   fldDesc:       "ins_discipline_omschrijving",
                   fldExtraParam: "extra",
                   fldTitle:      'mld_stdmelding_hint'
                 })
%>
