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

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="./mobile.inc" -->
<!-- #include file="./iface.inc" -->
<!-- #include file="./resultset_table.inc" -->
<!-- #include file="../mld/mld.inc" -->

<%
FCLTHeader.Requires({ plugins: ["jQuery"] });

var authparamsMLDBOF = user.checkAutorisation("WEB_MLDBOF", true);
var authparamsMLDBO3 = user.checkAutorisation("WEB_MLDBO3", true);
var authparamsMLDFOF = user.checkAutorisation("WEB_MLDFOF", true);
var authparamsMLDUSE = user.checkAutorisation("WEB_MLDUSE", true);
var authparams = user.auth_required_or_abort(authparamsMLDBOF || authparamsMLDBO3 || authparamsMLDFOF || authparamsMLDUSE);


var mld_key = getQParamInt("mld_key", -1);
if (mld_key > 0)
{
  var mld_info = mld.mld_melding_info(mld_key);
  var stdmelding_key = mld_info.stdm_key;
  var ruimtekey = mld_info.room_key;
  var verdiepingkey =  mld_info.flr_key;
  var gebouwkey =  mld_info.bld_key;
  var locatiekey =  mld_info.loc_key;
  var ins_arr = [];
  for (var i = 0; i < mld_info.room_obj_arr.length; i++)
  {
    ins_arr.push(mld_info.room_obj_arr[i].ins_key);
  }
  var isNew = false;
}
else
{
  var stdmelding_key = getQParamInt("stdmelding_key");
  var ruimtekey = getQParamInt("ruimtekey");
  var verdiepingkey = getQParamInt("verdiepingkey");
  var gebouwkey = getQParamInt("gebouwkey");
  var locatiekey = getQParamInt("locatiekey");
  var ins_arr = getQParamIntArray("ins_str", []);
  var isNew = true;
}
var stdm_info = mld.mld_stdmeldinginfo(stdmelding_key);

var one_object = ins_arr.length == 1; // Is er 1 object geselecteerd?
var where_alg = "";
var where_ins = "";
%>

<html>
  <head>

    <script type="text/javascript">
      function mld_double(mode, parent_key)
      {
          mldDoubleCallback({ mode: mode, parent_key: (parent_key? parent_key : -1) }); // callBack function in melding.asp
          window.history.back();
      }

      function mld_askdouble(parent_key)
      {
        if (confirm(L("lcl_mld_linktoparent").format(parent_key)))
        {
          mld_double(1, parent_key);
        }
      }

      function mld_dbl_cancel()
      {
        $("a.btn_disabled", "form#meld").removeClass("btn_disabled");
        window.history.back();
      }
    </script>
  </head>

  <body class="fclt-modal" id="mod_mlddouble">
  <%
    var sql = "SELECT sm.alg_onrgoed_niveau"
            + "  FROM mld_stdmelding sm"
            + " WHERE sm.mld_stdmelding_key = " + stdmelding_key;
    oRs = Oracle.Execute(sql);
    var alg_onrgoed_niveau = oRs("alg_onrgoed_niveau").value;

    if (one_object) // Als er 1 object is geselecteerd
      where_ins = " OR mo.ins_deel_key = " + ins_arr[0];

    if (!stdm_info.ins_srtdiscipline_alg || alg_onrgoed_niveau === null || alg_onrgoed_niveau == "H")
    {
        where_alg = "1=1"
    }
    else if (alg_onrgoed_niveau == "R")
    {
      where_alg = "org.alg_ruimte_key = " + ruimtekey
    }
    else if (alg_onrgoed_niveau == "V")
    {
      where_alg = "org.alg_verdieping_key = " + verdiepingkey
    }
    else if (alg_onrgoed_niveau == "G")
    {
      where_alg = "org.alg_gebouw_key = " + gebouwkey
    }
    else if (alg_onrgoed_niveau == "L")
    {
        where_alg = "m.mld_alg_locatie_key = " + locatiekey
                + " AND m.mld_alg_onroerendgoed_keys IS NULL";
    }

