<%@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:           Doe niet moeitlijk over die paar interne bedrijven die misschien PRSMAN vereisen ipv RELMAN

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

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

var intern = getQParamInt("intern", -1) == 1;
var autfunction = "";
if (intern)
  autfunction = "WEB_PRSMAN";   // TODO of PRSUSE?
else
  autfunction = "WEB_RELMAN";
var authparams = user.checkAutorisation(autfunction);

var companyName = getQParam("companyName", "");
var bezoekPostcode = getQParam("bezoekPostcode", "");
var bezoekPlaats = getQParam("bezoekPlaats", "");
var loctekst = getQParam("loctekst", ""); // Locatie wildcard.
var diensttekst = getQParam("diensttekst", ""); // Dienst wildcard.
var contactpersoon = getQParam("contactPrs", "");

var leverancier = getQParamInt("leverancier", -1) == 1;
var uitvoerende = getQParamInt("uitvoerende", -1) == 1;
var contract = getQParamInt("contract", -1) == 1;
var huurder = getQParamInt("huurder", -1) == 1;

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');
      $(row).addClass('dirty');
      var lKey = row.getAttribute("ROWKEY");
      var lData = JSON.parse(row.getAttribute("ROWDATA"));
      var name = $(row).find("span.xdcustname").text()||"";
      var url = sd[lData.custid].deeplinkurl  +"appl/prs/prs_bedrijf.asp?bedrijf_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/companies.json?x=1";

    // Condities toevoegen (komen van prs_bedrijf_search.asp)
    if (companyName != "") url += "&name="+ safe.url(companyName);
    if (bezoekPostcode != "") url += "&postzipcode="+ safe.url(bezoekPostcode);
    if (bezoekPlaats != "") url += "&visitcity="+ safe.url(bezoekPlaats);
    if (loctekst) url += "&location=~" + safe.url(loctekst);
    if (diensttekst) url += "&service=~" + safe.url(diensttekst);
    if (contactpersoon != "") url += "&contactname="+ safe.url(contactpersoon);
    if (leverancier != "") url += "&issupplier=1";
    if (uitvoerende != "") url += "&isexecutor=1";
    if (contract != "") url += "&iscontractor=1";
    if (huurder != "") url += "&isrenter=1";

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

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

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

// 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,
                      custname: xd.SecundaryDomains[cid].name,
                      id: thisdata.id,
                      name: thisdata.name,
                      visitaddress: thisdata.visitaddress,
                      visitcity: thisdata.visitcity,
                      contactname: thisdata.contactname,
                      issupplier: thisdata.issupplier,
                      isexecutor: thisdata.isexecutor,
                      iscontractor: thisdata.iscontractor,
                      isrenter: thisdata.isrenter,
                      postaddress: thisdata.postaddress,
                      postzipcode: thisdata.postzipcode,
                      postcity: thisdata.postcity,
                      postcountry: thisdata.postcountry,
                      phone: thisdata.phone,
                      email: thisdata.email
                   });
    }
}
dataset.sort(function (a, b) { return (a.name <= b.name? -1: 1) });

function fnRol (oRs)
{
    var res = "";
    if (oRs.Fields("issupplier").Value == 1)
        res += L("lcl_prs_companies_leverancier").substring(0,1);
    if (oRs.Fields("isexecutor").Value == 1)
        res += L("lcl_prs_companies_uitvoerende").substring(0,1);
    if (oRs.Fields("iscontractor").Value == 1)
        res += L("lcl_prs_companies_contract").substring(0,1);
    if (oRs.Fields("isrenter").Value == 1)
        res += L("lcl_prs_companies_huurder").substring(0,1);
    return res;
}

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

rst.addColumn(new Column({caption: /* L("lcl_custid")*/"", content:"custname"}));
rst.addColumn(new Column({caption: L("lcl_prs_companies_name"), content: "name" }));
rst.addColumn(new Column({caption: L("lcl_prs_companies_bezoek_adres"), content: "visitaddress" }));
rst.addColumn(new Column({caption: L("lcl_prs_companies_bezoek_plaats"), content: "visitcity" }));
rst.addColumn(new Column({caption: L("lcl_prs_companies_contact_person"), content: "contactname" }));
rst.addColumn(new Column({caption: L("lcl_prs_companies_rol"), content: fnRol }));
if (outputmode != 0) // voor printer of naar Excel
{
  rst.addColumn(new Column({caption: L("lcl_prs_companies_post_adres"),       content: "postaddress" }));
  rst.addColumn(new Column({caption: L("lcl_prs_companies_post_postcode"),    content: "postzipcode" }));
  rst.addColumn(new Column({caption: L("lcl_prs_companies_post_plaats"),      content: "postcity" }));
  rst.addColumn(new Column({caption: L("lcl_prs_companies_post_land"),        content: "postcountry" }));
  rst.addColumn(new Column({caption: L("lcl_prs_companies_telefoon"),         content: "phone"}));
  rst.addColumn(new Column({caption: L("lcl_prs_companies_email"),            content: "email"}));
  rst.addColumn(new Column({caption: L("lcl_prs_companies_contact_telefoon"), content: "contactphone"}));
  rst.addColumn(new Column({caption: L("lcl_prs_companies_overeenkomst_nr"),  content: "agreement"}));
  rst.addColumn(new Column({caption: L("lcl_prs_companies_leverancier_nr"),   content: "code"}));
}

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

rst.processResultset();
%><% ASPPAGE_END(); %> 
