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

    File:               fac_refresh_accept.asp
    Description:        Refresh de acceptatie database
    Parameters:

    Context:
    Note:
*/
%>

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

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

user.checkAutorisation("WEB_FACRFR");
var fso = Server.CreateObject("Scripting.FileSystemObject");


function getUserEmail()
{
    var adr = user.prs_perslid_email() || S("puo_forcemailaddress");
    if (adr.indexOf("<") > 0)
    {   // Verwijder de eventuele alias voor het echte mailadres.
        adr = adr.substr(adr.indexOf("<")+1);
        adr = adr.substr(0, adr.indexOf(">"));
    }
    return adr;
}

// Let op! De settings kunnen overruled worden in \CUST\custenv.wsc
// "e:\\Proddump\\fclt.dmp"
// of "e:\\Proddump\\fclt_{0}.dmp.7z" voor fclt_LOGC.dmp.7z
var dumpfile = S("refreshfromdumpfile", "").format(customerId);
if (!dumpfile)
    abort_with_warning(L("lcl_fac_refresh_environment"));  // Alleen voor acceptatieomgeving

if (!fso.FileExists(dumpfile))
    abort_with_warning(L("lcl_fac_refresh_nodumpfile").format(dumpfile));

if ( S("refreshfromdumpscriptfolder", "") == "")
    abort_with_warning(L("lcl_fac_refresh_paramserror"));  // Onvoldoende parameters

var refresh_bat = S("refreshfromdumpscriptfolder") + "\\refresh_" + customerId + "_TEST.bat";
var isscheduled = fso.FileExists(refresh_bat);
if (isscheduled)
    var scheduled_timestamp = fso.getFile(refresh_bat).DateLastModified;

var dumpfile_timestamp = fso.getFile(dumpfile).DateLastModified;
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script>
        function isValidEmailAddress(emailAddress)
        {
            var pattern = new RegExp(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/);
            return pattern.test(emailAddress);
        }

        async function doSchedule()
        {
            if (!await validateForm("refreshform"))
                return false;

            var m = $("#emailadr").val();
            if (!isValidEmailAddress(m))
                return false;

            $.post( $("form[name=refreshform]")[0].action
                  , $("[name=refreshform]").serialize()
                  , doScheduleCallback
                  , "json"
                  );
            return true; // disable button
        }

        function doScheduleCallback(jsondata)
        {
            var txt = "<%=safe.jsstring(L("lcl_fac_refresh_newschedule").format(customerId))%>";
            FcltToast(txt);
            $("#accept_hint").closest("tr").hide();
            $("#accept_msg").text(txt);
        }

        function doCancel()
        {
            FcltMgr.closeDetail(window, { cancel: true } );
        }

    </script>
  </head>

  <body class="fclt-modal" id="mod_refresh">
      <% MODAL_START();
      %>
    <form id="refreshform" name="refreshform" action="fac_refresh_accept_save.asp">
        <% MODAL_BLOCK_START("dbrefresh", L("lcl_fac_refresh_accept"), {icon: "fa-siren-on"}); %>
        <%
            if (user.has("WEB_FACTAB"))
                ROFIELDTR("fld", L("lcl_fac_refresh_dumpfile"), dumpfile);
            RWFIELDTR("emailadr", "fld mailto", L("lcl_fac_refresh_notify"), getUserEmail(), {readonly: (isscheduled?true:false), required: true});
            ROFIELDTR("fldfacrefresh", "", isscheduled?L("lcl_fac_refresh_oldschedule").format(customerId, toDateTimeString(scheduled_timestamp)):"", {id: "accept_msg"});
if (!isscheduled) {
%>
         <tr>
            <td class="label"><label for="fac_refresh_anon"><%=L("lcl_fac_refresh_anonimize")%></label></td>
                <td>
                  <label for="fac_refresh_anon">
                    <input type="radio" name="fac_refresh_anon" id="fac_refresh_anon_none" value="none" checked="1"><%=L("lcl_fac_refresh_anon_none")%>
                  </label>
                  <br />
                  <label for="fac_refresh_anon_pseudo">
                    <input type="radio" name="fac_refresh_anon" id="fac_refresh_anon_pseudo" value="pseudo"><%=L("lcl_fac_refresh_anon_pseudo")%>
                  </label>
                  <br />
                  <label for="fac_refresh_anon_delete">
                    <input type="radio" name="fac_refresh_anon" id="fac_refresh_anon_delete" value="delete"><%=L("lcl_fac_refresh_anon_delete")%>
                  </label>
               </td>
            </tr>
<%
            // Hier kunnen dan allerlei waarschuwingen worden getoond.
            ROTEXTAREATR("fldfacrefresh", L("lcl_fac_refresh_messagelbl"), L("lcl_fac_refresh_message").replace(/\x0D/g, "<br>").format(toDateString(dumpfile_timestamp)), { ishtmlsafe: true, id: "accept_hint" });
}
        MODAL_BLOCK_END();

        var buttons = [];
        buttons.push({id: "docancel", title: L("lcl_cancel"), icon: "fa-fclt-cancel", action: "doCancel()" });

        if (!isscheduled)
        {
            buttons.push({id: "dorefresh", title: L("lcl_fac_refresh_inplannen"), action: "doSchedule()", singlepress: true, icon: "fa-fclt-refresh"});
        }
        SIMPLE_BLOCK_START();
        CreateButtons(buttons);
        SIMPLE_BLOCK_END();
    IFACE.FORM_END();
%>
    </form>
    <% MODAL_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
