<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Main.aspx.cs" Inherits="GigaTech.CMS.Web.Common.SystemAdmin.Account.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 grid;
    
    function getGridSelectedIdent() {
        var list = "";
        Ext.Array.each(grid.getSelectionModel().getSelection(), function(v) {
            if (list != "") list = list + ",";
            list = list + v.get("ident");
        });
        return list;
    }
    
    Ext.onReady(function () {
        Ext.QuickTips.init();

        var gridStore = Ext.create('Ext.data.Store', {
            pageSize: 20,
            remoteSort: true,
            autoLoad: true,
            fields: ['ident', 'name', 'id', 'group', 'status', 'last_datetime'],
            proxy: {
                type: 'ajax',
                url: 'Handler.ashx?action=GetGridData',
                reader: {
                    type: 'json',
                    root: 'items',
                    totalProperty: 'totalCount'
                },
                simpleSortMode: true,
                extraParams: {
                    isSearch: '0',
                    name: '',
                    id: '',
                    group: '',
                    status: ''
                }
            },
            sorters: [{
                property: "name",
                direction: "ASC"
            }]
        });
        
        grid = Ext.create('Ext.grid.Panel', {
            tbar: [{
                text: '<%=GetGlobalResourceObject("CMSResource", "ClearSearchResult")%>',
                id: 'clearButton',
                cls: 'x-btn-default-small',
                border: 1,
                style: {
                    borderColor: 'gray',
                    borderStyle: 'solid'
                },
                icon: '/Common/DMZ/Images/16x16/broom.png',
                hidden: true,
                handler: function() {
                    grid.store.getProxy().setExtraParam('isSearch', "0");
                    grid.store.load({
                        callback: function(records, options, success) {
                            if (success) {
                                Ext.getCmp("clearButton").hide();
                                Ext.getCmp("SearchForm").getForm().reset();
                            }
                        }
                    });
                }
            }, '->',
                {
                    text: '<%=GetGlobalResourceObject("CMSResource", "NewID")%>',
                    id: 'NewAccount',
                    cls: 'x-btn-default-small',
                    border: 1,
                    style: {
                        borderColor: 'gray',
                        borderStyle: 'solid'
                    },
                    icon: '/Common/DMZ/Images/16x16/user_add.png',
                    handler: function() {
                        ShowWin("");
                    }
                },
                {
                    text: '<%=GetGlobalResourceObject("CMSResource", "Delete")%>',
                    id: 'DeleteButton',
                    cls: 'x-btn-default-small',
                    border: 1,
                    style: {
                        borderColor: 'gray',
                        borderStyle: 'solid'
                    },
                    disabled: true,
                    icon: '/Common/DMZ/Images/16x16/delete.png',
                    handler: function() {
                        if (confirm("<%=GetGlobalResourceObject("CMSResource", "SureDeleteAccount")%>")) {
                            Ext.Ajax.request({
                                url: 'Handler.ashx',
                                params: {
                                    action: 'DelData',
                                    idents: getGridSelectedIdent()
                                },
                                success: function(response) {
                                    var result = Ext.decode(response.responseText);
                                    if(result.success) grid.store.load();
                                }
                            });

                        }

                    }
                },
                {
                    text: '<%=GetGlobalResourceObject("CMSResource", "Enable")%>',
                    id: 'EnableButton',
                    cls: 'x-btn-default-small',
                    border: 1,
                    style: {
                        borderColor: 'gray',
                        borderStyle: 'solid'
                    },
                    disabled: true,
                    icon: '/Common/DMZ/Images/16x16/enabled.png',
                    handler: function() {
                        Ext.Ajax.request({
                            url: 'Handler.ashx',
                            params: {
                                action: 'SetStatus',
                                idents: getGridSelectedIdent(),
                                status: '1'
                            },
                            success: function(response) {
                                var result = Ext.decode(response.responseText);
                                    if(result.success) grid.store.load();
                            }
                        });
                    }
                },
                {
                    text: '<%=GetGlobalResourceObject("CMSResource", "Disable")%>',
                    id: 'DisableButton',
                    cls: 'x-btn-default-small',
                    border: 1,
                    style: {
                        borderColor: 'gray',
                        borderStyle: 'solid'
                    },
                    disabled: true,
                    icon: '/Common/DMZ/Images/16x16/disabled.png',
                    handler: function() {
                        Ext.Ajax.request({
                            url: 'Handler.ashx',
                            params: {
                                action: 'SetStatus',
                                idents: getGridSelectedIdent(),
                                status: '0'
                            },
                            success: function(response) {
                                var result = Ext.decode(response.responseText);
                                    if(result.success) grid.store.load();
                            }
                        });
                    }
                }
            ],
            region: 'center',
            store: gridStore,
            columns: [
                { text: '',
                    menuDisabled: true,
                    sortable: false,
                    width:25,
                    renderer: function(value) {
                       return "<img src='/Common/DMZ/Images/16x16/user.png'/>";

                    } },
                { text: '<%=GetGlobalResourceObject("CMSResource", "Name")%>', dataIndex: 'name', flex: 1 },
                { text: '<%=GetGlobalResourceObject("CMSResource", "ID")%>', dataIndex: 'id' },
                { text: '<%=GetGlobalResourceObject("CMSResource", "Group")%>', dataIndex: 'group', flex: 1 },
                {
                    text: '<%=GetGlobalResourceObject("CMSResource", "Status")%>',
                    dataIndex: 'status',
                    width: 35,
                    align: 'center',
                    renderer: function(value) {
                        if (value == "1")
                            return "<img src='/Common/DMZ/Images/16x16/enabled.png'/>";
                        else
                            return "<img src='/Common/DMZ/Images/16x16/disabled.png'/>";

                    }
                },
                { text: '<%=GetGlobalResourceObject("CMSResource", "LastLogin")%>', dataIndex: 'last_datetime', flex: 1 },
                {
                    xtype: 'actioncolumn',
                    hideable: false,
                    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) {
                            ShowWin(record.get("ident"));
                        }
                    }]
                },
                {
                    xtype: 'actioncolumn',
                    hideable: false,
                    width: 30,
                    sortable: false,
                    menuDisabled: true,
                    align: 'center',
                    items: [{
                        icon: '/Common/DMZ/Images/16x16/pwd.png',
                        tooltip: '<%=GetGlobalResourceObject("CMSResource", "ModifyPWD")%>',
                        scope: this,
                        handler: function(view, rowIndex, colIndex, item, e, record, row) {
                            var w=Ext.create('Ext.window.Window', {
                                title: '<%=GetGlobalResourceObject("CMSResource", "ModifyPWD")%>',
                                layout: 'fit',
                                modal: true,
                                constrain: true,
                                 shadow: true,
            shadowOffset: 50,
                                resizable: false,
                                width: 300,
                                items: {
                                    xtype: 'form',
                                    border: false,
                                    url: 'Handler.ashx',
                                    bodyPadding: 5,
                                    layout: 'anchor',
                                    defaults: {
                                        anchor: '100%',
                                        labelWidth: 70
                                    },
                                    defaultType: 'textfield',
                                    items: [{
                                        fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "NewPWD")%>',
                                        name: 'pwd',
                                        allowBlank: false,
                                         vtype: 'alphanum'
                                    }, {
                                        xtype: 'hiddenfield',
                                        name: 'action',
                                        value: 'SetPWD'
                                    }, {
                                        xtype: 'hiddenfield',
                                        name: 'ident',
                                        value: record.get("ident")
                                    }
                                    ],
                                    buttons: [
                                        {
                                            text: '<%=GetGlobalResourceObject("CMSResource", "ConfirmChange")%>',
                                            formBind: true,
                                            handler: function() {
                                                var form = this.up('form').getForm();
                                                if (form.isValid()) {
                                                    form.submit({
                                                        success: function(f, action) {
                                                            w.close();
                                                            alert("<%=GetGlobalResourceObject("CMSResource", "ModifySuccess")%>");
                                                        },
                                                        failure: function(f, action) {
                                                            var msg = "<%=GetGlobalResourceObject("CMSResource", "ModifyFail")%>";
                                                            if(action.result.msg!="") msg = action.result.msg;
                                                            alert(msg);
                                                        }
                                                    });
                                                }
                                            }
                                        },
                                        {
                                            text: '<%=GetGlobalResourceObject("CMSResource", "Cancel")%>',
                                            handler: function() {
                                                this.up("window").close();
                                            }
                                        }
                                    ]
                                }
                            }).show();

                        }
                    }]
                }
            ],
            selModel: Ext.create('Ext.selection.CheckboxModel'),
            dockedItems: [{
                xtype: 'pagingtoolbar',
                store: gridStore,
                dock: 'bottom',
                displayInfo: true
            }],
            listeners: {
                'selectionchange': function(e, selected, eOpts) {
                    if (selected.length == 0) {
                        Ext.getCmp("DeleteButton").setDisabled(true);
                        Ext.getCmp("EnableButton").setDisabled(true);
                        Ext.getCmp("DisableButton").setDisabled(true);
                    } else {
                        Ext.getCmp("DeleteButton").setDisabled(false);
                        Ext.getCmp("EnableButton").setDisabled(false);
                        Ext.getCmp("DisableButton").setDisabled(false);
                    }
                }
            }
        });

        Ext.create('Ext.container.Viewport', {
            layout: {
                type: 'border'
            },
            items: [
                {
                    xtype: 'panel',
                    region: 'west',
                    title: '<%=GetGlobalResourceObject("CMSResource", "Query")%>',
                    width: 200,
                    layout: 'fit',
                    split: true,
                    collapsible: true,
                    collapsed: true,
                    items: [
                        {
                            xtype: 'form',
                            id: 'SearchForm',
                            border: false,
                            bodyPadding: 5,
                            layout: 'anchor',
                            defaults: {
                                anchor: '100%',
                                labelWidth: 70
                            },
                            defaultType: 'textfield',
                            items: [{
                                fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "Name")%>',
                                name: 'name'
                            }, {
                                fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "ID")%>',
                                name: 'id'
                            }, {
                                fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "Group")%>',
                                name: 'group',
                                xtype: 'combo',
                                store: Ext.create('Ext.data.Store', {
                                    fields: ['abbr', 'name'],
                                    data: <%=ComboGroups %>
                                }),
                                editable: false,
                                queryMode: 'local',
                                displayField: 'name',
                                valueField: 'abbr',
                                value: "-1"
                            }, {
                                fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "Status")%>',
                                name: 'status',
                                xtype: 'combo',
                                store: Ext.create('Ext.data.Store', {
                                    fields: ['abbr', 'name'],
                                    data: [
                                        { "abbr": "-1", "name": "<%=GetGlobalResourceObject("CMSResource", "All")%>" },
                                        { "abbr": "1", "name": "<%=GetGlobalResourceObject("CMSResource", "Enable")%>" },
                                        { "abbr": "0", "name": "<%=GetGlobalResourceObject("CMSResource", "Disable")%>" }
                                    ]
                                }),
                                editable: false,
                                queryMode: 'local',
                                displayField: 'name',
                                valueField: 'abbr',
                                value: "-1"
                            }, {
                                xtype: 'button',
                                text: '<%=GetGlobalResourceObject("CMSResource", "StartSearch")%>',
                                scale:'medium',
                                formBind: true,
                                handler: function () {
                                    var name=this.up("form").getForm().findField("name").getValue();
                                    var id=this.up("form").getForm().findField("id").getValue();
                                    var group=this.up("form").getForm().findField("group").getValue();
                                    var status=this.up("form").getForm().findField("status").getValue();


                                    grid.store.getProxy().setExtraParam('isSearch', "1");
                                    grid.store.getProxy().setExtraParam('name', name);
                                    grid.store.getProxy().setExtraParam('id', id);
                                    grid.store.getProxy().setExtraParam('group', group);
                                    grid.store.getProxy().setExtraParam('status', status);
                                    grid.store.load({
                                        callback: function(records, options, success) {
                                            if(success) {
                                                Ext.getCmp("clearButton").show();
                                            }
                                        }
                                    });
                                }
                            }]
                        }
                    ]
                }, grid]
        });
    });

    function ShowWin(ident) {
        var title = ident == '' ? '<%=GetGlobalResourceObject("CMSResource", "NewID")%>' : '<%=GetGlobalResourceObject("CMSResource", "ModifyID")%>';

        var contentForm = Ext.create('Ext.form.Panel', {
            border: false,
            bodyPadding: 5,
            layout: 'anchor',
            url: 'Handler.ashx?action=SaveData',
            defaults: {
                anchor: '100%',
                labelWidth: 60
            },
            defaultType: 'textfield',
            items: [{
                xtype: 'hiddenfield',
                name: 'ident',
                value: ident
            }, {
                fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "Name")%>',
                name: 'name',
                id: 'accountName',
                allowBlank: false
            }, {
                fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "ID")%>',
                name: 'id',
                allowBlank: false,
                 vtype: 'alphanum'
            }, {
                fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "PWD")%>',
                name: 'pwd',
                allowBlank: ident != "",
                hidden: ident != "",
                 vtype: 'alphanum'
            }, {
                xtype: 'checkboxgroup',
                name: 'groups',
                fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "Group")%>',
                allowBlank: false,
                columns: 3,
                vertical: true,
                items: <%=Groups %>
            }, {
                fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "Status")%>',
                name: 'status',
                xtype: 'combo',
                store: Ext.create('Ext.data.Store', {
                    fields: ['abbr', 'name'],
                    data: [
                        { "abbr": "1", "name": "<%=GetGlobalResourceObject("CMSResource", "Enable")%>" },
                        { "abbr": "0", "name": "<%=GetGlobalResourceObject("CMSResource", "Disable")%>" }
                    ]
                }),
                editable: false,
                queryMode: 'local',
                displayField: 'name',
                valueField: 'abbr',
                value: "1"
            }],
            buttons: [
                {
                    text: '<%=GetGlobalResourceObject("CMSResource", "Save")%>',
                    formBind: true,
                    handler: function() {
                        var form = this.up('form').getForm();
                        if (form.isValid()) {
                            form.submit({
                                success: function(f, action) {
                                    grid.store.load();
                                    w.close();
                                },
                                failure: function(f, action) {
                                    var msg = "<%=GetGlobalResourceObject("CMSResource", "SaveFail")%>";
                                    if (action.result.msg != "") msg = action.result.msg;
                                    alert(msg);
                                }
                            });
                        }
                    }
                },
                {
                    text: '<%=GetGlobalResourceObject("CMSResource", "Cancel")%>',
                    handler: function() {
                        this.up("window").close();
                    }
                }
            ]
        });

        if (ident != "") {
            contentForm.getForm().load({
                url: 'Handler.ashx?action=GetData&ident=' + ident,
                success: function(form, action) {
                    var val = action.result.data.groups;
                    if (val.split) {
                        val = val.split(',');
                    }
                    var group = form.findField('groups');
                    group.reset();
                    for (var i = 0; i < val.length; i++) {
                        group.items.each(function(c) {
                            if (c.inputValue == val[i]) {
                                c.setValue(true);
                            }
                        });
                    }
                }
            });
        }

        var w = Ext.create('Ext.window.Window', {
            title: title,
            layout: 'fit',
            modal: true,
            constrain: true,
             shadow: true,
            shadowOffset: 50,
            width: 300,
            items: contentForm
        }).show();
        Ext.getCmp('accountName').focus(false, 200);
    }
</script>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
