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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; 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>
    <style>
      .fa-solid.k-sprite,
      .fa-solid.k-sprite::before {
        font-size: small;
        line-height: 12px;
        vertical-align: middle;
      }
    </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%>);

            // 현재 선택된 조직도
            var orgzId = $("#OrgzList option:selected").val();
            var partId = "";

            $("#OrgzList").change(function () {
                // 선택된 조직도 변경 시 새 조직도를 로딩한다.
                orgzId = $("#OrgzList option:selected").val();
                partId = "";
                $("#treeview").data("kendoTreeView").dataSource.read();
            });

            var orgzData = new kendo.data.HierarchicalDataSource({
                transport: {
                    read: {
                        url: "SelectOrgz.asmx/GetOrgzItems",
                        dataType: "jsonp",
                        // webmethod로 전송할 파라미터를 직접 정의
                        data: function () {
                            return { orgzId: orgzId, partId: partId };
                        }
                    }
                },
                // schema 정의가 없으면 자식 노드 데이터가 수신되어도 트리가 갱신되지 않는다.
                schema: {}
            });

            $("#treeview").kendoTreeView({
                checkboxes: {
                    checkChildren: true
                },
                dataSource: orgzData,
                expand: function (e) {
                    // 확장 시 자식을 가져오기 위한 ID를 partId에 저장
                    partId = this.dataItem(e.node).id;                    
                }
            });
        });

        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 GetCheckedNodes(checkedNodes, nodes) {
            for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].checked) {
                    checkedNodes.push({ id: nodes[i].id, text: nodes[i].text });
                } else if (nodes[i].hasChildren) {
                    GetCheckedNodes(checkedNodes, nodes[i].children.view());
                }
            }
        }

        function ReturnRecipients() {
            var checkedNodes = [];
            var treeview = $("#treeview").data("kendoTreeView");
            GetCheckedNodes(checkedNodes, treeview.dataSource.view());
            if (checkedNodes.length == 0) {
                Dialog.alert('<asp:Literal runat="server" Text="<%$ Resources:, Msg01%>" />');     // 수신자를 선택하십시오.
                return;
            }
            var rcpts = new Recipients();
            for (var i = 0; i < checkedNodes.length; i++) {
                var orzItem = checkedNodes[i];
                rcpts.Add(new Recipient(orzItem.text, RcptOrgz, orzItem.id), false);
            }

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

        function Close() {
            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" />
                    <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="OrgzList"><asp:Localize ID="Localize2" runat="server" meta:resourcekey="L02">조직도 목록</asp:Localize>:</label>
                <asp:DropDownList ID="OrgzList" runat="server" CssClass="form-control" AutoPostBack="false">
                </asp:DropDownList>
            </div>
            <div>
                <ul id="treeview">                   
                </ul>
            </div>
        </div>
    </form>
</body>
</html>
