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

    File:           fac_edit_xsl.asp
    Description:    Bewerken van een xsl
    Parameters:
        xmlnode     "reservering" o.i.d.
    Context:
    Note:           PRSSYS autorisatie is nodig
                    Voorwaarden:
                        - De klant cust.xsl moet opgesplitst zijn in meerdere bestanden, eentje
                          per xmlnode, dus er moet een reservering.xsl zijn voor reserveringen etc.
                        - Als er een reservering_concept.xsl is wordt die geopend, anders reservering.xsl
                        - Opslaan gaat *altijd* naar reservering_concept.xsl
                    TOOD: Knop om te promoveren/ door te voeren

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

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

var autfunction = "WEB_PRSSYS";
var authparams = user.checkAutorisation(autfunction);

var xmlnode = getQParam("xmlnode", "");
if (xmlnode == customerId && user.has("WEB_FACTAB"))
{
    // ok
}
else if (xmlnode) // safety check
{
    var sql = "SELECT 1"
            + "  FROM fac_srtnotificatie"
            + "   WHERE fac_srtnotificatie_xmlnode = " + safe.quoted_sql(xmlnode);
    var oRs = Oracle.Execute(sql);
    user.auth_required_or_abort(!oRs.Eof); // hij moet bestaan als srtnotificatie
    oRs.Close()
}

%>
<html>
<head>
   <title>XSLT Transform</title>
<%
    FCLTHeader.Generate();
%>
    <script type="text/javascript">

        // We laten het testen altijd door de server doen.
        function revertXSL()
        {
            FcltMgr.confirm(L("lcl_xsl_revert") + "?", function() {
                $.post($("form[name=xsledit]")[0].action + "?revert=1", $("[name=xsledit]").serialize(), FcltCallbackRefresh, "json");
            });
        }

        function saveXSL(promote)
        {
            FcltMgr.confirm(L("lcl_xsl_doorvoeren_confirm"), { autoconfirm: !promote }, function() {
                $.post($("form[name=xsledit]")[0].action + (promote ? "?promote=1" : ""), $("[name=xsledit]").serialize(), FcltCallbackRefresh, "json");
            });
        }

        function saveAndPreviewXSL()
        {
            // Een controle op een ingevulde key om te testen zou nog mogen
            $.post($("form[name=xsledit]")[0].action + "?preview=1", $("[name=xsledit]").serialize(),
                FcltCallbackAndThen(function (data) {
                    if (data.success)
                    {
                        viewXSL();
                    }
                    }),
             "json");
        }

        function viewXSL()
        {
            // XML krijgt eigen window, die wil je vaak laten staan
            $("form[name=xslview]")[0].target = ($("#mode :selected").val()=='xml'?"RapPreview":"XmlPreview");
            $("form[name=xslview]")[0].submit();
        }

        function onOtherFile()
        {
            window.location.href = "<%=Request.ServerVariables("SCRIPT_NAME")%>?xmlnode=" + $("#sDocuments").val();
        }

        $(function ()
        {
            $("#concept,#sDocuments").change(onOtherFile);
        });

    </script>
</head>
<body class="editmode">
<%
    SUBFRAME_START();
    var buttons = [];
    if (xmlnode == customerId)
    {
        buttons = [ { title: L("lcl_xsl_preview"), icon: "fa-eye", action: "viewXSL()", id: "xslpreview" }
                   ];
    }
    else if (xmlnode)
    {
        buttons = [ { title: L("lcl_xsl_revert"), icon: "fa-undo", action: "revertXSL()", id: "xslrevert" },
                    { title: L("lcl_xsl_submit"), icon: "fa-fclt-save", action: "saveXSL()", id: "xslsave" },
                    { title: L("lcl_xsl_preview"), icon: "fa-eye", action: "saveAndPreviewXSL()", id: "xslsave" },
                    { title: L("lcl_xsl_doorvoeren"), icon: "fa-arrow-alt-right", action: "saveXSL(1)", id: "xslcommit" }
                   ];
    }
    IFRAMER_HEADER(L("lcl_xsledit_frame"), buttons);
    BLOCK_START("facEdit", L("lcl_xsledit_frame"), { icon: "fa-pen", wide: true });%>

    <tr>
        <td class="label">
            <label for="sDocuments"><%=safe.html(L("lcl_xsledit_bon"))%></label>
        </td>
        <td>
            <select id="sDocuments" class="fld">
