<template>
    <div class="IndexContainer">
        <keep-alive>
            <router-view></router-view>
        </keep-alive>
    </div>
</template>
<!--<script src="../../public/mui.js"></script>-->

<script>
    import {
        QueryVueFramework,
        DownloadVueFramework,
        getCheckUpdate,
        getUserList
    } from '@/api/getApiRes'

    import "../../public/mui.js"
    import '../Global'

    let qs = require('qs');

    export default {
        name: 'Home',
        data() {
            return {
                autoUpdate: false,//自动升级
                aSideState: false,
                thisClick: 0,
                tabwildState: 1,
                handleTabsList: [{
                    title: '系统首页',
                    name: '1',
                    clmurl: '/',
                }],
                editableTabsValue: '1',
                curVersion: '1.0.1'
            }
        },
        mounted() {
            // alert('clientWidth:' + document.documentElement.clientWidth);
            // alert('devicePixelRatio:' + window.devicePixelRatio);
            //浏览器默认的
            localStorage.eqSn = '30:9C:23:0C:8B:1E';
            localStorage.version = '1.1.0';
            this.getCurVersion();

        },
        methods: {
            // 获取当前版本号
            getCurVersion() {
                let that = this;
                //浏览器默认的
                localStorage.eqSn = '30:9C:23:0C:8B:1E';
                localStorage.version = '1.1.0';

                function plusReady() {
                    if (typeof plus == 'undefined') return false;
                    // 获取本地应用资源版本号
                    plus.runtime.getProperty(plus.runtime.appid, function (inf) {
                        localStorage.version = inf.version;
                        that.curVersion = inf.version;

                        //首发版本 '1.1.0' 在线版不需要自动升级
                        that.checkUpdate(that.curVersion); //开启自动升级

                    });

                    //   获取手机UUID
                    plus.device.getInfo({
                        success: function (e) {
                            let uuid = JSON.stringify(e.uuid).toString().substr(1);
                            uuid = uuid.substring(0, uuid.length - 1);
                            localStorage.eqSn = uuid;
                            console.log('===== uuid =====:' + uuid);
                            // const h = this.$createElement;
                            // that.$notify({
                            //     title: '获取UUID',
                            //     message: h('i', {style: 'color: teal'}, 'uuid:'+JSON.stringify(e).uuid)
                            // });
                            // 8e501b0bde9ce600
                            // 打印uuid
                            // alert('getDeviceInfo success: ' + JSON.stringify(e.uuid));
                            // alert('clientWidth:' + document.documentElement.clientWidth);
                            // alert('clientWidth:' + document.documentElement.clientHeight);
                            // alert('devicePixelRatio:' + window.devicePixelRatio);
                            console.log('getDeviceInfo success: ' + JSON.stringify(e.uuid));
                            // 正式版打开
                            that.getServeIpAddress(e.uuid);
                        },
                        fail: function (e) {
                            console.log('getDeviceInfo failed: ' + JSON.stringify(e));
                        }
                    });

                }

                if (window.plus) {
                    plusReady();
                } else {
                    console.log('mui is not ready');
                    // 调试时候关闭
                    // this.getServeIpAddress(localStorage.eqSn);
                    // plusReady();
                    document.addEventListener('plusready', plusReady, false);
                }
            },
            // 是否有新版本
            checkUpdate(version) {
                let that = this;
                let param = {};
                let postdata = qs.stringify(param);
                const h = this.$createElement;
                console.log('QueryVueFramework');
                QueryVueFramework(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        if (version != json.Version) {
                            // 正式
                            let url = "http://cal.beswell.com:85/v1/Sensors/DownloadVueFramework";
                            // 测试
                            // let url = "http://192.168.0.162/wgt/H5BB8BEB3.wgt";
                            this.downWgt(url);
                            // that.downWgt(url, json.Version);
                            // that.$notify({
                            //     title: '升级提醒',
                            //     message: h('i', {style: 'color: teal'}, '检测到新的版本，正在下载最新版本')
                            // });
                        }
                    }
                })
            },
            downWgt(url) {
                let that = this;
                plus.downloader.createDownload(url, {filename: "_doc/update/"}, function (d, status) {
                    // plus.downloader.createDownload(url, {}, function (d, status) {
                    if (status == 200) {
                        console.log(d);
                        console.log("下载wgt成功：" + d.filename);
                        that.installWgt(d.filename); // 安装wgt包
                    } else {
                        console.log("下载wgt失败！");
                        // plus.nativeUI.alert("下载wgt失败！");
                    }
                    plus.nativeUI.closeWaiting();
                }).start();
            },
            installWgt(path) {
                // plus.nativeUI.showWaiting("安装wgt文件...");
                plus.runtime.install(path, {}, function () {
                    plus.nativeUI.closeWaiting();
                    console.log("安装wgt文件成功！");
                    // plus.nativeUI.alert("应用资源更新完成！", function () {
                    plus.runtime.restart();
                    // });
                }, function (e) {
                    plus.nativeUI.closeWaiting();
                    console.log("安装wgt文件失败[" + e.code + "]：" + e.message);
                    // plus.nativeUI.alert("安装wgt文件失败[" + e.code + "]：" + e.message);
                });
            },
            // 获取转发端口地址
            getServeIpAddress(eqsn) {
                const that = this;
                let url = 'http://cal.beswell.com:85/v1/Sensors/GetShopConfigParam';
                // 测试 todo
                // url = "http://192.168.0.3:19096/v1/Sensors/GetShopConfigParam";
                let param = {
                    eqSn : eqsn
                };
                let postdata = qs.stringify(param);
                axios.post(url, postdata).then(function (data) {
                    let json = data.data;
                    if (json.Code == 0) {
                        headapi = json.Rs.DataServiceIP;
                    } else {
                        headapi = 'http://192.168.0.10:8080/';
                        console.log(json.memo);
                        that.$message.error(json.memo);
                    }
                }, function (response) {
                    console.info(response);
                })
            }

        },
        components: {}
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
    ul, li {
        list-style: none;
        margin: 0;
        padding: 0;
    }

    .el-header {
        padding: 0 !important;
        height: 72px !important;
    }

    .shortElaside {
        width: 140px !important;
        overflow: hidden;
        height: 100%;
        float: left;
    }

    .lognElaside {
        width: 256px !important;
        overflow: hidden;
        height: 100%;
        float: left;
    }

    /deep/ .nav {
        width: 60px;
    }

    .IndexContainer {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: #F0F2F5;
    }

    .IndexContainer > .el-container {
        width: 100%;
        height: 100%;
        overflow: hidden;
        display: block;
        margin: 0 auto;
    }

    .footer {
        position: absolute;
        bottom: 0px;
        width: 100%;
        padding: 10px;
        overflow: hidden;
        display: block;
        margin: 0 auto;
        text-align: center;
        text-indent: -30%;
        font-size: 12px;
        color: #BDBDBD;
        background: #F0F2F5;
        z-index: 222;
    }

    .el-main {
        width: 100%;
        height: 100%;
        /*min-height: 800px;*/
        overflow: hidden;
        display: block;
        margin: 0 auto;
        overflow-y: hidden;
        padding: 10px 20px;
    }

    /*ipad only*/
    @media only screen and (max-width: 1366px) {
        .lognElaside {
            width: 70px !important;
        }

        .logoContainer img {
            display: none !important;
        }

        .el-main {
            padding: 3px !important;
            overflow-y: scroll;
        }

        .el-main > .context {
            /*height: 640px;*/
            /*overflow-y: scroll;*/
            padding: 6px !important;
        }

        .table {
            /*width: 950px;*/
            /*overflow-x: scroll;*/
        }

        .el-dialog {
            max-width: 960px;
        }

        .el-dialog__body {
            padding: 3px;
        }

        .el-transfer__buttons {
            width: 60px;
            padding: 3px;
        }

        .el-transfer .el-button + .el-button {
            float: right;
        }

        .el-container.is-vertical {
            position: absolute;
            left: 140px;
            top: 0;
            right: 0;
            bottom: 0;
            height: 100%;
        }

        .context {
            border-radius: 12px;
            height: 823px !important;
            overflow-y: scroll;
        }

        .el-dialog {
            padding: 5px;
            padding-bottom: 15px;
        }
    }
</style>
