<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SpamList.aspx.cs" Inherits="External_SpamList" meta:resourcekey="Page" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <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>
        .table-fixed
        {
            table-layout:fixed;
        }        
        .vertical-align
        {
            display: flex;
            align-items: center;
        }
    </style>

    <script type="text/javascript">
        var paramMsgId = 0;
        var paramDuId = "<%=_duId.ToString()%>";
        var deliveredStr = "<%=GetLocalResourceObject("FlagValue3").ToString()%>";         

        $(document).ready(function () {
            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%>);

            BindEventMsgAction();
            // Grid에 UpdatePanel 사용 시: ajax 호출(refresh grid, grid paging) 후 갱신된 Grid 항목에 대한 이벤트 재연결 필요
            //Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function (sender, args) {
            //    if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {
            //        BindEventMsgAction();
            //    }
            //});

            $("#getMailOKButton").click(function () {
                $("#getMailModal").modal("hide");
                $.ajax({
                    type: "POST",
                    url: "SpamList.aspx/GetMail",
                    data: JSON.stringify({
                        id: paramMsgId,
                        duid: paramDuId,
                        forwardChecked: ($("#forwardCheck").is(":checked") == true) ? "true" : "false"
                    }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        //__doPostBack("RefreshButton", "");  Grid에 UpdatePanel 사용 시 Grid 갱신을 위해서 사용되었음
                        if (response.d) $(String.format("[data-status-msgid='{0}']", paramMsgId)).text(deliveredStr);
                    },
                    failure: function (response) {
                        Dialog.alert('<asp:Literal runat="server" Text="<%$ Resources:, Msg01%>" />');
                     }
                 });
            });

            $("#addSafeOKButton").click(function () {
                $("#addSafeModal").modal("hide");
                var category = $("input[name=addSafeFromRadio]:checked").val();
                var addrValue = (category == "mail") ? $("#addSafeMailFrom1").text() : $("#addSafeMailFrom2").text();
                $.ajax({
                    type: "POST",
                    url: "SpamList.aspx/AddSafeList",
                    data: JSON.stringify({
                        duid: paramDuId,
                        category: category,
                        addrValue: addrValue
                    }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        Dialog.alert('<asp:Literal runat="server" Text="<%$ Resources:, Msg02%>" />');
                    },
                    failure: function (response) {
                    }
                });
            });            
        });

        function BindEventMsgAction() {
            console.log("BindEventMsgAction");
            $("[data-msg-action1]").click(function (event) {
                paramMsgId = $(this).data("msg-id");
                var mailFrom = $(this).data("msg-from");
                $("#dlgMailFrom").text(mailFrom);
                $("#getMailModal").modal();
                event.stopPropagation();
            });

            $("[data-msg-action2").click(function (event) {
                paramMsgId = $(this).data("msg-id");
                var mailFrom = $(this).data("msg-from");
                $("#addSafeMailFrom1").text(mailFrom);
                var i = mailFrom.indexOf("@");
                if (i > 0) $("#addSafeMailFrom2").text(mailFrom.substr(i + 1));
                $("input:radio[name=addSafeFromRadio]:radio[value='mail']").prop('checked', true);
                $("#addSafeModal").modal();
                event.stopPropagation();
            });
        }

        function Close() {
            const params = new URLSearchParams(window.location.search);
            const paramClose = params.get("close");
            if (paramClose != null && paramClose == "back") history.back();
            window.close();
        }
    </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>

        <div class="container-fluid">
            <div>
                <asp:Label ID="ExpireLabel" runat="server" Text="※ [스팸 처리] 확인 시간이 만료되어 페이지를 확인할 수 없습니다." ForeColor="Red" meta:resourcekey="ExpireLabel" />
            </div>
            <div class="mc-header">
                <div class="row">
                    <div class="col-xs-10">
                        <h4>
                            <span class="fa-solid fa-bug-slash"></span>
                            <asp:Label ID="ToAddrLabel" runat="server" Text="Unknown User"></asp:Label> 
                            <asp:Localize ID="Localize1" runat="server" meta:resourcekey="Localize1" >스팸 저장함</asp:Localize>
                        </h4>
                    </div>
                    <div class="col-xs-2 text-right">
                        <button type="button" id="Button2" class="btn btn-primary" onclick="Close();"><span class="fa-solid fa-xmark"></span></button>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-12">
                    <asp:Localize ID="Localize3" runat="server" meta:resourcekey="Localize3">* 메일캐리어가 제공하는 스팸 필터에 의해 차단된 메일을 보여줍니다.</asp:Localize>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-12">
                    <asp:Localize ID="Localize4" runat="server" meta:resourcekey="Localize4">* 스팸 저장함에 저장된 메일을 받은 편지함으로 이동시키거나 메일 발송자를 수신 허용할 수 있습니다.</asp:Localize>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-12">
                    &nbsp;
                </div>
            </div>
                        
            <asp:GridView ID="MsgGridView" runat="server" Width="100%" CssClass="table table-hover table-fixed" AutoGenerateColumns="false" 
                AllowPaging="true" PageSize="10" AllowSorting="true" GridLines="None" ShowHeader="false"
                DataSourceID="SpamMsgDataSource" EnableViewState="true" DataKeyNames="ID">
                <EmptyDataTemplate>
                    <div>
                        <asp:Localize ID="Localize14" runat="server" meta:resourcekey="Localize14">스팸 메일이 존재하지 않습니다.</asp:Localize>
                    </div>
                </EmptyDataTemplate> 
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <div class="row">
                                <div class="col-xs-12">
                                    <div class="row">
                                        <div class="col-xs-6">
                                            <%# GetFromAddr((string)Eval("MailFrom"))%>
                                        </div>
                                        <div class="col-xs-6 text-right">
                                            <small><%# Eval("RecvTime")%></small>
                                        </div> 
                                    </div>
                                    <div class="row">
                                        <div class="col-xs-12">
                                            <%#: Eval("Subject")%>
                                        </div> 
                                    </div>
                                    <div class="row">
                                        <div class="col-xs-5">
                                            <asp:Localize ID="Localize2" runat="server" meta:resourcekey="Localize2">상태</asp:Localize>:
                                            <%# GetSpamboxFlag((int)Eval("ID"), (int)Eval("Flag")) %>
                                        </div>
                                        <div class="col-xs-7 text-right">
                                            <%# GetActionLink((int)Eval("ID"), (string)Eval("MailFrom")) %>
                                        </div> 
                                    </div>
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns> 
                <PagerSettings PageButtonCount="5" Mode="NumericFirstLast" />
                <PagerStyle CssClass="mc-gridpager"/>
            </asp:GridView>
            
            <asp:ObjectDataSource ID="SpamMsgDataSource" runat="server" TypeName="SpamMsgDataSource"
                SelectMethod="GetMessages" SelectCountMethod="TotalNumberOfProducts" EnablePaging="true">
                <SelectParameters>
                    <asp:Parameter Name="duId" Type="String" DefaultValue="" ConvertEmptyStringToNull="false" />
                </SelectParameters>
            </asp:ObjectDataSource>

            <div style="display:none">
                <asp:LinkButton ID="RefreshButton" runat="server" OnClick="RefreshButton_Click" /> 
            </div>
        </div>
    </form>

    <div class="modal fade" id="getMailModal" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header" style="">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4><asp:Localize ID="Localize5" runat="server" meta:resourcekey="Localize5">선택한 메일을 수신자에게 전달합니다.</asp:Localize></h4>
                </div>
                <div class="modal-body">
                    <form role="form">
                        <div class="row">
                            <div class="col-xs-3">
                                <asp:Localize ID="Localize6" runat="server" meta:resourcekey="Localize6">발송자</asp:Localize>
                            </div>
                            <div class="col-xs-9">
                                <span id="dlgMailFrom"></span>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12">
                                <div class="checkbox"><label><input type="checkbox" id="forwardCheck" value=""><asp:Localize ID="Localize8" runat="server" meta:resourcekey="Localize8"></asp:Localize></label></div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12">
                                <button type="button" class="btn btn-success btn-block" id="getMailOKButton"><asp:Localize ID="Localize7" runat="server" meta:resourcekey="Localize7">확인</asp:Localize></button>
                            </div>
                        </div>
                    </form>
                </div>                    
            </div>
        </div>
    </div>

    <div class="modal fade" id="addSafeModal" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header" style="">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4><asp:Localize ID="Localize9" runat="server" meta:resourcekey="Localize9">앞으로 다음 메일 주소에서 보내는 모든 메시지 허용</asp:Localize></h4>
                </div>
                <div class="modal-body">
                    <form role="form">
                        <div class="row">
                            <div class="col-xs-12">
                                <div class="radio"><label><input type="radio" name="addSafeFromRadio" value="mail" checked><asp:Localize ID="Localize10" runat="server" meta:resourcekey="Localize10">메일 주소</asp:Localize></label></div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12" style="padding-left:40px">
                                <span id="addSafeMailFrom1"></span>
                            </div>
                        </div>
                        <div class="row" style="display:none;">
                            <div class="col-xs-12">
                                <div class="radio"><label><input type="radio" name="addSafeFromRadio" value="domain"><asp:Localize ID="Localize11" runat="server" meta:resourcekey="Localize11">메일 주소 내의 도메인</asp:Localize></label></div>
                            </div>
                        </div>
                        <div class="row" style="display:none;">
                            <div class="col-xs-12" style="padding-left:40px">
                                <span id="addSafeMailFrom2"></span>
                            </div>
                        </div>
                        <div class="row">
                            &nbsp;
                        </div>
                        <div class="row">
                            <div class="col-xs-12">
                                <asp:Localize ID="Localize13" runat="server" meta:resourcekey="Localize13"></asp:Localize>
                            </div>
                        </div>
                        <div class="row">
                            &nbsp;
                        </div>
                        <div class="row">
                            <div class="col-xs-12">
                                <button type="button" class="btn btn-success btn-block" id="addSafeOKButton"><asp:Localize ID="Localize12" runat="server" meta:resourcekey="Localize12">확인</asp:Localize></button>
                            </div>
                        </div>
                    </form>
                </div>                    
            </div>
        </div>
    </div>
</body>
</html>
