<%@ Page Language="C#" AutoEventWireup="true" CodeFile="List.aspx.cs" Inherits="Contact_List" meta:resourcekey="Page" %>
<%@ Register src="../Common/TopNavbar.ascx" tagname="TopNavbar" tagprefix="tabs" %>
<%@ 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">
    <meta name="format-detection" content="telephone=no">
    <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>
        .col-cbox {
            width: 32px;
        }
        input[type="checkbox"] {
            width: 20px;
            height: 20px;
        }
    </style>
    <script type="text/javascript">
        $(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%>);
        });

        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 GetSelectedCount() {
            var count = 0;
            var table = $get("<%=ContactGridView.ClientID %>");
            for (var i = 0; i < table.rows.length; i++) {
                var cell = table.rows[i].cells[0];
                for (var j = 0; j < cell.childNodes.length; j++) {
                    var el = cell.childNodes[j];
                    if (el.nodeName == 'INPUT' && el.type == 'checkbox' && el.checked) {
                        count++;
                    }
                }
            }
            return count;
        }

        function ConfirmDelete() {
            var count = GetSelectedCount();
            if (count > 0) {
                var addmsg = (<%=_favorite ? "true" : "false"%>) ? '\n\n' + '<asp:Literal runat="server" Text="<%$ Resources:, Msg01%>" />' : '';
                Dialog.confirm({
                    content: String.format('<asp:Literal runat="server" Text="<%$ Resources:, Msg02%>" />', count) + addmsg,
                    ok: function () {
                        __doPostBack("DeleteButton", "");
                    }
                });
            } else {
                Dialog.alert('<asp:Literal runat="server" Text="<%$ Resources:, Msg03%>" />');     // 삭제할 연락처를 선택하십시오
            }
        }

        function CallPhone(e, number) {
            location.href = "tel:" + number;
            e.stopPropagation();
        }

        function CallSms(e, number) {
            location.href = "sms:" + number;
            e.stopPropagation();
        }

        function OpenNewMail(e, contactId) {
            location.href = "../Mail/NewQ.aspx?action=new&contact=" + contactId;
            e.stopPropagation();
        }

        function OpenNewGroupMail(e, contactId) {
            location.href = "../Mail/NewQ.aspx?action=new&contact=" + contactId;
            e.stopPropagation();
        }

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

        function NewContact() {
            location.href = "Edit.aspx?folder=<%=_folderId%>&page=<%=ContactGridView.PageIndex%>";
            return false;
        }

        function ViewContact(contactId, page) {
            location.href = "View.aspx?folder=<%=_folderId%>&page=" + page + "&contact=" + contactId;
        }

        function SearchOnEnter(event) {
            if (event.keyCode == 13) {
                __doPostBack("SearchButton", "");
                return false;
            }
            return true;
        }

        function NoAction(e) {
            console.log(e);
            e.stopPropagation();
        }

        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="folderToolButton" class="btn btn-primary" onclick="OpenFolders()"><span class="fa-solid fa-folder-open"></span></button>
                    <button type="button" id="newToolButton" runat="server" class="btn btn-primary" onclick="NewContact()"><span class="fa-solid fa-pen"></span></button>
                    <button type="button" id="deleteToolButton" runat="server" class="btn btn-primary" onclick="ConfirmDelete()"><span class="fa-solid fa-trash-can"></span></button>
                    <button type="button" id="searchCollapseButton" class="btn btn-primary" data-toggle="collapse" data-target="#searchPanel"><span class="fa-solid fa-magnifying-glass"></span></button>
                </div>
            </div>
        </div>
       
        <div class="container-fluid" style="padding-top:40px">
            <div id="searchPanel" runat="server" class="collapse">
                <div class="row">
                    <div class="col-xs-9">
                        <asp:DropDownList ID="FilterComboBox" runat="server" AutoPostBack="true" CssClass="form-control"
                            onselectedindexchanged="FilterComboBox_SelectedIndexChanged">
                            <asp:ListItem Selected="True" Value="" meta:resourcekey="FilterAll">전체</asp:ListItem>
                            <asp:ListItem Value="ㄱ">ㄱ</asp:ListItem>
                            <asp:ListItem Value="ㄴ">ㄴ</asp:ListItem>
                            <asp:ListItem Value="ㄷ">ㄷ</asp:ListItem>
                            <asp:ListItem Value="ㄹ">ㄹ</asp:ListItem>
                            <asp:ListItem Value="ㅁ">ㅁ</asp:ListItem>
                            <asp:ListItem Value="ㅂ">ㅂ</asp:ListItem>
                            <asp:ListItem Value="ㅅ">ㅅ</asp:ListItem>
                            <asp:ListItem Value="ㅇ">ㅇ</asp:ListItem>
                            <asp:ListItem Value="ㅈ">ㅈ</asp:ListItem>
                            <asp:ListItem Value="ㅊ">ㅊ</asp:ListItem>
                            <asp:ListItem Value="ㅋ">ㅋ</asp:ListItem>
                            <asp:ListItem Value="ㅌ">ㅌ</asp:ListItem>
                            <asp:ListItem Value="ㅍ">ㅍ</asp:ListItem>
                            <asp:ListItem Value="ㅎ">ㅎ</asp:ListItem>
                            <asp:ListItem Value="etc">123</asp:ListItem>
                        </asp:DropDownList>
                    </div>
                    <div class="col-xs-3">
                        <button type="button" id="searchButton" class="btn btn-default" onclick="__doPostBack('SearchButton', '')"><span class="fa-solid fa-magnifying-glass"></span></button>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-9">
                        <asp:TextBox ID="KeywordTextBox" runat="server" CssClass="form-control" AutoPostBack="false" onkeydown="return SearchOnEnter(event);" />
                    </div>
                    <div class="col-xs-3">
                        <button type="button" id="searchCancelButton" class="btn btn-default" onclick="__doPostBack('SearchCancelButton', '')"><span class="fa-solid fa-xmark"></span></button>
                    </div>
                </div>
            </div>
            
            <asp:GridView ID="ContactGridView" runat="server" Width="100%" CssClass="table table-hover" AutoGenerateColumns="false" 
                AllowPaging="true" PageSize="20" AllowSorting="true" GridLines="None" ShowHeaderWhenEmpty="true" ShowHeader="true" 
                DataSourceID="ContactDataSource" EnableViewState="true" DataKeyNames="ID"
                OnPreRender="ContactGridView_PreRender">
                <EmptyDataTemplate>
                    <div>
                        <asp:Localize ID="Localize1" runat="server" meta:resourcekey="Localize1">연락처가 없습니다.</asp:Localize>
                    </div>
                </EmptyDataTemplate> 
                <Columns>
                    <asp:TemplateField>
                        <HeaderStyle CssClass="col-cbox text-left" />                               
                        <ItemStyle CssClass="text-left" />
                        <HeaderTemplate>
                            <input id="chkAll" onclick="SelectAllCheckboxes();" runat="server" type="checkbox" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="SelectCheckBox" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderStyle Font-Size="Larger" />
                        <ItemTemplate>
                            <div class="row" onclick="<%# GetClickEventFunction((Contact)Container.DataItem)%>">
                                <div class="col-xs-7">
                                    <div><%#: Eval("DisplayName") %></div>
                                    <div class="small"><%#: GetPhoneNumber((Contact)Container.DataItem) %></div>
                                </div>
                                <div class="col-xs-5 text-right">
                                    <%# GetActionButtons((Contact)Container.DataItem)%>
                                </div> 
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns> 
                <PagerSettings PageButtonCount="5" Mode="NumericFirstLast" />
                <PagerStyle CssClass="mc-gridpager"/>
            </asp:GridView>
        </div>

        <div style="display:none">
            <asp:LinkButton ID="DeleteButton" runat="server" OnClick="DeleteButton_Click" Text="" />
            <asp:LinkButton ID="SearchButton" runat="server" OnClick="SearchButton_Click" Text="" />
            <asp:LinkButton ID="SearchCancelButton" runat="server" OnClick="SearchCancelButton_Click" Text="" />
        </div>

        <asp:HiddenField ID="FilterField" runat="server" />
        <asp:HiddenField ID="SearchField" runat="server" />	  

        <asp:ObjectDataSource ID="ContactDataSource" runat="server" TypeName="ContactDataSource"
            SelectMethod="GetContacts" SortParameterName="SortExpression">
            <SelectParameters>
                <asp:QueryStringParameter Name="folder" Type="String" QueryStringField="folder" />
                <asp:ControlParameter ControlID="FilterField" Name="filter" Type="String" DefaultValue="" ConvertEmptyStringToNull="false" PropertyName="Value" />
                        <asp:ControlParameter ControlID="SearchField" Name="searchKeyword" Type="String" DefaultValue="" ConvertEmptyStringToNull="false" PropertyName="Value" />
            </SelectParameters>
        </asp:ObjectDataSource>
        <asp:ObjectDataSource ID="FavoriteDataSource" runat="server" TypeName="FavoriteDataSource"
            SelectMethod="GetFavorites" SortParameterName="sortExpression">
            <SelectParameters>
                <asp:ControlParameter ControlID="FilterField" Name="filter" Type="String" DefaultValue="" ConvertEmptyStringToNull="false" PropertyName="Value" />
                        <asp:ControlParameter ControlID="SearchField" Name="searchKeyword" Type="String" DefaultValue="" ConvertEmptyStringToNull="false" PropertyName="Value" />
            </SelectParameters>
        </asp:ObjectDataSource>
    </form>
</body>
</html>
