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

<!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>
                <script src="/Scripts/jquery-1.6.4.min.js"></script>
       <script src="/Scripts/jquery.signalR-2.2.0.min.js"></script>
        <script src="/signalr/hubs"></script>
    <style>
        .check-icon {
    background-image: url('/Common/DMZ/Images/32x32/Save.png');
    background-repeat: no-repeat;
}
    </style>
</head>
    <script language=javascript>
        Ext.apply(Ext.data.SortTypes, {
            asNatural: function (str) {
                // Pad all the numbers we can find with 10 zeros to the left, then trim
                // down to the last 10 digits. A primitive natural sort occurs.
                // WARN: May do odd things to any numbers longer than 10 digits. It will
                // also not work as you might expect on decimals.
                return str.replace(/(\d+)/g, "0000000000$1").replace(/0*(\d{10,})/g, "$1");
            }
        });

        var unitList = "<%=unitList %>";
        var totalLevel = unitList.split("@").length;

        window.onbeforeunload = function () {
            try {
                if ($.connection && $.connection.hub) {
                    $.connection.hub.stop();
                }
            }
            catch (ex) { }
            finally { }
        }

        $(function () {

            var chat = $.connection.deviceHub;

            chat.client.settingMe = function (name,model,mac,ip,ids) {
                if (!atSetting) {
                    atSetting = true;
                    Ext.MessageBox.hide();

                    var w = Ext.create('Ext.window.Window', {
                        title: '<%=GetGlobalResourceObject("CMSResource", "RemoteSetDevice")%>',
                        layout: {
                            type: 'border'
                        },
                        modal: true,
                        constrain: true,
                        shadow: true,
                        shadowOffset: 50,
                        width: 600,
                        height:350,
                        closable: false,
                        draggable: false,
                        items: [
                            {
                                xtype: 'panel',
                                region: 'north',
                                border: false,
                                defaults: {
                                    xtype: 'button',
                                    margin: '5 5 5 5',
                                    pressed: true,
                                    border: '0 0 5 0',
                                    style: {
                                        borderColor: 'red',
                                        borderStyle: 'solid'
                                    }
                                },
                                items: [
                                     {
                                         text: 'Name：' + name
                                     },
                                     {
                                         text: 'Model：' + model
                                     },
                                          {
                                              text: 'MAC：' + mac
                                          },
                                             {
                                                 text: 'IP：' + ip
                                             }

                                ]
                            },
                            {
                                xtype: 'panel',
                                region: 'center',
                                border: false,
                                frame:true,
                                layout: {
                                    type: 'vbox',
                                    align: 'stretch'
                                },
                                items: [

                                                                                            {
                                                                                                xtype: 'button',
                                                                                                text: '<%=GetGlobalResourceObject("CMSResource", "ParamMainten")%>',
                                                                                                iconAlign: 'top',
                                                                                                scale: 'large',
                                                                                                border: "0 2 2 0",
                                                                                                margin: 10,
                                                                                                style: {
                                                                                                    borderColor: 'gray',
                                                                                                    borderStyle: 'solid'
                                                                                                },
                                                                                                icon: '/Common/DMZ/Images/24x24/gear.png',
                                                                                                handler: function () {
                                                                                                    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=" + mac
                                                                                                               }
                                                                                                           }
                                                                                                        ],
                                                                                                        buttons: [
                                                                                 {
                                                                                     text: '<%=GetGlobalResourceObject("CMSResource", "Close")%>',
                                                                                     handler: function () {
                                                                                         this.up("window").close();
                                                                                     }
                                                                                 }
                                                                                                        ]
                                                                                                    }).show();
                                                                                                }
                                                                                            },
                                                                                                                {
                                                                                                                    xtype: 'button',
                                                                                                                    text: '<%=GetGlobalResourceObject("CMSResource", "LocationSetting")%>',
                                                                                                                    iconAlign: 'top',
                                                                                                                    scale: 'large',
                                                                                                                    border: "0 2 2 0",
                                                                                                                    margin: 10,
                                                                                                                    style: {
                                                                                                                        borderColor: 'gray',
                                                                                                                        borderStyle: 'solid'
                                                                                                                    },
                                                                                                                    icon: '/Common/DMZ/Images/24x24/location.png',
                                                                                                                    handler: function () {
                                                                                                                        var locationWin = Ext.create('Ext.window.Window', {
                                                                                                                            title: '<%=GetGlobalResourceObject("CMSResource", "SetLocationPls")%>',
                                                                                                                            layout: 'fit',
                                                                                                                            modal: true,
                                                                                                                            constrain: true,
                                                                                                                            shadow: true,
                                                                                                                            shadowOffset: 50,
                                                                                                                            width: '80%',
                                                                                                                            height: '80%',
                                                                                                                            maximizable: true,
                                                                                                                            items: Ext.create('Ext.tree.Panel', {
                                                                                                                                flex: 1,
                                                                                                                                border: true,
                                                                                                                                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: '/Common/SystemAdmin/Building/Handler.ashx?action=GetStructureTree_Temp'
                                                                                                                                    },
                                                                                                                                    root: {
                                                                                                                                        text: '<%=GetGlobalResourceObject("CMSResource", "AllArea1")%>',
                                                                                                                                        id: '<%=rootIdent %>'
                                                                                                                                    },
                                                                                                                                    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: {
                                                                                                                                    'render': function (t) {
                                                                                                                                        t.getSelectionModel().select(t.getRootNode());
                                                                                                                                    },
                                                                                                                                    //'afteritemexpand': function (t) {
                                                                                                                                    //    t.getStore().sort("text", "ASC");
                                                                                                                                    //},
                                                                                                                                    'itemappend': function (n, node, index, eOpts) {

                                                                                                                                        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")%>',
                                                                                                                                                 handler: function () {

                                                                                                                                                     var msg=Ext.Msg.wait("<%=GetGlobalResourceObject("CMSResource", "SettingPlsWait")%>", "");
                                                                                                                                                     Ext.Ajax.request({
                                                                                                                                                         url: 'Handler.ashx?action=SettingDeviceID',
                                                                                                                                                         params: {
                                                                                                                                                             mac: mac,
                                                                                                                                                             ident: this.up("window").down("treepanel").getSelectionModel().getLastSelected().get("id")
                                                                                                                                                         },
                                                                                                                                                         success: function (response) {
                                                                                                                                                             msg.hide();
                                                                                                                                                             var result = Ext.decode(response.responseText);
                                                                                                                                                             if (result.success) {
                                                                                                                                                                 locationWin.close();
                                                                                                                                                             }
                                                                                                                                                             else {
                                                                                                                                                                 alert(result.msg);
                                                                                                                                                             }
                                                                                                                                                         }
                                                                                                                                                     });

                                                                                                                                                 }
                                                                                                                                             },
                                                                                                     {
                                                                                                         text: '<%=GetGlobalResourceObject("CMSResource", "Cancel")%>',
                                                                                                         handler: function () {
                                                                                                             locationWin.close();
                                                                                                         }
                                                                                                     }
                                                                                                                            ]
                                                                                                                        }).show();
                                                                                                                    }
                                                                                                                },
                                                                                                                    {
                                                                                                                        xtype: 'button',
                                                                                                                        text: '<%=GetGlobalResourceObject("CMSResource", "EditNick")%>',
                                                                                                                        iconAlign: 'top',
                                                                                                                        scale: 'large',
                                                                                                                        border: "0 2 2 0",
                                                                                                                        margin: 10,
                                                                                                                        style: {
                                                                                                                            borderColor: 'gray',
                                                                                                                            borderStyle: 'solid'
                                                                                                                        },
                                                                                                                        icon: '/Common/DMZ/Images/24x24/pencil.png',
                                                                                                                        handler: function () {
                                                                                                                            Ext.Ajax.request({
                                                                                                                                url: 'Handler.ashx',
                                                                                                                                params: {
                                                                                                                                    action: 'GetNickName',
                                                                                                                                    mac: mac
                                                                                                                                },
                                                                                                                                success: function (response) {
                                                                                                                                    var result = Ext.decode(response.responseText);
                                                                                                                                    if (result.success) {
                                                                                                                                        var answer = prompt("<%=GetGlobalResourceObject("CMSResource", "InputNickPls")%>", result.nickName);
                                                                                                                                        if (answer != null) {
                                                                                                                                            Ext.Ajax.request({
                                                                                                                                                url: 'Handler.ashx',
                                                                                                                                                params: {
                                                                                                                                                    action: 'SetNickName',
                                                                                                                                                    mac: mac,
                                                                                                                                                    nickName: answer
                                                                                                                                                },
                                                                                                                                                success: function (response) {
                                                             
                                                                                                                                                }
                                                                                                                                            });
                                                                                                                                        }
                                                                                                                                    }

                                                                                                                                }
                                                                                                                            });
                                                                                                                        }
                                                                                                                    }
                                ]
                            }


                        ],
                        buttons: [
                            {
                                text: '<%=GetGlobalResourceObject("CMSResource", "Close")%>',
                                handler: function () {
                                    atSetting = false;
                                    this.up('window').close();
                                    showWait();
                                }
                            }
                        ]
                    }).show();
                }
            };

            //把connection打開
            $.connection.hub.start().done(function () {

            });
        });

        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 showWait() {
            Ext.MessageBox.show({
                msg: '<%=GetGlobalResourceObject("CMSResource", "WaitDeviceCall")%>',
                width: 300,
                wait: true,
                waitConfig: { interval: 200 },
                icon: 'check-icon'
            });
        }

        var atSetting = false;
        Ext.onReady(function () {
            Ext.QuickTips.init();

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