import {getVersion} from "../api";

const updateVersion = {
    data() {
        return {
            updateVersion: {
                modalShow: false,
                showConfirmButton: false,
                showCancelButton: false,
                confirmText: '',
                cancelText: '',
                modalContent: '',
                confirmType: '',
                newVersionUrl: '',
                downloadCompleteTemporaryPath: '',
            }
        }
    }, methods: {
        async fnUpdateVersion(toast) {
            // #ifdef APP-PLUS
            let resData = await getVersion({FILETYPE:plus.os.name})
            if (resData.pd) {
                if (plus.runtime.version != resData.pd.VERSION) {
                    this.updateVersion.newVersionUrl = resData.pd.FILEURL
                    uni.getNetworkType({
                        success: ({
                                      networkType
                                  }) => {
                            if (networkType === 'wifi') {
                                this.updateVersion.modalContent = '发现新版本，是否更新？为了更好的体验，请更新到最新版本。'
                            } else {
                                this.updateVersion.modalContent = '发现新版本，检查到您当前使用的是移动网络，是否更新？更新时请注意流量消耗。为了更好的体验，请更新到最新版本。'
                            }
                            this.updateVersion.confirmType = 'download'
                            this.updateVersion.confirmText = '立即更新'
                            // this.updateVersion.cancelText = '稍后更新'
                            this.updateVersion.showConfirmButton = true;
                            // this.updateVersion.showCancelButton = true;
                            this.updateVersion.modalShow = true;
                            if (resData.pd.ISUPDATE === '1') {
                            } else {
                                this.updateVersion.cancelText = '稍后更新'
                                this.updateVersion.showCancelButton = true;
                            }
                            uni.hideTabBar({
                                animation: true
                            })
                        }
                    })
                } else {
                    if (toast) {
                        uni.showToast({
                            title: '当前已是最新版本，当前版本号：' + plus.runtime.version, icon: 'none'
                        })
                    }
                }
            }
            /*else {
                if(toast !== false){
                    uni.showToast({
                        title: '当前已是最新版本，当前版本号：' + plus.runtime.version,
                        icon: 'none'
                    })
                }
            }*/

            // #endif
        }, modalConfirm() {
            // #ifdef APP-PLUS
            console.log(plus.os.name)
            if (plus.os.name != 'iOS') {
                this.updateVersion.confirmType === 'download' ? this.downloadNewVersion() : this.installNewVersion()
            } else {
                let appleId = 6739233192
                plus.runtime.launchApplication({
                    action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
                }, function (e) {
                    console.log('跳转商城失败 ' + e.message);
                })
            }
            // #endif
        }, modalCancel() {
            this.updateVersion.modalShow = false
            uni.showTabBar({
                animation: true
            })
        }, downloadNewVersion() {
            this.updateVersion.showConfirmButton = false;
            this.updateVersion.showCancelButton = false;
            this.updateVersion.modalContent = '正在下载新版本，请稍后...'
            const downloadTask = uni.downloadFile({
                url: this.updateVersion.newVersionUrl, success: (res) => {
                    this.updateVersion.downloadCompleteTemporaryPath = res.tempFilePath
                }
            });
            downloadTask.onProgressUpdate((res) => {
                this.updateVersion.modalContent = `正在下载新版本，请稍后...<br>已经下载${res.progress}%`
                if (res.progress === 100) {
                    this.updateVersion.showConfirmButton = true;
                    this.updateVersion.confirmType = 'install'
                    this.updateVersion.confirmText = '立即安装'
                    this.updateVersion.modalContent = '下载成功，点击立即安装进行安装。'
                }
            });
        }, installNewVersion() {
            plus.runtime.install(this.updateVersion.downloadCompleteTemporaryPath, {
                force: true
            }, function (res) {
                uni.showTabBar({
                    animation: true
                })
                plus.runtime.restart();
            });
        },
    }
}
export default updateVersion
