<% /*
     $Revision$
     $Id$
    File:               ins_verbruik_list.inc
    Description:        Show an overview of verbruiksitems. in list-form,
                        which meets with the given requirements

                        The item values are editable from whitin the list. When done the list will be submitted to itself.
    Parameters:
    Context:            Search action from ins_verbruik_search_list.asp form
    Note:
*/ %>

<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="ins.inc"-->

<%
FCLTHeader.Requires({ plugins:["jQuery"],
                      js: ["../ins/ins_verbruik.js", "num2curr.js"]
                    })


function fnrowData(oRs)
{
  var insdeelkey        = oRs("ins_deel_key").value;
  var ins_deel_oms      = oRs("omschr").value;
  var inskenmerkkey     = oRs("ins_kenmerk_key").value;
  var inskenmerkdeelkey = oRs("inskenmerkdeelkey").value;

  var data = {insdeelkey: insdeelkey,
              ins_deel_oms: ins_deel_oms,
              inskenmerkkey: inskenmerkkey,
              inskenmerkdeelkey: inskenmerkdeelkey};

  return JSON.stringify(data);
}
%>

<script type="text/javascript">
  var autosearch = 1; // automatisch laden van resultaat op search pagina

  function showHistory(row)
  {
    var insData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/INS/ins_verbruik_history.asp?urole=fe&insdeelkey=" + insData.insdeelkey + "&consumption_reg=1&inskenmerkkey=" + insData.inskenmerkkey + "&inskenmerkdeelkey=" + insData.inskenmerkdeelkey;
    FcltMgr.openDetail(url, "<%=L("lcl_history")%>" + ' ' + insData.ins_deel_oms);
  }

  function doSubmit()
  {
    var valid = true;
    $("[id^='cnt']").each(function()
        {
            var this_input = $(this).attr("id");
            valid = checkInputChk($("#"+this_input), true);
            if (!valid)
                return;
        });
    if (valid)
    {
        document.u2.submit();
    }
  }
</script>

