<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SystemTextEdit.aspx.cs" Inherits="eDocument.Server.Administrator.Resources.SystemTextEdit" MasterPageFile="~/Site.Master" ValidateRequest="false" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">
    <style type="text/css">
        .Hide
        {
            display: none;
        }
    </style>
    <div class="skjema-admin__page-pre-title">
        Systemadministrasjon
    </div>
    
    <h1 class="skjema-admin__page-title mt-0" >
        <asp:label id="lblHeader" runat="server"></asp:label>
    </h1>
    
    <asp:label id="lblMessage" runat="server" ForeColor="Red"></asp:label>
    
    <div id="moveToTableHeader" class="d-flex">
        <div class="d-flex mr-2">
            <div class="budicon budicon-world-connection mr-2"></div>
            <span>Språk:</span>
        </div>

        <asp:DropDownList ID="ddlCultures"
                          runat="server"
                          AutoPostBack="True"
                          OnSelectedIndexChanged="ddlCultures_OnSelectedIndexChanged"
                          CssClass="ddlCultures"
        >
        </asp:DropDownList>
    </div>

    <div>
        <asp:GridView 
            ID="gvResourceKeys"
            runat="server" 
            AutoGenerateColumns="False"
            CssClass="skjema-admin__table skjema-admin__table--first-column-min-width skjema-admin__table--3-4-50percent skjema-admin__table--last-td-no-padding-left skjema-admin__table--hide-first-td w-100"
            Border="0">
            <Columns>
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <asp:Label Text='<%# Eval("ResourceKeyId") %>' ID="ResourceKeyId" CssClass="Hide" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Panel ID="key" Title='<%# Eval("Key") %>' runat="server">
                            <svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" viewBox="0 0 19 19">
                                <g fill="none" fill-rule="evenodd">
                                    <g>
                                        <g transform="translate(-381 -1069) translate(381 1069)">
                                            <circle cx="9.5" cy="9.5" r="9.5" fill="#494949"/>
                                            <path fill="#FFF" fill-rule="nonzero" d="M9.496 6.123c.357 0 .644-.102.86-.308.218-.205.327-.468.327-.79 0-.318-.108-.58-.322-.784-.215-.205-.503-.308-.865-.308-.366 0-.655.103-.868.308-.212.205-.318.466-.318.783 0 .323.108.586.326.791.217.206.504.308.86.308zM10.558 15V7.075H8.434V15h2.124z"/>
                                        </g>
                                    </g>
                                </g>
                            </svg>
                        </asp:Panel>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Standardverdi (norsk bokmål)">
                    <ItemTemplate>
                        <div class="skjema-admin__table__input">
                            <asp:Label Text='<%# Eval("BaseValue") %>' ID="BaseValue" runat="server"></asp:Label>
                        </div>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="txtEditBaseValue" Text='<%# Eval("CultureValue") %>' CssClass="Update skjema-admin__table__input" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    
    <div id="divNewText" class="mb-2 mt-4" Visible="false" runat="server">
        <span class="skjema-admin__icon__plus skjema-admin__icon__big-and-bold"></span>
        
        <asp:LinkButton
            Class="skjema-admin__plus-anchor"
            ID="btnNewText"
            runat="server"
            Text="Legg inn ny tekst"
            CausesValidation="false"
            OnClick="btnOpenModalNewText_Click">
        </asp:LinkButton>
    </div>
    <asp:Panel ID="pnlNewText" Visible="False" runat="server" CssClass="skjema-admin__modal" data-jscomponent="modal">
        <div class="skjema-admin__modal__window">

            <button class="skjema-admin__modal__close" type="button" data-jscomponent="modalClose"></button>

            <asp:Label CssClass="skjema-admin__modal__title" Text="Legg til ny tekst" runat="server" />
            
            <div>
                <asp:Label ID="lblNewTextInfo" ForeColor="Red" runat="server" />
            </div>
            <div class="skjema-admin__modal__input-container">
                <div>
                    <asp:Label CssClass="skjema-admin__modal__label" ID="lblResourceKey" Text="Nøkkel" runat="server" />
                    <asp:TextBox id="txtResourceKey" class="" runat="server" autocomplete="off"></asp:TextBox>
                    <asp:RequiredFieldValidator ForeColor="Red" ID="RequiredfieldvalidatorKey" runat="server" Display="Dynamic" ControlToValidate="txtResourceKey" ErrorMessage="Skriv inn nøkkel." />
                </div>
                <div>
                    <asp:Label ID="lblResourceValue" CssClass="skjema-admin__modal__label" Text="Ny tekst" runat="server" />
                    <asp:TextBox  ID="txtResourceValue" TextMode="MultiLine" Height="50px" Width="300px" class="" runat="server" autocomplete="off"></asp:TextBox>
                    <asp:RequiredFieldValidator ForeColor="Red" ID="RequiredfieldvalidatorValue" runat="server" Display="Dynamic" ControlToValidate="txtResourceValue" ErrorMessage="Skriv inn teksten." />
                </div>
            </div>
            <div>
                <asp:Button CssClass="skjema-admin__button skjema-admin__button--primary" runat="server" id="btnSetNewText" OnClick="btnSetNewText_OnClick" Text="Lagre"/>
            </div>
        </div>
    </asp:Panel>
    <div>
        <script type="text/javascript">
            $(document).ready(function () {
                $('.Update').on('change', function () {
                    var editBaseValue = $(this).closest('tr').find('[id*=txtEditBaseValue]').val();
                    //var key = $(this).closest('tr').find('[id*=key]').attr("title");
                    var keyId = $(this).closest('tr').find('[id*=ResourceKeyId]').html();
                    var cultureId = $('.ddlCultures').val();

                    //TODO: test med HTML-tags, spesialtegn, mm

                    var editBaseValueEscaped = editBaseValue.escapeSpecialChars();

                    var jsonValues = '{'
                        + 'editBaseValue:"' + editBaseValueEscaped + '",'
                        + 'keyId:"' + keyId + '",'
                        + 'cultureId:"' + cultureId + '"'
                        + '}';

                    $.ajax({
                        type: "POST",
                        url: "SystemTextEdit.aspx/SetValue",
                        data: jsonValues,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (r) {
                            //alert(r.responseText + "Success. TODO: markere at det er vellykket??");
                        },
                        error: function (r) {
                            alert("En feil oppstod, feilmelding: " + r.responseText);
                        }
                    });
                });

                $('.skjema-admin__table__input').focus(function () {
                    $(this).parent().parent().addClass("skjema-admin__table__active-row");
                });

                $('.skjema-admin__table__input').focusout(function () {
                    $(this).parent().parent().removeClass("skjema-admin__table__active-row");
                });

                $(".skjema-admin__table th:last-child").html($("#moveToTableHeader"));
            });

            String.prototype.escapeSpecialChars = function () {
                return this.replace(/[\\]/g, '\\\\')
                    .replace(/[\"]/g, '\\\"')
                    .replace(/[\/]/g, '\\/')
                    .replace(/[\b]/g, '\\b')
                    .replace(/[\f]/g, '\\f')
                    .replace(/[\n]/g, '\\n')
                    .replace(/[\r]/g, '\\r')
                    .replace(/[\t]/g, '\\t');
            };
        </script>
    </div>
</asp:Content>
