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

     TODO: styling eruit, kostenplaatsselector toepassen. Verder niets.

*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/calendar.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/kostenplaatsselector.inc" -->
<!-- #include file="../Shared/kostensoortselector.inc" -->

<%
FCLTHeader.Requires({ plugins:["jQuery"] });

var autosearch = getQParamInt("autosearch", 1) == 1;
var date_from = getQParamDate("date_from", new Date());
var date_to = getQParamDate("date_to", new Date());
var budgetoverz = getQParamInt("budget", 0) == 1;
var kp_key = getQParamInt("kp_key", -1);

if (budgetoverz)  // Laat voor budget=1 de periode altijd op de eerste van de maand beginnen.
  date_from.setDate(1);

%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script language=JavaScript>
      async function doSubmit()
      {
         if (!await validateForm("u2"))
            return false;
         document.forms.u2.submit();
      }
      function checkWhatDate(box) {
        if (box==1) {
          if (document.getElementById("uitv_date").checked) document.getElementById("exp_date").checked = false;
          else document.getElementById("exp_date").checked = true;
        }
        else if (box==2) {
          if (document.getElementById("exp_date").checked) document.getElementById("uitv_date").checked = false;
          else document.getElementById("uitv_date").checked = true;
        }
      }

<%
			if (autosearch)
    	{ %>
				$(function() { doSubmit() });
<%  	} %>
    </script>
  </head>

<% // no authorisation needed at this point
   // Should we check that we are logged in or so instead?
%>
  <body id="searchbody">
    <form name="u2" method="get" action="../prs/kpn_overview_list.asp" target="workFrame">
      <% SEARCH_PAGE_START({ header: L("lcl_filterblok") });
        SEARCH_BLOCK_START("searchtable", L("lcl_filterblok"));%>
      <%         // <!-- Datum van -->
           FCLTcalendar("date_from",
                         { label : L("lcl_period_from"),
                           datum: date_from,
                           trclass:"primsearch",
                           volgnr: 1,
                           required: true
                         });
           // <!-- Datum tot -->
           FCLTcalendar("date_to",
                         { label: L("lcl_period_to"),
                           datum: date_to,
                           trclass:"primsearch",
                           volgnr: 2,
                           required: true
                         });
%>

           <td colspan="2"><nobr>
               <input type="checkbox" id="uitv_date" name="uitv_date" onclick='checkWhatDate(1);' value="1" checked>
               <label for="uitv_date"><%=L("lcl_prs_uitv_date")%></label>
               <input type="checkbox" id="exp_date"  name="exp_date" onclick='checkWhatDate(2);' value="1">
               <label for="exp_date"><%=L("lcl_prs_exp_date")%></label></nobr>
           </td>

<%         sql = "SELECT 1, " + safe.quoted_sql(L("lcl_prs_kosten")) + ", 1 FROM DUAL"
               //+ " UNION SELECT  2, " + safe.quoted_sql(L("lcl_prs_kostenpl_group")) + ", 2 FROM DUAL"
               + " ORDER BY 3";
           FCLTselector("groupby",
                        sql,
                        { initKey: budgetoverz? 1 : -1,
                          label: L("lcl_mld_groepering"),
                          emptyOption : ""
                        }); %>
<% SEARCH_BLOCK_END();
 SEARCH_BLOCK_START(); %>
<!-- Kostenplaats -->
<%
 FCLTkostenplaatsselector( "kostpl",
                           "kostpl",
                           user_key,
                           { kostenplaatsKey: kp_key,
                             label: L("lcl_prs_kosten"),
                             whenEmpty: L("lcl_all"),
                             inzage: true,
                             mine: true
                           });

sql = "SELECT prs_kostenplaatsgrp_key"
    + "     , prs_kostenplaatsgrp_oms"
    + "  FROM prs_kostenplaatsgrp kpg"
    // Kostenplaatsgroep van kostenplaatsen waar ik inzage rechten op heb of waarvan ik kp of kpg budgethouder ben.
    + " WHERE kpg.prs_kostenplaatsgrp_key IN (SELECT k.prs_kostenplaatsgrp_key"
      + "                                       FROM prs_v_aanwezigkostenplaats k"
      + "                                          , prs_perslidkostenplaats pk"
      + "                                      WHERE pk.prs_kostenplaats_key = k.prs_kostenplaats_key"
      + "                                        AND pk.prs_perslidkostenplaats_inzage = 1"
      + "                                        AND pk.prs_perslid_key = " + user_key
      + "                                     UNION"
      + "                                     SELECT k.prs_kostenplaatsgrp_key"
      + "                                       FROM prs_v_aanwezigkostenplaats k"
      + "                                          , prs_kostenplaatsgrp kg"
      + "                                      WHERE k.prs_kostenplaatsgrp_key = kg.prs_kostenplaatsgrp_key(+)"
      + "                                        AND (k.prs_perslid_key = " + user_key + " OR kg.prs_perslid_key = " + user_key + "))"
    + " ORDER BY 2,1";
FCLTselector("kostgrp",
             sql,
             { label: L("lcl_prs_kostenpl_group"),
               emptyOption: L("lcl_all")
             });

FCLTkostensoortselector("kostsrt",
                        "ks_key",
                        { label: L("lcl_charge_type"),
                          whenEmpty: L("lcl_all"),
                          filtercode: "ALL"
                        });

sql = " SELECT prs_kostensoortgrp_key"
    + "      , "+lcl.xsqla('ksg.prs_kostensoortgrp_oms','ksg.prs_kostensoortgrp_key')
    + " FROM prs_kostensoortgrp ksg"
    + " ORDER BY 2, 1";

FCLTselector("kostsrtgrp", sql,
                    { label: L("lcl_charge_type_group"),
                      emptyOption: L("lcl_all")
                   });
%>
<%    SEARCH_BLOCK_END();

    var buttons =  [ {title: L("lcl_overview_list"),  action:"doSubmit()", icon: "fa-clipboard-list" } ];
    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(); %>
