<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Main.aspx.cs" Inherits="GigaTech.CMS.Web.Common.SystemAdmin.Group.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 treeGroup;
    var treePermission;
    Ext.onReady(function () {
        Ext.QuickTips.init();

        var storeGroup = Ext.create('Ext.data.TreeStore', {
            fields: [
                'children', 'expanded', 'icon', 'id', 'realId', 'leaf', 'text', 'url'
            ],
            proxy: {
                type: 'ajax',
                url: 'Handler.ashx?action=GetTreeGroup'
            },
            root: {
                expanded: true,
                text: 'ROOT',
                allowDrag: false,
                id: 'ROOT'
            }
        });

        treeGroup = Ext.create('Ext.tree.Panel', {
            id: 'TreeGroup',
            region: 'center',
            border: true,
            store: storeGroup,
            flex: 2,
            useArrows: true,
            rootVisible: false,
            multiSelect: false,
            singleExpand: false,
            viewConfig: {
                stripeRows: true
            },
            sortableColumns: false,
            tbar: [
                '->',
                {
                    text: '<%=GetGlobalResourceObject("CMSResource", "NewGroup")%>',
                    id: 'NewGroup',
                    icon: '/Common/DMZ/Images/16x16/group_add.png',
                    cls: 'x-btn-default-small',
                    border: 1,
                    style: {
                        borderColor: 'gray',
                        borderStyle: 'solid'
                    },
                    handler: function () {
                        var w = Ext.create('Ext.window.Window', {
                            title: '<%=GetGlobalResourceObject("CMSResource", "NewGroup")%>',
                            layout: 'fit',
                            modal: true,
                            constrain: true,
                            shadow: true,
                            shadowOffset: 50,
                            resizable: false,
                            width: 300,
                            items: {
                                xtype: 'form',
                                border: false,
                                bodyPadding: 5,
                                layout: 'anchor',
                                url: 'Handler.ashx?action=SaveData',
                                defaults: {
                                    anchor: '100%',
                                    labelWidth: 70
                                },
                                defaultType: 'textfield',
                                items: [{
                                    xtype: 'hiddenfield',
                                    name: 'ident',
                                    value: ''
                                }, {
                                    fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "GroupName")%>',
                                    name: 'name',
                                    id: 'textName',
                                    allowBlank: false
                                }, {
                                    fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "Code")%>',
                                    name: 'id',
                                    allowBlank: false
                                }],
                                buttons: [
                                    {
                                        text: '<%=GetGlobalResourceObject("CMSResource", "Save")%>',
                                        formBind: true,
                                        handler: function () {
                                            var form = this.up('form').getForm();
                                            if (form.isValid()) {
                                                form.submit({
                                                    success: function (f, action) {
                                                        treeGroup.store.load();
                                                        w.close();
                                                    },
                                                    failure: function (f, action) {

                                                    }
                                                });
                                            }
                                        }
                                    },
                                    {
                                        text: '<%=GetGlobalResourceObject("CMSResource", "Cancel")%>',
                                        handler: function () {
                                            this.up("window").close();
                                        }
                                    }
                                ]
                            }
                        }).show();
                        Ext.getCmp('textName').focus(false, 200);
                    }
                }
            ],
            columns: [{
                xtype: 'treecolumn', //this is so we know which column will show the tree
                text: '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%=GetGlobalResourceObject("CMSResource", "GroupName")%>',
                flex: 1,
                sortable: false,
                dataIndex: 'text',
                menuDisabled: true
            }, {
                text: '<%=GetGlobalResourceObject("CMSResource", "Code")%>',
                dataIndex: 'realId',
                sortable: false,
                menuDisabled: true
            }, {
                text: '<%=GetGlobalResourceObject("CMSResource", "GroupCount")%>',
                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) {
                            //只能修改名稱
                            Ext.MessageBox.prompt('<%=GetGlobalResourceObject("CMSResource", "ModifyGroup")%>', '<%=GetGlobalResourceObject("CMSResource", "InputGroupName")%>', function (action, text) {
                                if (action == "ok") {
                                    Ext.Ajax.request({
                                        url: 'Handler.ashx',
                                        params: {
                                            action: 'SaveData',
                                            Ident: record.get("id"),
                                            name: text
                                        },
                                        success: function (response) {
                                            var result = Ext.decode(response.responseText);
                                            if (result.success) treeGroup.store.load();
                                        }
                                    });
                                }
                            }, this, false, record.get("text"));
                        }
                    }]
                },
                {
                    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) {
                            if (confirm(String.format('<%=GetGlobalResourceObject("CMSResource", "SureDeleteGroup")%>', record.get("text")))) {
                                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) treeGroup.store.load();
                                    }
                                });
                            }
                        },
                        isDisabled: function (view, rowIndex, colIndex, item, record) {
                            return (record.get('realId') == "PowerAdmins" || record.get('realId') == "Admins" || record.get('realId') == "Users");
                        }
                    }]
                }
            ],
            listeners: {
                'select': function (tree, record, item, index, e, eOpts) {
                    //顯示權限內容
                    treePermission.store.load({ params: { groupIdent: record.get("id")} });
                }
            }
        });

        treePermission = Ext.create('Ext.tree.Panel', {
            region: 'east',
            title: '<%=GetGlobalResourceObject("CMSResource", "PremissionContent")%>',
            titleAlign: 'center',
            useArrows: true,
            split: true,
            disableSelection: true,
            flex: 1,
            rootVisible: false,
            store: Ext.create('Ext.data.TreeStore', {
                fields: [
                    'children', 'expanded', 'icon', 'id', 'realId', 'leaf', 'text', 'url', 'checked'
                ],
                proxy: {
                    type: 'ajax',
                    url: 'Handler.ashx?action=GetTree'
                },
                root: {
                    expanded: true,
                    text: '<%=RootFolderName %>',
                    allowDrag: false,
                    id: '<%=RootFolderIdent %>'
                }
            }),
            bbar: [
                '->',
                {
                    text: '<%=GetGlobalResourceObject("CMSResource", "SavePremission")%>',
                    id: 'savePermission',
                    scale: 'medium',
                    icon: '/Common/DMZ/Images/24x24/floppy.png',
                    cls: 'x-btn-default-medium',
                    border: 1,
                    style: {
                        borderColor: 'gray',
                        borderStyle: 'solid'
                    },
                    disabled: true,
                    handler: function () {
                        var list = "";
                        treePermission.getRootNode().cascadeBy(function (child) {
                            if (child.get("id") != treePermission.getRootNode().get("id") && child.get("checked")) {
                                if (list != "") list = list + ",";
                                list = list + child.get("id");
                            }
                        });
                        Ext.Ajax.request({
                            url: 'Handler.ashx',
                            params: {
                                action: 'SavePermission',
                                groupIdent: treeGroup.getSelectionModel().getLastSelected().get("id"),
                                list: list
                            },
                            success: function(response) {
                                var result = Ext.decode(response.responseText);
                                if (result.success) {
                                    alert("<%=GetGlobalResourceObject("CMSResource", "SaveSuccess")%>");
                                    Ext.getCmp("savePermission").setDisabled(true);
                                }

                            }
                        });
                    }
                }
            ],
            viewConfig: {
                deferEmptyText: false,
                emptyText: "<img src='/Common/DMZ/Images/24x24/back.png' style='vertical-align:middle;'>&nbsp;&nbsp;<font size=3><%=GetGlobalResourceObject("CMSResource", "SelectItemFunc")%></font>"
            },
            listeners: {
                checkchange: function (node, checked) {

                    Ext.getCmp("savePermission").setDisabled(false);

                    node.cascadeBy(function (child) {
                        child.set("checked", checked);
                    });
                    parentnode(node);
                }
            }
        });

        Ext.create('Ext.container.Viewport', {
            layout: {
                type: 'border'
            },
            items: [
                treeGroup, treePermission
            ]
        });

    });

    var nodep = function (node) {
        var bnode = false;
        Ext.Array.each(node.childNodes, function (v) {
            if (v.data.checked) {
                bnode = true;
                return;
            }
        });
        return bnode;
    };

    var parentnode = function (node) {
        if (node.parentNode != null) {
            if (nodep(node.parentNode)) {
                node.parentNode.set('checked', true);
            } else {
                node.parentNode.set('checked', false);
            }
            parentnode(node.parentNode);
        }
    };

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

        var w = Ext.create('Ext.window.Window', {
            title: "<%=GetGlobalResourceObject("CMSResource", "Premission")%>",
            layout: 'fit',
            modal: true,
            constrain: true,
            shadow: true,
            shadowOffset: 50,
            width: 400,
            height:300,
            buttons: ['->',
                {
                    text: '<%=GetGlobalResourceObject("CMSResource", "Save")%>'
                },
                {
                    text: '<%=GetGlobalResourceObject("CMSResource", "Cancel")%>',
                    handler: function() {
                        this.up('window').close();
                    }
                }
            ],
            items: [
                {
                    xtype: 'tree',
                    useArrows: true,
                    rootVisible: false,
                    store: store,
                    listeners: {
                        checkchange: function(node, checked) {
                            node.cascadeBy(function(child) {
                                child.set("checked", checked);
                            });
                            parentnode(node);
                        }
                    }
                }
            ]
        }).show();
    }

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