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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
        <script language=javascript>
            var tree;
            Ext.onReady(function () {
                Ext.QuickTips.init();

                var store = Ext.create('Ext.data.TreeStore', {
                    fields: [
                        'children', 'expanded', 'icon', 'id', 'realId', 'leaf', 'text', 'url'
                    ],
                    proxy: {
                        type: 'ajax',
                        url: 'Handler.ashx?action=GetTree'
                    },
                    root: {
                        expanded: true,
                        text: '<%=RootFolderName %>',
                        allowDrag: false,
                        id: '<%=RootFolderIdent %>'
                    }
                });

                tree = Ext.create('Ext.tree.Panel', {
                    id: 'Tree',
                    border: true,
                    store: store,
                    useArrows: true,
                    rootVisible: false,
                    multiSelect: false,
                    singleExpand: false,
                    viewConfig: {
                        stripeRows: true
                    },
                    sortableColumns: false,
                    tbar: [
                        {
                            xtype: 'tbtext',
                            text: '<%=GetGlobalResourceObject("CMSResource", "FunctionMenuManage")%>'
                        },
                        '->',
                        {
                            text: '<%=GetGlobalResourceObject("CMSResource", "NewCategory")%>',
                            id: 'NewGroup',
                            cls: 'x-btn-default-small',
                            border: 1,
                            style: {
                                borderColor: 'gray',
                                borderStyle: 'solid'
                            },
                            icon: '/Common/DMZ/Images/16x16/folder_add.png',
                            handler: function () {
                                ShowWin("", "G");
                            }
                        },
                        {
                            text: '<%=GetGlobalResourceObject("CMSResource", "NewFunc")%>',
                            id: 'NewFunc',
                            cls: 'x-btn-default-small',
                            border: 1,
                            style: {
                                borderColor: 'gray',
                                borderStyle: 'solid'
                            },
                            icon: '/Common/DMZ/Images/16x16/form_add.png',
                            disabled: true,
                            handler: function () {
                                ShowWin("", "F");
                            }
                        }
                    ],
                    columns: [{
                        xtype: 'treecolumn', //this is so we know which column will show the tree
                        text: '<%=GetGlobalResourceObject("CMSResource", "Name")%>',
                        flex: 1,
                        sortable: false,
                        dataIndex: 'text',
                        menuDisabled: true
                    }, {
                        text: '<%=GetGlobalResourceObject("CMSResource", "Code")%>',
                        dataIndex: 'realId',
                        sortable: false,
                        menuDisabled: true,
                        renderer: function (value, metaData, record) {
                            return record.get("leaf") ? value : "";
                        }
                    }, {
                        text: '<%=GetGlobalResourceObject("CMSResource", "ProgramPath")%>',
                        flex: 3,
                        dataIndex: 'url',
                        sortable: false,
                        menuDisabled: true
                    },
                        {
                            xtype: 'actioncolumn',
                            width: 30,
                            sortable: false,
                            menuDisabled: true,
                            align: 'center',
                            items: [{
                                icon: '/Common/DMZ/Images/16x16/edit.png',
                                tooltip: '<%=GetGlobalResourceObject("CMSResource", "Eidt")%>',
                                scope: this,
                                handler: function (view, rowIndex, colIndex, item, e, record, row) {
                                    if (record.get("leaf")) {
                                        //功能
                                        ShowWin(record.get("id"), "F");
                                    } else {
                                        //分類
                                        ShowWin(record.get("id"), "G");
                                    }
                                }
                            }]
                        },
                        {
                            xtype: 'actioncolumn',
                            width: 30,
                            sortable: false,
                            menuDisabled: true,
                            align: 'center',
                            items: [{
                                icon: '/Common/DMZ/Images/16x16/delete.png',
                                tooltip: '<%=GetGlobalResourceObject("CMSResource", "Delete")%>',
                                scope: this,
                                handler: function (view, rowIndex, colIndex, item, e, record, row) {
                                    var type = record.get("leaf") ? "<%=GetGlobalResourceObject("CMSResource", "Classification")%>" : "<%=GetGlobalResourceObject("CMSResource", "Classification")%>";
                                    if (confirm("<%=GetGlobalResourceObject("CMSResource", "SureDelete")%>【" + record.get("text") + "】" + type + "？")) {
                                        var node = tree.store.getNodeById(record.get("id"));

                                        Ext.Ajax.request({
                                            url: 'Handler.ashx',
                                            params: {
                                                action: 'DelData',
                                                Ident: record.get("id")
                                            },
                                            success: function(response) {
                                                var result = Ext.decode(response.responseText);
                                                if (result.success) {
                                                    if (node) {
                                                        tree.store.load({ node: node.parentNode });
                                                    }
                                                }

                                            }
                                        });
                                    }
                                }
                            }]
                        },
                        {
                            xtype: 'actioncolumn',
                            width: 30,
                            sortable: false,
                            menuDisabled: true,
                            align: 'center',
                            items: [{
                                icon: '/Common/DMZ/Images/16x16/arrow_up.png',
                                tooltip: '<%=GetGlobalResourceObject("CMSResource", "UpLayer")%>',
                                scope: this,
                                handler: function (view, rowIndex, colIndex, item, e, record, row) {
                                    var node = tree.store.getNodeById(record.get("id"));
                                    var parentNode = node.parentNode;
                                    if (parentNode.indexOf(node) == 0) {
                                        return;
                                    }

                                    var newIndex = parentNode.indexOf(node) - 1;
                                    parentNode.insertChild(newIndex, node);

                                    Save2Server(parentNode);
                                }
                            }]
                        },
                        {
                            xtype: 'actioncolumn',
                            width: 30,
                            sortable: false,
                            menuDisabled: true,
                            align: 'center',
                            items: [{
                                icon: '/Common/DMZ/Images/16x16/arrow_down.png',
                                tooltip: '<%=GetGlobalResourceObject("CMSResource", "DownLayer")%>',
                                scope: this,
                                handler: function (view, rowIndex, colIndex, item, e, record, row) {
                                    var node = tree.store.getNodeById(record.get("id"));
                                    var parentNode = node.parentNode;
                                    if (parentNode.indexOf(node) == parentNode.childNodes.length - 1) {
                                        return;
                                    }

                                    var newIndex = parentNode.indexOf(node) + 2;
                                    parentNode.insertChild(newIndex, node);
                                    Save2Server(parentNode);
                                }
                            }]
                        }],
                    listeners: {
                        'itemclick': function (tree, record, item, index, e, eOpts) {
                            var btnFunc = Ext.getCmp("NewFunc");
                            var btnGroup = Ext.getCmp("NewGroup");
                            if (record.get("leaf")) {
                                btnFunc.setDisabled(true);
                                btnGroup.setDisabled(true);
                            }
                            else {
                                btnFunc.setDisabled(false);
                                btnGroup.setDisabled(false);
                            }

                        }
                    }
                });

                Ext.create('Ext.container.Viewport', {
                    layout: {
                        type: 'fit'
                    },
                    items: tree
                });

                tree.getSelectionModel().select(0); //Select Root

            });

            function ShowWin(ident, type) {
                var parentId = tree.getSelectionModel().getCount() == 0 ? '<%=RootFolderIdent %>' : tree.getSelectionModel().getLastSelected().get("id");
                var title;
                if (type=="G") {
                    title = ident == '' ? '<%=GetGlobalResourceObject("CMSResource", "NewCategory")%>' : '<%=GetGlobalResourceObject("CMSResource", "ModifyCategory")%>';
                }
                else {
                    title = ident == '' ? '<%=GetGlobalResourceObject("CMSResource", "NewFunc")%>' : '<%=GetGlobalResourceObject("CMSResource", "ModifyFunc")%>';
                }

                var contentForm = Ext.create('Ext.form.Panel', {
                    border: false,
                    bodyPadding: 5,
                    layout: 'anchor',
                    url: 'Handler.ashx?action=SaveData',
                    defaults: {
                        anchor: '100%',
                        labelWidth: 70
                    },
                    defaultType: 'textfield',
                    items: [{
                        xtype: 'hiddenfield',
                        name: 'parentId',
                        value: parentId
                    }, {
                        xtype: 'hiddenfield',
                        name: 'ident',
                        value: ident
                    }, {
                        xtype: 'hiddenfield',
                        name: 'isFolder',
                        value: type == "G" ? 1 : 0
                    }, {
                        fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "Name")%>',
                        name: 'name',
                        id: 'textName',
                        allowBlank: false
                    }, {
                        fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "Code")%>',
                        name: 'id',
                        hidden:type=="G",
                        allowBlank: type == "G"
                    }, {
                        fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "IconPath")%>',
                        name: 'icon',
                        allowBlank: true
                    }, {
                        fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "ProgramPath")%>',
                        name: 'url',
                        hidden: type == "G",
                        allowBlank: type == "G"
                    }],
                    buttons: [
                        {
                            text: '<%=GetGlobalResourceObject("CMSResource", "Save")%>',
                            formBind: true,
                            handler: function () {
                                var form = this.up('form').getForm();

                                if (form.isValid()) {
                                    form.submit({
                                        success: function (f, action) {
                                            var node;
                                            if (ident == "") {
                                                node = tree.store.getNodeById(parentId);
                                                if (node) {
                                                    tree.store.load({ node: node });
                                                }
                                            }
                                            else {
                                                node = tree.store.getNodeById(ident);
                                                if (node) {
                                                    tree.store.load({ node: node.parentNode });
                                                }
                                            }
                                            w.close();
                                        },
                                        failure: function (f, action) {

                                        }
                                    });
                                }
                            }
                        },
                        {
                            text: '<%=GetGlobalResourceObject("CMSResource", "Cancel")%>',
                            handler: function () {
                                this.up("window").close();
                            }
                        }
                    ]
                });

                if (ident != "") {
                    contentForm.getForm().load({
                        url: 'Handler.ashx?action=GetData&ident=' + ident
                    });
                }

                var w = Ext.create('Ext.window.Window', {
                    title: title,
                    layout: 'fit',
                    modal: true,
                    constrain: true,
                    shadow: true,
                    shadowOffset: 50,
                    width: 400,
                    items: contentForm
                }).show();
                Ext.getCmp('textName').focus(false, 200);
            }

            function Save2Server(parentNode) {
                var list = "";
                parentNode.eachChild(function (n) {
                    if (list != "") list = list + ",";
                    list = list + n.get("id");
                });


                Ext.Ajax.request({
                    url: 'Handler.ashx',
                    params: {
                        action: 'SortData',
                        list: list
                    },
                    success: function (response) {

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