<%@ LANGUAGE = JavaScript %>
<% /*
     $Revision$
     $Id$
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/afdelingselector.inc" -->
<!-- #include file="../Shared/persoonselector.inc" -->
<!-- #include file="../Shared/plaatsselector.inc" -->
<!-- #include file="../Shared/calendar.inc" -->
<%
FCLTHeader.Requires({ js: ["./bez_search.js", "jquery.timepicker-table.js"],
                      css: ["timePicker-table.css"]
                    });

/***** Get webform parameters *****/
var urole = getQParamSafe("urole");
var fronto = (urole == "fo");
var backo = (urole == "bo");
var minfo = (urole == "mi");
var frontend = (urole == "fe" || (!fronto && !backo && !minfo));

if (minfo)
{
  var date_from = getQParamDate("date_from", null);
  var date_to = getQParamDate("date_to", null);

  if (!date_from && date_to)
    date_from = date_to;
  else if (date_from && !date_to)
    date_to = date_from;
  else if (!date_from && !date_to)
  {
    var dt = new Date(); // Vandaag
    dt = new Date(dt.getFullYear(), dt.getMonth(), 1); // Eerste dag van de huidige maand
    var dateto = dt.setDate(0); // Laatste dag van de vorige maand
    var datefrom = dt.setDate(1); // Eerste dag van de vorige maand

    date_from = new Date(parseInt(datefrom, 10));
    date_to = new Date(parseInt(dateto, 10));
  }
}
else
{
  var date_from = getQParamDate("date_from", new Date);
  var date_to = getQParamDate("date_to", new Date);
}

var time_hour = (new Date()).getHours();
var time_min = (new Date()).getMinutes();
var vis_end = time_hour + (time_min / 60) + S("vis_bo_end_interval");
if (vis_end > S("vis_t2"))
  vis_end = S("vis_t2");
var starttimedate = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), Math.floor(S("vis_t1")), (S("vis_t1") - Math.floor(S("vis_t1"))) * 60)
var endtimedate = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), Math.floor(vis_end), Math.round((vis_end - Math.floor(vis_end)) * 60))
// padout functie zet een voorloop nul voor één cijfer (Shared.inc)
var starttime = padout(Math.floor(S("vis_t1"))) + ":" + padout((S("vis_t1") - Math.floor(S("vis_t1"))) * 60);
var endtime = padout(Math.floor(vis_end)) + ":" + padout(Math.round((vis_end - Math.floor(vis_end)) * 60));
var step = S("vis_h") * 60;

var regio_key = getQParamInt("regio_key", -1); // Regio
var district_key = getQParamInt("district_key", -1);
var locatie_key = getQParamInt("locatie_key", (!minfo? user.alg_locatie_key() : -1)); // Als geen input en wel eigen werkplek dan eigen locatie
var gebouw_key = getQParamInt("gebouw_key", -1);
var verdieping_key = getQParamInt("verdieping_key", -1);

var afspr_key = getQParamInt("afspr_key", "", true); // Afspraak nummer
var prs_key = getQParamInt("prs_key", -1); // Aanvrager key
var host_key = getQParamInt("host_key", -1); // Bezoek voor / gastheer key
var dep_key = getQParamInt("dep_key", -1);
var searchtext = getQParam("searchtext", ""); // Zoektekst
var groupby = getQParamInt("groupby", 0); // Groepering, default waarde district(=0)
var alg_startlevel = getQParamInt("alg_startlevel", -1); // Startlevel van plaatsselector
var bezPark = getQParamInt("bezPark", 0) == 1;
var action_key = getQParamInt("action_key", -1);

var bezExpect = getQParam("bezExpect", 1) == 1;
var bezInside = getQParam("bezInside", fronto ? 1 : S("vis_bo_show_all")) == 1; // Als S("vis_bo_show_all") == 1 dan alle checkboxen geselecteerd
var bezDepart = getQParam("bezDepart", S("vis_bo_show_all")) == 1; // Als S("vis_bo_show_all") == 1 dan alle checkboxen geselecteerd
var bezClosed = getQParam("bezClosed", S("vis_bo_show_all")) == 1; // Als S("vis_bo_show_all") == 1 dan alle checkboxen geselecteerd
if (!bezExpect && !bezInside && !bezDepart && !bezClosed)
  bezExpect = true;

