<% /*
     $Revision$
     $Id$
    File:               alg_verdieping.inc
    Description:        Show an overview of verdieping items in list-form,
                        which meets with the given requirements
    Parameters:
    Context:            Search action from alg_verdieping_search_list.asp form
    Note:
*/ %>

<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="../SlnkDWF/DWFProperties.inc" -->
<!-- #include file='../CAD/FGII.inc' -->
<!-- #include file="../alg/alg.inc" -->

<%
FCLTHeader.Requires({ plugins:["jQuery"],
                      js: ["jQuery-ui.js"]
                    })
%>

<script type="text/javascript">
  var autosearch = 1; //automatische laden van resultaat op search pagina
</script>
<%

function tekening_list(pautfunction, params)
{
  var buttons = [];

  autfunction = "WEB_CADFOF";
  var authparams = user.checkAutorisation(autfunction);

  var canAdd = (authparams.ALGwritelevel < 9);

  // Voor resulttable de globalen zetten; zou ng anders moeten.
  var outputmode = params.outputmode; // print to 0 = screen, 1 = printer, 2 = excel, etc.
  var showall = params.showall;

  var loc_key = params.loc_key;
  var bldter_key = params.bldter_key;
  var bstndnm = params.bstndnm;

%>

  <html>
    <head>
      <% FCLTHeader.Generate({outputmode:outputmode}) %>
      <script>
        function tekeningEdit(row)
        {
          var cadData = eval('(' + row.getAttribute("ROWDATA") + ')');
          var url = "../CAD/tekening_edit.asp?cad_tek_key=" + cadData.cad_tek_key
                  + "&loc=" + escape(cadData.loc_oms)
                  + "&bld=" + escape(cadData.bld_oms)
                  + "&flr=" + escape(cadData.flr_oms)
                  + "&location_key=" + cadData.loc_key
                  + "&floor_key=" + cadData.flr_key
                  + "&file_name=" + escape(cadData.bstndnm);
          FcltMgr.openModalDetail(url, L("lcl_change"), { callback: FcltMgr.reload });
          // TODO: evt. robuster maken. omschrijvingen etc horen aan de edit kant te worden opgehaald. Alleen keys meegeven.
        }

        function editContouren(row)
        {
          var cadData = eval('(' + row.getAttribute("ROWDATA") + ')');
          var url = "appl/CAD/contour_search.asp?locatie_key=" + cadData.loc_key
                  + "&gebouw_key=" + cadData.bldter_key
                  + "&terrein_key=" + cadData.bldter_key
                  + "&verdieping_key=" + cadData.flr_key
                  + "&autosearch=1";
          FcltMgr.openDetail(url, "<%=L("lcl_fg_tek_rapp")%>");
        }

        function viewConcept(row)
        {
          var cadData = eval('(' + row.getAttribute("ROWDATA") + ')');
          var url = "appl/CAD/default.asp?floor_key=" + cadData.flr_key + "&concept=1";
          FcltMgr.openDetail(url, "<%=L("lcl_fg_tek_rapp")%>");
        }

        function Upload()
        {
          url = "<%=protectQS.create("../shared/BijlagenForm.asp?module=FGII")%>";
          FcltMgr.openModalDetail(url, L("lcl_upload_file"));
        }
      </script>
    </head>
    <body id="listbody">
<%
      var sqln = "SELECT t.alg_locatie_omschrijving"
               + "     , t.gebteroms"
               + "     , t.alg_verdieping_omschrijving"
               + "     , t.cad_tekening_filenaam"
               + "     , t.cad_tekening_geimporteerd"
               + "     , t.cad_tekening_key"
               + "     , t.alg_locatie_key"
               + "     , t.gebterkey"
               + "     , t.alg_verdieping_key"
               + "     , t.gebterkey row_key"
               + "     , t.gebtertype"
               + "  FROM (SELECT l.alg_locatie_omschrijving"
               + "             , g.alg_gebouw_omschrijving gebteroms"
               + "             , v.alg_verdieping_omschrijving"
               + "             , ct.cad_tekening_filenaam"
               + "             , ct.cad_tekening_geimporteerd"
               + "             , ct.cad_tekening_key"
               + "             , l.alg_locatie_key"
               + "             , g.alg_gebouw_key gebterkey"
               + "             , v.alg_verdieping_key"
               + "             , alg_verdieping_volgnr"
               + "             , '' alg_terreinsector_code"
               + "             , 'B' gebtertype"
               + "          FROM alg_locatie l"
               + "             , cad_tekening ct"
               + "             , alg_verdieping v"
               + "             , alg_gebouw g"
               + "         WHERE v.alg_gebouw_key = g.alg_gebouw_key"
               + "           AND ct.alg_verdieping_key(+) = v.alg_verdieping_key"
               + "           AND l.alg_locatie_key = g.alg_locatie_key"
               + "           AND v.alg_verdieping_verwijder IS NULL"
               + "        UNION ALL "
               + "        SELECT l.alg_locatie_omschrijving"
               + "             , t.alg_terreinsector_omschrijving gebteroms"
               + "             , ''"
               + "             , ct.cad_tekening_filenaam"
               + "             , ct.cad_tekening_geimporteerd"
               + "             , ct.cad_tekening_key"
               + "             , l.alg_locatie_key"
               + "             , t.alg_terreinsector_key gebterkey"
               + "             , -1"
               + "             , -1 alg_verdieping_volgnr"
               + "             , alg_terreinsector_code volgnr"
               + "             , 'T' gebtertype"
               + "          FROM alg_locatie l"
               + "             , cad_tekening ct"
               + "             , alg_terreinsector t"
               + "         WHERE ct.alg_locatie_key(+) = t.alg_locatie_key"
               + "           AND l.alg_locatie_key = t.alg_locatie_key"
               + "           AND t.alg_terreinsector_verwijder IS NULL"
               + "           AND ct.alg_verdieping_key IS NULL) t"
               + " WHERE 1 = 1"

      if (authparams.ALGreadlevel > -1)
      { // Er is een scope-beperking van kracht
        sqln += " AND t.alg_verdieping_key IN"
              + "(SELECT alg_verdieping_key FROM fac_v_my_floors"
              + "  WHERE prs_perslid_key = " + user_key
              + "    AND niveau = " + authparams.ALGreadlevel + ")";
      }

      if (bldter_key)
      {
        sqln += " AND t.gebterkey = " + bldter_key;
      }

      else if (loc_key)
      {
        sqln += " AND t.alg_locatie_key = " + loc_key;
      }

      if (bstndnm)
      {
        sqln += " AND UPPER(t.cad_tekening_filenaam) LIKE " + safe.quoted_sql_wild("%" + bstndnm + "%")
      }

      sqln  += " ORDER BY UPPER (t.alg_locatie_omschrijving)"
            + "       , gebtertype"
            + "       , UPPER (t.gebteroms)"
            + "       , t.alg_verdieping_volgnr, t.alg_terreinsector_code";

      function CvtXML(In)
      {
        var D = In.replace(/^(\d{4})-(\d\d)-(\d\d)T([0-9:]*)([.0-9]*)(.)(\d\d):(\d\d)$/, '$1/$2/$3 $4 GMT$6$7$8')
        D = Date.parse(D) + 1000*RegExp.$5
        return (new Date(D))
      }
      // Merk op dat dit uiteindelijk de datum is waarop de DWF is gegenereerd. Die datum zit
      // binnenin de DWF in de properties
      // Dat hoeft niet noodzakelijkerwijs gelijk te zijn aan de bestandsdatum van de DWF
      function DWGDate(oRs) // Levert tekst op
      {
        var dt = DWGjsDate(oRs, S("fg_dwf_path"))
        if (dt)
        {
          return toDateTimeString(dt)
        }
        else
          return L("lcl_fg_notavailable");  // or date could not determined
      }

      function DWGConceptDate(oRs) // Levert tekst op
      {
        var dtActueel = DWGjsDate(oRs, S("fg_dwf_path"))
        var dt = DWGjsDate(oRs, S("fg_dwf_path_concept"))
        if (dt && (!dtActueel || (dt.getTime() != dtActueel.getTime())))
        {
          return toDateTimeString(dt)
        }
        else
          return "";
      }

      function DWGjsDate(oRs, path) // Levert null of jsDate op
      {
        var teknaam = oRs("cad_tekening_filenaam").value + ".dwf";
        teknaam = path + teknaam;
        if (!fso.FileExists(teknaam))
        {
          return null;
        }

        var slnkXML = Server.CreateObject("MSXML2.DOMDocument.6.0");
        var slnkProps = GetEplotPropertiesXML(teknaam, 0);
        if (!slnkProps)
        {
          __Log (teknaam+': Geen properties (pre-2006 DWF)');
          return null;
        }
        slnkXML.loadXML(slnkProps);
        slnkXML.setProperty("SelectionLanguage", "XPath");
        slnkXML.setProperty("SelectionNamespaces", "xmlns:ePlot='DWF-ePlot:1.2'");
        // Er zitten twee Modification-times in de DWF, eentje van de DWG en eentje van de DWF
        // Die van de DWG heeft category='AutoCAD Drawing', die van de DWF heeft geen category
        // We willen die laatste hebben
        var node = slnkXML.selectSingleNode("ePlot:Page/ePlot:Properties/ePlot:Property[@name = 'Modification Time' and not(@category)]");
        if (node)
        {
          dt = CvtXML(node.attributes.getNamedItem("value").text);
          return dt;
        }

        return null;
      }

      function formatDate(oRs)
      {
        var sd = oRs("cad_tekening_geimporteerd").value;
        if (sd == null)
        {
          return "";
        }
        return toDateTimeString(new Date(sd));
      }

      function note(oRs)
      {
        var sd = oRs("cad_tekening_geimporteerd").value; // Never scanned is very old
        if (sd == null || DWGjsDate(oRs) > (new Date(sd)))
          return "<i class='fa fa-lg fa-exclamation-triangle' style='color:red'></i>";
        else
          return "";
      }

      function fnrowData(oRs)
      {
        var loc_key = oRs("alg_locatie_key").value;
        var bldter_key = oRs("gebterkey").value;
        var flr_key = oRs("alg_verdieping_key").value;
        var bstndnm = oRs("cad_tekening_filenaam").value;
        var cad_tek_key = oRs("cad_tekening_key").value;
        var loc_oms = oRs("alg_locatie_omschrijving").value;
        var bld_oms = oRs("gebteroms").value;
        var flr_oms = oRs("alg_verdieping_omschrijving").value;

        var data = {bldter_key: bldter_key,
                    loc_key: loc_key,
                    flr_key: flr_key || -1,
                    bstndnm: bstndnm || "",
                    cad_tek_key: cad_tek_key || -1,
                    loc_oms: loc_oms,
                    bld_oms: bld_oms || "",
                    flr_oms: flr_oms || "" };

        return JSON.stringify(data);
      }

      function fnrowActionEnabler(oRs)
      {
        var eContouren =    oRs("cad_tekening_filenaam") .Value
                         && authparams.ALGwritelevel<9
                         && alg.canWriteVerdieping(oRs("alg_verdieping_key").value, authparams.ALGwritelevel);

        var dtActueel = DWGjsDate(oRs, S("fg_dwf_path"));
        var dt = DWGjsDate(oRs, S("fg_dwf_path_concept"));
        var gebtertype = oRs("gebtertype").value;
        var eVerschil = (gebtertype == "B" && dt && (!dtActueel || (dt.getTime() != dtActueel.getTime())))

        return {eVerschil : eVerschil, eContouren: eContouren};
      }

      buttons.push({title: L("lcl_upload_file"), action:"Upload();", icon: "folder.png", id:"bSearch" });

      var rst = new ResultsetTable({ sql: sqln,
                                     keyColumn: "row_key", // we gebruiken hem niet maar hij mag niet leeg zijn
                                     ID: "workTable",
                                     showAll: showall,
                                     rowData: fnrowData,
                                     rowActionEnabler: fnrowActionEnabler,
                                     outputmode: outputmode,
                                     filterParams: params,
                                     buttons: buttons
                                   });

      rst.addColumn(new Column({caption: L("lcl_fg_tek_loc"), content: "alg_locatie_omschrijving"}));
      rst.addColumn(new Column({caption: L("lcl_fg_tek_build"), content: "gebteroms"}));
      rst.addColumn(new Column({caption: L("lcl_fg_tek_floor"), content: "alg_verdieping_omschrijving"}));
      rst.addColumn(new Column({caption: L("lcl_fg_tek_file"), content: "cad_tekening_filenaam"}));
      rst.addColumn(new Column({caption: L("lcl_fg_tek_DWGdate"), content: DWGDate}));
      rst.addColumn(new Column({caption: L("lcl_fg_tek_DWGdate") + "<br>Concept", content: DWGConceptDate}));
      rst.addColumn(new Column({caption: "!", content: note})); // TODO:("center style='color:#FF0000;padding-bottom:0;padding-top:0;'")

      rst.addAction({action: "tekeningEdit", caption: L("lcl_edit"), isDefault: true});
      rst.addAction({action: "editContouren", caption: L("lcl_fg_tek_rapp"), enabler: "eContouren"});
      rst.addAction({action: "viewConcept", caption: L("lcl_fg_tek_verschil"), enabler: "eVerschil"});

      var cnt = rst.processResultset();
%>
    </body>
  </html>
<%
}
%>

