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

    File:               res_search_plan_obj.asp
    Descripion:         Toon objecten planbord
    Parameters:
    Context:            vanuit res_search.asp
    Note:               Via het potloodje kan edit-mode worden geactiveerd.
*/

 %>

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

<!--#include file="../shared/iface.inc"-->
<!--#include file="../Shared/discxalg3d.inc"-->
<!--#include file="res_plan_obj.inc"-->
<!--#include file="res.inc"-->

<%
    FCLTHeader.Requires({plugins: ["jQuery"],
                              js: ["date.js", "jquery-ui.js", "./res_plan.js"]})

var urole = getQParamSafe("urole");
var rsv_ruimte_key = getQParamInt("rsv_ruimte_key", -1);
var dis_key = getQParamInt("dis_key", -1); // (Alleen) default openklappen
var srtact = getFParamInt("srtact", -1);

var dateoffset=0; // TODO deeplinking??
var date_from = getFParamDate("date_from", new Date((new Date).getTime() + dateoffset));
var date_to = getFParamDate("date_to", new Date(date_from.getTime()));
var show_park = getFParamInt("park", 0)==1;
  var fnStep = getFParam("fnStep", null); // Clientside functie voor Next/Prev

  var fronto = (urole == "fo");
  var backo = (urole == "bo");
  var frontend = (urole == "fe");

  var newres = getQParamInt("reservNew", 0) == 1;

  if (urole=="fo")
    var autfunction = "WEB_RESFOF";
  else if (urole=="bo")
    var autfunction = "WEB_RESBOF";
  else // fe
    var autfunction = "WEB_RESUSE";

  var authparams = user.checkAutorisation(autfunction);

  var disc_key = getQParamInt("disc", -1);

  var objcatmulti  = getFParamIntArray("objcatmulti", []);
  var act_key = getFParamInt("act", -1);
  if (act_key > 0)
  { // Voeg de disciplines van de activiteit aan de objectcatalogus toe.
    var sql = "SELECT res_discipline_key"
            + "  FROM res_activiteitdiscipline"
            + " WHERE res_activiteit_key = "+act_key;
    var oRs = Oracle.Execute(sql);
    while (!oRs.eof)
    {
      objcatmulti.push(oRs("res_discipline_key").value);
      oRs.MoveNext();
    }
    oRs.Close();
  }

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

// Bereken dynamisch breedte blokjes

var width_px = getQParamInt("width_px", 1024);
var nr_days = Math.round(1+(date_to.midnight() - date_from.midnight())/24/60/60/1000);
var hour_px = res.hour_px(width_px - 250, nr_days);
%>

<script type="text/javascript">


var hour_px = <%=hour_px%>;

var rsv_ruimte_key = -1;
var urole = "<%=urole%>";

// Worden in res_plan.js gebruikt:
var res_van = new Date(<%=date_from.getTime()%>);
var res_tot = new Date(<%=date_to.getTime()%>);


    function res_print()
    {
       window.print();
    }

    function res_change()
    {
        $('.plantd').droppable({
                        drop: function(event, ui) {
                                   var jtr = $(this).closest('tr');
                                   var dx = ui.offset.left - jtr.find('.plantd').offset().left;
                                   dx = (dx-1)/<%=hour_px%> + <%=safe.jsfloat(S("res_t1"))%>;
                                   var res_deel_key = $(this).closest('tr')[0].id;
                                   if (res_deel_key)
                                   {
                                       var url = "./res_update_obj.asp?rsv_deel_key="+ui.draggable[0].getAttribute("rsv_deel_key")
                                               + "&res_deel_key="+$(this).closest('tr')[0].id
                                               + "&fres_van="+dx;
                                       FcltMgr.openModalDetail(url, "",
                                                                    { callback: function (data)
                                                                        {
                                                                            if (data.refresh)
                                                                            {
                                                                                <% if (fnStep)
                                                                                    Response.Write(fnStep + "(0,0)"); // hersubmit vanuit parent
                                                                                %>
                                                                            }
                                                                         }
                                                                     }
                                                        );
                                    }

                        }
                });

        // TODO: hetzij voorkomen hetzij ondersteunen dat naar andere dag gesleept wordt
        $('table.planbord').each(function ()
            {
                $(this).find('div.gepland:not(.overflowleft,.overflowright)').draggable(
                        {cursor: 'crosshair' ,
                            containment: $(this), //.find(".plantd.reserv"),
                             start: function(event, ui) { //ui.helper[0].title="Sleep naar nieuw tijdstip";
                                                          window.beingdragged = true; /* to prevent onclick */ },
                             drag: function(event, ui) { var dx = ui.offset.left - ui.helper.closest('table').offset().left;
                                                         var dy = ui.offset.top - ui.helper.closest('table').offset().top;
                                                         var ttl = "sdx: " + dx + " dy:" + dy;
                                                         // TODO: Evt. omrekenen naar tijdstip voor tooltip
                                                         //ui.helper[0].title = ttl;
                                                         //window.beingdragged = false;  doet rsv_click wel
                                                        },
                             helper: 'clone' ,
                             opacity: 0.50,
                             appendTo: this,
                             grid: [<%=hour_px*S("res_h")%>, 20],
                             scroll: false,
                             axis: "<%=show_park?'y':'xy'%>" // werkt slecht binnen plantd
                        });
            });
        $("#btnchange").html(L("lcl_res_edit_inplanbord"));
    };

