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

    File:           opdr_search_list_xd.asp
    Description:    Show a readonly list of order 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" -->
<%

var outputmode = getQParamInt("outputmode", 0);
var showall = getQParamInt("showall", 0) == 1;

var urole    = getQParamSafe("urole", "fe");

// de checkbox versies
var st_New  = getQParamInt("opdrNew", 0) == 1; // Te behandelen
var st_Busy = getQParamInt("opdrBusy", 0) == 1; // Bij Frontoffice
var st_Rej  = getQParamInt("opdrRej", 0) == 1; // Afgewezen
var st_Afm  = getQParamInt("opdrAfm", 0) == 1; // Afgemeld
var st_Afr  = getQParamInt("opdrAfr", 0) == 1; // Afgerond
var st_Ver  = getQParamInt("opdrVer", 0) == 1; // Archief

// Voor velden die de user in kan typen valideren we zodanig dat we er tegen kunnen
var opdr_id       = getQParam("opdr_key", null, true);   // Opdrachtnummer dus, niet de key
var orderdatefrom = getQParamDate("date_from", null);       // Periode van
var orderdateto   = getQParamDate("date_to", null);         // Periode tot
var searchtekst   = getQParam("searchtekst", "");           // Zoektekst
var suppliertekst = getQParam("suppliertekst", "");
var ordertypetekst = getQParam("ordertypetekst", "");


FCLTHeader.Requires({ plugins: ["jQuery"], js: ["jquery-ui.js"]});
FCLTHeader.Title(L("lcl_xd_orders"));
%>
<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_opdr.asp?opdr_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/orders.json?scope="+urole;

    // Condities toevoegen (komen van opdr_search.asp)
    if (opdr_id)            url += "&name="+ safe.url(opdr_id);
    if (searchtekst != "")  url += "&description="+ safe.url(searchtekst);
    if (suppliertekst) url += "&supplier=~" + safe.url(suppliertekst);
    if (ordertypetekst) url += "&ordertype=~" + safe.url(ordertypetekst);

    // De gevraagde statussen.
    var statussen = [];
    if (st_Rej == 1)  statussen.push (1, 2);
    if (st_New == 1)  statussen.push (3,5, 10);
    if (st_Busy == 1) statussen.push (4, 8);
    if (st_Afm == 1)  statussen.push (6);
    if (st_Ver == 1)  statussen.push (7);
    if (st_Afr == 1)  statussen.push (9);
    if (statussen.length > 0)
        url += "&status="+statussen.join(',');

    // Datumfilter werkt op mld_opdr_datumbegin
    if (orderdatefrom) url+="&start_orderdate="+orderdatefrom.toISOString();
    if (orderdateto) url+="&end_orderdate="+orderdateto.toISOString();

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

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

    reqresult = HTTP.getJSON(url, { APIKEY : myAPIKEY });
    if (reqresult) {
        gottenitems[cid] = "order" in reqresult ? [reqresult["order"]] : reqresult.orders;
    } 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 issues
       var thisdata = gottenitem[j];
       dataset.push({
                      custid: cid,
                      id: thisdata.id,
                      name: thisdata.name,
                      orderdate: thisdata.orderdate,
                      enddate: thisdata.enddate,
                      typeopdr: thisdata.ordertype.name,
                      description: thisdata.description,
                      remark: thisdata.remark,
                      status: thisdata.status.name,
                      supplier: thisdata.supplier ? thisdata.supplier.name : null,
                      handler: thisdata.contact ? thisdata.contact.name : null,
                      costs: thisdata.costs,
                      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-orders-table",
                               title: L("lcl_xd_orders"),
                               outputmode: outputmode,
                               showAll: showall
                              });

rst.addColumn(new Column({caption: L("lcl_shared_order"), content:xd.fnCustName }));
rst.addColumn(new Column({caption: L("lcl_mld_typeopdr"), content:"typeopdr", combine: true}));
rst.addColumn(new Column({caption: L("lcl_place"), content:"plaatsaanduiding"}));
rst.addColumn(new Column({caption: L("lcl_mld_opdr_description"), content:"description"}));
rst.addColumn(new Column({caption: L("lcl_date"), content: "orderdate", datatype: "date"}));
rst.addColumn(new Column({caption: L("lcl_mld_opdr_status"), content: "status"}));
rst.addColumn(new Column({caption: L("lcl_opdr_enddate"), content: "enddate", datatype: "datetime"}));
rst.addColumn(new Column({caption: L("lcl_handler"), content: "supplier"}));
rst.addColumn(new Column({caption: L("lcl_contact_pers"), content: "handler", combine: true}));
rst.addColumn(new Column({caption: L("lcl_mld_inf_Opmerking"), content: "remark"}));
rst.addColumn(new Column({caption: L("lcl_opdr_kosten"), content: "costs", datatype: "currency"}));

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

rst.processResultset();

%>