<%@ Page Language="vb" Title="Rest Api Config" MasterPageFile="~/WFAdmin/ModalAdminPage.master" AutoEventWireup="false" CodeBehind="RestApiConfig.aspx.vb" Inherits="WebFoundations.WFWorkflow.Modules.UI.RestApiConfig" ValidateRequest="false" %>

<%@ Register Src="~/WFAdmin/WFWorkflow/Components/RestApiHeaders.ascx" TagPrefix="wf" TagName="RestApiHeaders" %>
<%@ Register Src="~/WFAdmin/WFWorkflow/Components/RestApiParameters.ascx" TagPrefix="wf" TagName="RestApiParameters" %>
<%@ Register Src="~/WFAdmin/WFWorkflow/Components/OutputsByStatusCode.ascx" TagPrefix="wf" TagName="OutputsByStatusCode" %>


<asp:Content ContentPlaceHolderID="TagHead" runat="server">
    <style type="text/css">
        .config-error {
            max-height: 9em;
            overflow: auto;
            background: rgba(255,0,0,.15);
            margin-bottom: .5em;
        }

        .config-error li {
            white-space: pre-wrap;
            margin: .5em;
        }

        .fields > fieldset > .rest-method, .fields > fieldset > .rest-reqtype, .fields > fieldset > .rest-addheader {
            display: grid;
            grid-template-columns: 15em;
            grid-column-gap: .5em;
        }

        .rest-addheader input, .rest-addparam input {
            max-width: 7em !important;
        }

        .rest-method select, .rest-reqtype select, .rest-addheader select {
            grid-column-start: 1;
        }

        .rest-method select + input, .rest-reqtype select + input, .rest-addheader select + input {
            grid-column-start: 2;
        }

        .rest-body textarea {
            font-family: Consolas, monospace;
            min-height: 15em;
        }

        @supports (display: grid) {
            .rest-method select + input, .rest-reqtype select + input, .rest-addheader select + input {
                height: 100%;
            }
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function () {
            var met = $(".rest-method select");
            var req = $(".rest-reqtype select");
            var resp = $(".rest-responsetype select");

            var checkReqType = function () {
                var v = req.val();
                var v2 = met.val();
                if (v === "application/x-www-form-urlencoded" || v2 === "GET" || v2 === "DELETE") {
                    a = $(".rest-body").hide();
                } else {
                    a = $(".rest-body").css("display", "");
                }
                if (v && $(".rest-reqtype").is(":visible")) {
                    b = $(".rest-reqtype select + input").hide();
                } else {
                    $(".rest-reqtype select + input").css("display", "");
                }
            };
            req.change(function () { checkReqType(); });

            var checkMethod = function () {
                var v = met.val();
                if (v === "GET" || v === "DELETE") {
                    a = $(".rest-reqtype").hide();
                } else {
                    a = $(".rest-reqtype").css("display", "");
                }
                if (v) {
                    b = $(".rest-method select + input").hide();
                } else {
                    b = $(".rest-method select + input").css("display", "");
                }
                checkReqType();
            };
            met.change(function () { checkMethod(); });

            var checkResponseType = function () {
                var v = resp.val();
                if (v) {
                    $(".attach-info").show();
                } else {
                    $(".attach-info").hide();
                }
            };
            resp.change(function () { checkResponseType(); });

            checkMethod();
            checkResponseType();

            var x = document.getElementById("<%=hdApiResult.ClientID%>");
            if (x && x.value) {
                var w = window.open("OutputXmlContent.aspx?k=" + x.value, x.value, 'resizable=1,directories=0,toolbar=0,scrollbars=0,status=0,menubar=0,location=0,top=0,left=0,width=700,height=500');
                w.focus();
            }
        });
    </script>
</asp:Content>

