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

    File:           bgt/bgt_search.asp
    Description:    Menuoptie BGT Totaaloverzicht
    Parameters:
    Context:
    Note:

    @todo
    - project (dis_key) is vereist
    - deelproject (bgt_project) is optioneel. Indien gegeven dan niet muteerbaar getoond, anders te kiezen default leeg
    - groepering kan als groupby ook optioneel worden meegegeven


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

<!-- #include file="../Shared/disciplineselector.inc" -->
<%

FCLTHeader.Requires({ plugins:["suggest", "jQuery", "bootstrap"],
                      js: [ "jQuery-ui.js"]
                    });

/***** Get webform parameters *****/
var autosearch = getQParamInt("autosearch", 0) == 1;

var bgtdisc_key = getQParamInt("dis_key"); // Project
var bgtproj_key = getQParamInt("bgtproj_key", -1); // Deelproject
var bgtrubr_key = getQParamInt("bgtrubr_key", -1); // Rubriek
var groupby = getQParamInt("groupby", -1); // Groepering

var autfunctionBGTUSE = "WEB_BGTUSE";
var authparamsBGTUSE = user.checkAutorisation("WEB_BGTUSE");
%>
<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script type="text/javascript">

        $(function ()
        {
            setTimeout("change_project()", 100); // listbox rubriek laden met geselecteerde project_key.
        });

		function doSubmit()
		{
            document.forms.u2.submit();
        }
<% if (autosearch) { %>
      window.afterfiltersready = function () { doSubmit() };
<%}%>

    function change_project()
    {
        fill_selector("R", $("#bgtproj_key :selected").val());
        if ($("#bgtproj_key :selected").length > 1)
        {
            $("#budgetcostcategory").prop("disabled", true).addClass("btn_disabled");
            $("#costtypegroup").prop("disabled", true).addClass("btn_disabled");
            $("#groupby").val(5); // zet groepering op Rubriek.
            $("#groupby").prop("disabled", true).addClass("btn_disabled");
        }
        else
        {
            $("#budgetcostcategory").prop("disabled", false).removeClass("btn_disabled");
            $("#costtypegroup").prop("disabled", false).removeClass("btn_disabled");
            $("#groupby").prop("disabled", false).removeClass("btn_disabled");
        }
    }
    function change_rubriek()
    {
        fill_selector("G", $("#budgetcostcategory :selected").val());
    }

    function fill_selector(niveau, parent_key, init_key)
    {
        $.getJSON( "../bgt/get_bgt_info_ajax.asp",
                   { req_info: "projectsearch",
                     niveau: niveau,
                     parent_key: parent_key,
                     init_key: init_key
                   },
                   process_info);
    }
    function process_info(data)
    {
        var select;
        switch(data.niveau)
        {
            //case "D":  select = $("#budgetdiscipline"); break;
            //case "P":  select = $("#budgetproject"); break;
            case "R":  select = $("#budgetcostcategory"); break;
            case "G":  select = $("#costtypegroup"); break;
            //case "S":  select = $("#costtype"); break;
            //case "KP": select = $("#account"); break;
        }

        var sel_key = -1;
        $new_lov = $("<option>").val(-1).html("");
        select.empty().append($new_lov);
        if (data.lov.length > 0)
        {
            for (i=0; i < data.lov.length;i++)
            {
                var opt = data.lov[i];
                $new_lov = $("<option>").val(opt.key).html(opt.oms);
                if (opt.sel)
                {
                    $new_lov.attr("selected", "selected");
                    sel_key = opt.key;
                }

                if (data.niveau == "KP")
                {
                    $new_lov.attr("par_key",  opt.atr.par_key);
                }
                else
                {
                    $new_lov.attr("incl_btw", opt.atr.btw_inc);
                    $new_lov.attr("perc_btw", opt.atr.btw_val);
                }
                select.append($new_lov);
            }
            select.prop("disabled", false);
        }
        else
        {
            select.prop("disabled", true);
        }

        if (sel_key > -1)
            select.val(sel_key);
    }

    </script>
  </head>

  <body id="searchbody">
      <form name="u2" target="workFrame" action="bgt_search_list.asp" method="get">
        <% SEARCH_PAGE_START({ header: L("lcl_filterblok") });
        SEARCH_BLOCK_START();%>
        <input type="hidden" name="autfunctionBGTUSE" id="autfunctionBGTUSE" value="<%=autfunctionBGTUSE%>">
        <input type="hidden" name="dis_key" value="<%=bgtdisc_key%>"> <%
            var sql = "SELECT bgt_project_key"
                    + "     , bgt_project_code||' '||bgt_project_omschrijving"
                    + "  FROM bgt_project"
                    + " WHERE bgt_project_verwijder IS NULL"
                    + "   AND ins_discipline_key = "+ bgtdisc_key
                    + " ORDER BY bgt_project_volgnr"
                    + "        , bgt_project_code";

            FCLTselector("bgtproj_key",
                         sql,
                         { label: L("bgt_project"),
                           xdisable: bgtproj_key > -1,  // dan komt de waarde niet door, jammer
                           xreadonly: bgtproj_key > -1, // dan komt de waarde niet door, jammer
                           multi: true,
                           emptyOption: null, //L("lcl_all"),
                           initKey: bgtproj_key,
                           onChange: "change_project()"
                          }
                        );

            var sql = "SELECT bgt_kostenrubriek_key"
                    + "     , bgt_kostenrubriek_code||' '||bgt_kostenrubriek_oms"
                    + "  FROM bgt_kostenrubriek"
                    + " WHERE bgt_project_key = " + bgtproj_key;

            FCLTselector("budgetcostcategory",
                         sql,
                         { "label": "Rubriek",
                           onChange: "change_rubriek()"
                         }
                        );
            var sql = "SELECT prs_kostensoortgrp_key"
                    + "     , prs_kostensoortgrp_altcode||' '||prs_kostensoortgrp_oms"
                    + "  FROM prs_kostensoortgrp"
                    + " WHERE bgt_kostenrubriek_key = " + bgtrubr_key;
            FCLTselector("costtypegroup",
                         sql,
                         { "label": "Groep"
                         }
                        );



            sql = "SELECT "
                  + "               1, " + safe.qL("lcl_fin_invoices")   + ", 6 FROM DUAL"
                  + " UNION SELECT  2, " + safe.qL("lcl_orders")         + ", 5 FROM DUAL"
                  + " UNION SELECT  3, " + safe.qL("prs_kostensoort")    + ", 4 FROM DUAL"  // de default
                  + " UNION SELECT  4, " + safe.qL("prs_kostensoortgrp") + ", 3 FROM DUAL"
                  + " UNION SELECT  5, " + safe.qL("bgt_kostenrubriek")  + ", 2 FROM DUAL"
                  + " UNION SELECT  6, " + safe.qL("bgt_project")        + ", 1 FROM DUAL"
                  + " ORDER BY 3";
            FCLTselector("groupby",
                         sql,
                         { label: L("lcl_bgt_group"),
                           xreadonly: groupby > -1,
                           initKey: (groupby == -1 ? 5 : groupby)
                         }
                        );
SEARCH_BLOCK_END(); 
SEARCH_BLOCK_START();
            FCLTcalendar("date_from",
                         { label: L("lcl_period_from"),  /* TODO default startdatum project? */
                           initEmpty: true
                         });

            FCLTcalendar("date_to",
                         { label: L("lcl_period_to"),
                           initEmpty: true
                         });
            CHECKBOXTR(L("lcl_fin_total_sum"), "fldbgtinclbtw", "bgt_inclbtw", false);
      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(); %> 
