<% /*
     $Revision$
     $Id$

    File:        prs_contactpersoon_list.inc
    Description: Vangt de parameters van prs_contactpersoon_search op en verwerkt die in een lijst
    Parameters:  -
    Context:     Vanuit prs_contactpersoon_search_list.asp (of wie maar wil)
    Note:

                wat doet de 'intern' parameter nog?
*/ %>

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

<%
FCLTHeader.Requires({ plugins:["jQuery"],
                      js: []
                    })
function contactpersoon_list (params)
{
  // Valid and supported parameters are:
  // params.outputmode    : print to 0 = screen, 1 = printer, 2 = excel, etc.
  // params.showall       : boolean (true | false)
  // params.intern        : boolean (true | false)
  // params.search        : boolean (true | false)
  // params.bedrijf_key   : key
  // params.contact_name  : wildcard string
  // params.loc_key       : key
  // params.bld_key       : key

  if (intern)
  {
    var authparams = user.checkAutorisation('WEB_PRSMAN')
    var canAdd = (authparams.ALGwritelevel < 9);
  }
  else
  {
    var authparams = user.checkAutorisation('WEB_RELUSE', true)
    user.auth_required_or_abort(authparams);
    var canAdd = (authparams && authparams.ALGwritelevel < 9); // Alleen met WEB_RELUSE rechten mag ik contactpersonen toevoegen en wijzigen.
  }

  // Voor resulttable de globalen zetten.
  var outputmode = params.outputmode; // print to 0 = screen, 1 = printer, 2 = excel, etc.
  var print = (outputmode == 1 || outputmode == 5);
  var excel = (outputmode == 2 || outputmode == 6);
  var showall = params.showall;

  var intern = params.intern;
  var search = params.search;
  var bedrijf_key = params.bedrijf_key;
  var contact_name = params.contact_name;
  var loc_key = params.loc_key;
  var bld_key = params.bld_key;
%>
  <html>
    <head>
<%    FCLTHeader.Generate({ outputmode: outputmode }); %>
      <script type="text/javascript">
        var autosearch = 1; // automatisch laden van resultaat op search pagina
        function refreshall(data)
        {
          if (data.refresh)
              window.location.reload();
        }

        function contactPersoonEdit(row)
        {
          var cprs_key = row.getAttribute("ROWKEY");
          var url = "appl/prs/prs_contactpersoon.asp?mode=viewUpdate&key=" + cprs_key;
          FcltMgr.openDetail(url, "<%=L("lcl_person")%>", {callback: refreshall});
        }

        function contactPersoonDelete(row)
        {
          FcltMgr.confirm(L("lcl_prs_del_txt_contactpers"), function() {
              var cprs_key = row.getAttribute("ROWKEY");
              var data = {
                  key: cprs_key,
                  level: "C"
              };
              protectRequest.dataToken(data);
              $.post("prs_delete.asp",
                  data,
                  FcltCallbackRefresh,
                  "json");
          });
        }
      </script>
    </head>

    <body id="listbody">
<%
      var sql = "SELECT n.prs_contactpersoon_locatie_key"
              + "     , b.prs_bedrijf_naam"
              + "     , "+ S("prs_contactpers_string") +" prs_contactpersoon_naam"
              + "     , l.alg_locatie_omschrijving"
              + "     , cp.prs_contactpersoon_telefoon_1"
              + "     , cp.prs_contactpersoon_telefoon_2"
              + "     , cp.prs_contactpersoon_fax"
              + "     , cp.prs_contactpersoon_email"
              + "     , cp.prs_contactpersoon_key"
              + "     , cp.prs_contactpersoon_functie"
              + "  FROM alg_locatie l"
              + "     , prs_contactpersoon cp"
              + "     , prs_contactpersoon_locatie n"
              + "     , prs_bedrijf b"
              + (bld_key > 0
                ? "   , alg_gebouw g"
                : "")
              + " WHERE n.alg_locatie_key = l.alg_locatie_key(+)"
              + (bld_key > 0
                ? " AND l.alg_locatie_key = g.alg_locatie_key(+)"
                : "")
              + "   AND b.prs_bedrijf_key = cp.prs_bedrijf_key"
              + "   AND b.prs_bedrijf_verwijder IS NULL"
              + "   AND cp.prs_contactpersoon_key = n.prs_contactpersoon_key(+)"
              + "   AND cp.prs_contactpersoon_verwijder IS NULL";

      if (bedrijf_key > 0)
        sql += "    AND b.prs_bedrijf_key = " + bedrijf_key;

      if (!search)
      {
        if (intern)
          sql += "  AND b.prs_bedrijf_intern IS NOT NULL";
        else
          sql += "  AND b.prs_bedrijf_intern IS NULL";
      }

      if (contact_name)
        sql += " AND UPPER("+ S("prs_contactpers_string") +") LIKE " + safe.quoted_sql_wild("%" + contact_name + "%");

      if (loc_key > 0)
        sql += "    AND l.alg_locatie_key = " + loc_key;

      if (bld_key > 0)
        sql += "    AND g.alg_gebouw_key = " + bld_key;

      var ALGreadlevel = (authparams? authparams.ALGreadlevel : 9);
      if (ALGreadlevel > -1)
      { // Er is een scope-beperking van kracht
        sql += " AND l.alg_locatie_key IN "
             + "(SELECT alg_locatie_key FROM fac_v_my_locations "
             + "  WHERE prs_perslid_key = " + user_key
             + "    AND niveau = " + ALGreadlevel + ")";
      }

      // Nu filtercondities voor flexkenmerken toevoegen
      sql += getKenmerkSql("PRS", "cp.prs_contactpersoon_key");

      sql += " ORDER BY b.prs_bedrijf_naam_upper ASC"
           + (!search
             ? "      , l.alg_locatie_upper ASC"
             : "")
           + "        , cp.prs_contactpersoon_naam";

      var buttons = [];
      var addurl = "appl/prs/prs_contactpersoon.asp" + (bedrijf_key? "?bedrijf_key=" + bedrijf_key : "");
      if (canAdd)
      {
        buttons = [{ icon: "page_refresh.png", title: L("lcl_refresh"), action: "FcltMgr.reload()" },
                   { icon: "plus.png", title: L("lcl_add"), action: "FcltMgr.openDetail('" + addurl + "', '" + L("lcl_add") + "')" }];
      }

      var rst = new ResultsetTable({ sql: sql,
                                     keyColumn: "prs_contactpersoon_key",
                                     outputmode: outputmode,
                                     ID: "contacttable",
                                     buttons: buttons,
                                     showAll: showall,
                                     title: L("lcl_prs_companies_contact_title"),
                                     flexModule: "PRS",
                                     flexId: "prs_contactpersoon_key",
                                     filterParams: params
                                   });

      if (search || bedrijf_key == -1)
        rst.addColumn(new Column({caption: L("lcl_prs_companies_name"), content: "prs_bedrijf_naam"}));
      rst.addColumn(new Column({caption: L("lcl_location"), content: "alg_locatie_omschrijving"}));
      rst.addColumn(new Column({caption: L("lcl_prs_companies_contact_naam"), content: "PRS_CONTACTPERSOON_NAAM"})); //TODO totale naam als in oude situatie?
      rst.addColumn(new Column({caption: L("lcl_prs_companies_contact_functie"), content: "prs_contactpersoon_functie"}));
      rst.addColumn(new Column({caption: L("lcl_prs_companies_contact_tel1"), content: "prs_contactpersoon_telefoon_1"}));
      rst.addColumn(new Column({caption: L("lcl_prs_companies_contact_tel2"), content: "prs_contactpersoon_telefoon_2"}));
      rst.addColumn(new Column({caption: L("lcl_prs_companies_contact_fax1"), content: "prs_contactpersoon_fax"}));
      rst.addColumn(new Column({caption: L("lcl_prs_companies_contact_email"), content: "prs_contactpersoon_email"}));

      rst.addAction({ action: "contactPersoonEdit", caption: L("lcl_edit"), isDefault: true});
      if (canAdd)
        rst.addAction({ action: "contactPersoonDelete", caption: L("lcl_delete")});

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