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

    File:           prs_edit_perslid.asp
    Description:    edit or add persons
    Parameters:     prs_key (required)

    Context:
    Note:           De meeste kenmerken mag je alleen bewerken als de WEB_PRSMAN hebt
                    Sommige echter ook als je WEB_PRSUSE hebt.
                    OSLOGIN mag alleen als je WEB_PRSSYS hebt.
                    Tenslotte mag je als je PROFIL hebt voor jezelf hetzelfde als PRSUSE

*/ %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/persoonselector.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/afdelingselector.inc" -->
<!-- #include file="../Shared/functieselector.inc" -->
<!-- #include file="prs_flexkenmerk.inc" -->
<!-- #include file="../Shared/calendar.inc"-->
<!-- #include file="prs.inc"-->

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

var prs_key  = getQParamInt("prs_key", -1);

var prsauthparams = prs.checkAutorisation(prs_key);
user.auth_required_or_abort(prsauthparams.writeman || prsauthparams.writeuse || prsauthparams.writeself);

if (prs_key > 0)
    var thisUser = new Perslid(prs_key); // geeft ook handige informatie

var startEmpty = 1;
var endEmpty = 1;

//   if (!alg.canWriteRuimte(room_key, authparams.mALGreadlevel))
//     user.auth_required_or_abort(false);
if (prs_key > -1)
{
  sql = "SELECT p.prs_perslid_key, "
      + "       prs_srtperslid_key, "
      + "       p.prs_afdeling_key, "
      + "       prs_perslid_naam, "
      + "       prs_perslid_geslacht, "
      + "       prs_perslid_voorletters, "
      + "       prs_perslid_voornaam, "
      + "       prs_perslid_titel, "
      + "       prs_perslid_initialen, "
      + "       prs_perslid_nr, "
      + "       prs_perslid_dienstverband, "
      + "       prs_perslid_opp, "
      + "       p.mld_adres_key, "
      + "       prs_perslid_uurloon, "
      + "       prs_perslid_telefoonnr, "
      + "       prs_perslid_lang, "
      + "       prs_perslid_oslogin, "
      + "       prs_perslid_oslogin2, "
      + "       prs_perslid_apikey, "
      + "       prs_perslid_ingangsdatum, "
      + "       prs_perslid_einddatum, "
      + "       d.prs_bedrijf_key, "
      + "       prs_perslid_tussenvoegsel, "
      + "       fac_profiel_key, "
      + "       prs_perslid_email, "
      + "       prs_perslid_mobiel, "
      + "       prs_perslid_ingids "
      + "  FROM prs_v_aanwezigperslid p, "
      + "       prs_afdeling d "
      + " WHERE p.prs_perslid_key = " + prs_key
      + "   AND p.prs_afdeling_key = d.prs_afdeling_key ";

  oRsMes = Oracle.Execute(sql);
  if (!oRsMes.eof) {
    var prs_srtkey = oRsMes("prs_srtperslid_key").value;
    var prs_afdkey = oRsMes("prs_afdeling_key").value;
    var prs_naam   = oRsMes("prs_perslid_naam").value;
    var prs_vltr   = oRsMes("prs_perslid_voorletters").value;
    var prs_vrnaam = oRsMes("prs_perslid_voornaam").value;
    var prs_titel  = oRsMes("prs_perslid_titel").value;
    var prs_ini    = oRsMes("prs_perslid_initialen").value;
    var prs_nr     = oRsMes("prs_perslid_nr").value;
    var prs_dnst   = oRsMes("prs_perslid_dienstverband").value;
    var prs_opp    = oRsMes("prs_perslid_opp").value;
    var mld_adrkey = oRsMes("mld_adres_key").value;
    var prs_uurln  = oRsMes("prs_perslid_uurloon").value;
    var prs_telnr  = oRsMes("prs_perslid_telefoonnr").value;
    var prs_lang   = oRsMes("prs_perslid_lang").value;
    var prs_oslog  = oRsMes("prs_perslid_oslogin").value;
    var prs_oslog2 = oRsMes("prs_perslid_oslogin2").value;
    var prs_apikey = oRsMes("prs_perslid_apikey").value;
    var prs_ingdat = oRsMes("prs_perslid_ingangsdatum").value;
    var prs_enddat = oRsMes("prs_perslid_einddatum").value;
    var prs_bdrkey = oRsMes("prs_bedrijf_key").value;
    var prs_geslacht = oRsMes("prs_perslid_geslacht").value;
    var prs_tussen = oRsMes("prs_perslid_tussenvoegsel").value;
    var fac_prkey  = oRsMes("fac_profiel_key").value;
    var prs_email  = oRsMes("prs_perslid_email").value;
    var prs_mobiel = oRsMes("prs_perslid_mobiel").value;
    var prs_bdrkey = oRsMes("prs_bedrijf_key").value;
    var ingids     = oRsMes("prs_perslid_ingids").value == 1;
  } else {
      shared.record_not_found("<!--"+prs_key+"-->");
  }
  oRsMes.Close();

  if (prs_ingdat != null)
  {
    prs_ingdat = new Date(prs_ingdat);
    startEmpty = 0;
  }

  if (prs_enddat != null)
  {
    prs_enddat = new Date(prs_enddat);
    endEmpty = 0;
  }
}
else // nieuw
{
    ingids = true;
    var prs_srtkey = getQParamInt("prs_srtkey", -1);
}
%>
<html>
  <head>
