<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadRecipients.aspx.cs" Inherits="Mail_ReadRecipients" meta:resourcekey="Page" %>
<%@ Register src="../Common/TopNavbar.ascx" tagname="TopNavbar" tagprefix="tabs" %>

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../libs/fontawesome/css/all.min.css" >
    <script type="text/javascript" src="../libs/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="../libs/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
    <style>
        .topline
        {
            border-top:1px solid #ddd;
        }
        .rrdata
        {
            margin-left: -5px;
            margin-right:-5px;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function () {
            $(".rrgroup").click(function () {
                var mrgid = $(this).data("mrgid");
                OpenRrGroupData($(this), mrgid, false);
            });
            Dialog.setMessage('<asp:Literal runat="server" Text="<%$ Resources:Dialog, Title%>" />', '<asp:Literal runat="server" Text="<%$ Resources:Dialog, OK%>" />', '<asp:Literal runat="server" Text="<%$ Resources:Dialog, Cancel%>" />');
            Dialog.setUseBrowserConfirm(<%=AppSetting.UseBrowserConfirm%>);
        });

        function Refresh() {
            location.href = "ReadRecipients.aspx";
        }

        function OpenFolders() {
            location.href = "Folders2.aspx";
        }

        function OpenRrGroupData(rrgroup, mrgId, refresh) {
            var r = rrgroup.find(".rrdata");
            if (refresh && r.length > 0) {
                r.remove();
                r = rrgroup.find(".rrdata");
            }
            if (r.length == 0) {
                $.ajax({
                    type: "POST",
                    url: "ReadRecipients.aspx/GetRrGroupHtml",
                    data: JSON.stringify({ mrgId: mrgId }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        rrgroup.append(response.d);
                    },
                    failure: function (response) {
                        Debug.alert('<asp:Literal runat="server" Text="<%$ Resources:, Msg01%>" />');  // 수신 확인 데이터를 가져오는데 실패했습니다.
                    }
                });
            } else {
                r.remove();
            }
        }

        function CancelGroupSentMessage(e, id, addresses) {
            e.stopPropagation();
            var msg = '<asp:Literal runat="server" Text="<%$ Resources:, Msg02%>" />';
            Dialog.confirm({
                content: msg + addresses,
                ok: function () {
                    $.ajax({
                        type: "POST",
                        url: "ReadRecipients.aspx/CancelGroupSentMessage",
                        data: JSON.stringify({ mrgId: id }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            RefreshGridView()
                        },
                        failure: function (response) {
                            Debug.alert('<asp:Literal runat="server" Text="<%$ Resources:, Msg03%>" />'); // 발송 취소하는데 실패했습니다.
                        }
                    });
                }
            });
        }

        function CancelScheduledMailing(e, id) {
            e.stopPropagation();
            var msg = '<asp:Literal runat="server" Text="<%$ Resources:, Msg04%>" />';
            Dialog.confirm({
                content: msg,
                ok: function () {
                    $.ajax({
                        type: "POST",
                        url: "ReadRecipients.aspx/CancelScheduledMailing",
                        data: JSON.stringify({ mrgId: id }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            RefreshGridView()
                        },
                        failure: function (response) {
                            Debug.alert('<asp:Literal runat="server" Text="<%$ Resources:, Msg03%>" />'); // 발송 취소하는데 실패했습니다.
                        }
                    });
                }
            });
        }

        function CancelSentMessage(e, mrgId, mrrId, address) {
            e.stopPropagation();
            var msg = String.format('<asp:Literal runat="server" Text="<%$ Resources:, Msg05%>" />', address);
            Dialog.confirm({
                content: msg,
                ok: function () {
                    $.ajax({
                        type: "POST",
                        url: "ReadRecipients.aspx/CancelSentMessage",
                        data: JSON.stringify({ mrrId: mrrId }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            var x = $(".rrgroup[data-mrgid='" + mrgId + "']");
                            OpenRrGroupData($(".rrgroup[data-mrgid='" + mrgId + "']"), mrgId, true);
                        },
                        failure: function (response) {
                            Debug.alert('<asp:Literal runat="server" Text="<%$ Resources:, Msg03%>" />'); // 발송 취소하는데 실패했습니다.
                        }
                    });
                }
            });
        }

        function RefreshGridView() {
            __doPostBack('<%=RefreshButton.ClientID %>', '');
        }

        String.prototype.format = function () {
            var formatted = this;
            for (var i = 0; i < arguments.length; i++) {
                var regexp = new RegExp('\\{' + i + '\\}', 'gi');
                formatted = formatted.replace(regexp, arguments[i]);
            }
            return formatted;
        }
    </script>

    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <CompositeScript>
                <Scripts>
                    <asp:ScriptReference Path="~/libs/tabs/common.js" />
                    <asp:ScriptReference Path="~/libs/tabs/dialog.js" />
                </Scripts>
            </CompositeScript>
        </asp:ScriptManager>

        <tabs:TopNavbar ID="TopMenu1" runat="server" />

        <div class="container-fluid topNavbarRegion">
            <div class="row" id="toolBar" data-spy="affix">
                <div class="col-xs-12" style="padding-top:2px;padding-bottom:2px;">
                    <button type="button" id="refreshToolButton" class="btn btn-primary" onclick="Refresh()"><span class="fa-solid fa-rotate-right"></span></button>
                    <button type="button" id="folderToolButton" class="btn btn-primary" onclick="OpenFolders()"><span class="fa-solid fa-folder-open"></span></button>
                </div>
            </div>
        </div>

        <div class="container-fluid" style="padding-top:40px">
            <asp:GridView ID="RrGroupGridView" runat="server" Width="100%" CssClass="table table-hover" AutoGenerateColumns="false" 
                AllowPaging="true" PageSize="10" AllowSorting="true" GridLines="None" ShowHeaderWhenEmpty="true" ShowHeader="true" 
                DataSourceID="ReadRecipientGroupDataSource" EnableViewState="true" DataKeyNames="ID"
                OnPreRender="RrGroupGridView_PreRender"
                >
                <EmptyDataTemplate>
                    <div>
                        <asp:Localize ID="Localize1" runat="server" meta:resourcekey="Localize1">수신 확인 정보가 존재하지 않습니다.</asp:Localize>
                    </div>
                </EmptyDataTemplate> 
                <Columns>                    
                    <asp:TemplateField HeaderText="수신 확인" meta:resourcekey="GridHeader" >
                        <HeaderStyle Font-Size="Larger" />
                        <ItemTemplate>
                            <div class="rrgroup" data-mrgid='<%# Eval("ID")%>'>
                                <div class="row" >
                                    <div class="col-xs-6">
                                        <%# GetToAddr((string)Eval("FirstRecipientName"), (string)Eval("FirstRecipientAddress"), (int)Eval("RecipientsCount"))%>                          
                                    </div>
                                    <div class="col-xs-6 text-right">
                                        <small><%# GetSentTime((DateTime)Eval("SentTime"), (int)Eval("Flag"), (DateTime)Eval("ReserveTime"))%></small>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-xs-9">
                                        <%# GetSubject((string)Eval("Subject"))%>
                                    </div>
                                    <div class="col-xs-3 text-right">
                                        <%# GetCancelSentMessage((Guid)Eval("ID"), (int)Eval("Flag"))%>
                                    </div> 
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns> 
                <PagerSettings PageButtonCount="5" Mode="NumericFirstLast" />
                <PagerStyle CssClass="mc-gridpager"/>
            </asp:GridView>
        </div>

        <asp:ObjectDataSource ID="ReadRecipientGroupDataSource" runat="server" TypeName="ReadRecipientGroupDataSource"
            SelectMethod="GetReadRecipients" SelectCountMethod="TotalNumberOfProducts" EnablePaging="true">
        </asp:ObjectDataSource>

        <div style="display:none">
            <asp:Button ID="RefreshButton" runat="server" Text="" 
                onclick="RefreshButton_Click" />
        </div>
    </form>
</body>
</html>
