<template>
    <div class="software-update" v-if="showType">
        <!--报错提示-->
        <div class="error-max-box" v-if="boxType === 1">
            <p class="install-title">发生错误,请稍候再试</p>
            <div class="button-box">
                <p class="null-p"></p>
                <p class="confirm-p" @click="outProgram">确定</p>
                <p class="null-p"></p>
            </div>
        </div>
        <!--更新提示-->
        <div class="reminder-max-box" v-if="boxType === 2">
            <p class="install-title">发现新版本是否下载更新?</p>
            <div class="button-box">
                <p class="null-p"></p>
                <p class="out-p" @click="outProgram">退出</p>
                <p class="confirm-p" @click="downloadProgram">下载</p>
                <p class="null-p"></p>
            </div>
        </div>
        <!--更新进度条-->
        <div class="renewal-max-box" v-if="boxType === 3">
            <p class="renewal-title">正在下载更新请稍候</p>
            <div class="renewal-progress-bar">
                <p class="progress-bar" :style="'width:'+scheduleNum+'%;'"></p>
            </div>
            <p class="renewal-text">{{scheduleNum}}%</p>
        </div>
        <!--安装提示-->
        <div class="install-max-box" v-if="boxType === 4">
            <p class="install-title">下载已完成,是否现在安装?</p>
            <div class="button-box">
                <p class="null-p"></p>
                <p class="out-p" @click="outProgram">退出</p>
                <p class="confirm-p" @click="installProgram">安装</p>
                <p class="null-p"></p>
            </div>
        </div>
    </div>
</template>
<script>
    const {version} = require("../../../package.json");
    export default {
        data() {
            return {
                showType: false,
                boxType: null,
                scheduleNum: 0,
            }
        },
        mounted() {
            this.upDate();
        },
        methods: {
            upDate() {
                setTimeout(() => {
                    console.log('定时器执行')
                    this.$electron.ipcRenderer.send('inspect-update');
                }, 500);
                this.$electron.ipcRenderer.on("ev-message", (message, type, res, text) => {
                    if (type == 'error') {
                        //检查更新出错
                        this.$set(this,'showType',true);
                        this.$set(this,'boxType',1);
                    } else if (type == 'update-not-available') {
                        //未发现有新版本
                        this.$set(this,'showType',false);
                        this.$set(this,'boxType',null);
                    } else if (type == 'checking-for-update') {
                        //检查是否有版本更新
                        this.$set(this,'showType',false);
                        this.$set(this,'boxType',null);
                    } else if (type == 'update-available') {
                        //检测到有版本更新
                        this.updateAvailable(res);
                    } else if (type == 'download-progress') {
                        //监测到更新进度
                        this.$set(this, 'scheduleNum', res.percent.toFixed(2));
                    }
                });
                //下载完成
                this.$electron.ipcRenderer.on("ev-should-update", (event, message, text) => {
                    this.$set(this,'showType',true);
                    this.$set(this,'boxType',4);
                });
            },
            updateAvailable(arg) {
                //根据版本号来判断是否需要强制更新
                if (arg.version) {
                    let text = '发现新版本,是否更新?';
                    let localVersionNumber = version.split('.')
                    let onlineVersionNumber = arg.version.split('.')
                    if ((onlineVersionNumber[0] > localVersionNumber[0]) ||
                        (onlineVersionNumber[0] === localVersionNumber[0] && onlineVersionNumber[1] > localVersionNumber[1]) ||
                        (onlineVersionNumber[0] === localVersionNumber[0] && onlineVersionNumber[1] === localVersionNumber[1] && onlineVersionNumber[2] > localVersionNumber[2])
                    ) {
                        this.$set(this,'showType',true);
                        this.$set(this,'boxType',2);
                    }
                }
            },
            //退出程序
            outProgram() {
                this.$set(this,'showType',false);
                this.$set(this,'boxType',null);
                this.$electron.ipcRenderer.send('app-close')
            },
            //下载程序
            downloadProgram() {
                this.$electron.ipcRenderer.send("download-update");
                this.$set(this,'boxType',3);
            },
            //安装程序
            installProgram() {
                this.$electron.ipcRenderer.send("install-update");
            },
        },
    }
