<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="GigaTech.CMS.Web.Common.Home" %>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!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">
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <style>
            /*讓Accrodion的Item Header有手勢鼠標*/
            .x-accordion-hd { cursor: pointer; }

           .x-btn.x-btn-my-pressed {
               border-bottom: solid 3px red
            }


        </style>

        <style type="text/css">

		#instructions ul li {
			list-style-type:disc;
			list-style-position:outside;
			font-size:12px;
			margin:0px 0px 0px 20px;
		}

		/* Icons */
		.ux-notification-icon-information {
			background-image: url('/Common/DMZ/Images/16x16/information2.png');
		}

		.ux-notification-icon-error {
			background-image: url('/Common/DMZ/Images/16x16/alert.png');
		}


		/* Using standard theme */
		.ux-notification-window .x-window-body {
			text-align:left;
            line-height: 24px;
            letter-spacing: 1px;
			padding: 15px 5px 15px 5px;
		}


		/* Custom styling */
		.ux-notification-light .x-window-header {
			background-color: transparent;
		}

		body .ux-notification-light {
			background-image: url('/Common/DMZ/Images/Other/fader.png');
		}

		.ux-notification-light .x-window-body {
			text-align: center;
			padding: 15px 5px 20px 5px;
			background-color: transparent;
			border: 0px solid white;
            
		}


	</style>
        <script src="/Scripts/jquery-1.6.4.min.js"></script>

        <script src="/Common/DMZ/JS/Extjs/4.2.1/ux/Notification.js"></script>
    </head>
    <script language=javascript>
        try {
            //For ChromiumWebBrowser
            CMSSetTitle(document.title);
            if("<%=adminGroup%>"=="1") {
                CMSAdminLogin();
            }else if("<%=IsAdmin%>"=="Y"){
                CMSSysAdminLogin();
            }

        } catch (e) {    
        }

        function checkHighlineWindow(){
            var cnt=0;
            parent.Ext.WindowManager.eachTopDown(function(win){
                cnt++;
                if(cnt==1){
                    window.clearInterval(win.highline);
                    win.highline=null;
                    win.getEl().applyStyles('border-top:solid 4px red');
                }
                else{
                    if(win.highline==null) win.highline = setHighline(win);
                }
                //console.log(win.id);
            });
        }

        function setHighline(w){
           return setInterval(function(){
               //console.log('setHighline');
                if(w.isHighline)
                    w.getEl().setStyle('border-top');
                else
                    w.getEl().applyStyles('border-top:solid 4px red');

                w.isHighline=!w.isHighline;
            },1000); 
        }

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

            var phoneTest = /^([1-9])*[0-9]+$/;
            Ext.apply(Ext.form.field.VTypes, {
                phone: function(val, field) {
                    return phoneTest.test(val);
                },
                phoneText: '<%=GetGlobalResourceObject("CMSResource", "AllowNumOnly")%>'
            });

            //var IsCMSClient="1";
            //try {
            //    IsCMSClient=IsCMSClient();

            //} catch (e) {
    
            //}
            //Ext.Ajax.request({
            //    url: '/Apps/CardSystem/Handler.ashx',
            //    params: {
            //        action:'SetWhereCardReader',
            //        value: IsCMSClient
            //    }
            //});

            Ext.create('Ext.container.Viewport', {
                layout: {
                    type: 'border',
                    padding: '0 0 0 0' // pad the layout from the window edges
                },
                items: [{
                    xtype: 'panel',
                    border: false,
                    hidden: false,
                    region: 'north',
                    bbar: Ext.create('Ext.toolbar.Toolbar', {
                        style: 'background-image:url(/Common/DMZ/Images/Other/TopBanner.jpg)',
                        items: [
                            {
                                xtype: 'tbtext',
                                text: '<%=SystemTitle %> (<%=IOTID%>)',
                                style: 'font-size:24px;color:white;font-family:Times New Roman Bold;text-shadow:2px 2px 2px #000000;'
                            //style: 'font-size:24px;color:#2B547E;font-family:Times New Roman Bold;text-shadow:1px  1px  1px  #999999;'
                            }, '->',
                            {
                                xtype: 'tbtext',
                                text: '<font color=red><b><%=GetGlobalResourceObject("CMSResource", "DemoMax20")%></b></font>',
                                hidden:'<%=CMSVersion %>'!='DEMO'
                            },
                            {
                                text: '<%=GetGlobalResourceObject("CMSResource", "GoHome")%>',
                                iconAlign: 'top',
                                scale: 'medium',
                                                    border: "0 2 2 0",
                    style: {
                        borderColor: 'gray',
                        borderStyle: 'solid'
                    },
                                icon: '/Common/DMZ/Images/24x24/home.png',
                                handler: function() {
                                    Ext.getDom('iframe-win').src = "<%=PortalPath %>";
                                }
                            },
                            {
                                xtype: 'splitbutton',
                                text: '<%=GetGlobalResourceObject("CMSResource", "Console")%>',
                                icon: '/Common/DMZ/Images/16x16/utility.png',
                                arrowAlign: 'bottom',
                                                     border: "0 2 2 0",
                    style: {
                        borderColor: 'gray',
                        borderStyle: 'solid'
                    },
                                height: 53,
                                menu: [{
                                    text:'<%=GetGlobalResourceObject("CMSResource", "MyData")%>',
                                    icon: '/Common/DMZ/Images/16x16/user.png',
                                    hidden:"<%=IsUser%>"=="N",
                                    handler: function() {

                                        var contentForm = Ext.create('Ext.form.Panel', {
                                            border: false,
                                            bodyPadding: 5,
                                            layout: 'anchor',
                                            url: '/Common/SystemAdmin/Account/Handler.ashx?action=SaveProfile',
                                            defaults: {
                                                anchor: '100%',
                                                labelWidth: 60
                                            },
                                            defaultType: 'textfield',
                                            items: [{
                                                xtype: 'hiddenfield',
                                                name: 'ident',
                                                value: '<%=GigaTech.CMS.Kernel.CurrentSession.UserIdent%>'
                                            }, {
                                                fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "Email")%>',
                                                name: 'email',
                                                id: 'email',
                                                //allowBlank: false,
                                                vtype: 'email'
                                            }, {
                                                fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "Mobile")%>',
                                                name: 'mobile',
                                                //allowBlank: false,
                                                vtype: 'phone'
                                            }],
                                            buttons: [
                                                {
                                                    text: '<%=GetGlobalResourceObject("CMSResource", "Save")%>',
                                                    formBind: true,
                                                    handler: function() {
                                                        var form = this.up('form').getForm();
                                                        if (form.isValid()) {
                                                            form.submit({
                                                                success: function(f, action) {
                                                                    
                                                                    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();
            }
        }
                                            ]
                                        });

                                            contentForm.getForm().load({
                                                url: '/Common/SystemAdmin/Account/Handler.ashx?action=GetProfile&ident=' + '<%=GigaTech.CMS.Kernel.CurrentSession.UserIdent%>',
                                                success: function(form, action) {

                                                }
                                            });


                                        var w = Ext.create('Ext.window.Window', {
                                            title: "<%=GetGlobalResourceObject("CMSResource", "MyData")%>",
                                            layout: 'fit',
                                            modal: true,
                                            constrain: true,
                                            shadow: true,
                                            shadowOffset: 50,
                                            width: 300,
                                            items: contentForm
                                        }).show();
                                        Ext.getCmp('email').focus(false, 200);

                                    }
                                },
                                {
                                    text:"<%=GetGlobalResourceObject("CMSResource", "MiniCmsP")%>", 
                                    icon: '/Common/DMZ/Images/16x16/edit.png',
                                    handler: function() {
                                        setIOTID();
                                    }
                                },
                                {
                                    text:"<%=GetGlobalResourceObject("CMSResource", "IOTDeviceP")%>", 
                                    icon: '/Common/DMZ/Images/16x16/edit.png',
                                    handler: function() {
                                        Ext.Ajax.request({
                                            url: '/Apps/IOTDeviceSystem/Handler.ashx', 
                                            params: {
                                                action: 'ExecCMD',
                                                option: 'IOTDevicePair'
                                            },
                                            success: function (response) {
                                            }
                                        });
                                    }
                                },
                                    {
                                    text: '<%=GetGlobalResourceObject("CMSResource", "ChangePWD")%>',
                                    icon: '/Common/DMZ/Images/16x16/pwd.png',
                                    handler: function() {
                                        var w = Ext.create('Ext.window.Window', {
                                            title: '<%=GetGlobalResourceObject("CMSResource", "ChangePWD")%>',
                                            layout: 'fit',
                                            modal: true,
                                            constrain: true,
                                             shadow: true,
            shadowOffset: 50,
                                            resizable: false,
                                            width: 300,
                                            items: {
                                                xtype: 'form',
                                                url: '/Common/DMZ/PWDProcess.ashx?action=ChangePWD',
                                                border: false,
                                                bodyPadding: 5,
                                                layout: 'anchor',
                                                defaults: {
                                                    anchor: '100%',
                                                    labelWidth: 70
                                                },
                                                defaultType: 'textfield',
                                                items: [{
                                                    fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "OldPWD")%>',
                                                    name: 'old_pwd',
                                                    inputType: 'password',
                                                    allowBlank: false,
                                                     vtype: 'alphanum'
                                                }, {
                                                    fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "NewPWD")%>',
                                                    name: 'new_pwd',
                                                    inputType: 'password',
                                                    allowBlank: false,
                                                     vtype: 'alphanum'
                                                }, {
                                                    fieldLabel: '<%=GetGlobalResourceObject("CMSResource", "ConfirmNewPWD")%>',
                                                    name: 'confirm_pwd',
                                                    inputType: 'password',
                                                    allowBlank: false,
                                                     vtype: 'alphanum'
                                                }],
                                                buttons: [
                                                    {
                                                        text: '<%=GetGlobalResourceObject("CMSResource", "SureChange")%>',
                                                        formBind: true,
                                                        handler: function() {
                                                            var form = this.up('form').getForm();
                                                            var new_pwd=this.up("form").getForm().findField("new_pwd").getValue();
                                                            var confirm_pwd=this.up("form").getForm().findField("confirm_pwd").getValue();
                                                            
                                                            if(confirm_pwd!=new_pwd) {
                                                                alert("<%=GetGlobalResourceObject("CMSResource", "ConfirmNewPWDError")%>");
                                                                return;
                                                            }

                                                            if (form.isValid()) {
                                                                form.submit({
                                                                    success: function(f, action) {
                                                                        w.close();
                                                                        alert("<%=GetGlobalResourceObject("CMSResource", "PWDChangeSuccess")%>");
                                                                    },
                                                                    failure: function(f, action) {
                                                                        alert(action.result.msg);
                                                                    }
                                                                });
                                                            }
                                                        }
                                                    },
                                                    {
                                                        text: '<%=GetGlobalResourceObject("CMSResource", "Cancel")%>',
                                                        handler: function() {
                                                            this.up("window").close();
                                                        }
                                                    }
                                                ]
                                            }
                                        }).show();
                                    }
                                }, {
                                    text: '<%=GetGlobalResourceObject("CMSResource", "SystemInfo")%>',
                                    icon: '/Common/DMZ/Images/16x16/information.png',
                                    handler: function() {
                                        Ext.MessageBox.show({
                                            title: '<%=GetGlobalResourceObject("CMSResource", "SystemInfo")%>',
                                            msg: 'Web <%=GetGlobalResourceObject("CMSResource", "Version")%>：<%=webVersion%><p>' + 'Service <%=GetGlobalResourceObject("CMSResource", "Version")%>：<%=wsVersion%><p>' + 'DB <%=GetGlobalResourceObject("CMSResource", "Version")%>：<%=dbVersion%>',
                                            buttons: Ext.MessageBox.OK,
                                            icon: Ext.MessageBox.INFO
                                        });
                                    }
                                }]
                            },
                            {
                                text: '<%=GetGlobalResourceObject("CMSResource", "Logout")%>',
                                iconAlign: 'top',
                                scale: 'medium',
                                                     border: "0 2 2 0",
                    style: {
                        borderColor: 'gray',
                        borderStyle: 'solid'
                    },
                                icon: '/Common/DMZ/Images/24x24/logout.png',
                                handler: function() {
                                    try {
                                        //For ChromiumWebBrowser
                                        CMSLogout();
                                    } catch (e) {
    
                                    }
                                    location.href = "/";
                                }
                            }
                        ]
                    })
                }, {
                    xtype: 'container',
                    region: 'center',
                    layout: {
                        type: 'border',
                        padding: '0 5 5 5' // pad the layout from the window edges
                    },
                    items: [{
                        title: '<%=GetGlobalResourceObject("CMSResource", "FunctionMenu")%>',
                        icon: '/Common/DMZ/Images/16x16/menu.png',
                        region: 'west',
                        animCollapse: true,
                        width: 210,
                        minWidth: 150,
                        maxWidth: 400,
                        split: true,
                        collapsible: true,
                        layout: {
                            type: 'accordion',
                            animate: true
                        },
                        defaults: {
                            border: false,
                            layout: 'fit',
                            listeners: {
                                collapse: function(p, eOpts) {
                                    p.down("treepanel").getSelectionModel().deselectAll();
                                }
                            }
                        },
                        items: [<%=AppGroupItem %>],
                        bbar:[
                            '->',
                            {
                                xtype: 'label',
                                id:'clockLabel'
                            },
                            '->'
                        ]
                    }, {
                        region: 'center',
                        xtype: "component",
                        border: false,
                        id: 'iframe-win',
                        autoEl: {
                            tag: "iframe",
                            src: "<%=PortalPath %>"
                        }
                    }
                    ]
                }]
            });

            if('<%=IOTID%>'==''){

                Ext.Msg.show({
                    title: "<%=GetGlobalResourceObject("CMSResource", "Notify")%>",
                    msg: "<%=GetGlobalResourceObject("CMSResource", "GContent3")%>",
                    buttons: Ext.Msg.YES,
                    fn:function(btn, text){
                        setIOTID();
                    }
                });
                     
                }


            var task = {
                run: function() {
                    Ext.Ajax.request({
                        url: 'Empty.ashx'
                    });
                },
                interval: 300000
            };
            
            var runner = new Ext.util.TaskRunner();
            runner.start(task);

  
            //指定哪個Panel先開啟
            //Ext.getCmp('SampleItem').expand();


            startTime();

        });

        function setIOTID(){
            //Ext.Msg.prompt('網關ID設定', '網關ID：', function(btn, text){
            //    if (btn == 'ok'){
            //        // process text value and close...
            //    }
            //});

           var iot_w= Ext.create('Ext.window.Window', {
                title: "<%=GetGlobalResourceObject("CMSResource", "MiniCmsP")%>",
                modal:true,
                height: 150,
                width: 300,
                layout: {
                    type: 'fit'
                },
                items: [
                Ext.create('Ext.form.Panel', {
                    url: '/Common/SystemAdmin/Utility/Handler.ashx?action=IOTSetupCodeGen',
                    id: 'panel-step1',
                    html:"<%=GetGlobalResourceObject("CMSResource", "GContent1")%>", 
                    layout: 'anchor',
                    defaults: {
                        anchor: '100%'
                    },
                    defaultType: 'textfield',
                    items:[
                        {
                            fieldLabel: "<%=GetGlobalResourceObject("CMSResource", "GID")%>",
                            labelWidth:60,
                            id: 'Setp1IOTID',
                            name: 'Setp1IOTID',
                            allowBlank: false
                        }
                    ],
                    bodyPadding: 5,
                    buttons: [{
                        text: '下一步',
                        formBind:true,
                        handler: function() {
                                              var form = this.up('form').getForm();
                                                if (form.isValid()) {
                                                    form.submit({
                                                        success: function(f, action) {
                                                            Ext.getCmp("IOTID").setValue(Ext.getCmp("Setp1IOTID").getValue());
                                                            Ext.getCmp("panel-step1").hide();
                                                            Ext.getCmp("panel-step2").show();
                                                        },
                                                        failure: function(f, action) {
                                                           
                                                        }
                                                    });
                                                }
                        }
                    }]
                }), Ext.create('Ext.form.Panel', {
                    url: '/Common/SystemAdmin/Utility/Handler.ashx?action=IOTSetupCodeCheck',
                    id: 'panel-step2',
                    html: "<%=GetGlobalResourceObject("CMSResource", "GContent2")%>", 
                    layout: 'anchor',
                    defaults: {
                        anchor: '100%'
                    },
                    defaultType: 'textfield',
                    items:[
                        {
                            xtype: 'hiddenfield',
                            id: 'IOTID',
                            name: 'IOTID'
                        },
                        {
                            fieldLabel: "<%=GetGlobalResourceObject("CMSResource", "Pcode")%>", 
                            labelWidth:60,
                            name: 'IOTSetupCode',
                            allowBlank: false
                        }
                    ],
                    bodyPadding: 5,
                    hidden:true,
                    buttons: [{
                        text: "<%=GetGlobalResourceObject("CMSResource", "SOK")%>",
                        formBind:true,
                        handler: function() {
                            var form = this.up('form').getForm();
                            if (form.isValid()) {
                                form.submit({
                                    success: function(f, action) {

                                        location.reload();
                                        alert("<%=GetGlobalResourceObject("CMSResource", "POK")%>");
                                        
                                    },
                                    failure: function(f, action) {
                                        alert("<%=GetGlobalResourceObject("CMSResource", "PFail")%>");                  
                                    }
                                });
                            }

                           
                        }
                    }]
                })]
            }).show();

        }

        var ServerDate=<%=ServerDate%>;

        function formatDate(date, format) {
            if (!date) return;
            if (!format) format = "yyyy-MM-dd";
            switch(typeof date) {
                case "string":
                    date = new Date(date.replace(/-/, "/"));
                    break;
                case "number":
                    date = new Date(date);
                    break;
            } 
            if (!date instanceof Date) return;
            var dict = {
                "yyyy": date.getFullYear(),
                "M": date.getMonth() + 1,
                "d": date.getDate(),
                "H": date.getHours(),
                "m": date.getMinutes(),
                "s": date.getSeconds(),
                "MM": ("" + (date.getMonth() + 101)).substr(1),
                "dd": ("" + (date.getDate() + 100)).substr(1),
                "HH": ("" + (date.getHours() + 100)).substr(1),
                "mm": ("" + (date.getMinutes() + 100)).substr(1),
                "ss": ("" + (date.getSeconds() + 100)).substr(1)
            };    
            return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function() {
                return dict[arguments[0]];
            });                
        }

        function startTime() {
            
            var today = new Date(ServerDate);
            var h = today.getHours();
            var m = today.getMinutes();
            var s = today.getSeconds();
            m = checkTime(m);
            s = checkTime(s);
            Ext.getCmp("clockLabel").setText("<b><font color=black>" + formatDate(today, "yyyy-MM-dd HH:mm:ss")  + "</font></b>",false);
            var t = setTimeout(function () { startTime() }, 1000);
            ServerDate += 1000;
        }

        function checkTime(i) {
            if (i < 10) { i = "0" + i };  // add zero in front of numbers < 10
            return i;
        }
    </script>
    <body>
        <form id="form1" runat="server">
        </form>
        
    </body>
</html>