<template>
	<view>
		<status-nav navBarTitle="设置" returnColor="#c2c2c2"></status-nav>
		<container-subgroup>
			<view slot="content" style="margin: 0 -30rpx;" class="fon28">
				<view class="bacf pad30 disjbac bbot">
					<view>开启语音提醒</view>
					<view><evan-switch v-model="voiceStatus" @change="voiceNewsEv($event,1)" :size="18" inactive-color="#b3b3b3" active-color="#fd8956"></evan-switch></view>
				</view>
				<view class="bacf pad30 disjbac mar-x20">
					<view>接受新消息通知</view>
					<view><evan-switch v-model="newsStatus" @change="voiceNewsEv" :size="18" inactive-color="#b3b3b3" active-color="#fd8956"></evan-switch></view>
				</view>
				<view @tap="updatePassword" class="bacf pad30 disjbac bbot">
					<view>修改登录密码</view>
					<i class="icon icon-next col9" style="font-size: 26rpx;"></i>
				</view>
				<!-- #ifdef APP-PLUS -->
					<view @tap="cleanEv" class="bacf pad30 disjbac">
						<view>清除缓存</view>
						<view class="col9">{{cacheSize}}</view>
					</view>
				<!-- #endif -->
				<view @tap="gopageEv(0)" class="bacf pad30 disjbac mar-s20 bbot">
					<view>上门服务条款</view>
					<view class="disac col9" v-if="agreementArr.length">
						{{agreementArr[0].version}}<i class="icon icon-next col9 mar-z10" style="font-size: 26rpx;"></i>
					</view>
				</view>
				<view @tap="gopageEv(1)" class="bacf pad30 disjbac bbot">
					<view>用户服务协议</view>
					<view class="disac col9" v-if="agreementArr.length">
						{{agreementArr[1].version}}<i class="icon icon-next col9 mar-z10" style="font-size: 26rpx;"></i>
					</view>
				</view>
				<view @tap="gopageEv(2)" class="bacf pad30 disjbac bbot">
					<view>飞猴云服务隐私政策</view>
					<view class="disac col9" v-if="agreementArr.length">
						{{agreementArr[2].version}}<i class="icon icon-next col9 mar-z10" style="font-size: 26rpx;"></i>
					</view>
				</view>
				<view @tap="gopageEv(3)" class="bacf pad30 disjbac bbot">
					<view>技术服务合作协议</view>
					<view class="disac col9" v-if="agreementArr.length">
						{{agreementArr[3].version}}<i class="icon icon-next col9 mar-z10" style="font-size: 26rpx;"></i>
					</view>
				</view>
				<view @tap="gopageEv(4)" class="bacf pad30 disjbac bbot">
					<view>关于飞猴云服务</view>
					<view class="disac col9" v-if="agreementArr.length">
						{{agreementArr[4].version}}<i class="icon icon-next col9 mar-z10" style="font-size: 26rpx;"></i>
					</view>
				</view>
				<!-- #ifdef APP-PLUS -->
					<view @tap="checkEdition" class="bacf pad30 disjbac">
						<view>版本更新</view>
						<view class="col9">当前版本 5.1.4</view>
					</view>
				<!-- #endif -->
			</view>
		</container-subgroup>
	</view>
</template>

