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

    File:               prs/prs_perslid_substitutes.asp
    Description:        Interface to add substitutes of a person
    Parameters:
        pkey            Optional PRS_PERSLID_KEY
        mode            'modify','save'
    Context:            From perslid.asp or from self (form-submit)
    Note:

*/ %>

<!-- #include file="../Shared/common.inc"-->
<!-- #include file="../Shared/iface.inc"-->
<!-- #include file="../Shared/persoonselector.inc"-->
<!-- #include file="../Shared/calendar.inc" -->
<!-- #include file="prs.inc" -->

<%
FCLTHeader.Requires({ plugins:["jQuery"],
                      js: ["../prs/sel_substitute_tab.js"]
                    })

// Form: on submit prssubstitutes.asp.  QueryString: on initial call prssubstitutes.asp
if (hasFParam("mode"))
    protectRequest.validateToken();
var mode = getFParam("mode", getQParam("mode", "modify"));
var pkey = getFParamInt("pkey", getQParamInt("pkey"));

var prsauthparams = prs.checkAutorisation(pkey);
user.auth_required_or_abort(prsauthparams.canSubChange);
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script type="text/javascript">
      function prs_submit () {
          //u2.submit();  // TODO
          SaveSubstitutes();
      }

      function prs_cancel()
      {
          FcltMgr.closeDetail(window, { cancel: true } );
      }
    </script>
  </head>
  <body class="fclt-modal" id="mod_substitutes">
<%  MODAL_START();
    if (mode == 'save')
    {
      var substitutes_key_arr = getFParamIntArray("substitutes_string", []);
      var substitutes_exp_arr = getFParamIntArray("substitutes_date" , []);

      // Delete de oude vervangers
      var sql = "DELETE FROM prs_collega"
              + " WHERE prs_perslid_key = " + pkey;
      Oracle.Execute(sql);

      // Sla de nieuwe vervangers op
      for (i = 0; i < substitutes_key_arr.length; i++)
      {
        var sql = "INSERT INTO prs_collega"
                + "( prs_perslid_key"
                + ", prs_perslid_key_alt"
                + ", prs_collega_vervaldatum"
                + ") VALUES ("
                + pkey
                + ", " + substitutes_key_arr[i]
                + ", " + (substitutes_exp_arr[i] > 0 ? (new Date(substitutes_exp_arr[i])).toSQL() : "NULL")
                + ")";
        Oracle.Execute(sql);
      }
%>
      <script type="text/javascript">
        FcltMgr.closeDetail(window, { success: true });
      </script>
<%
    }
    else // mode == 'modify'
    {  %>
      <form name="u2" method="post" target="hidFrame" onsubmit="return false">
      <% MODAL_BLOCK_START("", L("lcl_prs_vervangers"), {icon: "fa-people-arrows"}); %>
        <input type="hidden" name="mode" id="mode" value="save">
        <input type="hidden" name="prs_key" id="prs_key" value="<%=pkey%>">
        <input type="hidden" name="substitutes_string" id="substitutes_string" value="">
        <input type="hidden" name="substitutes_date" id="substitutes_date" value="">
        <table width="100%">
<%
      var sql = "SELECT " + S("prs_dep_string") + " prs_afdeling_naam"
              + "     , " + S("prs_pers_string") + " prs_perslid_naam"
              + "  FROM prs_v_afdeling d"
              + "     , prs_perslid p"
              + " WHERE prs_afdeling_verwijder IS NULL"
              + "   AND p.prs_afdeling_key = d.prs_afdeling_key"
              + "   AND p.prs_perslid_key = " + pkey;
      var oRs = Oracle.Execute(sql);
      if (!oRs.eof)
      {
        dept_name = oRs(0).Value;
        prs_name = oRs(1).Value;
      }
      oRs.Close();

      FCLTpersoonselector( "person",
                           "sgPerson",
                           { label: L("lcl_prs_substitute"),
                             extracode: "A",
                             extraParamField: "dept",
                             fieldNameKey: "prs_key",
                             filtercode: "SUBS",
                             urlAdd: [{ urlParam: "prs_key",  field: "prs_key", init: pkey }]
                           });
      FCLTcalendar( "person_exp",
                    { datum: null,
                      initEmpty: true,
                      label: L("lcl_prs_expire"),
                      suppressEmpty: true
                    });

%>
        <tr>
            <td colspan="2">
                <!-- SELECTED ITEMS -->
                <label><%=L("lcl_prs_substitutes")%></label>
                <table id="sel_items" cellspacing="0" cellpadding="0" width="100%">
                <tr>
                    <th width="15"></th>
                    <th><%=L("lcl_prs_name")%></th>
                    <th><%=L("lcl_prs_organisatie")%></th>
                    <th><%=L("lcl_prs_expire")%></th>
                </tr>
                </table>
            </td>
        </tr>
<%      MODAL_BLOCK_END();

        var buttons = [{ title: L("lcl_prs_subst_add_btn"), action: "AddItem()", icon: "fa-plus" },
                       { title: L("lcl_submit"), action: "prs_submit()", icon: "fa-fclt-save" }];
        SIMPLE_BLOCK_START();
          CreateButtons(buttons);
        SIMPLE_BLOCK_END();
IFACE.FORM_END();
%>
      </form>
      <iframe src="../Shared/empty.asp" frameborder="10" name="hidFrame" id="hidFrame" style="display:none"></iframe>

      <script type="text/javascript">
<%      // Bestaande vervangers weergeven
        var sql = "SELECT pc.prs_perslid_key_alt"
                + "     , " + S("prs_pers_string") + " prs_perslid_naam"
                + "     , " + S("prs_dep_string") + " prs_afdeling_naam"
                + "     , pc.prs_collega_vervaldatum"
                + "  FROM prs_collega pc"
                + "     , prs_perslid p"
                + "     , prs_v_afdeling d"
                + " WHERE pc.prs_perslid_key_alt = p.prs_perslid_key"
                + "   AND p.prs_afdeling_key = d.prs_afdeling_key"
                + "   AND pc.prs_perslid_key = " + pkey
                + " ORDER BY 2";
        var oRs = Oracle.Execute(sql);
        while (!oRs.eof)
        {
          var collega_vervaldatum = (oRs(3).Value != null ? (new Date(oRs(3).Value)).getTime() : "");
%>
          AddItem( <%=oRs(0).value%>
                 , "<%=safe.jsstring(oRs(1).Value)%>"
                 , "<%=safe.jsstring(oRs(2).Value)%>"
                 , "<%=collega_vervaldatum%>"
                 );
<%        oRs.MoveNext();
        }
        oRs.Close();
%>
      </script>
<%  }
    MODAL_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
