<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Folders2.aspx.cs" Inherits="Mail_Folders2" %>

<!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" />
    <link rel="stylesheet" href="../libs/kendoui/2023.3.1114/styles/bootstrap-3.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>
    <script src="../libs/kendoui/2023.3.1114/mjs/kendo.treeview.js" type="module"></script>    
</head>
<body>
    <style>
        .fa-solid.k-sprite,
        .fa-solid.k-sprite::before,
        .fa-regular.k-sprite,
        .fa-regular.k-sprite::before {
            font-size: small;
            line-height: 12px;
            vertical-align: middle;
        }

        .k-treeview {
            /*font-size: larger;*/
        }
    </style>
    <script id="treeview" type="text/kendo-ui-template">
        # if (item.newMsgCount >= 0 && item.totalMsgCount >= 0) { #
            # if (item.newMsgCount > 0) { #
                #: item.text # &nbsp;(<span class='badge mc-badge'>#: item.newMsgCount #</span>/#: item.totalMsgCount #)
            # } else { #
                #: item.text # &nbsp;(#: item.newMsgCount #/#: item.totalMsgCount #)
            # } #
        #} else if (item.newMsgCount >= 0) { #
            #: item.text # &nbsp;<span class='badge mc-badge'>#: item.newMsgCount #</span>
        #} else { #
            #: item.text #
        #} #
    </script>

    <script type="text/javascript">
        $(document).ready(function () {
            $("#folderView").kendoTreeView({
                dataSource: JSON.parse($("#folderViewData").val()),
                template: kendo.template($("#treeview").html()),
                select: onSelectFolderViewItem
            });

            const params = new URLSearchParams(window.location.search);
            const paramSelect = params.get("select");
            if (paramSelect != null && paramSelect != "") {
                console.log(paramSelect);
                var treeview = $("#folderView").data("kendoTreeView");
                var dataItem = treeview.dataSource.get(paramSelect);
                if (dataItem) {
                    var item = treeview.findByUid(dataItem.uid);
                    treeview.select(item);
                    var parentItem = treeview.parent(item);
                    while (parentItem.length > 0) {
                        console.log(parentItem);
                        treeview.expand(parentItem);
                        parentItem = treeview.parent(parentItem);
                    }
                }
            }

            // 선택된 노드를 다시 클릭해도 이벤트를 발생 시키는 코드
            $("#folderView").on("click", ".k-selected", function () {
                const treeview = $("#folderView").data("kendoTreeView");
                const node = $(this)[0];
                treeview.trigger("select", { node: node });
            });

        });

        function onSelectFolderViewItem(e) {
            var dataItem = this.dataItem(e.node);
            var itemAttrs = null;
            switch (dataItem.id) {
                case "<%=MailUtil.MailToApprove.ToString()%>":
                    if (dataItem.name != "") location.href = "PermitSend/ConfirmerList.aspx";
                    break;
                case "<%=MailUtil.MailToApprove_Delegate.ToString()%>":
                    location.href = "PermitSend/ConfirmerList.aspx?delegate={0}".format(dataItem.attrs["delegate"]);
                    break;
                case "<%=MailUtil.MailWaitForApproval.ToString()%>":
                    location.href = "PermitSend/ConfirmedList.aspx";
                    break;
                case "<%=MailUtil.ReadRecipients.ToString()%>":
                    location.href = "ReadRecipients.aspx";
                    break;
                case "<%=MailUtil.SpamNotification.ToString()%>":
                    location.href = "../External/SpamList.aspx?close=back";
                    break;
                default:
                    location.href = "List.aspx?folder={0}&init=true".format(dataItem.id);
                    break;
            }
        }
    </script>

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

        <div class="container-fluid">
            <div class="mc-header">
                <div class="row">
                    <div class="col-xs-10">
                        <asp:Localize ID="Localize1" runat="server" meta:resourcekey="L01">폴더 선택</asp:Localize>
                        <asp:Label ID="UsageLabel" runat="server" CssClass="small"></asp:Label>
                    </div>
                    <div class="col-xs-2 text-right">
                        <button type="button" id="closeToolButton" class="btn btn-primary" onclick="history.back();"><span class="fa-solid fa-xmark"></span></button>
                    </div>
                </div>
            </div>
            <div id="folderView"></div>
            <asp:HiddenField ID="folderViewData" runat="server" />
        </div>
    </form>
</body>
</html>
