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

<!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>
            <script src="/Scripts/jquery-1.6.4.min.js"></script>
</head>
        <script language=javascript>
            var scan = false;
            var viewType = 2;


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

                var store = Ext.create('Ext.data.TreeStore', {
                    folderSort: true,
                    fields: [
                        'children', 'expanded', 'icon', 'mac', 'nick', 'leaf', 'text', 'status', 'assigned', 'ver', 'ip', 'code', 'device_id', 'children_count', 'is3rd'
                    ],
                    proxy: {
                        type: 'ajax',
                        url: 'Handler.ashx?action=GetTree'
                    },
                    root: {
                        expanded: true,
                        text: '<%=GetGlobalResourceObject("CMSResource", "AllDevice")%>',
                        allowDrag: false,
                        id: '-1'
                    },
                    listeners: {
                        beforeload: function (store, operation, eOpts) {
                            store.proxy.extraParams = { viewType: viewType };
                        }
                    },
                    sorters: [{ property: 'text', direction: 'ASC' }]
                });

                
                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", "DeviceList")%>】'
                        },
                        '->',
                        {
                            xtype: 'combo',
                            fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "DisplayMode")%>',
                            labelWidth: 55,
                            width:140,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['abbr', 'name'],
                                data: [
                                    { "abbr": "1", "name": "<%=GetGlobalResourceObject("CMSResource", "ByStructure")%>" },
                                    { "abbr": "2", "name": "<%=GetGlobalResourceObject("CMSResource", "ByType")%>" }
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'name',
                            valueField: 'abbr',
                            editable: false,
                            value: "2",
                            hidden:'<%=TargetSystem%>'=='1',
                            listeners: {
                                change: function (c, newValue, oldValue, eOpts) {
                                    viewType = newValue;

                                    tree.store.load();
                                }
                            }
                        },'-',
                        {
                            text: '<%=GetGlobalResourceObject("CMSResource", "ScanDevice")%>',
                            id: 'NewGroup',
                            cls: 'x-btn-default-small',
                            border: 1,
                            style: {
                                borderColor: 'gray',
                                borderStyle: 'solid'
                            },
                            icon: '/Common/DMZ/Images/16x16/find.png',
                            handler: function () {
                                
                                Ext.MessageBox.show({
                                    msg: '<%=GetGlobalResourceObject("CMSResource", "ScanningDevice")%>',
                                    progressText: '<%=GetGlobalResourceObject("CMSResource", "Scanning")%>',
                                    width: 300,
                                    wait: true,
                                    waitConfig: { interval: 200 }
                                });

                                setTimeout(function () {
                                    try {
                                        scan = true;
                                        Ext.MessageBox.hide();
                                    } catch (e) {

                                    }
                                    
                                }, 5000);
                                Ext.Ajax.request({
                                    url: 'Handler.ashx?action=ScanDevice',
                                    success: function (response) {
                                        var result = Ext.decode(response.responseText);
                                        if (result.success) {
                                            scan = true;
                                         }
                                        else {
                                            scan = false;
                                            Ext.MessageBox.hide();
                                        }
                                    },
                                    failure: function ( result, request ) {
                                        scan = false;
                                        Ext.MessageBox.hide();
                                    }
                                });
                            }
                        }
                    ],
                    columns: [{
                        xtype: 'treecolumn', //this is so we know which column will show the tree
                        text: '<%=GetGlobalResourceObject("CMSResource", "DeviceType")%>',
                        flex: 1,
                        sortable: false,
                        dataIndex: 'text',
                        menuDisabled: false,
                        renderer: function (value, metaData, record) {
                            if (record.get("children_count") != "0" && record.get("children_count") != "")
                                return value + "<font color=blue>&nbsp;(" + record.get("children_count") + ")</font>";
                            else
                            {
                                return value.split("-")[0] == record.get("nick") || record.isRoot() ? value : value + "&nbsp;[" + record.get("nick") + "]";
                            }
                                
                        }
                    }, {
                        text: 'MAC',
                        width: 120,
                        dataIndex: 'mac',
                        sortable: false,
                        menuDisabled: false
                    }, {
                        text: 'IP',
                        width: 120,
                        dataIndex: 'ip',
                        sortable: false,
                        //hidden: true,
                        menuDisabled: false
                    }, {
                        text: '<%=GetGlobalResourceObject("CMSResource", "DeviceCode")%>',
                        width: 60,
                        dataIndex: 'code',
                        sortable: false,
                        hidden:true,
                        menuDisabled: false
                    }, {
                        text: '<%=GetGlobalResourceObject("CMSResource", "DeviceID")%>',
                        width: 90,
                        dataIndex: 'device_id',
                        sortable: false,
                        menuDisabled: false
                    }, {
                        text: '<%=GetGlobalResourceObject("CMSResource", "FWVersion")%>',
                        align:'center',
                        width: 60,
                        dataIndex: 'ver',
                        sortable: false,
                        hidden: true,
                        hideable: false,
                        menuDisabled: false
                    }, {
                                text:'<%=GetGlobalResourceObject("CMSResource", "Nick")%>',
                                dataIndex:'nick',
                                renderer: function (value, id, record) {
                                    return "<font color=blue>" + value + "</font>";
                                }
                    },
                           {
                                align: 'center',
                                renderer: function (value, id, record) {
                                    return (value=="" || value==null) ? "" : "<input type=button value='<%=GetGlobalResourceObject("CMSResource", "EditNick")%>' onclick='EditNick(\"" + record.get("id") + "\",\"" + record.get("nick") + "\");'/>";
                },
                                dataIndex: 'mac',
                width: 150
                            },
                    {
                        text: '<%=GetGlobalResourceObject("CMSResource", "Status")%>',
                        width: 35,
                        dataIndex: 'status',
                        sortable: false,
                        menuDisabled: true,
                        align: 'center',
                        hidden: true,
                        hideable: false,
                        renderer: function (value, metaData, record) {
                            if (value == "" || record.get("is3rd") == "1") return;
                            //return value == "1" ? '<img src=/Common/DMZ/Images/16x16/enabled.png />' : '<img src=/Common/DMZ/Images/16x16/alert.png />';
                            switch (value) {
                                case "1":
                                    return '<img src=/Common/DMZ/Images/16x16/enabled.png />';
                                    break;
                                case "-1":
                                    return '<img src=/Common/DMZ/Images/16x16/question.png />';
                                    break;
                                default:
                                    return '<img src=/Common/DMZ/Images/16x16/alert.png />';
                                    break;
                            }
                        }
                    }, {
                        text: '<%=GetGlobalResourceObject("CMSResource", "Description")%>',
                        flex: 1,
                        dataIndex: 'nick',
                        sortable: false,
                        hidden: true,
                        menuDisabled: true,
                        hidden: true,
                        hideable: false,
                    }, {
                        text: '<%=GetGlobalResourceObject("CMSResource", "InPosition")%>',
                        width: 50,
                        dataIndex: 'assigned',
                        sortable: false,
                        menuDisabled: true,
                        hidden: true,
                        hideable:false,
                        align:'center',
                        renderer: function (value) {
                            if (value == "") return;
                            return value == "1" ? "<font color=green><%=GetGlobalResourceObject("CMSResource", "Yes")%></font>" : "<font color=red><%=GetGlobalResourceObject("CMSResource", "No")%></font>";
                        }
                    },
                        {
                            xtype: 'actioncolumn',
                            width: 30,
                            sortable: false,
                            menuDisabled: true,
                            hideable:false,
                            align: 'center',
                            hidden: true,
                            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");
                                    }
                                },
                                isDisabled: function (view, rowIndex, colIndex, item, record) {
                                    return rowIndex == 0;
                                }
                            }]
                        },
                                                {
                                                    xtype: 'actioncolumn',
                                                    width: 30,
                                                    sortable: false,
                                                    menuDisabled: true,
                                                    hideable: false,
                                                    align: 'center',
                                                    items: [{
                                                        icon: '/Common/DMZ/Images/16x16/delete.png',
                                                        tooltip: '<%=GetGlobalResourceObject("CMSResource", "Remove")%>',
                                                        scope: this,
                                                        handler: function (view, rowIndex, colIndex, item, e, record, row) {
                                                            if (confirm("<%=GetGlobalResourceObject("CMSResource", "SureRemoveDevice")%>")) {
                                                                Ext.Ajax.request({
                                                                    url: 'Handler.ashx?action=RemoveDevice&mac=' + record.get("id"),
                                                                    success: function (response) {
                                                                        var result = Ext.decode(response.responseText);
                                                                        if (result.success) {
                                                                            var node = tree.store.getNodeById(record.get("id"));
                                                                            if (record.get("id") == "-1") {
                                                                                tree.store.load();
                                                                            }
                                                                            else {
                                                                                node.parentNode.removeChild(node);
                                                                            }
                                                                            
                                                                            
                                                                        }
                                                                        else {

                                                                        }
                                                                    }
                                                                });
                                                            }
                                                        },
                                                        isDisabled: function (view, rowIndex, colIndex, item, record) {
                                                            //return rowIndex == 0 || record.get("is3rd") == "1";
                                                            return record.get("is3rd") == "1" || record.get("code")=="240-2";
                                                        }
                                                    }]
                                                }

                    ]
                    ,
                buttons: [
                  {
                      text: '<%=GetGlobalResourceObject("CMSResource", "SaveSetting")%>',
                                scale: 'medium',
                                id: 'SaveButton',
                                disabled: true,
                                icon: '/Common/DMZ/Images/24x24/floppy.png',
                                border: 1,
                                style: {
                                    borderColor: 'gray',
                                    borderStyle: 'solid'
                                },
                                handler: function () {
                                    var list = "";

                                    tree.getRootNode().cascade(function (n) {
                                        if (n.dirty && n.get("doUpdate")=="Y") {
                                            if (list != "") list = list + "|";
                                            list = list + n.get("id") + "#" + n.get("nick");
                                        }
                                    });

                                    //console.log(list);
                                    Ext.Ajax.request({
                                        url: 'Handler.ashx?action=SaveNickSetting',
                                        params: {
                                            list: list
                                        },
                                        success: function (response) {
                                            var result = Ext.decode(response.responseText);
                                            if (result.success) {

                                                tree.getRootNode().cascade(function (n) {
                                                    if (n.dirty && n.get("doUpdate")=="Y") {
                                                        n.set("doUpdate","");
                                                        n.commit();
                                                    }
                                                });

                                                alert("<%=GetGlobalResourceObject("CMSResource", "SetSuccess")%>");
                                                Ext.getCmp("SaveButton").setDisabled(true);
                                            }
                                            else {

                                            }
                                        }
                                    });
                                }
                            }
                          ]
                    ,
                    listeners: {
                        'itemcontextmenu': function (view, record, item, index, event) {
                            if (record.isLeaf()) {
                                var menu = Ext.create('Ext.menu.Menu', {
                                    items: [ {
                                        text: '<%=GetGlobalResourceObject("CMSResource", "Parameters")%>',
                                        icon: '/Common/DMZ/Images/16x16/gear.png',
                                        handler: function () {

                                            var w = parent.Ext.create('Ext.window.Window', {
                                                title: '<%=GetGlobalResourceObject("CMSResource", "ParamMainten")%>',
                                                layout: 'fit',
                                                modal: true,
                                                constrain: true,
                                                shadow: true,
                                                shadowOffset: 50,
                                                width: '95%',
                                                height: '95%',
                                                maximizable: true,
                                                items: [
                                                   {
                                                       xtype: "component",
                                                       border: false,
                                                       autoEl: {
                                                           tag: "iframe",
                                                           src: "/Apps/DeviceSystem/DeviceParameter.aspx?MAC=" + record.get("mac")
                                                       }
                                                   }
                                                ],
                                                buttons: [
                         {
                             text: '<%=GetGlobalResourceObject("CMSResource", "Close")%>',
                             handler: function () {
                                 this.up("window").close();
                             }
                         }
                                                ]
                                            }).show();
                                        }
                                    }, {
                                        text: '<%=GetGlobalResourceObject("CMSResource", "Action")%>',
                                        menu: Ext.create('Ext.menu.Menu', {
                                            items: [
                                                {
                                                    text: '<%=GetGlobalResourceObject("CMSResource", "CallIdent")%>',
                                                    icon: '/Common/DMZ/Images/16x16/bell.png',
                                                    handler: function () {

                                                        Ext.Ajax.request({
                                                            url: 'Handler.ashx?action=HandsUP',
                                                            params: {
                                                                ip: record.get("ip")
                                                            }
                                                        });
                                                    }
                                                }, '-',
                                                    {
                                                        text: '<%=GetGlobalResourceObject("CMSResource", "Reboot")%>',
                                                        icon: '/Common/DMZ/Images/16x16/reload_all_tabs.png',
                                                        handler: function () {
                                                            if (confirm("<%=GetGlobalResourceObject("CMSResource", "RebootConfirm")%>")) {
                                                                Ext.Ajax.request({
                                                                    url: 'Handler.ashx?action=Reboot',
                                                                    params: {
                                                                        ip: record.get("ip")
                                                                    }
                                                                });
                                                            }
                                                        }
                                                    }
                                            ]
                                        })
                                    }]
                                });
                                menu.showAt(event.getXY());
                                event.stopEvent();
                            }

                        }

                    }
                });


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


            });

                 function EditNick(id,value){

                      var answer = prompt ("<%=GetGlobalResourceObject("CMSResource", "InputNickPls")%>",value);
                      if (answer != null) {
                          tree.store.getNodeById(id).set("nick",answer);
                          tree.store.getNodeById(id).set("doUpdate","Y");
                          Ext.getCmp("SaveButton").setDisabled(false);
                      }

                 }

            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>
