<template>
	<view>
		<cu-custom bgColor="bg-gradual-blueness" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">设置</block>
		</cu-custom>
		<view class="cu-list menu">
			<view class="cu-item arrow">
				<navigator class="content" hover-class="none" url="/pages/login/forget/forget-reset" open-type="redirect">
					<text class="text-semi">修改密码</text>
				</navigator>
			</view>
<!--			<view class="cu-item arrow">-->
<!--				<navigator class="content" hover-class="none" url="/pages/my/face/index" open-type="redirect">-->
<!--					<text class="text-semi">更新人脸信息</text>-->
<!--				</navigator>-->
<!--			</view>-->
<!--			<view class="cu-item arrow">-->
<!--				<navigator class="content" hover-class="none" url="/pages/my/updateSignature/index" open-type="redirect">-->
<!--					<text class="text-semi">更新签字信息</text>-->
<!--				</navigator>-->
<!--			</view>-->
			<view class="cu-item" @click="upVersionmanager()">
				<view class="content">
					<text class="text-semi" >检查更新</text>
				</view>
			</view>
			<view class="cu-item margin-top text-center" @click="unlogin()">
				<view class="content">
					<text class="text-semi">退出当前账户</text>
				</view>
			</view>
		</view>
    <uni-popup ref="popup" background-color="#fff" :is-mask-click="false">
      <view style="text-align: center;color:#606266;width: 500upx;padding: 40upx">
        <view style="margin-bottom: 40upx;">温馨提示</view>
        <rich-text :nodes="updateVersion.modalContent"></rich-text>
      </view>
      <view style="margin-top: 40upx;display: flex">
        <button v-if="updateVersion.showCancelButton" class="cu-btn bg-blue" style="flex: 1;border-radius: 0" @click="modalCancel">{{updateVersion.cancelText}}</button>
        <button v-if="updateVersion.showConfirmButton" class="cu-btn bg-green" style="flex: 1;border-radius: 0" @click="modalConfirm">{{updateVersion.confirmText}}</button>
      </view>
    </uni-popup>
	</view>
</template>

<script>
	import {
		basePath,loginUserId,setloginUserId,setCorpinfoId,setloginUser,setDeptId,version,baseImgPath,loginUser
	} from '@/common/tool.js';
	export default {
		data() {
			return {
				platform :'', //系统 android
				version :'',
				baseImgPath: baseImgPath,
        updateVersion:{
          showConfirmButton: false,
          showCancelButton: false,
          confirmText: '',
          cancelText: '',
          modalContent: '',
          confirmType: '',
          newVersionUrl: '',
          downloadCompleteTemporaryPath: '',
        }
			}
		},
		onLoad(e) {
			uni.getSystemInfo({
				success: (res) => {
					this.platform = res.platform
				}
			});
			if(!this.validStr(this.version)){
				this.getwgtVer()
			}
			// getwgtVer
		},
		methods: {
			unlogin(){
				var _this = this
				uni.showModal({
					title: '提示',
					cancelText: '确认',
					confirmText: '取消',
					content: '确定退出当前账户？',
					success: function (res) {
						if (res.cancel) {
							if (_this.validStr(loginUserId) && _this.validStr(_this.$store.state.socketTask)) {
								_this.$store.dispatch('WEBSOCKET_SEND', '[leave]' + 'mobile-'+loginUserId)
							}
							setloginUserId(null);
							setCorpinfoId(null);
							setDeptId(null);
							setloginUser(null);
							uni.removeStorage({key: 'USER'});
							uni.navigateTo({
								url: '/pages/login/home'
							});
              _this.updatePushCid();
						}
					}
				});
			},
			getwgtVer() {
				var that = this;
				// 获取本地应用资源版本号
				plus.runtime.getProperty(plus.runtime.appid, function (inf) {
					that.version = inf.version;  //获取当前版本号
					setVersion(that.version);
					console.info('inf.version')
					console.info(inf.version)
				});

			},
			upVersionmanager(){
				var _this = this

				uni.request({
					url: basePath + '/app/versionManagerFirst/getVersion',  //获取最新版本号
					method: 'POST',
					data: {FILETYPE:_this.platform},
					success: res => {
						var updateState = false
						var updateVersion = res.data.pd.VERSION.split('.')
						var phoneVersion = _this.version.split('.')
						if(updateVersion.length == phoneVersion.length){
							for (let i =0;i<updateVersion.length;i++){
								let upNub = Number(updateVersion[i])
								let phNub = Number(phoneVersion[i])
								if(upNub > phNub){
									updateState = true
								}
							}
						}else{
							updateState = true
						}
						if(updateState){
							if(_this.platform==="android"){
                this.updateVersion.newVersionUrl = this.baseImgPath + res.data.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.showConfirmButton = true;
                    this.$refs.popup.open()
                  }
                })
                if(res.data.pd.ISUPDATE =='1'){

                } else {
                  this.updateVersion.cancelText = '稍后更新'
                  this.updateVersion.showCancelButton = true;
                }
							}
						}else{
							uni.showToast({
								icon: 'none',
								title: '您已经时候最新版本！',
								duration: 1500
							});
						}
					},
					fail: res => {
						console.info(res)
					}
				});
			},
      modalConfirm() {
        this.updateVersion.confirmType === 'download' ? this.downloadNewVersion() : this.installNewVersion()
      },
      modalCancel() {
        this.$refs.popup.close()
      },
      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) {
              plus.runtime.restart();
            }
        );
      },
      updatePushCid(){
        uni.request({
          url: basePath + '/app/user/updatePushCid',
          method: 'POST',
          dataType: 'json',
          header: {
            'Content-type':'application/x-www-form-urlencoded'
          },
          data: {
            USER_ID:loginUser.USER_ID,
            PUSH_CID:""
          }
        })
      },
		}
	}
</script>

<style>

</style>
