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

    File:               prscontactlocs.asp
    Description:        (Modal)dialoog om aan een contactpersoon een scope (locaties) te koppelen
    Globalen:
        ckey            contactpersoon_key
    Context:
    Note:
*/
%>

<!--#include file="../Shared/common.inc"-->
<!--#include file="../Shared/selector.inc"-->
<!--#include file="../Shared/iface.inc"-->

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

var ckey = getQParamInt("ckey"); // bedrijf key

var authparams = user.checkAutorisation('WEB_RELUSE');
// Ik heb nu enige rechten (read of write). Maar heb ik nu schrijfrechten?
user.auth_required_or_abort(authparams.ALGwritelevel < 9);
// Ik heb schrijfrechten.
var ALGwritelevel = authparams.ALGwritelevel;
%>
<html>
  <head>
<%  FCLTHeader.Generate()

    var submitting = getQParamInt("submit", 0) == 1;
    if (submitting)
    {
      protectRequest.validateToken();
      var locs = getFParamIntArray("fldLocExist", []);
      // Verwijderen oude waarden
      sql = "DELETE FROM prs_contactpersoon_locatie"
          + " WHERE prs_contactpersoon_key = " + ckey
          + "    OR (prs_contactpersoon_key IS NULL AND alg_locatie_key IS NULL)";
      Oracle.Execute(sql);

      for (i in locs)
      {
        var loc_key = locs[i];
        var sql = "INSERT INTO prs_contactpersoon_locatie (prs_contactpersoon_key, alg_locatie_key)"
                + " VALUES (" + ckey + ", " + loc_key + ")";
        Oracle.Execute(sql);
      }
%>
      <script>
        FcltMgr.closeDetail(window, { refresh: true } );
      </script><%
      Response.End;
    }
%>
    <script>
      function moveRightLoc(obj)
      {
        $('#fldLocPossible option:selected').appendTo('#fldLocExist');
      }

      function removeLoc(obj)
      {
        $('#fldLocExist option:selected').appendTo('#fldLocPossible');
      }

      function bdl_submit()
      {
        $('#fldLocExist>option').prop('selected','true'); // alles submitten
        document.u2.submit();
      }
    </script>
  </head>
  <body class="fclt-modal" id="mod_cprs">
  <% MODAL_START(); %>
    <form name="u2" action="prscontactlocs.asp?submit=1&ckey=<%=ckey%>" method="post">
<%    MODAL_BLOCK_START("prs_sellocbld", L("lcl_prs_locbld_select"), { "icon": "fa-location-arrow" }) %>
      <tr><td style="width:45%">
          <label for="fldLocPossible"><%=L("lcl_location")%></label><br>
<%
            var existing_loc_sql = "SELECT c.alg_locatie_key"
                                 + "     , l.alg_locatie_omschrijving"
                                 + "  FROM prs_contactpersoon_locatie c"
                                 + "     , alg_locatie l"
                                 + " WHERE prs_contactpersoon_key = " + ckey
                                 + "   AND c.alg_locatie_key = l.alg_locatie_key"

            var new_loc_sql = "SELECT l.alg_locatie_key"
                            + "     , l.alg_locatie_omschrijving"
                            + "  FROM alg_v_aanweziglocatie l"
                            + (ALGwritelevel > -1
                              ? " WHERE l.alg_locatie_key IN (SELECT alg_locatie_key"
                                + "                               FROM fac_v_my_locations "
                                + "                              WHERE prs_perslid_key = " + user_key
                                + "                                AND niveau = " + ALGwritelevel + ")"
                              : "")
                            + " MINUS "
                            + existing_loc_sql
                            + " ORDER BY 2"

            FCLTselector("fldLocPossible", new_loc_sql,
                    {   startmulti: true,
                        size: 10
                    } ); %>

            </td>
            <td class="nowrap">
                <div class="mv r" onclick="moveRightLoc()"><%=I("fa-chevron-right")%><%=L("lcl_add")%></div>
                <div class="mv l" onclick="removeLoc()"><%=L("lcl_delete")%><%=I("fa-chevron-left")%></div>
            </td>
            <td style="width:45%">
              <label for="fldLocExist"><%=L("lcl_prs_loc_selected")%></label><br/>
<%
              FCLTselector("fldLocExist", existing_loc_sql+ " ORDER BY l.alg_locatie_upper" ,
                    {   startmulti: true,
                        size: 10
                    });
%>
        </td>
      </tr>
      </table>
<%   MODAL_BLOCK_END();
 IFACE.FORM_END(); %>
    </form>
<%
    var buttons = [{title: L("lcl_submit"), icon: "fa-fclt-save", action: "bdl_submit()", importance: 1},
                   {title: L("lcl_cancel"), icon: "fa-fclt-cancel", action: "FcltMgr.closeDetail(window, { cancel: true })", importance: 3}];
    SIMPLE_BLOCK_START();
      CreateButtons(buttons);
    SIMPLE_BLOCK_END();
%>
<% MODAL_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