/***** End get webform parameters *****/

var autfunction = "";
switch(urole)
{
  case "fe": autfunction = "WEB_BEZUSE";
           break;
  case "fo": autfunction = "WEB_BEZFOF";
           break;
  case "bo": autfunction = "WEB_BEZBOF";
           break;
  case "mi": autfunction = "WEB_BEZBAC";
           break;
}

// De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
var authparams = user.checkAutorisation(autfunction);
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>

    <script type="text/javascript">

        var vis_checkout_enable = (<%=S("vis_checkout_enable")%> == 1);
        $(function()
        {
<%        if (frontend) { %>
               window.afterfiltersready = function () { document.forms.u2.submit() };
<%        } %>
<%        else { %>
<%          if (!minfo) { %>
              if ($("#locatiekey").val() > 0)
              {
                window.afterfiltersready = function () { document.forms.u2.submit() };
              }
<%          } %>
<%        } %>
        });

    </script>
  </head>

  <body id="searchbody">
    <form name="u2" target="workFrame" action="bez_search_list.asp" method="get">
      <% SEARCH_PAGE_START({ header: L("lcl_filterblok") });
      SEARCH_BLOCK_START(); %>
        <input type="hidden" name="urole" value="<%=urole%>">
        <input type="hidden" name="user_key" value="<%=user_key%>">
        <input type="hidden" name="contactonly" value="<%=getQParamInt("contactonly",0)%>">

<%          if (fronto || backo || frontend)
            { %>
              <!-- Afspraaknummer -->
              <tr class="primsearch">
                <td class="label"><label for="afspr_key"><%=L("lcl_vis_appointment_no")%></label></td>
                <td><input type="text" class="fldshort" name="afspr_key" value="<%=afspr_key%>"></td>
              </tr>
<%          }

            if (!frontend)
            { %>
              <!-- Zoektekst -->
              <tr class="primsearch">
                <td class="label"><label for="searchtext"><%=L("lcl_search_diff_fields")%></label></td>
                <td><input type="text" class="fld wildcard" name="searchtext" value="<%=safe.htmlattr(searchtext)%>"></td>
              </tr>
<%          }

            // <!-- Datum -->
            FCLTcalendar( "date_from",
                          { label : L("lcl_period_from"),
                            datum: starttimedate,
                            maxPast: (frontend ? S("vis_user_history") : -1),
                            trclass: "primsearch",
                            volgnr: 1,
                            timeField: true,
                            timeStep: step,
                            startTime: starttime,
                            endTime: endtime,
                            timeClass: "secsearch"
                          });

            if (!frontend)
            {
              // <!-- Datum tot -->
                FCLTcalendar( "date_to",
                              { label: L("lcl_period_to"),
                                datum: endtimedate,
                                trclass: (minfo ? "primsearch" : "secsearch"),
                                volgnr: 2,
                                timeField: true,
                                timeStep: step,
                                startTime: starttime,
                                endTime: endtime,
                                timeClass: "secsearch"
                              });
            }

            // Parkeerplaats
            if (minfo)
                CHECKBOXTR(L("lcl_park_only"), "fldbezpark", "bezPark", bezPark)

            var initGroup = -1;
            if (minfo)
            {
              // <!-- Groepering -->
                sql = "SELECT        1, " + safe.quoted_sql(L("lcl_district")) + " FROM DUAL"
                    + " UNION SELECT 2, " + safe.quoted_sql(L("lcl_location")) + " FROM DUAL"
                    + " UNION SELECT 3, " + safe.quoted_sql(L("lcl_building")) + " FROM DUAL"
                    + " UNION SELECT 4, " + safe.quoted_sql(L("lcl_month"))    + " FROM DUAL"
                    + " UNION SELECT 5, " + safe.quoted_sql(L("lcl_week"))     + " FROM DUAL"
                    + " UNION SELECT 6, " + safe.quoted_sql(L("lcl_day"))      + " FROM DUAL"
                    + " ORDER BY 1";
                initGroup = 1;
            }
            else
            {
              sql = "       SELECT -1, " + safe.qL("lcl_res_group_none")        + ", 1 FROM DUAL"
                  + " UNION SELECT 7, " + safe.qL("lcl_bez_group_appointment") + ", 2 FROM DUAL"
                  + " ORDER BY 1";
            }
            FCLTselector("groupby",
                         sql,
                         { initKey: groupby > 0? groupby : initGroup,
                           label: L("lcl_BEZ_group"),
                           trclass: "primsearch"
                         });

            if (!frontend && S("bez_bezoekers_flags") > 1) // Flags checkboxen -->
            {
                for (flagkey=0; flagkey < S("bez_bezoekers_flags"); flagkey++)
                {
                    var label = "<label for='flag" + flagkey + "'>"
                              + "  <span class='bezflag" + flagkey + " bezs'>"
                              +      I("fa-fclt-flag" + (flagkey == 0 ? "0" : ""))
                              + "  </span> " + safe.html(L("lcl_bez_flag" + flagkey))
                              + "</label>";
                    RWCHECKBOXTR("flag" + flagkey, "fldcheck", (flagkey == 0 ? L("lcl_bez_flags") : ""), 1, { twocols: true, posthtml: label });
                }
            }
