<%@ Page Language="C#" AutoEventWireup="true" CodeFile="List.aspx.cs" Inherits="Filebox_List" meta:resourcekey="Page" %>
<%@ Register src="../Common/TopNavbar.ascx" tagname="TopNavbar" tagprefix="tabs" %>
<%@ Import Namespace="TABS.MailCarrier.WebSDK.Filebox" %>

<!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>
    <style>
        .filename-wrap
        {
            word-break: break-all;
            white-space: normal;
        }
        .col-cbox {
            width: 32px;
        }
        input[type="checkbox"] {
            width: 20px;
            height: 20px;
        }
    </style>
</head>
<body>
    <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("<%=FileGridView.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("<%=FileGridView.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 OpenFolders() {
            location.href = "Folders.aspx";
        }

        function NewUpload() {
            location.href = "UploadEx.aspx?folder=<%=_folderId%>&type=<%=_type%>&page=<%=FileGridView.PageIndex%>";
            return false;
        }

        function ConfirmDelete() {
            var count = GetSelectedCount();
            if (count > 0) {
                // {0}개의 선택한 파일을 삭제 하시겠습니까?
                Dialog.confirm({
                    content: String.format('<asp:Literal runat="server" Text="<%$ Resources:, Msg01%>" />', count),
                    ok: function () {
                        __doPostBack("DeleteButton", "");
                    }
                });
            }
            else {
                // 삭제할 파일을 선택하십시오.
                Dialog.alert('<asp:Literal runat="server" Text="<%$ Resources:, Msg02%>" />');
            }
        }

        function DownloadFile(url) {
            var downLink = document.getElementById("DownloadFrame");
            try {
                downLink.href = url;
                downLink.click();
            } catch (e) {
                window.open(url);
            }
        }
    </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="uploadToolButton" runat="server" class="btn btn-primary" onclick="NewUpload()"><span class="fa-solid fa-upload"></span></button>
                    <button type="button" id="deleteToolButton" runat="server" class="btn btn-primary" onclick="ConfirmDelete()"><span class="fa-solid fa-trash-can"></span></button>
                </div>
            </div>
        </div>
       
        <div class="container-fluid" style="padding-top:40px">
            <asp:GridView ID="FileGridView" runat="server" Width="100%" CssClass="table table-hover" AutoGenerateColumns="false" 
                AllowPaging="true" PageSize="20" AllowSorting="false" GridLines="None" ShowHeaderWhenEmpty="true" ShowHeader="true" 
                DataSourceID="FileboxDataSource" EnableViewState="true" DataKeyNames="ID"
                OnPreRender="FileGridView_PreRender">
                <EmptyDataTemplate>
                    <div>
                        <asp:Localize ID="Localize1" runat="server" meta:resourcekey="L01">파일이 존재하지 않습니다.</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((Guid)Eval("ID"))%>">
                                <div class="col-xs-7">
                                    <div class="filename-wrap"><img src='../Images/Ext/<%#GetImageName((string)Eval("Name")) %>' /> <%# GetName((string)Eval("Name"))%></div>
                                    <div><%# GetDescription((string)Eval("Description"))%></div>
                                </div>
                                <div class="col-xs-5 text-right">
                                    <div><%# ((DateTime)Eval("CreateTime")).ToString("d")%></div>
                                    <div><%# GetSize((long)Eval("Size"))%></div>                                    
                                </div> 
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns> 
                <PagerSettings PageButtonCount="5" Mode="NumericFirstLast" />
                <PagerStyle CssClass="mc-gridpager"/>
            </asp:GridView>
        </div>

        <asp:ObjectDataSource ID="FileboxDataSource" runat="server" TypeName="FileboxDataSource"
            SelectMethod="GetFileboxes">
            <SelectParameters>
                <asp:QueryStringParameter Name="folder" Type="String" QueryStringField="folder" />
                <asp:Parameter Name="noAccess" Type="Boolean" />
                <asp:Parameter Name="sortExpression" Type="String" DefaultValue="Name" />
            </SelectParameters>            
        </asp:ObjectDataSource>

        <div style="display:none">
            <asp:LinkButton ID="DeleteButton" runat="server" OnClick="DeleteButton_Click" Text="" />
            <a id="DownloadFrame"></a>
        </div>
    </form>
</body>
</html>
