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

    File:        fac_faq_search.asp (FSN#15947)
    Description: Zoekt in FAQ
    Parameters:  -
    Context:
    Note:
*/ %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../shared/iface.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/faqselector.inc" -->
<!-- #include file="../Shared/plaatsselector.inc" -->
<!-- #include file="../Shared/calendar.inc" -->
<%
FCLTHeader.Requires({ plugins:["kenmerk"],
                      js: ["../FAC/fac_faq_search.js"]
                    });
var sstring = getQParam("s","");
var chksearchtextOR = getQParamInt("chksearchtextOR", 0) == 1; // boolean zoeken in tekst met OR
var urole = getQParamSafe("urole", "fe");
var fronto = (urole == "fo");
var backo = (urole == "bo");
var minfo = (urole == "mi");
var frontend = (urole == "fe" || (!fronto && !backo && !minfo));
var autosearch = getQParamInt("autosearch", -1) == 1;
var faq_key = getQParamInt("faq_key", -1);
var faq_disc_arr = getQParamIntArray("faq_disc", []);
var disc_key_arr = getQParamIntArray("disc_key_str", []); // Productgroep
var stdm_arr = getQParamIntArray("stdm_str", []); // Melding (Stdmelding)
var locatie_key = getQParamInt("locatie_key", -1); // Locatie
var gebouw_key = getQParamInt("gebouw_key", -1); // Gebouw
var verdieping_key = getQParamInt("verdieping_key", -1); // Verdieping
var ruimte_key = getQParamInt("ruimte_key", -1); // ruimte
var date_from = getQParamDate("date_from", null);
var date_to = getQParamDate("date_to", null);
var dateFromInitEmpty = true;
var dateToInitEmpty = true;
if (date_from)
{
  dateFromInitEmpty = false;
}
if (date_to)
{
  dateToInitEmpty = false;
}

if (disc_key_arr.length == 0 && stdm_arr.length != 0)
{
  var sql = "SELECT stdm.mld_ins_discipline_key FROM mld_stdmelding stdm WHERE stdm.mld_stdmelding_key = " + stdm_arr.join(",")
  oRs = Oracle.Execute(sql);
  while (!oRs.eof)
  {
    disc_key_arr[disc_key_arr.length] = oRs("mld_ins_discipline_key").Value;
    oRs.MoveNext();
  }
}


var authparamsBOF = user.checkAutorisation("WEB_FAQBOF", true); // ziet alles
var authparamsFOF = user.checkAutorisation("WEB_FAQFOF", true); // ziet ook FO-items (en eigen items)
var authparamsUSE = user.checkAutorisation("WEB_FAQUSE", true); // ziet ook FO-items (en eigen items)

user.auth_required_or_abort(authparamsBOF || authparamsFOF || authparamsUSE);

sql= "SELECT 1 FROM mld_stdmeldingfaq WHERE ROWNUM = 1";
oRs = Oracle.Execute(sql);
var anymldfaq = !oRs.eof;
oRs.close();
sql= "SELECT 1 FROM alg_algfaq WHERE ROWNUM = 1";
oRs = Oracle.Execute(sql);
var anyalgfaq = !oRs.eof;
oRs.close();

%>
<html>
  <head>
   <% FCLTHeader.Generate() %>
      <script type="text/javascript">
        async function doSubmit()
        {
          if (!await validateForm("u2"))
            return false;
          // Submitten met kenmerken
          doSubmitWithKenmerken();
        }

<%   if (autosearch && (sstring != "" || disc_key_arr.length != 0 || stdm_arr.length != 0)) { %>
       $(doSubmit);
<%   } %>

        async function myModal(init)
        {
          var url = "load_kenmerk.asp?act_key=-1"
                  + "&urole=<%=urole%>"
                  + "&advanced=1"
                  + "&hasFilter=1";
          var titel = L("lcl_faq_kenmerken");
          await showKenmerkModal(url, titel, init);
        }


      </script>

  </head>
  <body id="searchbody">
    <form name="u2" method="get" action="fac_faq_list.asp" target="workFrame">
      <% SEARCH_PAGE_START({ header: L("lcl_filterblok") });
      SEARCH_BLOCK_START();%>
            <input type="hidden" name="urole" value="<%=urole%>">
<%
              RWFIELDTR("faqnum", "fldshort", L("lcl_faq_kennisbank_nr"), (faq_key != -1 ? faq_key : ""));
%>
              <td class="label"><label for="searchtext"><%=L("lcl_search_diff_fields")%></label>
                <input type="checkbox" class="fldcheck" name="chksearchtextOR" id="chsearchtextOR" value="1" <%=chksearchtextOR? "checked" : ""%>>
                <label for="chksearchtextOR"><%=L("lcl_search_or_fields")%></label>
              </td>
              <td>
                <input type="text" class="fld wildcard" name="searchtext" value="<%=safe.htmlattr(sstring)%>">
              </td>
<%
              RWCHECKBOXTR("wholeword", "fldwholeword", L("lcl_faq_wholeword"), false);

              if (faq_disc_arr.length > 0)
              { // Als er faq_disc key(s) zijn meegegeven de selectbox niet tonen.
                for (var i=0; i<faq_disc_arr.length; i++)
                {
%>
                    <input type="hidden" id="faq_disc" name="faq_disc" value="<%=faq_disc_arr[i]%>">
<%
                }
              }
              else
              {
                var faq_aut_sql = "SELECT ins_discipline_key"
                                + "  FROM fac_v_webgebruiker g"
                                + "     , fac_functie f"
                                + " WHERE g.prs_perslid_key = " + user_key
                                + "   AND f.fac_functie_key = g.fac_functie_key"
                                + "   AND f.fac_functie_code IN (" + safe.quoted_sql_join(["WEB_FAQUSE", "WEB_FAQFOF", "WEB_FAQBOF"]) + ")";

                var faq_disc_sql = " SELECT ins_discipline_key"
                                 + "      , " + lcl.xsqla('fd.ins_discipline_omschrijving', 'fd.ins_discipline_key')
                                 + "  FROM faq_v_aanwezigdiscipline fd"
                                 + " WHERE EXISTS (SELECT 'x'"
                                 + "                 FROM fac_faq ff"
                                 + "                WHERE fd.ins_discipline_key = ff.faq_ins_discipline_key"
                                 + "                  AND fd.ins_discipline_verwijder IS NULL)"
                                 + "   AND fd.ins_discipline_key IN (" + faq_aut_sql + ")"
                                 + " ORDER BY 2";
                FCLTselector( "faq_disc",
                              faq_disc_sql,
                              { label: L("faq_discipline"),
                                multi: true,
                                emptyOption: L("lcl_search_generic")
                              });
              }

              if (authparamsBOF && anymldfaq)
              {
                // <!-- Productgroep -->
                var discsql = " SELECT m1.ins_discipline_key"
                        + ", CASE "
                          + " WHEN sd.ins_srtdiscipline_prefix IS NULL"
                          + " THEN ''"
                          + " ELSE sd.ins_srtdiscipline_prefix || '-'"
                          + " END"
                          + " || "+ lcl.xsqla('m1.ins_discipline_omschrijving', 'm1.ins_discipline_key')
                        + " FROM mld_v_aanwezigdiscipline m1"
                        + ", ins_srtdiscipline sd"
                        + ", mld_disc_params dp"
                        + " WHERE EXISTS (SELECT 'x'"
                          + "               FROM mld_stdmelding ms"
                          + "              WHERE m1.ins_discipline_key = ms.mld_ins_discipline_key"
                          + "                AND ms.mld_stdmelding_verwijder IS NULL)"
                        + "   AND m1.ins_srtdiscipline_key = sd.ins_srtdiscipline_key(+)"
                        + "   AND dp.mld_ins_discipline_key = m1.ins_discipline_key"
                        + "   AND BITAND(dp.mld_disc_params_srtgroep, 1) = 1" // Alleen primaire
                        + (disc_key_arr.length != 0
                          ? " AND m1.ins_discipline_key IN (" + disc_key_arr.join(",") + ")"
                          : "")
                        + " AND EXISTS (SELECT ''"
                          + "             FROM mld_stdmeldingfaq f"
                          + "            WHERE f.ins_discipline_key = m1.ins_discipline_key)" // Alleen waar faqs van zijn
                        + " ORDER BY 2"

                FCLTselector("mld_disc",
                             discsql,
                             { initKey: disc_key_arr.join(","),
                               label: L("lcl_vakgroup"),
                               onChange: 'updateStdm("' + urole + '");',
                               trclass: "primsearch",
                               multi: true,
                               emptyOption: (disc_key_arr.length==0? L("lcl_search_generic") : null) // Als een disc_key_arr is meegegeven dan geen lege optie
                             })

                // <!-- Melding (Stdmelding) -->
                // LET OP!!
                // deze selector wordt on-change dynamisch en totaal vervangen door ../Shared/loadStdmelding.asp!
                // Wat hier staat is dus alleen de initiele situatie.
                // SQL-queries uit loadStdmelding:

                var stdmsql = "SELECT stdm.mld_stdmelding_key"
                            + "     , " + lcl.xsqla('stdm.mld_stdmelding_omschrijving', 'stdm.mld_stdmelding_key')
                            + "     , stdm.mld_ins_discipline_key"
                            + " FROM mld_stdmelding stdm"
                            + " WHERE mld_stdmelding_verwijder IS NULL"
                            // Vervallen stdmeldingen niet tonen
                            + "   AND (stdm.mld_stdmelding_vervaldatum IS NULL"
                            + "        OR stdm.mld_stdmelding_vervaldatum > SYSDATE)"
                            + (frontend
                              ? " AND stdm.mld_stdmelding_notfrontend = 0"
                              : "")
                            + (disc_key_arr.length != 0
                              ? " AND stdm.mld_ins_discipline_key IN (" + disc_key_arr.join(",") + ")"
                              : "")
                            + " AND EXISTS (SELECT ''"
                              + "             FROM mld_stdmeldingfaq f"
                              + "            WHERE (f.mld_stdmelding_key = stdm.mld_stdmelding_key"
                              + "                   OR (f.ins_discipline_key = stdm.mld_ins_discipline_key"
                              + "                       AND f.mld_stdmelding_key IS NULL)))" // Alleen waar faqs van zijn
                            + " ORDER BY " + lcl.xsql('stdm.mld_stdmelding_omschrijving', 'stdm.mld_stdmelding_key');

                FCLTselector("stdmmulti",
                             stdmsql,
                             { initKey: stdm_arr.length == 0? -1 : stdm_arr.join(","),
                               label: L("lcl_complain"),
                               trclass: "primsearch",
                               multi: true,
                               emptyOption: L("lcl_search_generic")
                             });
              } %>
<%
SEARCH_BLOCK_END();
SEARCH_BLOCK_START();
            if (!frontend)
            {
              // Zichtbaar voor (faq level)
              sql = "           SELECT 1,  " + safe.quoted_sql(L("lcl_faq_level1")) + " FROM DUAL"
                  + " UNION ALL SELECT 2,  " + safe.quoted_sql(L("lcl_faq_level2")) + " FROM DUAL"
                  + " UNION ALL SELECT 3,  " + safe.quoted_sql(L("lcl_faq_level3")) + " FROM DUAL"
              FCLTselector("fac_faq_level",
                           sql,
                           { initKey: -1,
                             label: L("lcl_faq_level"),
                             emptyOption: L("lcl_search_generic")
                           });

              if (S("language_toggles").length > 1)
              {
                sql = "";
                for (var i = 0; i < S("language_toggles").length; i++)
                {
                  sql += (sql != ""? " UNION ": "") + "SELECT " + safe.quoted_sql(S("language_toggles")[i]) + ", " + safe.quoted_sql(S("language_toggles")[i]) + ", 2 FROM DUAL";
                }
                sql += " ORDER BY 3, 2";
                FCLTselector("fac_faq_lang",
                             sql,
                             { initKey: -1,
                               label: L("lcl_faq_lang"),
                               emptyOption: L("lcl_search_generic")
                             });
              }
            }
            if (authparamsBOF && anyalgfaq)
            {
                FCLTplaatsselector(authparamsBOF.ALGreadlevel, {
                                              locatiekey: locatie_key,
                                              gebouwkey: gebouw_key,
                                              terrein: 1,
                                              verdiepingkey: verdieping_key,
                                              ruimtekey: ruimte_key,
                                              startlevel: 2, // locatie
                                              eindlevel: 5, // ruimte
                                              whenEmpty: L("lcl_search_generic")  // want filter
                                            });
            }
            // <!-- Datum --> lcl_faq_wijzigdatum
            FCLTcalendar( "wijzigdate_from",
                          { label: L("lcl_faq_wijzigdatum_from"),
                            initEmpty: dateFromInitEmpty,
                            trclass: "primseach",
                            datum: date_from,
                            maxFuture: 0,
                            volgnr: 1
                          });
            <!-- Datum tot -->
            FCLTcalendar( "wijzigdate_to",
                          { label: L("lcl_faq_wijzigdatum_to"),
                            initEmpty: dateToInitEmpty,
                            trclass:"primsearch",
                            datum: date_to,
                            maxFuture: 0,
                            volgnr: 2
                          });
        if (authparamsBOF)
          RWCHECKBOXTR("faq_terminated", "fldfaqterminated", "", false, {twocols: true, posthtml: "<label for='indult'> " + L("lcl_faq_terminated") + "</label>"});
        SEARCH_BLOCK_END();
        var buttons =  [];
        buttons.push ( {title: L("lcl_overview_list"),  action:"doSubmit()", importance: 1, icon: "fa-clipboard-list" } ); // Overzicht
        buttons.push ( {title: L("lcl_shared_advanced"),  action: "myModal()", importance: 3, icon: "fa-filter" } );

        SIMPLE_BLOCK_START();
        CreateButtons(buttons, { entersubmit: true, showIcons: true });
        SIMPLE_BLOCK_END();
        SEARCH_PAGE_END();
%>
      </form>

    <div id="result">
      <iframe width="100%" height="100%"
              src="../Shared/empty.asp"
              name="workFrame" id="workFrame"
              onload="FcltMgr.iframeLoaded(this)"
              frameborder="0" scrolling="no">
      </iframe>
    </div>
  </body>
</html>
<% ASPPAGE_END(); %>
