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

    File:         prs_move_huidigewerkplek.asp
    Description:  Verplaats de huidige werkplekbezetting
                  PRS_PERSLID.alg_onroerendgoed_keys wordt aangepast
                  PRS_PERSLID.alg_onroerendgoed_type wordt aangepast
                  PRS_PERSLID.prs_perslid_bezetdatum wordt aangepast
    Parameters:   prs_key, prs_perslid_key waarvan huidige werkplek wordt veranderd

    Context:      fac_user_info zet de huidige werklocatie van een persoon
    Note:         Er worden dus geen nieuwe records aangemaakt

*/ %>

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


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

var prs_key = getQParamInt("prs_key");  // De persoon waarvan de werklocatie wordt gemuteerd

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

var perslidInfo = prs.prs_perslid(prs_key);

var aog_key = perslidInfo.alg_onroerendgoed_keys;    // De te verplaatsen werklocaie
var aog_type = perslidInfo.alg_onroerendgoed_type;   // Type van de te verplaatsen werklocaie

var loc_key = -1;
var geb_key = -1;
var ter_key = -1;
var ver_key = -1;
var rui_key = -1;
var wp_key  = -1;

if (aog_type == "L")
{
  var loc_key = aog_key;
  var geb_key = -1;
  var ter_key = -1;
  var ver_key = -1;
  var rui_key = -1;
  var wp_key  = -1;
}
else if (aog_type == "W")
{
  var sql = " SELECT w.alg_locatie_key"
          + "      , w.alg_gebouw_key"
          + "      , w.alg_verdieping_key"
          + "      , w.alg_ruimte_key"
          + "   FROM prs_v_werkplek_gegevens w"
          + "  WHERE w.prs_werkplek_key = " + aog_key;
  var oRs = Oracle.Execute(sql);
  var loc_key = oRs("alg_locatie_key").value;
  var geb_key = oRs("alg_gebouw_key").value;
  var ter_key = -1;
  var ver_key = oRs("alg_verdieping_key").value;
  var rui_key = oRs("alg_ruimte_key").value;
  var wp_key  = aog_key;
  oRs.Close();
}
else
{ // aog_type is "G", "T", "V" of "R".
  var sql = "SELECT g.alg_locatie_key"
          + "     , aog.alg_gebouw_key"
          + "     , aog.alg_terreinsector_key"
          + "     , aog.alg_verdieping_key"
          + "     , aog.alg_ruimte_key"
          + "  FROM alg_v_onroerendgoed aog"
          + "     , alg_gebouw g"
          + " WHERE aog.alg_gebouw_key = g.alg_gebouw_key"
          + "   AND aog.alg_onroerendgoed_keys = " + aog_key;
  oRs = Oracle.Execute(sql);
  if (!oRs.eof)
  {
    var loc_key = oRs("alg_locatie_key").value;
    var geb_key = oRs("alg_gebouw_key").value? oRs("alg_gebouw_key").value : -1;
    var ter_key = oRs("alg_terreinsector_key").value? oRs("alg_terreinsector_key").value : -1;
    var ver_key = oRs("alg_verdieping_key").value? oRs("alg_verdieping_key").value : -1;
    var rui_key = oRs("alg_ruimte_key").value? oRs("alg_ruimte_key").value : -1;
    var wp_key  = -1;
  }
  oRs.Close();
}
%>

<html>
  <head>
