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

    File:               SuggestKenmerkDomein
    Description:        Suggereer flexkenmerk
    Parameters:         k             : De eerste paar letters
                        filtcode      : beperk de lijst met getoonde waarden
                                        A = Actieve waarden (wordt bij toevoegen gebruikt)
                        kenmerk_key   : key van het kenmerk
                        module:       : "ALG", "PRS" etc.
    Globals:
    Context:
    Note:  Als kenmerk_key gegeven is, wordt daarmee de bijbhorende kenmerkdomein_key bepaald.

*/ %>
<%
JSON_Result = true;
%>

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

<%
var chars = getQParam("k", "");
var kenmerkdomein_key = getQParamInt("kenmerkdomein_key");
var parent_key = getQParamInt("parent_key", -1);
var bAll = getQParamInt("SuggestAll", 0) == 1;
var filtcode = getQParam("filtcode", "");

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

var sql = "SELECT fac_kenmerkdomein_objectnaam tblOJ"
        + ",      fac_kenmerkdomein_kolomnaam tblKN"
        + ",      fac_kenmerkdomein_kolomtxt tblKT"
        + ",      COALESCE(fac_kenmerkdomein_kolomtxt2, '') tblKT2"
        + ",      fac_kenmerkdomein_verval tblKV"
        + ",      fac_kenmerkdomein_xmlnode"
        + ",      fac_usrtab_key"
        + "  FROM fac_kenmerkdomein"
        + " WHERE fac_kenmerkdomein_key = " + kenmerkdomein_key;
var oRs = Oracle.Execute(sql);
if (oRs("tblOJ").value == "FAC_USRDATA")
{
  sql = "SELECT fac_usrdata_key key"
      + "     , " + lcl.xsql('fac_usrdata_omschr', 'fac_usrdata_key') + " name"
      + "     , " + lcl.xsql('fac_usrdata_omschr2', 'fac_usrdata_key') + " descr"
      + "     , fac_usrdata_code code"  // not for GUI
      + "     , fac_usrdata_vervaldatum"
      + "  FROM fac_usrdata"
      + " WHERE fac_usrtab_key = " + oRs("fac_usrtab_key").Value
      + "   AND fac_usrdata_verwijder IS NULL"
      + "   AND (fac_usrdata_vervaldatum IS NULL OR fac_usrdata_vervaldatum > SYSDATE)"
      + "   AND (  UPPER("+ lcl.xsql('fac_usrdata_omschr','fac_usrdata_key') +") LIKE " + safe.quoted_sql_wild2(chars)
      + "       OR UPPER("+ lcl.xsql('fac_usrdata_omschr2','fac_usrdata_key') +") LIKE " + safe.quoted_sql_wild2(chars)+")"
      + (parent_key > 0
        ? " AND fac_usrdata_parentkey = " + parent_key
        : "")
      + " ORDER BY FAC_usrdata_volgnr, UPPER(" + lcl.xsql('fac_usrdata_omschr', 'fac_usrdata_key') + ")";
}
else
{

  if (oRs("fac_kenmerkdomein_xmlnode").Value == 'perslid') // Zorg dat André ook gevonden wordt als je Andre tikt
  {
    Oracle.Execute("alter session set nls_comp = 'LINGUISTIC' NLS_SORT = 'BINARY_AI'");
  }
  var vervalkolom = oRs("tblKV").value;
  sql = "SELECT " + oRs("tblKN").value + " key"
      + "     , TO_CHAR(SUBSTR(" + oRs("tblKT").value + ", 1, 4000))"
                  + (vervalkolom
                    ? " || CASE WHEN  " + vervalkolom + " < SYSDATE THEN " + safe.quoted_sql(L("lcl_inactive_data_suffix")) + " END"
                    : ""
                    )
                  + " name"
      + "     , " + oRs("tblKT2").value + " descr"
      + "  FROM " + oRs("tblOJ").value
      + " WHERE (  UPPER(" + oRs("tblKT").value + ") LIKE " + safe.quoted_sql_wild2(chars)
      + (oRs("tblKT2").value?"       OR UPPER(" + oRs("tblKT2").value + ") LIKE " + safe.quoted_sql_wild2(chars):"")
      + ")"
      + "   AND (" + vervalkolom + " IS NULL OR " + vervalkolom + " > SYSDATE)"
      + "  ORDER BY 2, 1";
}
oRs.Close();

//WriteResult(sql, bAll, "descr", "key", "name");
WriteResult2(sql, { bAll: bAll,
                    fldName: "name",
                    fldKey:  "key",
                    fldDesc: "descr"
                  });
%>
<% ASPPAGE_END(); %>
