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

    File:           mld_search_list_xd.asp
    Description:    Show a readonly list of issues from multiple environments (crossdomain xd)
                    using the issues-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" -->
<!-- #include file="../mld/mld.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));

// Statussen, gejat uit mld_search_list.asp. Achteraf gezien..
// uit de mi_search-mselect
var mldstatus_arr = getQParamIntArray("mldstatusmulti", "");
// uit de checkbox versies
var st_New  = getQParamInt("mldNew", 0) == 1; // Te behandelen
var st_Self = getQParamInt("mldSelf", 0) == 1; // In behandeling
var st_Busy = getQParamInt("mldBusy", 0) == 1; // Bij Frontoffice
var st_Rej  = getQParamInt("mldRej", 0) == 1; // Afgewezen
var st_Afm  = getQParamInt("mldAfm", 0) == 1; // Afgemeld
var st_Ver  = getQParamInt("mldVer", 0) == 1; // Archief
var st_Done = getQParamInt("mldDone", 0) == 1; // Gereed

// data-ranges? via start_veldnaam en end_veldnaam
var mldnum          = getQParamInt("mldnum", -1, true); // Meldingnr = key
var issuedate_start = getQParamDate("date_from", null); // Periode van
var issuedate_end   = getQParamDate("date_to", null); // Perdiode tot
var searchtekst     = getQParam("searchtekst", ""); // Zoektekst
var loctekst        = getQParam("loctekst", ""); // Locatie wildcard
var meldertekst     = getQParam("meldertekst", "");
var handlertekst    = getQParam("handlertekst", "");

flags = [];
for (flagkey=0; flagkey < S('mld_melding_flags'); flagkey++)
{
if (getQParam("flag" + flagkey, "off") == "on")
    flags.push(flagkey);
}


FCLTHeader.Requires({ plugins: ["jQuery"],
                      js: ["jquery-ui.js"]});
FCLTHeader.Title(L("lcl_xd_issues"));
%>
<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/mld/mld_melding.asp?mld_key=" + lKey + "<%=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/issues.json?scope="+urole;
    //lokaal testen: url = "http://<cust>.sgf12/facilitor5iwork/appl/" + "api2/issues.json?scope="+urole;

    // Condities toevoegen (komen van mld_search.asp)
    if (mldnum > -1) url += "&id="+ mldnum;
    if (searchtekst != "") url += "&description="+ safe.url(searchtekst);  // vast decode of zo
    if (loctekst) url += "&location=~" + safe.url(loctekst);
    if (meldertekst) url += "&requestor=~" + safe.url(meldertekst);
    if (handlertekst) url += "&handler=~" + safe.url(handlertekst);

    if (issuedate_start) url += "&start_issuedate="+ issuedate_start.toISOString();
    if (issuedate_end) url += "&end_issuedate="+ issuedate_end.toISOString();
    var statussen = [];
    if ( mldstatus_arr != "") {
        statussen = mldstatus_arr;
    }
    else
    {
        if ((frontend && st_Busy) || (fronto && st_Self) || (backo && st_Busy)) statussen.push (0);
        if (st_Rej)                                                             statussen.push (1);
        if ((frontend && st_New)  || (fronto && st_Busy) || (backo && st_New))  statussen.push (2);
        if ((frontend && st_Busy) || (fronto && st_Busy) || (backo && st_New))  statussen.push (3);
        if ((frontend && st_Busy) || (fronto && st_Busy) || (backo && st_Self)) statussen.push (4, 7);
        if ((frontend && st_Done) || (fronto && st_Afm) || (backo && st_Afm))   statussen.push (5);
        if ((frontend && st_Done) || (fronto && st_Ver) || (backo && st_Ver))   statussen.push (6);
        if ((frontend && st_Busy) || (fronto && st_Busy) || (backo && st_New))  statussen.push (99);
     }
    if (statussen.length > 0)
        url += "&status="+statussen.join(',');

    if (flags.length > 0)
        url += "&flag="+flags.join(',');

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

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

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

// 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 = [];
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 issues
       var thisdata = gottenitem[j];
       dataset.push({
                      custid: cid,
                      id: thisdata.id,
                      name: thisdata.name,
                      requestor: thisdata.requestor.name,
                      issuedate: thisdata.issuedate,
                      enddate: thisdata.enddate,
                      stdmelding: thisdata.issuetype.name,
                      description: thisdata.description,
                      status: thisdata.status.name,
                      xstatus: thisdata.xstatus.name,
                      flag: thisdata.flag,
                      handler: thisdata.handler ? thisdata.handler.name : null,
                      plaatsaanduiding: xd.fnPlaatsaanduiding(thisdata.location, thisdata.building, thisdata.floor, thisdata.room)
                   });
    }
}
dataset.sort(function (a, b) { return (a.enddate <= b.enddate? -1: 1) });

function fnrowClass(oRs)
{
    var resclass ="";
    var lnow = new Date();
    if (oRs.Fields("enddate").Value < lnow)
        resclass += ' expired2';
    return resclass;
}

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

rst.addColumn(new Column({caption: "o", content: xd.fncolFlags }));
rst.addColumn(new Column({caption: L("lcl_mld_number"), content: xd.fnCustName }));
rst.addColumn(new Column({caption: L("lcl_mld_complain"), content: "stdmelding"}));
rst.addColumn(new Column({caption: L("lcl_mld_date"), content: "issuedate", datatype: "date"}));
rst.addColumn(new Column({caption: L("lcl_time"), combine: true, content: "issuedate", datatype: "time"}));
rst.addColumn(new Column({caption: L("lcl_caller"), content: "requestor"}));
rst.addColumn(new Column({caption: L("lcl_place"), content: "plaatsaanduiding"}));
rst.addColumn(new Column({caption: L("lcl_txt"), content: "description"}));
rst.addColumn(new Column({caption: L("lcl_mld_status"), content: "xstatus"}));
rst.addColumn(new Column({caption: L("lcl_mld_behandelaar"), content: "handler"}));
rst.addColumn(new Column({caption: L("lcl_end_date_plan"), content: "enddate", datatype: "datetime"}));

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

rst.processResultset();

%>