</script>

</head>
<body>
<%
   var buttons = [ { icon: "wijzigen.png", title: L("lcl_change"), action: 'res_change()', singlepress: true, id: "btnchange" },
                   { icon: "legenda.png",  title: L("lcl_res_legenda"), action: 'openLegenda()' },
                   { icon: "print.png",  title: L("lcl_print_table"), action: 'res_print()' }
                   // , { icon: "excel.png", title: L("lcl_export_to_excel"), action: 'doOutput(2)' }
                 ];
IFRAMER_HEADER(L("lcl_res_frame_voorziening"), buttons);
  var res_hint = L("lcl_res_planbord_hint1");
  if (res_hint)
    %><span class="fld"><%=res_hint%></span><%
%>
<div id="list">
<%
    var params = { hour_px: hour_px,
                   reg_key: getFParamInt("regiokey", -1),
                   dist_key: getFParamInt("districtkey", -1),
                   loc_key: getFParamInt("locatiekey", null),
                   geb_key: getFParamInt("gebouwkey", null),
                   ver_key: getFParamInt("verdiepingkey", null),
                   rui_key: getFParamInt("ruimtekey", null),
                   act_key: getFParamInt("act", -1),
                   objcat_keys:  objcatmulti,
                   fnStep : fnStep,
                   srtact : srtact,
                   planbord: true,
                   autfunction: autfunction,
                   res_van: date_from
                 }

