<template>
	<view class="box">
		<cu-custom bgColor="bg-blue" :isBack="true">
			<block slot="backText">系统设置</block>
			<block slot="content"></block>
			<block slot="right">
				<view class=" flex justify-center">
				 <!-- 	<button  class="margin-top-ssm cu-btn round bg-blue text-blue"><text class="cuIcon-add"></text></button> -->
				</view>
			</block>
		</cu-custom>	
		<view class="">
			<view class="cu-form-group_1">
				<view class="title">软件版本</view>
				<input placeholder="软件版本" placeholder-style="color:#AEAEAE;font-size:30rpx" disabled="true" v-model="widgetInfo.version"
				 name="input"></input>
			</view>
			<!-- #ifdef APP-PLUS -->
			<view class="cu-list menu">
				<!-- 菜单-->
				<view class="cu-item arrow" >
				    <view class="content" @click="showModel">
				     	<text class="text-black ">新版本检测</text>
						<!-- <view v-if="tabberData.newVersion" class="cu-tag badge bg-red" style="top:8rpx;left: 2rpx;">
							
						</view> -->
				    </view>
					<text style="color: red;" v-if="tabberData.newVersion">有新版</text>
				</view>
			</view>
			
			<!-- <view>
				cid:<input v-model="cid" />
			</view> -->
			
			<uni-popup ref="popup" type="center">
				<view class="cu-dialog">
					<view class="cu-bar bg-white justify-end">
						<view class="content">
							发现新版本
							<text class="text-sm ">(v{{versionInfo.version}})</text>
						</view>
						<view class="action" @click="hideModel">
							<text class="cuIcon-close text-red"></text>
						</view>
					</view>
					<view class="padding-xl bg-white" style="white-space: pre-line;text-align: left;">
						{{versionInfo.remark}}
					</view>
					<view class="cu-bar bg-white  text-center">
						<view class=" margin-0 flex-sub" @click="hideModel">下次再说</view>
						<view class=" margin-0 flex-sub  solid-left text-blue" @click="toUpdate">立即更新</view>
					</view>
				</view>
			</uni-popup>
			
			<!-- #endif -->
			
		</view>
	</view>
</template>

<script>
	import config from "../../../core/api.js"
	export default {
		data() {
			return {
				widgetInfo:{},
				versionInfo:{
					version:'',
					remark:'',
				},
				cid:'',
			}
		},
		onLoad() {
			// #ifdef APP-PLUS
				plus.runtime.getProperty(plus.runtime.appid,(widgetInfo)=>{
				    //console.log(widgetInfo)
					this.widgetInfo = widgetInfo;
					
				})
				
				// console.log("异步获取客户端推送标识信息");
				// plus.push.getClientInfoAsync((info)=>{
				//   console.log('Success');
				//   console.log(JSON.stringify(info));
				//   this.cid = info.clientid;
				// }, function(e){
				//   console.log('Failed');
				//   console.log(JSON.stringify(e));
				// })
				
			// #endif
		},
		methods: {
			toUpdate(){
				// #ifdef APP-PLUS
					let that = this;
					//console.log(plus.os.name)
					this.http.request("/app/appversion/update",{name:this.widgetInfo.name,version:this.widgetInfo.version,platform:plus.os.name}).then((res)=>{		
						if(res.update){
							//检查是否有新版本
							if(res.wgtUrl){
								//console.log(config.api)
								let path ='';
								if(res.wgtUrl.indexOf('http://') > -1 || res.wgtUrl.indexOf('https://') > -1){
									path = res.wgtUrl;
								}else{
									path = res.domain + '/' + res.wgtUrl;
								}
								console.log(path)
								//资源升级包
								uni.showLoading({
									title: '下载更新中'
								});
								uni.downloadFile({
									url: path,  
									success: (downloadResult) => {
										if (downloadResult.statusCode === 200) {
											plus.runtime.install(downloadResult.tempFilePath, {  
												force: false  
											}, function() {  
												uni.hideLoading();
												console.log('install success...');
												//that.$prompt.none('更新成功，软件重启');
												plus.runtime.restart();  
											}, function(e) {
												uni.hideLoading();
												that.$prompt.none('更新失败');
												//console.error('install fail...');  
											});  
										}else{
											uni.hideLoading();
											that.$prompt.none('资源包状态错误');
										}
									},
									fail:()=>{
										uni.hideLoading();
										that.$prompt.none('下载失败');
									}
								});  
							}else if(res.pkgUrl){
								let path ='';
								if(res.pkgUrl.indexOf('http://') > -1 ||res.pkgUrl.indexOf('https://') > -1 
									|| res.pkgUrl.indexOf('itms-apps://') > -1){
									path = res.pkgUrl;
								}else{
									path = config.api + "/app/appversion/download?url=" + res.pkgUrl;
								}
								//console.log(path)
								//整包更新
								if(plus.os.name=="Android"){
									//安卓下载更新包更新
									plus.runtime.openURL(path);
								}else if(plus.os.name=="iOS"){
									//iOS
									//this.$prompt.none('请前往App Store进行更新');
									plus.runtime.openURL(path)
									//plus.runtime.openURL(res.pkgUrl);
								}else{
									this.$prompt.none('暂无对应版本的更新包');
								}
							}
						}else{
							this.$prompt.none('已经是最新版本');
						}
					});
				// #endif
			},
			showModel(){
				// #ifdef APP-PLUS
					this.http.request("/app/appversion/update",{name:this.widgetInfo.name,version:this.widgetInfo.version,platform:plus.os.name}).then((res)=>{
						if(res.update){
							//console.log(res)
							this.versionInfo = res;
							this.$refs.popup.open();
						}else{
							this.$prompt.none('已经是最新版本');
						}
					});
				// #endif
			},
			hideModel(){
				//console.log('hide')
				this.$refs.popup.close();
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	.cont {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-end;
	}

	.login-img {
		width: 60rpx;
		height: 60rpx;
	}

	.text-black {
		color: #101010;
	}
	.cu-form-group_top{background-color: #fff;display: flex;align-items: center;justify-content: space-between;padding: 30rpx 24rpx;}
	.cu-form-group_top_right{display: flex;align-items: center;}
	.cu-form-group_1 {
		display: flex;
		align-items: center;
		padding: 0 24rpx;
		border-bottom: 2rpx solid #E3E1E1;
		height: 88rpx;
		background-color: #fff;
	}

	.cu-form-group_1 .title {
		width: 148rpx;
		font-size: 30rpx;
		color: #101010;
	}

	.cu-form-group_1 input {
		width: 554rpx;
		color: #8a8a8a;
		font-size: 30rpx;
	}

	.cu-form-group_2 {
		display: flex;
		background-color: #fff;
		padding: 22rpx 24rpx;
	}

	.cu-form-group_2 .title {
		width: 148rpx;
		font-size: 30rpx;
		color: #101010;
	}

	.cu-form-group_2 textarea {
		width: 554rpx;
		color: #8a8a8a;
		font-size: 30rpx;
		height: 130rpx;
	}
	
	.cu-list.menu>.cu-item.arrow{
		padding-right: 70rpx;
	}
</style>

