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

    File:               mld_load_obj_info.asp
    Description:        Genereer HTML info voor een object bij melding
    Parameters:         ins_key
                        aantal_kenmerken
    Context:            asynchrone aanroep vanuit mld_object.asp
    Notes:

*/ %>
<%
DOCTYPE_Disable = 1;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../INS/ins.inc" -->
<!-- #include file="../Shared/get_objecten_sql.inc" -->
<%

function kenmerkTR(label, waarde, isLink)
{
  var txt = '<tr>'
          + '<td class="label"><label>'+safe.html(label)+'</label></td>'
          + '<td style="word-wrap:break-word">' +(isLink ? '<span class="details" OnClick="obj_info('+ins_key+')">' : '')+safe.html(waarde)+(isLink ? '</span>' : '')+'</td>'
          + '</tr>';
  return txt;
}
var ins_key    = getQParamInt("ins_key");
var max_rows   = getQParamInt("max_rows",1);

var sql = "SELECT s.ins_srtdeel_key"
        + "  FROM ins_deel d"
        + ", ins_srtdeel s"
        + " WHERE d.ins_srtdeel_key = s.ins_srtdeel_key"
        + " AND ins_deel_key = " + ins_key;
var oRs = Oracle.Execute(sql);
var srtdeel_key = oRs("ins_srtdeel_key").value;
oRs.close();

var result = '<div class="mldobj-info-wrapper">'
           + '  <div class="mldobj-info">'
           + '    <div>'
           + '      <table style="table-layout:fixed; width:100%">';

var element_description;
var wpprefix;
if (S("ins_element_description") == "")
{
  var parentdeel = "CASE WHEN d.ins_deel_parent_key IS NOT NULL"
                 + "     THEN (SELECT p.ins_deel_omschrijving"
                 + "             FROM ins_deel p"
                 + "            WHERE p.ins_deel_key = d.ins_deel_parent_key"
                 + "          ) ||' / '"
                 + "     ELSE ''"
                 + "END ";
  wpprefix = S("prs_wpprefix_objectid") == 1;
  var plaatsaanduiding = (wpprefix
                         ? " CASE"
                           + " WHEN d.ins_alg_ruimte_type = 'W' AND wp.prs_werkplek_omschrijving IS NOT NULL"
                           + " THEN ' ' || wp.prs_werkplek_omschrijving"
                           + " ELSE ''"
                           + " END"
                         : "''");
  element_description = ", " + parentdeel + "|| d.ins_deel_omschrijving || " + plaatsaanduiding
                   + " || ' (' || " + lcl.xsql("s.ins_srtdeel_omschrijving", "s.ins_srtdeel_key")
                   + " || ') ' || d.ins_deel_opmerking ";
}
else
{
  element_description = ", " + S("ins_element_description");
}

var sql = "SELECT d.ins_deel_key"
        + element_description + " deel"
        + "     , d.ins_deel_opmerking"
        + "     , d.ins_alg_ruimte_type"
        + ", CASE ins_alg_ruimte_type"
        + "       WHEN 'P' THEN " + S("prs_pers_string")
        + "       WHEN 'A' THEN " + S("prs_dep_string")
        + "       ELSE dg.alg_plaatsaanduiding"
        + "   END owner" // (P)ersoon, (A)fdeling of Plaats (plaats = (R)uimte, (T)errein of (W)erkplek)
        + "     , dg.alg_plaatsaanduiding"
        + "     , d.ins_deel_aantal"
        + "     , s.ins_srtdeel_eenheid"
        + "     , s.ins_srtdeel_image"
        + "     , d.ins_deel_flag"
        + "  FROM ins_deel d"
        + "     , ins_srtdeel s"
        + "     , ins_srtgroep g"
        + "     , ins_tab_discipline dis"
        + "     , alg_v_allonrgoed_gegevens dg"
        + (wpprefix
           ? "  , prs_werkplek wp"
           : "")
        + "     , prs_v_aanwezigafdeling d"
        + "     , prs_perslid p"
        + " WHERE d.ins_srtdeel_key = s.ins_srtdeel_key"
        + "   AND s.ins_srtgroep_key = g.ins_srtgroep_key"
        + "   AND g.ins_discipline_key = dis.ins_discipline_key"
        + "   AND d.ins_deel_verwijder IS NULL"
        + "   AND d.ins_alg_ruimte_type = dg.alg_type(+)"
        + "   AND d.ins_alg_ruimte_key = dg.alg_onroerendgoed_keys(+)"
        + (wpprefix
          ? " AND d.ins_alg_ruimte_key = wp.prs_werkplek_key(+)"
          : "")
        + "   AND d.ins_alg_ruimte_key = d.prs_afdeling_key(+)"
        + "   AND d.ins_alg_ruimte_key = p.prs_perslid_key(+)"
        + "   AND d.ins_deel_key = " + ins_key;

