<template>
	<view class='set-page'>
		<view class='set-main'>
			<view class='main-line'>
				<view class='line-item' @click='getProtol'>
					<view class='item-text'>相关协议</view>
					<view class='item-icon'></view>
				</view>
				<view class='line-item' @click='getProtol'>
					<view class='item-text'>隐私协议</view>
					<view class='item-icon'></view>
				</view>
				<view class='line-item' @click='cancelSize'>
					<view class='item-text'>清除缓存</view>
					<view class='item-r'>
						<view class='r-text'>{{cacheSize}}</view>
						<!-- <i class='r-icon'></i> -->
					</view>
				</view>
				<view class='line-item'>
					<view class='item-text'>当前版本</view>
					<view class='item-version'>{{versionNumber}}</view>
				</view>
			</view>
			<view  v-show="token!=''">
				<view class='line-des'>
					如果您今后不打算使用APP，您可以选择
					<view class='des-x' @click='goCancel'>注销账号</view>
				</view>
				<view  class='line-tips' >注：该操作不可恢复</view>
			</view>
			
		</view>
		<view  v-show="token!=''" class='set-footer' @click='goOut'>退出登录</view>
		<!-- 注销 -->
		<view :hidden="userFeedbackHidden" class="popup_content">
					<view class="popup_title">提示</view>
					<view class='popup_main'>
						您一但确认注销，您所有信息都会被清除，您确认注销账号？
					</view>
					<view class='btn-main'>
						<view class='btn-confirm' @click='cancelAccount()'>
							确认
						</view>
						<view class='btn-cel' @click="hideDiv()" >
							取消			 
						</view>
					</view>
		</view>
		<view class="popup_overlay" :hidden="userFeedbackHidden" ></view>
		<!-- 查看账单 -->
		<view :hidden="userHidden" class="popup_content">
					<view class="popup_title">提示</view>
					<view class='popup_main'>
						您当前存在未结清账单，不支持注销账号
					</view>
					<view class='btn-main'>
						<view class='btn-confirm' @click='goBack()'>
							返回
						</view>
						<view class='btn-cel' @click="goBill()" >
							查看账单
						</view>
					</view>
		</view>
		<view class="popup_overlay" :hidden="userHidden" ></view>
		<!-- 退出 -->
		<view :hidden="outHidden" class="popup_content">
					<view class="popup_title">提示</view>
					<view class='popup_main'>
						您确定退出登录吗？
					</view>
					<view class='btn-main'>
						<view class='btn-confirm' @click='loginOut()'>
							确认
						</view>
						<view class='btn-cel' @click="loginCel()" >
							取消
						</view>
					</view>
		</view>
		<view class="popup_overlay" :hidden="outHidden" ></view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				userFeedbackHidden:true,//弹窗
				userHidden:true,
				outHidden:true,
				protocolList:[],
				versionNumber:'',
				cacheSize:'',
				token:''
			}
		},
		onLoad() {
			let self = this;
			self.init()
			self.token=uni.getStorageSync('token')
			plus.cache.calculate(size => {
				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';
				}
				console.log(self.cacheSize,'size')
			});
			plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo){
					if(wgtinfo.version){
						self.versionNumber = wgtinfo.version;
					}else{
						self.versionNumber = plus.runtime.version;
					}
					console.log(self.versionNumber,'version')
			});
		},
		methods: {
			init(){
				const self=this
				self.$request('/app/v1/settings','GET').then(res => {
					console.log(res,'settings');
					const { protocolList }=res.result
					self.protocolList=protocolList
				}).catch(err=>{
				  console.log(err,'请求出错')
				})
			},
			cancelSize(){
								
				let self = this;
				plus.cache.clear(res=>{
						uni.showToast({
							title: '清除成功！',
							icon: 'none',
							success() {
								self.cacheSize = '0B';
							}
						});
				})
			},
			getProtol(){
				uni.navigateTo({
					url:'/pages/prolist/prolist?mydata='+encodeURIComponent(JSON.stringify(this.protocolList))
				})
			},
			goCancel(){
				
				this.userFeedbackHidden=false	
			},
			goOut(){
				// this.userHidden=false
				this.outHidden=false
			},
			cancelAccount(){
				//注销账号
				const self=this
				self.$request('/app/v1/close','POST').then(res => {
					console.log(res,'myinit');
					const { hasBills}=res.result
					if(hasBills==true){
						self.userFeedbackHidden=true
						self.userHidden=false
					}else{
						uni.removeStorageSync('token')
						uni.redirectTo({
							url:'/pages/index/index'
						})
					}
					
					
				})
			},
			loginOut(){
				//退出登录
				const self=this
				self.$request('/app/v1/logout','POST').then(res => {
					console.log(res,'logout');
					self.outHidden=false
					uni.removeStorageSync('token')	
					uni.switchTab({
						url:'/pages/my/index'
					})		
			
				})
			},
			goBack(){
				this.userHidden=false
				 uni.navigateBack({
				    delta: 1
				})
			},
			goBill(){
				this.userHidden=false
				uni.switchTab({
					url:'/pages/bill/index'
				})	
			},
			loginCel(){
				this.outHidden=true
			},
			hideDiv(){
				//隐藏弹窗
				this.userFeedbackHidden=true
				this.userHidden=true
				this.outHidden=true
			},
		},
		onShow() {
			// this.init()
			// this.token=uni.getStorageSync('token')
		}
	}
</script>

<style src='./index.less' lang="less" scoped></style>