<%
    FCLTHeader.Generate();
    // TODO: moet hier niet prs.checkAutorisation gebruikt worden?
    //       Die is er beter tegen bestand dat prs_key misschien null is?
    var xfunc = user.func_enabled("*", null, null, prs_key>0?prs_key:null);
%>
    <script type="text/javascript">
      function prs_submit()
      {
          if (!validateForm("u2"))
              return false;
          $.post($("form[name=u2]")[0].action, $("[name=u2]").serialize(), FcltCallbackClose, "json");
      }
      function prs_cancel()
      {
          FcltMgr.closeDetail(window, { cancel: true } );
      }

<% if (prs_key > 0) { %>
      function edit_werklocatie(img, prs_key)
      {
          var url = "../../appl/prs/prs_move_huidigewerkplek.asp?prs_key=" + prs_key;
          FcltMgr.openModalDetail(url, "<%=safe.jsstring(thisUser.naam())%>",
                      { callback: function (data) { if (data.wp_aanduiding) {$(img).closest("tr").find("input").val(data.wp_aanduiding);} } });
      }

      function edit_photo(img)
      {
          var url = "<%=protectQS.create("../../appl/shared/BijlagenForm.asp?module=SML&key="+prs_key)%>";
          FcltMgr.openModalDetail(url, L("lcl_prs_upload_foto"),
                      { callback: FcltMgr.reload } );
      }
<% } %>
    </script>
  </head>

  <body id="editbody">
<%
    var buttons =  [ {title: L("lcl_submit"), action:"prs_submit()", icon: "opslaan.png" },
                     {title: L("lcl_cancel"), action:"prs_cancel()", icon: "undo.png" }];

    IFRAMER_HEADER(L("lcl_prs_persoon_frame"), buttons);
%>
<div id="edit">
<form name="u2"
      action="prs_edit_perslid_save.asp?prs_key=<%=prs_key%>"
      method="post"
      target="hidFrameSubmit"
      onSubmit="prs_submit();">
<%
// 4i uitstapjes naar werkplekken en verplichtingen zijn als direct frame in prs_perslid.asp gerealiseerd
if (prsauthparams.writeman)
    manRWFIELD = RWFIELDTR;
else
    manRWFIELD = function (a,b,c,d,e) { ROFIELDTR(b,c,d,e) }; // als geen USE of <self> dan zijn er veel readonly

if ((prsauthparams.writeself || prsauthparams.writeuse) && S("prs_telefoonnr_aut_use")==1) // dan mag USE er ook een paar
    useRWFIELD = RWFIELDTR;
else
    useRWFIELD = manRWFIELD