var oRs = Oracle.Execute(sql);
if (!oRs.eof)
{
    var srtdeel_image = oRs("ins_srtdeel_image").Value;
    var hasImage = false;
    if (srtdeel_image)
    {
        var imageFile = S("ins_image_path") + srtdeel_image;
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        if (fso.FileExists(Server.MapPath(imageFile)))
            hasImage = true;
    }
    var this_photoinfo = ins.photoinfo(ins_key);
    var background_image_src = "";
    if (this_photoinfo.isDefault)
    {
        if (hasImage)
            background_image_src = imageFile;
    }
    else
        background_image_src = this_photoinfo.photopath;

    if (background_image_src)
    {
        result = '<div class="mldobj-info-wrapper mldobj-has-photo" style="background-image: url(\'' + safe.htmlattr(background_image_src) + '\')">'
               + '  <div class="mldobj-info">'
               + '    <div>'
               + '      <table style="table-layout:fixed; width:100%">';
    }

    var flagkey = oRs("ins_deel_flag").Value || 0;
    var txt = '<tr>'
            + '<td class="label"><label>'+safe.html(L("lcl_ins_flags"))+'</label></td>'
            + '<td><span class="insflag' + flagkey + ' inss">'
                + I("fa-fclt-flag" + (flagkey == 0 ? "0" : ""))
                + '</span> ' + safe.html(L("lcl_ins_flag" + flagkey))
            + '</td>'
            + '</tr>';
    result += txt;

    switch (oRs("ins_alg_ruimte_type").Value)
    {
        case "P":  { var lbl = L("lcl_obj_owner"); break; }       // Persoon(P)
        case "A":  { var lbl = L("lcl_obj_department"); break; }  // Afdeling (A)
        default:   { var lbl = L("lcl_obj_position"); break; }    // Plaats: Ruimte(R), Terrein(T) of Werkplek(W)
    }
    result += kenmerkTR(lbl, oRs("owner").Value);
    result += kenmerkTR(L("lcl_obj_identification"), oRs("deel").Value, true);
    if (oRs("ins_deel_opmerking").Value)
        result += kenmerkTR(L("lcl_ins_opmerking"), oRs("ins_deel_opmerking").value);
    if (oRs("ins_deel_aantal").Value != 1 || oRs("ins_srtdeel_eenheid").Value)
        result += kenmerkTR(L("lcl_amount"), oRs("ins_deel_aantal").Value + (oRs("ins_srtdeel_eenheid").Value ? " " + oRs("ins_srtdeel_eenheid").Value : ""));
}

oRs.close();

var sqlwaarde = " k.ins_kenmerk_default";
if (srtdeel_key > 0)
  sqlwaarde = "(SELECT (CASE WHEN sk.fac_kenmerkdomein_key IS NULL"
                         + " THEN v.ins_kenmerkdeel_waarde"
                         + " ELSE fac.getdomeinwaarde(sk.fac_kenmerkdomein_key, v.ins_kenmerkdeel_waarde)"
                         + " END) ins_kenmerkdeel_waarde"
            + "   FROM ins_kenmerkdeel v"
            +       ", ins_srtkenmerk sk"
            + "  WHERE sk.ins_srtkenmerk_key = k.ins_srtkenmerk_key"
            +    " AND sk.ins_srtkenmerk_kenmerktype NOT IN ('Q', 'L', 'l', 'M', 'E', 'F')"
            +    " AND v.ins_kenmerk_key = k.ins_kenmerk_key"
            +    " AND v.ins_kenmerkdeel_verwijder IS NULL"
            +    " AND (  k.ins_kenmerk_bewerkniveau = 'D' AND v.ins_deel_key = " + ins_key
            +        " OR k.ins_kenmerk_bewerkniveau = 'S' AND v.ins_deel_key = " + srtdeel_key
            +        " )"
            + ")";

var sqlt = "SELECT k.ins_kenmerk_volgnummer kenmerk_volgnr"
         + "     , COALESCE (" + lcl.xsql("k.ins_kenmerk_omschrijving", "k.ins_kenmerk_key")
         + "              ,  " + lcl.xsql("t.ins_srtkenmerk_omschrijving", "t.ins_srtkenmerk_key") + ") kenmerk_omschrijving"
         + "     ," + sqlwaarde + " kenmerk_waarde"
         + " FROM ins_srtkenmerk t"
         +     ", ins_kenmerk k"
         +     ", ins_srtdeel s"
         +     ", ins_srtgroep g"
         + " WHERE s.ins_srtdeel_key = " + srtdeel_key
         +   " AND k.ins_srtkenmerk_key = t.ins_srtkenmerk_key"
         +   " AND g.ins_srtgroep_key = s.ins_srtgroep_key"
         +   " AND k.ins_kenmerk_verwijder IS NULL"
         +   " AND t.ins_srtkenmerk_verwijder IS NULL"
         +   " AND (  (k.ins_srtinstallatie_key = s.ins_srtdeel_key"
               +    " AND k.ins_kenmerk_niveau = 'S')"
               + " OR (k.ins_srtinstallatie_key = s.ins_srtgroep_key"
               +    " AND k.ins_kenmerk_niveau = 'G')"
               + " OR (k.ins_srtinstallatie_key = g.ins_discipline_key"
               +    " AND k.ins_kenmerk_niveau = 'D')"
               + " )"
         +   " AND t.ins_srtkenmerk_kenmerktype NOT IN ('Q', 'L', 'l', 'M', 'E', 'F')"
         + " ORDER BY 1,2";
var sql = "SELECT * FROM ("+ sqlt +")"
        + " WHERE rownum <= " + max_rows;
var oRs = Oracle.Execute(sql);
while (!oRs.eof)
{ // elk gevonden record op een nieuwe regel
  result += kenmerkTR(oRs(1).value, oRs(2).value);
  oRs.MoveNext();
}
oRs.close();
result += '</table></div></div></div>';

Response.Write(result);
%><% ASPPAGE_END(); %>