<%
function verbruik_list(pautfunction, params)
{
  // De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
  var autfunction = "WEB_INSUSE";
  var authparams = user.checkAutorisation(autfunction);  var canAdd = (authparams.mALGwritelevel < 6);

  // Voor resulttable de globalen zetten; zou ng anders moeten.
  var outputmode = params.outputmode; // print to 0 = screen, 1 = printer, 2 = excel, etc.
  var showall = params.showall;

  var loc_key = params.loc_key;
  var srtdeel_key = params.srtdeel_key;

  var count = 1;

  var obj_srt = "";
  var object = "";
  var last_date = "";
  var last_value = -1;
  var min_consumpt = 1;
  var max_consumpt = 1;
  var saveButton = [];

  function fnrowActionEnabler(oRs)
  {
    var eFake = false;
    var data = {eFake: eFake};

    return data;
  }
%>

  <html>
    <head>
      <% FCLTHeader.Generate({outputmode:outputmode}) %>
    </head>
    <body id="listbody">
      <form action="ins_verbruik_search_list.asp?srtdeel_key=<%=srtdeel_key%>&locatiekey=<%=loc_key%>" method="post" name="u2">
<%      if (loc_key)
        {
          sql_loc = " AND d.ins_alg_locatie_key = " + loc_key;
        }
        else
        { // Note: we do not support alg-scope per discipline (yet)
          sql_loc = (authparams.ALGreadlevel >= 0
                  ? " AND d.ins_alg_locatie_key IN"
                    + " (SELECT alg_locatie_key FROM fac_v_my_locations "
                    + " WHERE prs_perslid_key = " + user_key
                    + " AND niveau = " + authparams.ALGreadlevel + ")"
                  : "");
        }

        if (srtdeel_key)
        {
          sql_obj_sort = " AND sk.ins_srtdeel_key = " + srtdeel_key;
        }
        else
        {
          sql_obj_sort = " AND sk.ins_discipline_key IN"
                       + " (SELECT gb.ins_discipline_key"
                         + " FROM fac_v_webgebruiker gb"
                         + ", fac_functie f"
                         + " WHERE prs_perslid_key = " + user_key
                         + " AND fac_functie_code = " + safe.quoted_sql(autfunction)
                         + " AND f.fac_functie_key = gb.fac_functie_key"
                         + " AND gb.fac_gebruiker_alg_level_read < 9)";
        }

        var sqln = " SELECT "+lcl.xsqla('sd.ins_srtdeel_omschrijving', 'sd.ins_srtdeel_key')
                 + "      , d.ins_deel_omschrijving || ' ('  || d.ins_deel_key || ')' omschr "
                 + "      , (SELECT akd.ins_kenmerkdeel_aanmaak"
                 + "           FROM ins_v_aanwezigkenmerkdeel akd "
                 + "          WHERE akd.ins_deel_key = d.ins_deel_key "
                 + "            AND akd.ins_kenmerk_key = sk.ins_kenmerk_key) datum "
                 + "      , ( COALESCE((SELECT akd.ins_kenmerkdeel_waarde "
                 + "                      FROM ins_v_aanwezigkenmerkdeel akd "
                 + "                     WHERE akd.ins_deel_key = d.ins_deel_key "
                 + "                       AND akd.ins_kenmerk_key = sk.ins_kenmerk_key)"
                 + "                  ,'0')) kenmerkwaarde "
                 + "      , sk.ins_srtdeel_key "
                 + "      , d.ins_deel_key "
                 + "      , sk.ins_kenmerk_key "
                 + "      , d.ins_alg_ruimte_key "
                 + "      , sk.ins_srtkenmerk_key "
                 + "      , sk.ins_srtkenmerk_kenmerktype "
                 + "      , sk.ins_srtkenmerk_lengte "
                 + "      , sk.ins_srtkenmerk_nmin "
                 + "      , sk.ins_srtkenmerk_nmax "
                 + "      , l.alg_locatie_omschrijving "
                 + "      , "+lcl.xsqla('sd.ins_srtdeel_eenheid', 'sd.ins_srtdeel_key')
                 + "      , sk.ins_kenmerk_volgnummer "
                 + "      , COALESCE ( "
                 + "        (SELECT akd.ins_kenmerkdeel_key "
                 + "           FROM ins_v_aanwezigkenmerkdeel akd "
                 + "          WHERE akd.ins_deel_key = d.ins_deel_key "
                 + "            AND akd.ins_kenmerk_key = sk.ins_kenmerk_key), "
                 + "         -1) inskenmerkdeelkey"
                 + ", "+ lcl.xsqla('sk.ins_srtkenmerk_dimensie','sk.ins_srtkenmerk_key')
                 + "   FROM ins_v_srtdeel_kenmerk sk, "
                 + "        ins_v_aanwezigdeel d, "
                 + "        ins_srtdeel sd, "
                 + "        alg_locatie l "
                 + "  WHERE sk.ins_srtdeel_key = sd.ins_srtdeel_key "
                 + "    AND d.ins_srtdeel_key = sd.ins_srtdeel_key "
                 + "    AND d.ins_alg_locatie_key = l.alg_locatie_key "
                 + sql_loc
                 + sql_obj_sort
                 + "    AND sk.ins_kenmerk_meetwaarde = 1"
                 + "    AND sk.ins_srtkenmerk_kenmerktype = 'N'"
                 + "  ORDER BY 14, 1, 2, 15";

        function invoer(oRs)
        {
          var write_ok
          if (authparams.ALGwritelevel >= 0)
          {
            sql2 = "SELECT alg_ruimte_key"
                 + " FROM fac_v_my_rooms"
                 + " WHERE prs_perslid_key = " + user_key
                 + " AND niveau = " + authparams.ALGwritelevel
                 + " AND alg_ruimte_key = " + oRs("ins_alg_ruimte_key").value;
            oRs2 = Oracle.Execute(sql2);
            if (!oRs2.eof) write_ok = true;
            else write_ok = false;
          }
          else
          {
            write_ok = true;
          }

          var result = "<input type='text' maxlength=10 style='width:100;text-align:right'"
                     + " onBlur='checkInput(event);'"
                     + " id='cnt" + count + "' name='cnt" + count + "' value=''"
                     + " value1='" + oRs("ins_srtkenmerk_kenmerktype").value + "' value2='" + oRs("ins_srtkenmerk_lengte").value + "' value3='" + oRs("ins_srtkenmerk_nmin").value + "' value4='" + oRs("ins_srtkenmerk_nmax").value + "'"

          if (!write_ok || oRs("ins_srtkenmerk_kenmerktype").value != 'N')
          {
            result += "style='background-color:#dddddd' readonly";
          }
          result += ">";

          result += "<input type='hidden' id='last_val" + count + "' name='last_val" + count + "' value='" + (oRs("kenmerkwaarde").value? oRs("kenmerkwaarde").value : "") + "'>"
                  + "<input type='hidden' id='deel_key" + count + "' name='deel_key" + count + "' value='" + oRs("ins_deel_key").value + "'>"
                  + "<input type='hidden' id='kenmerk_key" + count + "' name='kenmerk_key" + count + "' value='" + oRs("ins_kenmerk_key").value + "'>"

          count++;
          return result;
        }

        saveButton = [{icon: "opslaan.png", title: L("lcl_submit"), action: "doSubmit()"}]

        var rst = new ResultsetTable({ sql: sqln,
                                       keyColumn: "ins_deel_key",
                                       ID: "verbruiktable",
                                       title: L("lcl_obj_use_title"),
                                       showAll: showall,
                                       rowData: fnrowData,
                                       rowActionEnabler: fnrowActionEnabler,
                                       outputmode: outputmode,
                                       filterParams: params,
                                       buttons: saveButton
                                     });

        rst.addColumn(new Column({caption: L("lcl_location"), content: "alg_locatie_omschrijving"}));
        rst.addColumn(new Column({caption: L("lcl_obj_sort"), content: "ins_srtdeel_omschrijving"}));
        rst.addColumn(new Column({caption: L("lcl_obj_inf_name"), content: "omschr" }));
        rst.addColumn(new Column({caption: L("lcl_last_date"), content: "datum", datatype: "date"}));
        rst.addColumn(new Column({caption: L("lcl_last_value"), content: "kenmerkwaarde"}));
        if (outputmode == 0) // 0 = screen
          rst.addColumn(new Column({caption: L("lcl_obj_cons_value"), content: invoer}));
        rst.addColumn(new Column({caption: L("lcl_obj_unit"), content: "ins_srtkenmerk_dimensie"}));

        rst.addAction({ action: "showHistory", caption: L("lcl_history")});
        rst.addAction({ action: "", caption: "dummybutton", enabler: "eFake"}); //Dummy Button die niet getoont word, dit om de showHistory button te forceren.

        var cnt = rst.processResultset();
%>
        <input type='hidden' id='min_consumpt' name='min_consumpt' value='<%=min_consumpt%>'>
        <input type='hidden' id='max_consumpt' name='max_consumpt' value='<%=count%>'>
        <input type='hidden' id='do_submit' name='do_submit' value='1'>
        <input type='hidden' id='count' name='count' value='<%=count%>'>
        <input type='hidden' id='sel_deel_key' name='sel_deel_key' value='-1'>
        <input type='hidden' id='sel_deel_descr' name='sel_deel_descr' value=''>
        <input type='hidden' id='sel_ins_kdeel_key' name='sel_ins_kdeel_key' value='-1'>
        <input type='hidden' id='sel_kenmerk_key' name='sel_kenmerk_key' value='-1'>
<% IFACE.FORM_END(); %>
      </form>
    </body>
  </html>
<%
}
%>