<asp:Content ContentPlaceHolderID="MainContent" runat="server">
    <asp:HiddenField ID="hdApiResult" runat="server" />
    <h1>
        <asp:Label Text="" ID="lblModuleName" runat="server" /></h1>
    <div class="config-error">
        <asp:ValidationSummary runat="server" />
    </div>
    <div class="fields no-columns mod-rest-config">
        <fieldset class="main">
            <legend><asp:Literal runat="server" ID="litMainInfo" Text="Informações Iniciais" /></legend>
            <label class="rext-name"><asp:Literal runat="server" ID="litName" Text="Nome" />*:
            <asp:TextBox CssClass="remain" ID="txtName" runat="server" />
                <asp:RequiredFieldValidator runat="server" ControlToValidate="txtName" Display="None" ErrorMessage="O campo Nome é obrigatório." Text="" />
            </label>
            <label class="rest-method">
                <span><asp:Literal runat="server" ID="litMethod" Text="Método" />:</span>
                <asp:DropDownList runat="server" ID="ddlMethod" />
                <asp:TextBox ID="txtMethod" runat="server" />
                <asp:CustomValidator runat="server" Display="None" ID="MethodValidation" ErrorMessage="O método de request é obrigatório." OnServerValidate="MethodValidation_ServerValidate" />
            </label>
            <label class="rest-url">
                <span>Url*:</span>
                <asp:TextBox CssClass="remain" ID="txtUrl" runat="server" />
                <asp:RequiredFieldValidator runat="server" ControlToValidate="txtUrl" Display="None" ErrorMessage="O campo Url é obrigatório." Text="" />
                <asp:RegularExpressionValidator runat="server" ControlToValidate="txtUrl" Display="None" ValidationExpression="(h|H)(t|T){2}(p|P)(s|S)?://.{3,}" ErrorMessage="O campo Url deve ser uma url absoluta, começando com http." Text="" />
            </label>
            <label class="rest-reqtype">
                <span><asp:Literal runat="server" ID="litReqType" Text="Request Content Type" />:</span>
                <asp:DropDownList runat="server" ID="ddlRequestContentType" />
                <asp:TextBox ID="txtRequestContentType" runat="server" />
                <asp:CustomValidator runat="server" Display="None" ID="ReqTypeValidation" ErrorMessage="O tipo de conteúdo do request deve ser informado quando há conteúdo." OnServerValidate="ReqTypeValidation_ServerValidate" />
            </label>
        </fieldset>
        <wf:RestApiParameters runat="server" ID="restParameters" />
        <fieldset class="config rest-body">
            <legend><asp:Literal runat="server" ID="litContent" Text="Conteúdo" /></legend>
            <label class="rest-body">
                <span></span>
                <asp:TextBox runat="server" ID="txtContent" TextMode="MultiLine" />
            </label>
        </fieldset>
        <wf:RestApiHeaders runat="server" ID="restHeaders" />
        <asp:PlaceHolder ID="phResult" runat="server">
            <fieldset class="result">
                <legend><asp:Literal runat="server" ID="litResult" Text="Resultado e Saídas" /></legend>
                <label class="rest-responsetype"><span><asp:Literal runat="server" ID="litResultType" Text="Tipo de Resultado" />
                    :</span><asp:DropDownList runat="server" ID="ddlResultType" /></label>
                <label class="rest-alias attach-info"><span><asp:Literal runat="server" ID="litAttAlias" Text="Apelido Anexo" />
                    :</span><asp:TextBox runat="server" ID="txtAttachmentAlias" CssClass="remain" /></label>
                <wf:OutputsByStatusCode runat="server" ID="serviceOutputs" AllowErrorAttachment="true" />
            </fieldset>
        </asp:PlaceHolder>
    </div>
    <section class="actions mod-rest-config">
        <asp:Button runat="server" ID="btnTest" Text="Testar" OnClick="Test_Click" />
        <asp:Button runat="server" ID="btnOK" Text="Ok" Visible="false" OnClick="Ok_Click" />
        <asp:Button runat="server" ID="btnCancel" Text="Cancelar" OnClientClick="try{window.close();}catch(ex){} return false;" CausesValidation="false" />
    </section>
</asp:Content>