SEARCH_BLOCK_END();
SEARCH_BLOCK_START();
            if (!frontend)
            { // <!-- Locatie, gebouw en verdieping -->
                if (minfo)
                {
                  var startlevel = 1; // district
                  var endlevel = 3; // gebouw
                }
                else // front of backo
                {
                  var startlevel = 2; // locatie
                  if (S("vis_afspraak_onrgoed_uses_keys") == 1)
                    var endlevel = 4; // verdieping
                  else
                    var endlevel = 2; // locatie
                }
                FCLTplaatsselector(authparams.ALGreadlevel,
                                    { regiokey: regio_key,
                                      districtkey: district_key, // indien startlevel 2 is, dan wordt deze parameter niet gebruikt
                                      locatiekey: locatie_key,
                                      gebouwkey: gebouw_key, // indien eindlevel 2 is, dan wordt deze parameter niet gebruikt
                                      verdiepingkey: verdieping_key, // indien eindlevel 2 is, dan wordt deze parameter niet gebruikt
                                      onLocChange: "onChangeLocatie()",
                                      onGebChange: "onChangeGebouw()",
                                      //autoselect: true,
                                      multitoggle: 4,
                                      multionchange: true,
                                      startlevel: alg_startlevel >= 0? alg_startlevel : startlevel,
                                      eindlevel: endlevel,
                                      filtercode: "BEZ",
                                      secsearch: 4, // verdieping en hoger
                                      whenEmpty: L("lcl_search_generic")  // want filter
                                    });

                sql = "SELECT a.bez_actie_key"
                    + "     , " + lcl.xsql('a.bez_actie_omschrijving', 'a.bez_actie_key')
                                + "||' ('|| g.bez_actie_groep_omschrijving ||')' bez_actie_omschrijving"
                    + "  FROM bez_actie a"
                    + "     , bez_actie_groep g"
                    + " WHERE a.bez_actie_groep_key = g.bez_actie_groep_key(+)"
                    + "   AND a.bez_actie_verwijder IS NULL"
                    + "   AND g.bez_actie_groep_verwijder IS NULL"
                    + " ORDER BY g.bez_actie_groep_omschrijving"
                    + "        , a.bez_actie_omschrijving";

                var sqlcnt = "SELECT COUNT ( * ) nraction"
                           + "  FROM ("  + sql + ")";

                var oRs2 = Oracle.Execute(sqlcnt);
                var nractions = oRs2("nraction").Value;
                oRs2.close();

                if (nractions  > 1)
				{
				    FCLTselector("actie",
				                 sql,
				                 { initKey: action_key,
				                   trclass:"primsearch noxd",
				                 	 label: L("lcl_action"),
				                   emptyOption: (action_key == -1? L("lcl_search_generic") : null),
				                   readonly: action_key > -1
				                 }
				                );
            	}
            }

            if (!fronto || minfo)
            {
              // <!-- Aanvrager -->
              FCLTpersoonselector("sName_key", // requestor
                                  "sgPerson",
                                  { perslidKey: prs_key,
                                    label: L("lcl_vis_contactperson"),
                                    filtercode: "BEZR",
                                    trclass: "secsearch",
                                    autlevel: authparams.PRSreadlevel,
                                    whenEmpty: L("lcl_search_generic")  // want filter
                                  });
            }

            if (fronto || (S("vis_bo_search_host") && backo) || minfo)
            { // Akzo wil dit expliciet ook in de backoffice
              // <!-- Bezoek voor -->
              if ((S("vis_afspraak_host_uses_keys") & 1) == 1)
              {
                  FCLTpersoonselector("sHost_key",
                                      "sgHost",
                                      { perslidKey: host_key,
                                        label: L("lcl_vis_visit"),
                                        filtercode: "BEZH",
                                        trclass: "primsearch",
                                        autlevel: authparams.PRSreadlevel,
                                        whenEmpty: L("lcl_search_generic")  // want filter
                                      });
              }
            }

            if (minfo)
            {
              //<!-- Organisatie -->
                FCLTafdelingselector("sDept_key", // organisation
                                     "sgDept",
                                     { departmentKey: dep_key,
                                       label: L("lcl_dep_name_level2"), // Nu Afdeling, was L("lcl_bes_organisation") => Organisatie
                                       trclass: "secsearch",
                                       autlevel: authparams.PRSreadlevel, // Was voorheen niet aanwezig
                                       whenEmpty: L("lcl_search_generic")  // want filter
                                     });
            }

            if (backo || fronto)
            { // <!-- Statuses -->
              %>
              <tr class="primsearch">
                <td class="label"><label><%=L("lcl_bes_Bes_status")%></label></td>
                <td>
                  <div style="white-space: nowrap;">
                    <input type="checkbox" class="fldbezexpext" name="bezExpect" id="bezExpect" onClick="checkCheckboxes();" value="1" <%=bezExpect? "checked" : ""%>>
                    <label for="bezExpect"><%=L("lcl_vis_expected")%></label>
<%                  if (S("vis_checkout_enable") == 1)
                    { %>
                      <input type="checkbox" class="fldbezinside" name="bezInside" id="bezInside" onClick="checkCheckboxes();" value="1" <%=bezInside? "checked" : ""%>>
                      <label for="bezInside"><%=L("lcl_vis_inside")%></label>
                      <input type="checkbox" class="fldbezdepart" name="bezDepart" id="bezDepart" onClick="checkCheckboxes();" value="1" <%=bezDepart? "checked" : ""%>>
                      <label for="bezDepart"><%=L("lcl_vis_departed")%></label>
<%                  }
                    else
                    { %>
                      <input type="checkbox" class="fldbezclosed" name="bezClosed" id="bezClosed" onClick="checkCheckboxes();" value="1" <%=bezClosed? "checked" : ""%>>
                      <label for="bezClosed"><%=L("lcl_vis_processed")%></label>
<%                  } %>
                  </div>
                </td>
              </tr>
<%          }
      SEARCH_BLOCK_END();
      var buttons = [{ title: L("lcl_overview_list"), action: "document.forms.u2.submit();", importance: 1, icon: "fa-clipboard-list"}];
      if (!frontend)
          buttons.push({ title: L("lcl_shared_more"), action: "iface_toggleSecondarySearchblock()", id: "btnMoreLess", icon: "fa-chevron-right", importance: 3});
      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(); %>
