<% /*
     $Revision$
     $Id$
     File:               mrk_list.inc
     Description:        Show an overview of orders in list-form,
                         which meets with the given requirements
     Parameters:
         urole           user role (fe, fo, bo, mi)
         outputmode      behave in an output mode, print to 0 = screen, 1 = printer, 2 = excel, etc.
         showall         show all orders which meets with the given requirements
         categorie_key   search for orders with the given categorie key (discipline key)
     Context:            Search action from bes_search.asp form
     Note:
*/ %>

<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/kenmerk_common.inc" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="mrk.inc" -->

<%
FCLTHeader.Requires({ plugins:["jQuery"],
                      js: ["../mrk/mrk_list.js"]
                    });

function mrk_list (pautfunction, params)
{
  // Valid and supported parameters are:
  // params.urole         : the usuals two-letter lowercase string
  // params.outputmode    : print to 0 = screen, 1 = printer, 2 = excel, etc.
  // params.showall       : boolean (true | false)
  // params.categorie_key : key

  var authparams = user.checkAutorisation(pautfunction);
  var this_mrk = mrk.func_enabled_mrk(user_key); // Wat heb ik zoal aan rechten

  var urole = params.urole;
  var fronto = (urole == "fo");
  var backo = (urole == "bo");
  var minfo = (urole == "mi"); // NOT APPLICABLE?
  var frontend = (urole == "fe" || (!fronto & !backo & !minfo));

  // 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;

%>

  <html>
    <head>
<%
      FCLTHeader.Generate({ outputmode: outputmode });
%>
      <script type="text/javascript">
        // Text strings used clientside (mrk_list.js)
        var urole = "<%=urole%>";
        var outputmode = <%=outputmode%>;

        if (outputmode == 0)
          $(document).ready(function()
                            {
                              $(".photo").click(function(event)
                                             {
                                               FcltMgr.stopPropagation(event); // This prevents the event from bubbling up to the onclick event handler for the line.
                                               var obj = event.srcElement || event.target;
                                               var src = obj.src;
                                               window.open(src);
                                             });
                            });
      </script>
    </head>
    <body id="listbody">
<%
      sql = "SELECT b.mrk_bericht_key"
          + ", d.ins_discipline_omschrijving"
          + ", d.ins_discipline_key"
          + ", b.mrk_bericht_titel"
          + ", b.mrk_bericht_prijs"
          + ", " + S("prs_pers_string") + " adverteerder"
          + ", COALESCE(b.mrk_bericht_aanmaak, SYSDATE) aanmaakdatum"
          + ", b.mrk_bericht_omschrijving"
          + " FROM ins_tab_discipline d"
          + ", mrk_bericht b"
          + ", prs_perslid p"
          + " WHERE b.ins_discipline_key = d.ins_discipline_key"
          + " AND p.prs_perslid_key = b.prs_perslid_key"
          + " AND TO_CHAR((SYSDATE - " + S("mrk_message_expire_days") + " - 1), 'YYYYMMDD') < TO_CHAR (b.mrk_bericht_aanmaak, 'YYYYMMDD')";

      if (params.categorie_key)
          sql += " AND d.ins_discipline_key = " + params.categorie_key;
      if (params.searchtekst)
      sql += " AND (UPPER(b.mrk_bericht_titel) LIKE " + safe.quoted_sql_wild("%" + params.searchtekst + "%")
           + " OR UPPER(b.mrk_bericht_omschrijving) LIKE " + safe.quoted_sql_wild("%" + params.searchtekst + "%") + ")";

      sql += " ORDER BY b.mrk_bericht_aanmaak DESC";

      /**********************************
       * Callback functies ResultsetTable
       *********************************/

      function formatPrijs(oRs)
      {
        var v_prijs = oRs("mrk_bericht_prijs").value;
        if (v_prijs == null) {
          return "";
        } else {
          return "" + safe.curr(v_prijs);
        }
      }

      function fnplaatje(oRs)
      { // Er kan maar 1 plaatje getoond worden per bericht. Meer kan men ook niet uploaden.
        var mrk_key = oRs("mrk_bericht_key").value;

        var imgage = "";
        var vSubFolder = subfolderKey("M", mrk_key);
        var vAttachPath = mrk.bericht_attachPath(vSubFolder);
        var vBestandnaam = mrk.bericht_foto(vAttachPath);
        if (vBestandnaam)
        {
          var imagePathFile = mrk.bericht_imageFile(vSubFolder, vBestandnaam);
          imgage = "<img title='" + vBestandnaam + "' class='photo' src='" + safe.html(imagePathFile) + "'>";
        }
        return imgage;
      }

      /**************************************
       * End callback functies ResultsetTable
       *************************************/

      var buttons = [];
      if (this_mrk.canAdd)
      {
        var addurl = "appl/mrk/mrk_bericht.asp?urole=" + urole;
        buttons.push({ icon: "plus.png", title: L("lcl_add"), action: "FcltMgr.openDetail('" + addurl + "', '" + L("lcl_add") + "')" });
      }

      var rst = new ResultsetTable({ sql: sql,
                                     keyColumn: "mrk_bericht_key",
                                     ID: "mrktable",
                                     sql: sql,
                                     filterParams: params,
                                     outputmode: outputmode,
                                     title: L("lcl_mrk_marktplaats"),
                                     showAll: showall,
                                     buttons: buttons
                                   });

      // Kolommen
      rst.addColumn(new Column({caption: "", content: "mrk_bericht_key", datatype: "number"}));
      rst.addColumn(new Column({caption: L("lcl_mrk_categorie"), content: "ins_discipline_omschrijving"}));
      rst.addColumn(new Column({caption: L("lcl_mrk_onderwerp"), content: "mrk_bericht_titel"}));
      rst.addColumn(new Column({caption: L("lcl_mrk_omschrijving"), content: "mrk_bericht_omschrijving", combine:true, xpurpose: PRINTING_ONLY}));
      rst.addColumn(new Column({caption: L("lcl_mrk_prijs"), content: formatPrijs, datatype: "currency"}));
      rst.addColumn(new Column({caption: L("lcl_mrk_adverteerder"), content: "adverteerder"}));
      rst.addColumn(new Column({caption: L("lcl_mrk_datum"), content: "aanmaakdatum", datatype: "date"}));
      if (outputmode != 2 ) { // doe maar niet naar Excel
        rst.addColumn(new Column({caption: L("lcl_mrk_foto"), content: fnplaatje}));
      }

      // Acties
      rst.addAction({action: "mrk_edit", caption: L("lcl_edit"), isDefault: true}); // Als hij in de lijst staat mag ik ook de details zien.

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

