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

    File:         alg_edit_ruimte.asp
    Description:  wijzigen of toevoegen van een ruimte
    Parameters:   room_key

    Context:
    Note:

*/ %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/plaatsselector.inc" -->
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="alg_flexkenmerk.inc" -->
<!-- #include file="../API2/api2.inc" -->
<!-- #include file="../API2/model_rooms.inc" -->

<%
FCLTHeader.Requires({plugins:["suggest","jQuery", "kenmerk"],
                     js: []})

var room_key = getQParamInt("room_key", -1);
var loc_key = getQParamInt("loc_key", -1);
var geb_key = getQParamInt("geb_key", -1);
var ver_key = getQParamInt("ver_key", -1);
var cad_ruimte_nr = getQParam("cad_ruimte_nr", "");  // default nummer


var room_params = { filter: { id: room_key,
                              scope: "fe",
                              isNew: (room_key == -1 ? true : false)
                            },
                    include: ["cadcontours"]
                  };
var room_exists = false;
if (room_key > 0)
{
    var rooms = new model_rooms(room_key, room_params);          // Roep de API2 GET aan
    var room_data  = rooms.data;
    room_exists = true;
}
else
{
    var rooms = new model_rooms(-1, room_params);          // Roep de API2 GET aan
    var room_data = { id: -1, cadcontours: [] };
}
var this_alg = room_params.func_enabled;                   // room_params bevat nu ook waarden die in API2 zijn bepaald.

var room_deleted = room_exists && !!room_data.deleted;
var room_action = (room_exists ? "U" : "I");

if (!room_data.cadcontours.length)
    room_data.cadcontours = [{}];

if (!room_exists)
{
    if (cad_ruimte_nr != "")
        room_data.name = "[" + cad_ruimte_nr + "]";
}

%>

<html>
  <head>
<%
    FCLTHeader.Generate();
%>
    <script>
        function alg_submit()
        {
            document.activeElement.blur(); // trigger laatste onChanges
            if (!validateForm("u2"))
                return false;

            var data = $("[name=u2]").serialize();
            <% protectRequest.dataToken("data"); %>
            $.post($("form[name=u2]")[0].action,
                   data,
                   FcltCallbackClose,
                   "json");
            return true;
        }
        function alg_cancel()
        {
            FcltMgr.closeDetail(window, { cancel: true } );
        }
    </script>
  </head>

  <body id="editbody">
<%
    var buttons =  [ {title: L("lcl_submit"), action:"alg_submit()", icon: "opslaan.png" },
                     {title: L("lcl_cancel"), action:"alg_cancel()", icon: "undo.png" } ];
    IFRAMER_HEADER(L("lcl_alg_ruimte_frame"), buttons);
%>
    <div id="edit">
      <form name="u2"
            action="alg_edit_ruimte_save.asp?room_key=<%=room_data.id%>"
            method="post"
            onSubmit="return false">

        <input type="hidden" id="room_action" name="room_action" value="<%=room_action%>">


