<template>
	<view class="setting">
		<view class="header-bg"></view>
		<view class="block b1">
			<view class="row portrait">
				<image class="image" src="../../static/images/login/logo.png" mode="scaleToFill"></image>
			</view>
			<view class="row">
				<view class="label">
					昵称 <text class="info">{{nickname}}</text>
				</view>
				<view class="option">
					修改
				</view>
			</view>
		</view>

		<view class="block b2">
			<view class="row">
				<view class="label">支付宝绑定</view>
				<view class="option">未绑定</view>
			</view>
			<view class="line"></view>
			<view class="row">
				<view>微信绑定</view>
				<view class="option">未绑定</view>
			</view>
		</view>

		<view class="block b3">
			<view class="row">
				<view class="label">修改手机号 <text class="info">{{mobile}}</text></view>
				<view class="option">已绑定</view>
			</view>
			<view class="line"></view>
			<view class="row">
				<view class="label">修改密码</view>
				<view class="option"></view>
			</view>
		</view>

		<view class="block b4">
			<view class="row">
				<view class="label">清除缓存 <text class="info">{{fileSizeString}}</text></view>
				<view class="option"><button class="clear-cache-btn" type="default" @tap="clearCache()">清除缓存</button></view>
			</view>
		</view>

		<view class="logout-block">
			<button class="logout-btn" type="default" @tap="logout()">退出登录</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	export default {
		data() {
			return {
				fileSizeString: "计算中..."
			}
		},
		computed: mapState({
			nickname: state => state.user.nickname,
			mobile: state => state.user.mobile
		}),
		onLoad() {
			const info = uni.getStorageInfoSync();
			console.log(info)
			this.formatSize();
		},
		methods: {
			logout() {
				uni.navigateBack({
					delta: 999,
					animationType: 'none'
				})
				uni.navigateTo({
					url: '/pages/user/login'
				})
				this.$store.dispatch("user/logout")
			},
			formatSize() {
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.fileSizeString = "0B";
					} else if (sizeCache < 1024) {
						that.fileSizeString = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						that.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				});
			},
			clearCache() {
				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
									uni.showToast({
										icon:'none',
										title: '缓存清理完成',
										duration: 2000
									});
									that.formatSize(); // 重新计算缓存  
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
				} else { // ios暂时未找到清理缓存的方法，以下是官方提供的方法，但是无效，会报错  
					plus.cache.clear(function() {
						uni.showToast({
							icon:'none',
							title: '缓存清理完成',
							duration: 2000
						});
						that.formatSize();
					});
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.setting {
		overflow: hidden;
		position: relative;

	}

	.header-bg {
		background-color: $primary-color;
		height: 120upx;
		position: absolute;
		top: 0;
		width: 100%;
		z-index: 1;
	}

	.portrait {
		justify-content: center !important;
		margin-top: 20upx;

		.image {
			width: 168upx;
			height: 168upx;
			border: 10upx solid $primary-color;
			border-radius: 100upx;
		}
	}

	.block {
		border-radius: 20upx;
		background-color: white;
		margin: 20upx;
		position: relative;
		overflow: hidden;
		z-index: 2;

		.row {
			display: flex;
			justify-content: space-between;
			padding: 20upx 40upx;
			align-items: center;
			font-size: 28upx;

			.info {
				color: #999999;
				font-size: 24upx;
				display: inline-block;
				margin-left: 20upx;
			}

			.option {
				color: #999999;
			}
		}
	}

	.logout-block {
		margin: 20upx;
	}

	.clear-cache-btn {
		line-height: normal;
		padding: 4upx 20upx;
		background: white;
		border: 2upx solid $primary-color;
		font-size: 24upx;
		border-radius: 50upx;
		color: $primary-color;
	}

	.logout-btn {
		background: $primary-color;
		color: white;
		font-size: 30upx;
		line-height: 80upx;
	}

	.line {
		border: 2upx solid #EEEEEE;
		transform: scale(0.9, 0.5);
	}
</style>