BLOCK_START("prsPerslid", L("lcl_prs_basisblok"));
if (prs_key > 0) // Fotoblokje alleen bij bestaande records
{
%>  <tr valign="center"><td colspan="2"></td><td rowspan="20">
<%    if (prsauthparams.writeuse)
      { %>
        <img src='../../appl/Pictures/wijzigen.png'
             class='details'
             title='<%=L("lcl_change")%>'
             onclick='edit_photo(this)'>
<%    } %>
        <img id='photo' src="<%=thisUser.photoinfo().photopaththumb %>">
    </td></tr>
<%
}
    manRWFIELD("prs_naam",  "fld",          L("lcl_prs_person_name"),        prs_naam, {required: true, maxlength: 60});
    var sql = "SELECT  0, "+safe.quoted_sql(L("lcl_prs_person_geslachtV"))+" FROM DUAL UNION ALL"
            + " SELECT 1, "+safe.quoted_sql(L("lcl_prs_person_geslachtM"))+" FROM DUAL";
    FCLTselector("prs_geslacht", sql,
               { label: L("lcl_prs_person_geslacht"),
                 initKey: prs_geslacht,
                 emptyOption: L("lcl_prs_person_geslachtO"),
                 readonly: !prsauthparams.writeman
              });

    manRWFIELD("prs_tussen","fldshort",     L("lcl_prs_person_tussen"),      prs_tussen, {maxlength: 15, suppressEmpty: true});
    manRWFIELD("prs_vltr",  "fldshort",     L("lcl_prs_person_voorletters"), prs_vltr, {maxlength: 15, suppressEmpty: true});
    manRWFIELD("prs_vrnaam","fld",          L("lcl_prs_person_voornaam"),    prs_vrnaam, {maxlength: 30});
    manRWFIELD("prs_titel", "fldshort",     L("lcl_prs_person_title"),       prs_titel, {maxlength: 15, suppressEmpty: true});
    manRWFIELD("prs_ini",   "fldshort",     L("lcl_prs_person_initials"),    prs_ini, {maxlength: 10, suppressEmpty: true});

    if (S("language_toggles").length > 1)
    {
        var sql = "";
        for (var i=0; i<S("language_toggles").length; i++)
        {
            sql += (i>0 ? " UNION " : "") + "SELECT "+safe.quoted_sql(S("language_toggles")[i])+","+safe.quoted_sql(S("language_toggles")[i])+" FROM DUAL";
        }
        FCLTselector("prs_lang", sql,
                   { label: L("lcl_lcl_taal"),
                     initKey: prs_lang,
                     emptyOption: ""
                   });
    }

    if (S("prs_hide_fixed_fields") == 0 )
    {
        useRWFIELD("prs_telnr",  "fld",     L("lcl_prs_person_phone"),       prs_telnr, {maxlength: 15});
        useRWFIELD("prs_mobiel", "fld",     L("lcl_prs_person_mobile"),      prs_mobiel, {maxlength: 15});
        manRWFIELD("prs_email",  "fld",     L("lcl_prs_person_email"),       prs_email, {maxlength: 200});
    }
    if (prsauthparams.writesys || (S("prs_mgt_edit_login") && xfunc.canWrite("WEB_FACMGT"))) {
        RWFIELDTR("prs_oslog",  "fld",     L("lcl_prs_person_login"),       prs_oslog, {maxlength: 30});
        RWFIELDTR("prs_oslog2",  "fld",    L("lcl_prs_person_login2"),      prs_oslog2, {maxlength: 30});
        if (prsauthparams.writetab)
            RWFIELDTR("prs_apikey",  "fld",    L("lcl_prs_apiuser"),      prs_apikey, {maxlength: 64});
    }

    if (prs_key > 0)
    {
      var thisPrs = prs.prs_perslid(prs_key, { withWL: true }); // geeft ook alle informatie
      var wl = thisPrs.werklocatie.alg_locatie_key? thisPrs.werklocatie.aanduiding : "";
      if (prsauthparams.writeuse)
      {   %><tr><td class="label">
                  <label class="selector"><%=L("lcl_prs_work_location")%></label>
                  <span class='labelextra'>
                      <img class='details' src='../../appl/Pictures/wijzigen.png'
                           title='<%=L("lcl_change")%>'
                           onclick='edit_werklocatie(this, <%=prs_key%>)'>
                  </span></td>
                 <td><%ROFIELD('fldworklocation', null, wl); %></td>
          </tr><%
      }
      else
        ROFIELDTR('fld', L("lcl_prs_work_location"), wl, {suppressEmpty: true});
    }
BLOCK_END();