<%
        if (this_alg.writeman)
            manRWFIELD = RWFIELDTR;
        else
            manRWFIELD = function (a,b,c,d,e) { ROFIELDTR(b,c,d,e) }; // als geen USE of <self> dan zijn er veel readonly

        if (this_alg.writeuse && S("alg_ruimtenr_aut_use")==1) // dan mag USE er ook een paar
            useRWFIELD = RWFIELDTR;
        else
            useRWFIELD = manRWFIELD

        BLOCK_START("algLoc1", "");

          FCLTplaatsselector( this_alg.authparams("WEB_RUIMAN").ALGwritelevel,
                              { locatiekey: loc_key,
                                gebouwkey: geb_key,
                                verdiepingkey: ver_key,
                                ruimtekey: room_key,
                                startlevel: 2, //Locatie
                                eindlevel: 4, //verdieping
                                moreinfo: true,
                                readonly: (room_key > 0),
                                required: true
                              });

          // alguse mag deze twee ook als S("alg_ruimtenr_aut_use")==1
          useRWFIELD("room_nr",  "fldshort", L("lcl_estate_ruimte_man_nr"),    room_data.name,        {required: true, maxlength: 10});
          useRWFIELD("room_oms", "fld",      L("lcl_estate_ruimte_man_descr"), room_data.description, {maxlength: 30});

          sql = "SELECT alg_srtruimte_key"
              + "     , " + lcl.xsqla("alg_srtruimte_omschrijving", "alg_srtruimte_key")
              + "  FROM alg_v_aanwezigsrtruimte"
              + " ORDER BY UPPER("+lcl.xsql("alg_srtruimte_omschrijving", "alg_srtruimte_key")+")";
          FCLTselector( "room_srt",
                        sql,
                        { label: L("lcl_estate_ruimte_man_sort"),
                          initKey: (room_data.roomtype ? room_data.roomtype.id : -1),
                          emptyOption: "",
                          readonly: !this_alg.writeman
                        });

          sql = "SELECT mld_dienstniveau_key"
              + "     , " + lcl.xsqla("mld_dienstniveau_omschr", "mld_dienstniveau_key")
              + "  FROM mld_dienstniveau"
              + " ORDER BY UPPER("+lcl.xsql("mld_dienstniveau_omschr", "mld_dienstniveau_key")+")";
          FCLTselector( "dienstniveau",
                        sql,
                        { label: L("lcl_mld_dienst_niveau"),
                          initKey: (room_data.servicelevel ? room_data.servicelevel.id : -1),
                          emptyOption: "",
                          readonly: !this_alg.writeuse
                        });

        BLOCK_END();
        BLOCK_START("algLoc2", "");

          manRWFIELD("room_opp",  "fldshort",     L("lcl_estate_ruimte_bruto_vloeropp"), room_data.grossarea, {maxlength: 10, datatype: "float"});
          ROFIELDTR("fldshort", L("lcl_estate_ruimte_getekendopp"), room_data.cadcontours[0].cadarea);
          if (S("fg_contourlayers_alt1")!="" && S("fg_labellayers_alt1")!="") {
            manRWFIELD("room_oppa1",  "fldshort", L("lcl_estate_ruimte_opp_alt1"),       room_data.area1, {maxlength: 10, datatype: "float"});
            ROFIELDTR("fldshort", L("lcl_fg_tek_room_opp_g_alt1"), room_data.cadcontours[0].cadalt1);
          }
          if (S("fg_contourlayers_alt2")!="" && S("fg_labellayers_alt2")!="") {
            manRWFIELD("room_oppa2",  "fldshort", L("lcl_estate_ruimte_opp_alt2"),       room_data.area2, {maxlength: 10, datatype: "float"});
            ROFIELDTR("fldshort", L("lcl_fg_tek_room_opp_g_alt2"), room_data.cadcontours[0].cadalt2);
          }
          manRWFIELD("room_omt",  "fldshort",     L("lcl_estate_ruimte_man_omtrek"),     room_data.perimeter, {maxlength: 10, datatype: "float"});
          manRWFIELD("room_inh",  "fldshort",     L("lcl_estate_ruimte_man_inhoud"),     room_data.volume,  {maxlength: 13, datatype: "float"});
          manRWFIELD("room_opm",  "fld",          L("lcl_estate_ruimte_man_opmerk"),     room_data.remark,  {maxlength: 50});

          var times = [];
          for (i=0; i<24; i++)
          {
            sql = "           SELECT " + String(i+0/4) + ", '" + String(i) + ":00' FROM DUAL"
                + " UNION ALL SELECT " + String(i+1/4) + ", '" + String(i) + ":15' FROM DUAL"
                + " UNION ALL SELECT " + String(i+2/4) + ", '" + String(i) + ":30' FROM DUAL"
                + " UNION ALL SELECT " + String(i+3/4) + ", '" + String(i) + ":45' FROM DUAL"
            times.push(sql);
          }
          FCLTselector( "room_beginuur",
                        times.join(" UNION ALL "),
                        { label: L("lcl_estate_gebouw_beginuur"),
                          initKey: room_data.openfrom,
                          emptyOption: "",
                          readonly: !this_alg.writeman
                        }
                      );
          FCLTselector( "room_einduur",
                        times.join(" UNION ALL "),
                        { label: L("lcl_estate_gebouw_einduur"),
                          initKey: room_data.opento,
                          emptyOption: "",
                          readonly: !this_alg.writeman
                        }
                      );
          sql = "           SELECT null, " + safe.quoted_sql(L("lcl_alg_ruimte_volgens_gebouw")) + " FROM DUAL"
              + " UNION ALL SELECT 0, "    + safe.quoted_sql(L("lcl_No"))  + " FROM DUAL"
              + " UNION ALL SELECT 1, "    + safe.quoted_sql(L("lcl_Yes")) + " FROM DUAL";
          FCLTselector( "room_werkdagen",
                        sql,
                        { label: L("lcl_estate_gebouw_werkdagen"),
                          initKey: room_data.workdays,
                          readonly: !this_alg.writeman
                        }
                      );

        BLOCK_END();

        generateFlexKenmerkBlock ({
            onrgoed_key    : room_key,
            onrgoed_niveau : rooms.defaults.onrgoedlvl,
            reado          : false,
            flexcolumns    : S("alg_flexcolumns"),
            this_alg       : this_alg
            });

        IFACE.FORM_END();
%>
      </form>
    </div>
  </body>
</html>