//    ==========================================================================
//    Description:        Bouwen van het voorzieningen edit blok voor een reservering

  // Tel in elke catalogus hoeveel objecten in de gekozen ruimte echt gereservererd kunnen worden.
  // Als dat er nul zijn dan tonen we de discipline namelijk niet.
  var deel_params = {  select_fields  : " DISTINCT 1 ",
                       reg_key        : params.reg_key,
                       dist_key       : params.dist_key,
                       loc_key        : params.loc_key,
                       act_key        : params.act_key,
                       sdisc          : "dis.ins_discipline_key",
                       autfunction    : autfunction,
                       res_tot        : params.res_tot
                    };
  var sql_aantal = " ( " + get_res_deel_sql(deel_params) + ") aantal ";

  sql= "SELECT dis.ins_discipline_key,"
     + " ins_discipline_volgnr,"
     + "       "+ lcl.xsqla('dis.ins_discipline_omschrijving','dis.ins_discipline_key') +","
     +         sql_aantal
     + "  FROM RES_v_aanwezigDISCIPLINE dis"
     + "  WHERE ins_discipline_min_level=1"
     + "   AND dis.ins_discipline_key IN("
     + "        SELECT g.ins_discipline_key FROM fac_v_webgebruiker g, fac_functie f "
     + "         WHERE g.fac_functie_key = f.fac_functie_key "
     + "           AND f.fac_functie_code = " + safe.quoted_sql(autfunction)
     + "           AND g.fac_gebruiker_alg_level_read < 9"
     + "           AND g.prs_perslid_key ="+user_key
     + "        )"
     + "   AND dis.ins_discipline_key IN"
     + "        (SELECT res_discipline_key" /* DISTINCT */
     + "           from res_deel R, ins_deel D"
     + "          where r.res_deel_verwijder IS NULL"
     + "            and R.res_ins_deel_key=D.ins_deel_key)";

    if (show_park)
        sql += " AND dis.INS_DISCIPLINE_KEY = " + S("vis_parking_key");
    else if (S("vis_parking_key")>0)
        sql += " AND dis.INS_DISCIPLINE_KEY != " + S("vis_parking_key")

  if (disc_key>0) sql=sql  + " and dis.ins_discipline_key ="+disc_key;

  if (params.objcat_keys.length) sql=sql + " AND dis.ins_discipline_key IN ("+params.objcat_keys.join(",")+" )";

  if (S("res_fe_discs_only_in_res") != -1 && !fronto && !backo)
     sql=sql  + " and dis.ins_discipline_key NOT IN ("+S("res_fe_discs_only_in_res")+") ";
  sql += " GROUP BY ins_discipline_key, ins_discipline_volgnr, "+ lcl.xsql('ins_discipline_omschrijving','dis.ins_discipline_key');

  sql=sql  + " ORDER BY ins_discipline_volgnr, 3";
  oRs1 = Oracle.Execute( sql );
%>
<table>
<%
    var any = false;
    while( !oRs1.eof ) {
        if (oRs1("aantal").Value > 0) {
            any = true;
            %>
            <tr>
                <td>
                  <div class="objcatline">
                       <span onclick="$(this.parentElement).toggleClass('closed');">
                         <%=Server.HTMLencode(oRs1("INS_DISCIPLINE_OMSCHRIJVING").value)%>
                       </span>
                      <div class="objcatdiv">
        <%
          make_plan_obj(oRs1("INS_DISCIPLINE_KEY").value, date_from, date_to, params);
        %>
                      </div>
                  </div>
                </td>
            </tr>
    <%
        }
        oRs1.MoveNext();
    }
    oRs1.close();
    if (!any)
        Response.Write("<tr><td>" + L("lcl_empty_rstable") + "</td></tr>");
%>
</table>
<script type="text/javascript">
    function openLegenda()
    {
        params = { width: 140,
                   resizable: false,
                   position: ['right','top'],
                   title: "<%=L("lcl_res_legenda")%>"
                 }
        $('div#legenda').dialog(params).dialog('open');
    }
</script>
<div id="legenda" style="display:none;">
      <table width="100%" border="0">
        <tr>
          <td align="right"><img src="../Pictures/room_free.gif"></td>
          <td><% = L("lcl_free") %></td>
        </tr>
        <tr>
          <td align="right"><img src="../Pictures/room_opt.gif"></td>
          <td><% = L("lcl_optie") %></td>
        </tr>
        <tr>
          <td align="right"><img src="../Pictures/room_def.gif"></td>
          <td><% = L("lcl_def") %></td>
        </tr>
        <tr>
          <td align="right"><img src="../Pictures/room_blk.gif"></td>
          <td><% = L("lcl_blokkade") %></td>
        </tr>
        <tr>
          <td align="right"><img src="../Pictures/room_vvl.gif"></td>
          <td><% = L("lcl_vervallen") %></td>
        </tr>
        <tr>
          <td align="right"><img src="../Pictures/room_na.gif"></td>
          <td colspan=3><% = L("lcl_niet_beschikbaar") %></td>
        </tr>
<%
   if (S("res_reservering_flags") > 1)
   {
     for (flagIndex=1; flagIndex<S("res_reservering_flags"); flagIndex++)
     {
%>
        <tr>
        <td align="right"><img src="../Pictures/resflag<%=flagIndex%>.png"></td>
        <td colspan=3><% = L("lcl_res_flag" + flagIndex) %></td>
        </tr>
<%   }
  }
%>
      </table>
</div>
</div>
</body>
</html>