/* Geen table-layout, dus; */ %></table>
<div id="mldsimilar"><%=L("lcl_mld_askdouble").format(isNew?L("lcl_mld_no_own_mld"):L("lcl_not_link"), L("lcl_mld_cancel_double"))%></div>
<%
    sql = "SELECT DISTINCT m.mld_melding_key"
        + "     , sd.ins_srtdiscipline_prefix"
        + "     , " + S("prs_pers_string") + " naam"
        + "     , m.mld_melding_datum"
        + "     , m.mld_alg_onroerendgoed_keys plaats_key"
        + (stdm_info.ins_srtdiscipline_alg
           ? "     , l.alg_locatie_code || '-' || org.alg_gebouw_code || '-' || org.alg_verdieping_code || '-' || org.alg_ruimte_nr plaats"
           : ", '' plaats")
        + "     , 'Object'"
        + "     , TO_CHAR(SUBSTR(m.mld_melding_omschrijving, 1, 4000)) mld_melding_omschrijving"
        + "     , (SELECT MIN(d.ins_deel_omschrijving)"
          + "        FROM mld_melding_object mmo"
          + "           , ins_deel d"
          + "       WHERE mmo.ins_deel_key = d.ins_deel_key"
          + "         AND mmo.mld_melding_key = m.mld_melding_key)"
          + "     || "
          + "     CASE"
          + "     WHEN (SELECT COUNT(mmo.ins_deel_key)"
          + "             FROM mld_melding_object mmo"
          + "            WHERE mmo.mld_melding_key = m.mld_melding_key) > 1"
          + "     THEN '...'"
          + "     ELSE ''"
          + "     END  objecten"
        + "     , m.mld_melding_parentkey"
        + "     , COALESCE(m.mld_melding_parentkey, m.mld_melding_key) orderby"
        + "  FROM mld_melding m"
        + "     , mld_stdmelding sm"
        + "     , mld_discipline md"
        + "     , ins_srtdiscipline sd"
        + (stdm_info.ins_srtdiscipline_alg
                ?"     , alg_v_allonrgoed_gegevens org, alg_locatie l"
                :"")
        + "     , mld_melding_object mo"
        + "     , prs_perslid p"
        + " WHERE m.mld_stdmelding_key = sm.mld_stdmelding_key"
        + "   AND sm.mld_ins_discipline_key = md.ins_discipline_key"
        + "   AND md.ins_srtdiscipline_key = sd.ins_srtdiscipline_key"
        + (stdm_info.ins_srtdiscipline_alg
                ? "   AND (m.mld_alg_onroerendgoed_keys = org.alg_onroerendgoed_keys(+)) AND m.mld_alg_locatie_key = l.alg_locatie_key"
                : "")
        + "   AND m.mld_melding_key = mo.mld_melding_key(+)"
        + "   AND m.prs_perslid_key = p.prs_perslid_key"
        + "   AND m.mld_melding_status IN (0, 2, 4, 7)"
        + "   AND m.mld_stdmelding_key = " + stdmelding_key
        + "   AND m.mld_melding_parentkey IS NULL" // Geen child meldingen tonen
        + (mld_key > 0
          ? " AND m.mld_melding_key != " + mld_key
          : "") // De melding die geaccepteerd/afgewezen moet worden niet tonen.
        + "   AND (" + where_alg + where_ins + ")"
        + " ORDER BY orderby, mld_melding_parentkey desc";

    function fncolKey(oRs)
    {
      return oRs("ins_srtdiscipline_prefix").Value + oRs("mld_melding_key").Value;
    }
    var rst = new ResultsetTable({ keyColumn: "mld_melding_key",
                                   sql: sql,
                                   click: fncolClick,
                                   ID: "similarcallstable",
                                   title: L("lcl_complains"),
                                   headerColumn: fnHeader,
                                   showAll: true
                                  });
    function fncolClick(oRs)
    {
        return "mld_askdouble(" + oRs("mld_melding_key").Value + ")";
    }
    function fnHeader(oRs)
    {
        return fncolKey(oRs) + " " + safe.html(oRs("mld_melding_omschrijving").Value);
    };
    var cnt = rst.processResultset();

    CONTROLGROUP_START();
        BUTTON(L("lcl_mld_no_own_mld"),           { click: "mld_double(2)", icon: "fa-fclt-save" });
        BUTTON(L("lcl_mld_cancel_double"),  { click: "mld_dbl_cancel()",  icon: "fa-fclt-cancel" });
    CONTROLGROUP_END();
PDA_PAGE_END(); %>
	</body>
</html>
<% ASPPAGE_END(); %>