<%  if (!xmlnode) { %>
                <option value=""><%=safe.html(L("lcl_xsledit_selectnode"))%></option>
<%  }
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var sql = "SELECT DISTINCT(fac_srtnotificatie_xmlnode)"
            + "  FROM fac_srtnotificatie";
    var oRs = Oracle.Execute(sql);
    while (!oRs.Eof)
    {
        var vXmlnode = oRs("fac_srtnotificatie_xmlnode").Value;
        var vFilename = Server.MapPath(custpath + "/xsl/" + oRs("fac_srtnotificatie_xmlnode").Value + ".xsl");
        if (fso.FileExists(vFilename))
        {
%>    <option value="<%=safe.htmlattr(vXmlnode)%>" <%=vXmlnode==xmlnode?"selected='1'":""%>><%=safe.html(vXmlnode)%></option>
<%      }
        oRs.MoveNext();
    }
    oRs.Close()
    if (user.has("WEB_FACTAB"))
    {
        vXmlnode = customerId;
%>    <option value="<%=safe.htmlattr(vXmlnode)%>" <%=vXmlnode==xmlnode?"selected='1'":""%>><%=safe.html(vXmlnode)%> (FACTAB only)</option>
<%
    }
%>          </select>
        </td>
    </tr>
    <% if (xmlnode) {

    var xslsheet = Server.MapPath(custpath + "\\xsl\\" + safe.filename(xmlnode) + "_concept.xsl");
    if (!fso.FileExists(xslsheet)) // dan de default beweken
    {
        var xslsheet = Server.MapPath(custpath + "\\xsl\\" + safe.filename(xmlnode) + ".xsl");
        var teaser = L("lcl_xsledit_original");
    }
    else
    {
        var teaser = L("lcl_xsledit_concept_date");
    }
    var f = fso.GetFile(xslsheet);
    teaser = teaser.format(toDateTimeString(new Date(f.DateLastModified)));
%>  <tr>
        <td colspan="2"><%=safe.html(teaser)%></td>
    </tr>
    <tr>
        <td colspan="2">
            <form name="xsledit" action="fac_edit_xsl_save.asp" id="xsledit">
                <input type='hidden' name='xmlnode' value='<%=safe.htmlattr(xmlnode)%>'>
<% if (xmlnode != customerId) { %>
                <textarea name="xsldata" id="xsldata" class="fldtxt">
<%
    var streamResult = new ActiveXObject("ADODB.Stream")
    streamResult.Type = 2; // Text
    streamResult.Open();
    streamResult.CharSet = "utf-8";
    streamResult.LoadFromFile(xslsheet);

    Response.Write(safe.textarea(streamResult.ReadText(-1).replace(/\t/g,'    '))); // Anders is de tabgrootte 8 karakters
    streamResult.Close();
    // Let op: geen newline voor de volgende < / textarea>
%>              </textarea>
<% } %>
<% IFACE.FORM_END(); %>
            </form>
        </td>
    </tr>
<form name="xslview" action="fac_preview_xsl.asp" target='RapPreview' id="xslview">
    <tr>
        <td class="label">
            <input type='hidden' name='xmlnode' value='<%=safe.htmlattr(xmlnode)%>'>
            <label class="label" for="key"><%=L("lcl_xsledit_key")%></label>
        </td>
        <td>
            <input id="key" name="key" class="required fld">
        </td>
    </tr>
    <tr>
        <td class="label">
            <label class="label" for="xkey"><%=L("lcl_xsledit_xkey")%></label>
        </td>
        <td>
            <input id="xkey" name="xkey" class="fld">
        </td>
    </tr>
    <tr>
        <td class="label"><label class="label" for="srtnoti"><%=L("fac_srtnotificatie_code")%></label></td>
        <td>
<%
      var sql = "SELECT fac_srtnotificatie_code, fac_srtnotificatie_code" + (xmlnode == customerId ? " || ' (' || fac_srtnotificatie_xmlnode || ')'":"")
              + "  FROM fac_srtnotificatie"
              + " WHERE"
              +          (xmlnode == customerId?" fac_srtnotificatie_xmlnode IS NOT NULL":" fac_srtnotificatie_xmlnode = " + safe.quoted_sql(xmlnode))
              + " ORDER BY 1";
      FCLTselector("srtnoti", sql, {  emptyOption: L("lcl_xsledit_nosrtnoti") });
    %>
        </td>
    </tr>
    <tr>
        <td class="label"><label class="label" for="mode"><%=L("lcl_xsledit_output")%></label></td>
        <td>
            <select name='mode' id='mode' class='fld'>
                <option value=''><%= L("lcl_xsledit_nomode") %></option>
                <option value='xml'><%= L("lcl_xsledit_xmlmode") %></option>
                <option value='email'><%= L("lcl_xsledit_emailmode") %></option>
                <option value='emailsend'><%= L("lcl_xsledit_emailsendmode").format(safe.html(S("puo_forcemailaddress") || user.prs_perslid_email() || "No email?")) %></option>
                <option value='summary'><%= L("lcl_xsledit_summarymode") %></option>
                <option value='kto'><%= L("lcl_xsledit_ktomode") %></option>
                <option value='share'><%= L("lcl_mld_delen") %></option>
            </select>
        </td>
    </tr>
</form>
<%  }
    BLOCK_END();
SUBFRAME_END(); %>
</body>
</html>
<% ASPPAGE_END(); %>