BLOCK_START("prsPerslid2", L("lcl_prs_organisatieblok"));
    if (prsauthparams.writesys)
      CHECKBOXTR(L("lcl_prs_in_guide"), "fldnotingids", "ingids", ingids);
    manRWFIELD("prs_nr",  "fld", L("lcl_prs_person_nr"),        prs_nr);

    FCLTafdelingselector("prs_afdkey",
                         "prs_afdkey",
                        { departmentKey: prs_afdkey,
                          label: L("lcl_prs_organisatie"),
                          autlevel: prsauthparams.PRSreadlevel, // Was voorheen niet aanwezig
                          readonly: !prsauthparams.writeman,
                          required: true
                       });

    FCLTfunctieselector("prs_srtkey", "prs_srtkey" ,
                            {label: L("lcl_prs_person_function"),
                             srtperslidKey: prs_srtkey,
                             emptyOption: "",
                             readonly: !prsauthparams.writeman,
                             required: true
                            });

    manRWFIELD("prs_opp",  "fldshort", L("lcl_prs_person_opp"),           prs_opp, {maxlength: 10, datatype: "float", suppressEmpty: true});
    manRWFIELD("prs_dnst", "fldshort", L("lcl_prs_person_dienstverband"), prs_dnst, {maxlength: 5, datatype: "float", suppressEmpty: true});

    if (S("prs_perslid_datums") == 1)
    {
        FCLTcalendar( "prs_ingdat",
                    { label  : L("lcl_prs_person_ingangsdatum"),
                      datum  : prs_ingdat,
                      readonly: !prsauthparams.writeman,
                      required: true
                    });


        FCLTcalendar( "prs_enddat",
                    { label  : L("lcl_prs_person_einddatum"),
                      datum  : prs_enddat,
                      readonly: !prsauthparams.writeman,
                      initEmpty: endEmpty
                    });
    }

       sql = "SELECT   fac_profiel_key, "
           + "         fac_profiel_omschrijving "
           + "    FROM fac_profiel "
           + "ORDER BY 2 "

       FCLTselector("fac_prkey", sql,
                   { label: L("lcl_prs_profile"),
                     initKey: fac_prkey,
                     emptyOption: "",
                     readonly: !prsauthparams.writeman
                  });
     sql = "SELECT   mld_adres_key, "
         + "         mld_adres_naam "
         + "    FROM mld_v_afleveradres "
         + "ORDER BY mld_adres_naam "

       FCLTselector("mld_adrkey", sql,
                   { label: L("lcl_delivery_address"),
                     initKey: mld_adrkey,
                     emptyOption: "",
                     readonly: !prsauthparams.writeman
                  });

if (S("prs_hide_fixed_fields") == 0 ) {
    manRWFIELD("prs_uurln", "fldshort", L("lcl_prs_person_uurloon"), prs_uurln, {maxlength: 8, datatype: "float", suppressEmpty: true});
}
BLOCK_END();

// ============================= NOTIFICATIES ========================================
// Kan de gebruiker uberhaubt *iets* overrulen? Zo nee dan ook geen vinkjes tonen
var sql = "select count(*) from fac_srtnotificatie"
        + " where fac_srtnotificatie_mode IS NOT NULL"
        + "   AND fac_srtnotificatie_mode > 0"
        + "   AND fac_srtnotificatie_usermode IS NOT NULL"
        + "   and fac_srtnotificatie_usermode > 0"
var oRs = Oracle.Execute(sql);
var anyOverrule = oRs(0).Value > 0;
oRs.Close();

if (prs_key > 0 && (prs_key == user_key || prsauthparams.writesys || prsauthparams.writeman) && anyOverrule)
{
  BLOCK_START("prsNoti", L("lcl_noti_prsvoorkeur"));
    CHECKBOXTR(L("lcl_noti_niks"), "fldfacnoti", "noti0", thisUser.noti_niks());
    CHECKBOXTR(L("lcl_noti_portal"), "fldfacnoti", "noti1", thisUser.noti_portal());
    CHECKBOXTR(L("lcl_noti_email"), "fldfacnoti", "noti2", thisUser.noti_email());
    CHECKBOXTR(L("lcl_noti_sms"), "fldfacnoti", "noti4", thisUser.noti_sms());
    CHECKBOXTR(L("lcl_noti_popup"), "fldfacnoti", "noti8", thisUser.noti_popup());
  BLOCK_END();
}

BLOCK_START("prsFlex"+(S("prs_flexcolumns")!=1?"2":""), L("lcl_prs_flexblok"));
  generateFlexKenmerkCode ({
     link_key       : prs_key,
     srt_key        : prs_srtkey,
     niveau         : "P",
     reado          : false,
     flexcolumns    : S("prs_flexcolumns"),
     prsauthparams  : prsauthparams
});
BLOCK_END();
IFACE.FORM_END();
%>
</form>
<iframe src="../Shared/empty.html" name="hidFrameSubmit" style="display:none"></iframe>
</div>
</body>
</html>