</script>
<style scoped lang="scss">
    .software-update {
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        position: fixed;
        top: 0;
        left:0;
        height:100%;
        width:100%;
        background: rgba(0,0,0,0.4);
        /*报错提示*/
        .error-max-box{
            position: absolute;
            top: 50%;
            left: 50%;
            margin: -100px -300px;
            width: 600px;
            height: 160px;
            border-radius: 4px;
            -webkit-box-shadow: 0 0 50px rgba(0, 0, 0, 0.4);
            background: #fff;
            .install-title {
                font-size: 16px;
                text-align: center;
                line-height: 20px;
                margin: 40px 0;
                color: #FF6666;
            }
            .button-box {
                display: flex;
                .null-p {
                    flex: 1;
                }
                .out-p {
                    width: 100px;
                    height: 30px;
                    line-height: 30px;
                    color: #333;
                    background: #dedede;
                    text-align: center;
                    border-radius: 4px;
                    font-size: 14px;
                    cursor: pointer;
                    margin-right: 40px;
                }
                .confirm-p {
                    width: 100px;
                    height: 30px;
                    line-height: 30px;
                    color: #ffffff;
                    background: #409EFF;
                    text-align: center;
                    border-radius: 4px;
                    font-size: 14px;
                    cursor: pointer;
                }
            }
        }
        /*更新提示*/
        .reminder-max-box{
            position: absolute;
            top: 50%;
            left: 50%;
            margin: -100px -300px;
            width: 600px;
            height: 160px;
            border-radius: 4px;
            -webkit-box-shadow: 0 0 50px rgba(0, 0, 0, 0.4);
            background: #fff;
            .install-title {
                font-size: 16px;
                text-align: center;
                line-height: 20px;
                margin: 40px 0;
                color: #409EFF;
            }
            .button-box {
                display: flex;
                .null-p {
                    flex: 1;
                }
                .out-p {
                    width: 100px;
                    height: 30px;
                    line-height: 30px;
                    color: #333;
                    background: #dedede;
                    text-align: center;
                    border-radius: 4px;
                    font-size: 14px;
                    cursor: pointer;
                    margin-right: 40px;
                }
                .confirm-p {
                    width: 100px;
                    height: 30px;
                    line-height: 30px;
                    color: #ffffff;
                    background: #409EFF;
                    text-align: center;
                    border-radius: 4px;
                    font-size: 14px;
                    cursor: pointer;
                }
            }
        }
        /*更新进度条*/
        .renewal-max-box {
            position: absolute;
            top: 50%;
            left: 50%;
            margin: -100px -300px;
            width: 600px;
            height: 200px;
            border-radius: 4px;
            -webkit-box-shadow: 0 0 50px rgba(0, 0, 0, 0.4);
            background: #fff;
            .renewal-title {
                font-size: 16px;
                text-align: center;
                line-height: 20px;
                margin: 40px 0;
                color: #409EFF;
            }
            .renewal-progress-bar {
                margin: 0 40px;
                border: 2px solid #409EFF;
                padding: 2px;
                .progress-bar {
                    height: 20px;
                    background: #409EFF;
                }
            }
            .renewal-text {
                text-align: center;
                margin: 20px;
                color: #409EFF;
            }
        }
        /*安装提示*/
        .install-max-box {
            position: absolute;
            top: 50%;
            left: 50%;
            margin: -100px -300px;
            width: 600px;
            height: 160px;
            border-radius: 4px;
            -webkit-box-shadow: 0 0 50px rgba(0, 0, 0, 0.4);
            background: #fff;
            .install-title {
                font-size: 16px;
                text-align: center;
                line-height: 20px;
                margin: 40px 0;
                color: #409EFF;
            }
            .button-box {
                display: flex;
                .null-p {
                    flex: 1;
                }
                .out-p {
                    width: 100px;
                    height: 30px;
                    line-height: 30px;
                    color: #333;
                    background: #dedede;
                    text-align: center;
                    border-radius: 4px;
                    font-size: 14px;
                    cursor: pointer;
                    margin-right: 40px;
                }
                .confirm-p {
                    width: 100px;
                    height: 30px;
                    line-height: 30px;
                    color: #ffffff;
                    background: #409EFF;
                    text-align: center;
                    border-radius: 4px;
                    font-size: 14px;
                    cursor: pointer;
                }
            }
        }
    }
</style>
