<template>
	<view></view>
</template>

<script>
export default {
	data() {
		return {
			/*需要返回的图片*/
			imageList: []
		};
	},
	onLoad() {},
	props: ['num', 'storeType', 'fileType', 'isVideo'],
	mounted() {
		this.chooseImageFunc();
	},
	methods: {
		/*打开相机或者相册，选择图片*/
		chooseImageFunc() {
			console.log(this.isVideo)
			let self = this;
			if (this.isVideo) {
				uni.chooseVideo({
					// 来源相册或者拍摄
					maxDuration: 60,
					// 设置最长时间60s
					camera: 'back',
					// 后置摄像头
					success: res => {
						console.log(res);
						if (res) {
							let arr = [];
							arr.push(res.tempFilePath)
							console.log(arr)
							self.uploadFile(arr);
						}
					},
					fail(err) {
						console.log(err)
						self.$emit('getImgs', null);
					}
				});
			} else {
				uni.chooseImage({
					count: self.$props.num || 9, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: function(res) {
						self.uploadFile(res.tempFilePaths);
					},
					fail: function(res) {
						self.$emit('getImgs', null);
					},
					complete: function(res) {}
				});
			}
		},

		/*上传图片*/
		uploadFile: function(tempList) {
			let self = this;
			let i = 0;
			let img_length = tempList.length;
			uni.showLoading({
				title: self.$t('PER_DA_47')
			});
			console.log(tempList)
			tempList.forEach(function(filePath, fileKey) {
				console.log(filePath)
				uni.uploadFile({
					filePath: filePath,
					name: 'file',
					url: self.websiteUrl() + '/inter-api/member/api/maker-space/update-file?storeType=' + self.storeType + '&fileType=' + self.fileType,
					header: {
						Authorization: uni.getStorageSync('Admin-Token') || '',
						'Accept-Language': self.getLanguage()
					},
					success: function(res) {
						console.log(res)
						let result = typeof res.data === 'object' ? res.data : JSON.parse(res.data);
						if (result.code == 200) {
							self.imageList.push(result.data);
						} else {
							self.showError(result.msg);
						}
					},
					complete: function() {
						i++;
						console.log(i);
						console.log(img_length);
						if (img_length === i) {
							uni.hideLoading();
							// 所有文件上传完成
							self.$emit('getImgs', self.imageList);
						}
					}
				});
			});
		}
	}
};
</script>

<style></style>