<%  FCLTHeader.Generate();
    var submitting = getQParamInt("submit", 0) == 1;
    if (submitting)
    {
      protectRequest.validateToken();
      var newlockey = getFParamInt("locatiekey");
      var newgebkey = getFParamInt("gebouwkey");
      var newverkey = getFParamInt("verdiepingkey");
      var newruikey = getFParamInt("ruimtekey");
      var newwpkey = getFParamInt("werkplekkey");
      var newwpkey = (S("prs_werkplek_implicit") == 0? getFParamInt("werkplekkey") : -1); /* ruimte/werkplek */
      var bttype = getFParam("bttype");
      var aogkey;
      var aogtype;
      var bezetdatum = "SYSDATE";

      if (newwpkey != -1)
      {
        aogkey = newwpkey;
        aogtype = "W";
      }
      else if (newruikey != -1)
      {
        aogkey = newruikey;
        aogtype = "R";
      }
      else if (newverkey != -1)
      {
        aogkey = newverkey;
        aogtype = "V";
      }
      else if (newgebkey != -1)
      {
        aogkey = newgebkey;
        aogtype = "G";
      }
      else if (newlockey != -1)
      {
        aogkey = newlockey;
        aogtype = "L";
      }
      else
      {
        aogkey = "null";
        aogtype = "";
        bezetdatum = "null";
      }

      sql = "UPDATE prs_perslid"
          + "   SET alg_onroerendgoed_keys = " + aogkey
          + "     , alg_onroerendgoed_type = " + safe.quoted_sql(aogtype)
          + "     , prs_perslid_bezetdatum = " + bezetdatum
          + " WHERE prs_perslid_key = " + prs_key;
      var err = Oracle.Execute(sql);

      var perslidInfo = prs.prs_perslid(prs_key, { withWL: true });

%>    <script type="text/javascript">
        FcltMgr.closeDetail(window, { success: true,
                                      wp_aanduiding : "<%=safe.jsstring(perslidInfo.werklocatie.alg_locatie_key? perslidInfo.werklocatie.aanduiding : "")%>" } );
      </script>
<%    Response.End;
    }
%>
    <script type="text/javascript">
      async function prs_submit() {
        document.activeElement.blur(); // trigger laatste onChanges
        if (!await validateForm("u2")) {
          return false;
        }
        document.forms.u2.submit();
      }
      function prs_delete() {
        FcltMgr.confirm(L("lcl_prs_del_my_werklocatie"), async function () {
            for (var i in psObject[0]) { // Plaatsselector leegmaken
                if (typeof psObject[0][i] == "object" && psObject[0][i] instanceof Suggest) {
                  await psObject[0][i].setValue(-1);
                }
            }
            document.forms.u2.submit();
          }
        );
      }
      function prs_cancel() {
        FcltMgr.closeDetail(window);
      }
    </script>
  </head>

  <body class="fclt-modal" id="mod_movewerkplek" style="width:400px">
    <% MODAL_START(); %>
    <form name="u2"
          action="prs_move_huidigewerkplek.asp?submit=1<%=buildTransitParam(["prs_key"])%>"
          method="post"
          onSubmit="prs_submit();">
<%
    MODAL_BLOCK_START("bezettingInfo", user_key == prs_key ? L("lcl_prs_my_werklocatie") : L("lcl_prs_werklocatie_van").format(perslidInfo.naam), {icon: "fa-map-marker-smile"});
    var requiredLevel = 3; // Default 'Gebouw'
    switch (S("mobile_alg_level")) { // Anders incompatible met mobile want het gaat om dezelfde werklocatie
      case "L":
        requiredLevel = 2;
        break;
      case "V":
        requiredLevel = 4;
        break;
    }
    FCLTplaatsselector(S("werklocatie_alg_scope"), {
                             locatiekey: loc_key,
                             gebouwkey: geb_key,
                             verdiepingkey: ver_key,
                             ruimtekey: rui_key,
                             werkplekkey: wp_key,
                             startlevel: 2, // locatie
                             filtercode: "FAC",
                             eindlevel: (S("prs_werkplek_implicit") == 0)? 6 : 5, /* ruimte/werkplek */
                             requiredlevel: requiredLevel
                           });
    MODAL_BLOCK_END();
    SIMPLE_BLOCK_START();
      var buttons = [];
      buttons.push({ title: L("lcl_submit"), action: "prs_submit()", importance: 1, icon: "fa-fclt-save"});
      if (aog_key > 0) {
        buttons.push({ title: L("lcl_delete"), action: "prs_delete()", importance: 3, icon: "fa-trash-alt"});
      }
      buttons.push({ title: L("lcl_cancel"), action: "prs_cancel()", importance: 3, icon: "fa-fclt-cancel"});
      CreateButtons(buttons);
    SIMPLE_BLOCK_END();
IFACE.FORM_END();
%>
    </form>
    <% MODAL_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
