<template>
	<view>
		<view class="form-item">
			<view class="label">上传头像</view>
		</view>
		<view class="upload-box">
			<!-- <button v-if="mp_is_new" class="avatar-box" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
				<u-avatar size="180" :src="avatarUrl"></u-avatar>
				<view class="icon">
					<u-icon name="camera" color="#fff" size="30"></u-icon>
				</view>
			</button> -->
			
			<view class="avatar-box" @click="chooseImage">
				<u-avatar size="180" :src="avatarUrl"></u-avatar>
				<view class="icon">
					<u-icon name="camera" color="#fff" size="30"></u-icon>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import FileApi from '@/sheep/api/infra/file';
	export default {
		components: {
			
		},
		props: {
			modelValue: {
			  type: String,
			  default: ''
			},
		},
		data() {
			return {
				mp_is_new: false,
			}
		},
		created() {
			// #ifdef MP-WEIXIN
			const version = uni.getSystemInfoSync().SDKVersion;
			if(this.compareVersion(version, '2.21.2') >= 0){
				this.mp_is_new = true; 
			} 
			// #endif
		},
		computed: {
			avatarUrl() {
				return this.modelValue;
			},
		},
		watch: {
			
		},
		methods: {
			/**
			 * 小程序比较版本信息
			 * @param v1 当前版本
			 * @param v2 进行比较的版本 
			 * @return boolen
			 * 
			 */
			compareVersion(v1, v2) {
				v1 = v1.split('.')
				v2 = v2.split('.')
				const len = Math.max(v1.length, v2.length)
			
				while (v1.length < len) {
					v1.push('0')
				}
				while (v2.length < len) {
					v2.push('0')
				}
			
				for (let i = 0; i < len; i++) {
					const num1 = parseInt(v1[i])
					const num2 = parseInt(v2[i])
			
					if (num1 > num2) {
						return 1
					} else if (num1 < num2) {
						return -1
					}
				}
				return 0
			},
			//选照片 or 拍照
			chooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sourceType: ['album', 'camera'],
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					success: (res) => {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							uni.getImageInfo({
								src: res.tempFilePaths[i],
								success: (image) => {
									this.uploadImage(image.path);
								}
							});
						}
					}
				});
			},
			uploadImage(path) {
				FileApi.uploadFile(path).then((res) => {
					this.$emit('update:modelValue', res.data); 
				});
			},
			// 微信头像获取
			onChooseAvatar(e) {
				const {
					avatarUrl
				} = e.detail
				this.uploadImage(avatarUrl);
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.form-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 15px;
		
		.label {
			font-size: 30rpx;
			min-width: 200rpx;
		}
	}
	
	.upload-box {
		padding: 15px;
		display: flex;
		justify-content: center;
		align-items: center;
		padding-top: 0;
		
		.avatar-box {
			display: flex;
			justify-content: center;
			align-items: center;
			position: relative;
			
			.icon {
				position: absolute;
				right: 0;
				bottom: 5px;
				background-color: var(--ui-BG-Main);
				width: 50rpx;
				height: 50rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 100%;
			}
		}
		
		button{
			padding: unset;
			margin: unset;
			border: unset;
			position: relative;
			line-height: unset;
			background-color: unset;
			font-size: unset;
			color: unset;
			border-radius: unset;
			text-align: unset;
			text-decoration: unset;
			display: unset;
			overflow: unset;
		}
		button::after{ 
			border: none; 
		}
	}
</style>