<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SelectRcpt.aspx.cs" Inherits="Contact_SelectRcpt" meta:resourcekey="Page" %>
<%@ Import Namespace="TABS.MailCarrier.WebSDK.Contact" %>

<!DOCTYPE html>

<html>
<head 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>
    <script type="text/javascript">
        $(document).ready(function () {
        });

        function SelectAllCheckboxes() {
            var table = $get('<%=ContactGridView.ClientID %>');
            var cell = table.rows[0].cells[0];
            var status = false;
            var index = -1;

            for (var i = 0; i < cell.childNodes.length; i++) {
                var el = cell.childNodes[i];
                if (el.nodeName == 'INPUT' && el.type == 'checkbox') {
                    status = el.checked;
                    index = i;
                }
            }

            for (i = 1; i < table.rows.length; i++) {
                var cell = table.rows[i].cells[0];
                el = cell.childNodes[index];
                if (el != undefined) el.checked = status;
            }
            return false;
        }

        function GetRecipientsType() {
            recType = "To";
            var search = window.location.search.substring(1);
            var query = search.split('&');
            for (var i = 0; i < query.length; i++) {
                var keyValue = query[i].split('=');
                var key = keyValue[0];
                var value = keyValue[1];
                if (key == "RcptType") recType = value;
            }
            return recType;
        }

        function ReturnRecipients() {
            var rcpts = new Recipients();
            var table = $get("<%=ContactGridView.ClientID %>");
            for (var i = 1; i < table.rows.length; i++) {
                cell = table.rows[i].cells[0];
                var input = cell.getElementsByTagName('INPUT');
                var status = input[0].checked;
                if (status) {
                    var id = input[1].id.split('@')[1];
                    var value = input[1].value.trim();
                    var name = input[2].value;
                    var type = parseInt(input[3].value);
                    if ((type == RcptMailAddr && ValidateEmail(value)) || type == RcptMailGroup) {
                        rcpts.Add(new Recipient(name, type, value), false);
                    }
                }
            }

            var wnd = window.opener;
            window.setTimeout(function () {
                var recType = GetRecipientsType();
                var jsonParam = Recipients.ToJsonString(rcpts);
                if (recType == "to" && wnd.ReturnToRecipients != null) wnd.ReturnToRecipients(jsonParam);
                else if (recType == "cc" && wnd.ReturnCcRecipients != null) wnd.ReturnCcRecipients(jsonParam);
                else if (recType == "bcc" && wnd.ReturnBccRecipients != null) wnd.ReturnBccRecipients(jsonParam);
                self.close();
            }, 100);
        }

        function ValidateEmail(email) {
            var re = /(\x22([^\x22]*)\x22)?(\s*)[<\x28]?([-+.'!#$%&=/\w\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+@\w+([-.]\w+)*\.\w+([-.]\w+)*)[>\x29]?/gi;
            return re.test(email);
        }

        function Close() {
            window.close();
        }
    </script>

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

        <div class="container-fluid">
            <div class="row" id="toolBar" data-spy="affix">
                <div class="col-xs-12" style="padding-top:2px;padding-bottom:2px;">
                    <button type="button" id="selectToolButton" class="btn btn-primary" onclick="ReturnRecipients()"><span class="fa-solid fa-address-card"></span> <asp:Localize ID="Localize1" runat="server" meta:resourcekey="L01">수신자 선택</asp:Localize></button>
                    <span>&nbsp;</span>
                    <button type="button" id="Button2" class="btn btn-primary" onclick="Close();"><span class="fa-solid fa-xmark"></span></button>
                </div>
            </div>
        </div>

        <div class="container-fluid" style="padding-top:40px">
            <div class="form-group">
                <label for="AddrBookList"><asp:Localize ID="Localize2" runat="server" meta:resourcekey="L02">주소록 목록</asp:Localize>:</label>
                <asp:DropDownList ID="AddrBookList" runat="server" CssClass="form-control" AutoPostBack="true">
                </asp:DropDownList>
            </div>

            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:GridView ID="ContactGridView" runat="server" Width="100%" CssClass="table table-hover"
                        AllowSorting="true" AutoGenerateColumns="false" EnableViewState="true" GridLines="None"
                        OnPreRender="ContactGridView_PreRender">
                        <EmptyDataTemplate>
                            <div class="NoRecords"><asp:Localize ID="Localize3" runat="server" meta:resourcekey="L03">연락처가 없습니다.</asp:Localize></div>
                        </EmptyDataTemplate>
                        <Columns>
                            <asp:TemplateField HeaderText="Select">
                                <HeaderTemplate>
                                    <input id="checkAll" type="checkbox" onclick="SelectAllCheckboxes()" />
                                </HeaderTemplate>
                                <ItemStyle Width="25px" HorizontalAlign="Center" />  
                                <ItemTemplate>
                                    <input id="check@<%# Eval("ID") %>" type="checkbox" />
                                    <input id="hidden1@<%# Eval("ID") %>" type="hidden" value='<%# GetContactValue((Contact.ContactType)Eval("Type"), (string)Eval("Email"), (Guid)Eval("ID"))%>' />
                                    <input id="hidden2@<%# Eval("ID") %>" type="hidden" value='<%# Server.HtmlEncode((string)Eval("DisplayName"))%>' />
                                    <input id="hidden3@<%# Eval("ID") %>" type="hidden" value='<%# ((Contact.ContactType)Eval("Type") == Contact.ContactType.Contact) ? 1 : 2 %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="메일주소" meta:resourcekey="Header">
                                <ItemTemplate>
                                    <span>
                                        <%# GetContact((Contact.ContactType)Eval("Type"), (string)Eval("DisplayName"), (string)Eval("Email"))%>
                                    </span>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>                  
                    </asp:GridView> 
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="AddrBookList" EventName="SelectedIndexChanged" />
                </Triggers>
            </asp:UpdatePanel>
        </div>

        <asp:ObjectDataSource ID="FavoriteDataSource" runat="server" TypeName="FavoriteDataSource"
            SelectMethod="GetFavorites" SortParameterName="sortExpression">
            <SelectParameters>
                <asp:Parameter Name="filter" Type="String" DefaultValue="all" />
                <asp:Parameter Name="searchKeyword" Type="String" DefaultValue="" />
            </SelectParameters>
        </asp:ObjectDataSource>
                 
        <asp:ObjectDataSource ID="ContactDataSource" runat="server" TypeName="ContactDataSource"
            SelectMethod="GetContacts" SortParameterName="sortExpression">
            <SelectParameters>
                <asp:Parameter Name="folder" Type="String" />
                <asp:Parameter Name="filter" Type="String" DefaultValue="all" />
                <asp:Parameter Name="searchKeyword" Type="String" DefaultValue="" />
                <asp:Parameter Name="sortExpression" Type="String" />
            </SelectParameters>
        </asp:ObjectDataSource>
    </form>
</body>
</html>
