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

    File:           ins_search_list_xd.asp
    Description:    Show a readonly list of objects from multiple environments (crossdomain xd)
                    using the objects-API
                    providing a means to access these in their native environment by a single click
    Parameters:     see below
    Context:        -

    Status:
    Note:

*/ %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../shared/data_recordset.inc" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="../Shared/calendar.inc" -->
<!-- #include file="../Shared/xd.inc" -->
<%

var outputmode = getQParamInt("outputmode", 0);
var excel = (outputmode == 2 || outputmode == 6);
var showall = getQParamInt("showall", 0) == 1;
var urole = getQParamSafe("urole", "fe");
var fronto = (urole == "fo");
var backo = (urole == "bo");
var frontend = (urole == "fe" || (!fronto & !backo));

var inskey = getQParamInt("ins_key", -1);

var deel = getQParam("deel", ""); // Identificatie
var deel_opm = getQParam("deel_opm", ""); // Omschrijving

    var donedatefrom = getQParamDate("donedate_from", null); // Periode van
    var donedateto = getQParamDate("donedate_to", null); // Perdiode tot

    var insObjIncl = getQParamInt("insObjIncl", 0) == 1; // Ook onderdelen
    var expObjIncl = getQParamInt("expObjIncl", 0) == 1; // Ook vervallen objecten
    var insLentOut = getQParamInt("insLentOut", 0) == 1; // Uitgegeven
    var insAvail = getQParamInt("insAvail", 0) == 1; // Beschikbaar

    var insInspect = getQParamInt("inspect", 0) == 1;
    var inspDone = getQParamInt("insInspDone", 0) == 1;
    var inspFutu = !inspDone && (getQParamInt("insInspFutu", 0) == 1);
    var inspSoon = !inspDone && (getQParamInt("insInspSoon", 0) == 1);
    var inspLate = !inspDone && (getQParamInt("insInspLate", 0) == 1);


FCLTHeader.Requires({ plugins: ["jQuery"],
                      js: ["jquery-ui.js"]});
FCLTHeader.Title(L("lcl_xd_objects"));
%>
<html>
  <head>
<%  FCLTHeader.Generate({ outputmode: outputmode }); %>
<script>
    var sd = <%=JSON.stringify(xd.SecundaryDomains)%>;

    function xd_edit(row)
    {
      $(row).addClass('dirty');
      var lKey = row.getAttribute("ROWKEY");
      var lData = eval("(" + row.getAttribute("ROWDATA") + ")");
      var name = $(row).find("span.xdcustname").text()||"";
      url = sd[lData.custid].deeplinkurl  +"appl/ins/ins_deel.asp?ins_key=" + lKey + "&urole=<%=urole%>" + "<%=S("xd_add2deeplinkurl")%>";
      FcltMgr.openDetail(url, { reuse: true, titel: name });
    }
</script>
</head>
<%

var myAPIKEY = user.prs_perslid_apikey();
if (!myAPIKEY) shared.simpel_page(L("lcl_xd_crossdomain_noapikey"));

var gottenitems = {};
var url = "";

// Execute the API2 request for all domains
for (var cid in xd.SecundaryDomains) {
    url = xd.SecundaryDomains[cid].apiurl + "api2/objects.json?scope=" + urole;
    //lokaal testen: url = "http://<cust>.sgf12/facilitor5iwork/appl/" + "api2/objects.json?scope="+urole;

    // Condities toevoegen (komen van mld_search.asp)
    if (inskey > -1) url += "&id="+ inskey;
    if (deel != "") url += "&name="+ safe.url(deel);
    if (deel_opm != "") url += "&description="+ safe.url(deel_opm);
//    if (donedatefrom) url += "&start_issuedate="+ issuedate_start.toISOString();
//    if (donedateto) url += "&end_issuedate="+ issuedate_end.toISOString();

    if (expObjIncl) url += "&expired=1";

    if (Session("logging")&1)
      url += "&logging=1";

    if (showall)
      url += "&limit=-1";

    if (insLentOut)
      url += "&lentout=1";
    if (insAvail)
      url += "&avail=1";

    reqresult = HTTP.getJSON(url, { APIKEY : myAPIKEY });
    // This result is:
    // {objects: [{...}]}} in case of matching results
    // {} in case of HTTP-errors or no result
    // {issue : {}} in case of GET by existing ID (unique)
    if (reqresult) {
        gottenitems[cid] = "object" in reqresult ? [reqresult["objects"]] : reqresult.objects;
    } else {
        __Log("no result for " + cid.name);
    }
}

// Merge these result objects into one list for the resultset to process.
// Note: we could have combined this with the previous loop but we did not
var dataset = [];
__Log("gottenitems.length:" +gottenitems.length);
for(var cid in gottenitems){ // per omgeving
    var gottenitem = gottenitems[cid]
    __Log("gottenitems["+cid+"].length:" +gottenitem.length);
    for(var j=0; j< gottenitem.length; j++){ // de objects
       var thisdata = gottenitem[j];
       var plaats = xd.fnPlaatsaanduiding(thisdata.location, thisdata.building, thisdata.floor, thisdata.room);
       dataset.push({
                      custid: cid,
                      id: thisdata.id,
                      name: thisdata.name,
                      description: thisdata.description,
                      soort: thisdata.type,
                      plaatsaanduiding: (plaats != "" ? plaats : null),
                      eigenaar: thisdata.owner
                   });
    }
}
dataset.sort(function (a, b) { return (a.id <= b.id? -1: 1) });

function fnrowClass(oRs)
{
}

var rst = new ResultsetTable({ dataset: dataset,
                               keyColumn: "id",
                               rowData: xd.fnRowData,
                               rowClass: fnrowClass,
                               ID: "multi-objects-table",
                               title: L("lcl_xd_objects"),
                               outputmode: outputmode,
                               showAll: showall
                              });

rst.addColumn(new Column({caption: L("lcl_obj_identification"), content: xd.fnCustName }));
rst.addColumn(new Column({caption: L("lcl_ins_opmerking"), content: "description" }));
rst.addColumn(new Column({caption: L("lcl_obj_position"), content: "plaatsaanduiding"}));
rst.addColumn(new Column({caption: L("lcl_obj_owner"), content: "eigenaar", combine: true}));
rst.addColumn(new Column({caption: L("lcl_obj_sort"), content: "soort"}));

rst.addAction({ action: "xd_edit", caption: L("lcl_edit"), isDefault: true } );

rst.processResultset();

%>