<script>
	import evanSwitch from '@/components/evan-switch/evan-switch.vue';
	export default {
		components:{
			evanSwitch
		},
		data() {
			return {
				voiceStatus:false,//是否开启语音提醒
				newsStatus:false,//是否开启接受新消息通知
				cacheSize:'754.72 KB',//缓存数据大小
				agreementArr:[],//协议对象
			}
		},
		onLoad() {
			// 调用获取缓存数据的大小
			this.getStorageDataNum();
			// 调用获取设置信息
			this.getSettings();
		},
		methods: {
			// 获取缓存数据的大小
			getStorageDataNum(){
				// #ifdef APP-PLUS
				var self = this;
				plus.cache.calculate(function(size) { //size是多少个字节单位是b
				    //做下面相应的处理
				    if (size < 1024) {
				        self.cacheSize = size + ' B';
				    } else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
				        self.cacheSize = Math.floor(size / 1024 * 100) / 100 + ' KB';
				    } else if (size / 1024 / 1024 >= 1) {
				        self.cacheSize = Math.floor(size / 1024 / 1024 * 100) / 100 + ' M';
				    }
				});
				// #endif
			},
			// 语音提醒事件\新消息通知事件
			voiceNewsEv(status,num=0){
				num ? this.voiceStatus = status : this.newsStatus = status;
				let params = {
					voice_reminder:this.voiceStatus?1:0,
					receive_message:this.newsStatus?1:0
				}
				this.$requst.post('/universal/api.settings/settings',params).then(res=>{
					if(res.code){
						// uni.navigateBack({delta:1})
					}
				})
			},
			// 修改登录密码
			updatePassword(){
				uni.navigateTo({
					url:'/pages/forget/forget'
				})
			},
            // 获取设置信息
            getSettings(){
				let params = {
					voice_reminder:'',
					receive_message:''
				}
                this.$requst.get('/universal/api.settings/settings',params).then(res=>{
                	if(res.code){
						this.voiceStatus = [false,true][res.data.voice_reminder];
						this.newsStatus = [false,true][res.data.receive_message];
						for (let key in res.data) {
							if(res.data[key]!=1&&res.data[key]!=0){
								let obj = {
									...res.data[key],
									title:key
								}
								this.agreementArr.push(obj)
							}
						}
						console.log(this.agreementArr,77777);
                		// uni.navigateBack({delta:1})
                	}
                })
            },
			// 清除缓存事件
			cleanEv(){
				console.log('清除缓存事件');
				// #ifdef APP-PLUS
				var self = this;
				//使用plus.cache.clear 清除应用中的缓存数据
				plus.cache.clear(function() {
					uni.showToast({
						title: '清除成功!',
						icon: 'none',
						success() {
							self.cacheSize = '0B'
						}
					})
				});
				// #endif
			},
			// 检测版本事件
			checkEdition(){
				// status	int	 升级标志，1：需要升级；0：无需升级  
				// note	string	 升级日志  
				// android_url	string	 安卓包下载地址  
				// ios_url	string	 苹果更新商店地址  
				// 检测升级
				plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
					var platform = uni.getSystemInfoSync().platform;
					this.$requst.get('/universal/api.other/update_version',{version:widgetInfo.version}).then(res=>{
						if(res.code) {
							// 判断是否需要更新
							if (res.data.status) {
								//content是版本更新内容 \n换行
								uni.showModal({
									title:`检测到有新版本`,
									content:res.data.note,
									confirmText:'更新',
									cancelText:'取消',
									success: (result) => {
										if(result.confirm) {
											//ios用户pkgUrl是苹果市场App地址，android自定义下载地址
											if(platform == 'ios'){
												// plus.runtime.launchApplication({
												// 	action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
												// }, function(e) {
												// 	console.log('Open system default browser failed: ' + e.message);
												// });
												plus.runtime.openURL(res.data.ios_url);
											}else{
												var downloadTask = uni.downloadFile({
													url: res.data.android_url,
													success: (downloadResult) => {
														if (downloadResult.statusCode === 200) {
															plus.runtime.install(downloadResult.tempFilePath, {force: false }, function() {
																plus.runtime.restart();
															}, (e) => {
																uni.showToast({
																	title: '安装升级包失败',
																	icon: 'none'
																})
															});
														}
													}
												});
												downloadTask.onProgressUpdate((e)=>{
													// 监听进度条
													// this.progress = e.progress
												})
											}
										}
									}
								})
							} 
						}
					})
				});
			},
			// 去服务协议、服务条款、隐私政策、合作协议、飞猴云服务页面
			gopageEv(index) {
				uni.navigateTo({
					url:`/pages/login/agreement?title=${this.agreementArr[index].title}&id=${this.agreementArr[index].id}`
				})
			}
		}
	}
</script>

<style>

</style>
