<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SelectTarget.aspx.cs" Inherits="GigaTech.CMS.Web.Apps.Common.DMZ.SelectTarget" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
    <script language=javascript>
        var rootIdent = "<%=rootIdent %>";
        var totalLevel = "<%=totalLevel %>";

        Ext.onReady(function () {
            Ext.QuickTips.init();

            var tree = Ext.create('Ext.tree.Panel', {
                id: 'HouseTree',
                flex: 1,
                border: false,
                store: Ext.create('Ext.data.TreeStore', {
                    autoLoad: true,
                    fields: [
                        'children', 'realId', 'expanded', 'icon', 'mac', 'note', 'leaf', { name: 'text', sortType: 'asNatural' }, 'status', 'assigned', 'text_f', 'ver', 'level'
                    ],
                    proxy: {
                        type: 'ajax',
                        url: 'SelectTargetHandler.ashx?action=GetStructureTree'
                    },
                    root: {
                        text: '<%=GetGlobalResourceObject("CMSResource", "AllArea1")%>',
                        id: rootIdent,
                        realId: '-1',
                        checked: false
                    },
                    sorters: [{ property: 'text', direction: 'ASC' }],
                    folderSort: true
                }),
                multiSelect: false,
                singleExpand: false,

                viewConfig: {
                    stripeRows: true,
                    markDirty: false
                },
                sortableColumns: false,
                sorters: [{
                    property: 'text',
                    direction: 'ASC'
                }],

                columns: [{
                    xtype: 'treecolumn',
                    text: '',
                    flex: 1,
                    sortable: false,
                    dataIndex: 'text',
                    menuDisabled: true
                }, {
                    text: '<%=GetGlobalResourceObject("CMSResource", "Description")%>',
                    flex: 1,
                    dataIndex: 'note',
                    hidden: false,
                    sortable: false,
                    menuDisabled: true
                }],
                listeners: {
                    //'afteritemexpand': function () {
                    //    this.getStore().sort("text", "ASC");
                    //},
                    'itemappend': function (n, node, index, eOpts) {
                        //var note = getParentName(node) + " " + node.get("text");
                        var note = getParentName(node) + " " + processInt(node.get("text").split(" ")[0]) + " " + node.get("text").split(" ")[1];
                        if (totalLevel == node.getDepth()) {
                            node.set("icon", "/Common/DMZ/Images/16x16/home.png");
                        } else {
                            note = "<font color=gray>" + note + "</font>";
                        }
                        node.set("note", note);

                    }
                },
                buttons: [
                    {
                        text: '<%=GetGlobalResourceObject("CMSResource", "OK")%>',
                        id: 'SaveButton',
                        //                            disabled: true,
                        border: 1,
                        style: {
                            borderColor: 'gray',
                            borderStyle: 'solid'
                        },
                        handler: function () {
                            var tree = Ext.getCmp("HouseTree");
                            var selected = "";
                            var selectedNames = "";

                            tree.getRootNode().cascadeBy(function (n) {
                                if (n.get("checked")) {
                                    var selectedName = getParentName(n) + " " + n.get("text");
                                    var selectedID = getParentID(n) + n.get("realId") + (n.getDepth() == totalLevel ? "" : "#");

                                    if (selectedID == "-1#") {
                                        //全區
                                        selectedID = "#";
                                    }

                                    if (selected != "") selected = selected + ",";
                                    selected = selected + selectedID;

                                    if (selectedID.indexOf('#') != -1) {
                                        selectedNames = selectedNames + "【" + selectedName + "】";
                                    }
                                }

                            });

                            try {
                                WinClose(selected, selectedNames);
                            } catch (e) {

                            }
                        }
                    }, {
                        text: '<%=GetGlobalResourceObject("CMSResource", "Cancel")%>',
                        border: 1,
                        style: {
                            borderColor: 'gray',
                            borderStyle: 'solid'
                        },
                        handler: function () {
                            try {
                                WinClose("CANCEL","");
                            } catch (e) {

                            }
                        }
                    }
                ]
            });

            Ext.create('Ext.window.Window', {
                layout: 'fit',
                header:false,
                maximized: true,
                closable: false,
                border:false,
                items: [
                    tree
                ]
            }).show();


        });

        function getParentName(node) {
            var parentNode = node.parentNode;
            var names = "";
            if (parentNode) {
                if (parentNode.get("text").split(" ").length == 2)
                    names = getParentName(parentNode) + " " + (processInt(parentNode.get("text").split(" ")[0]) + " " + parentNode.get("text").split(" ")[1]);
                else
                    names = getParentName(parentNode) + " " + (processInt(parentNode.get("text").split(" ")[0]));
            }
            return names;

        }

        function processInt(str) {
            if (isNaN(parseInt(str))) {
                return str;
            }
            else {
                return parseInt(str);
            }
        }

        function getParentID(node) {
            var parentNode = node.parentNode;
            var names = "";
            if (parentNode) {
                if (parentNode.get("text").split(" ").length == 2)
                    names = getParentID(parentNode) + parentNode.get("realId");
                else
                    names = "";
            }
            return names;

        }
    </script>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
