<template>
	<uni-file-picker ref="filePicker" return-type="array" :limit="1" v-model="fileList" fileMediatype="image" mode="grid" :auto-upload="false" @select="select"
		@progress="progress" @success="success" @fail="fail"></uni-file-picker>
</template>

<script>
	import config from '@/until/config.js';
	export default {
		data() {
			return {
				httpUrl: null,
				fileList: []
			}
		},
		watch:{
			fileList(val){
				this.$emit('changeImageList',this.fileList)
			}
		},
		onLoad() {
			this.httpUrl = config.BASE_URL
		},
		methods: {
			// 选择文件回调
			select(e) {
				this.tempFiles = e.tempFiles // 临时存储选择的文件
				if (!this.tempFiles || this.tempFiles.length === 0) {
					uni.showToast({
						title: '请先选择文件',
						icon: 'none'
					})
					return
				}
				// 遍历所有选择的文件
				this.tempFiles.forEach(file => {
					this.uploadFile(file)
				})
				// const platform = uni.getSystemInfoSync().platform;
				// if (platform === 'ios') {
				// this.tempFiles = e.tempFiles // 临时存储选择的文件
				// if (!this.tempFiles || this.tempFiles.length === 0) {
				// 	uni.showToast({
				// 		title: '请先选择文件',
				// 		icon: 'none'
				// 	})
				// 	return
				// }
				// // 遍历所有选择的文件
				// this.tempFiles.forEach(file => {
				// 	this.uploadFile(file)
				// })
				// } else if (platform === 'android') {
				// 	// alert(e)
				// 	// console.log('安卓android安卓android安卓android安卓android安卓android安卓android安卓android安卓android安卓android安卓android安卓android安卓android')
				// 	uni.chooseImage({
				// 	    success: (chooseImageRes) => {
				// 	        const tempFilePaths = chooseImageRes.tempFilePaths;
				// 			// alert(tempFilePaths[0])
				// 			// debugger
				// 			this.uploadFile(tempFilePaths[0])
				// 	    }
				// 	});		
				// }

			},
			uploadFile(file) {
				uni.uploadFile({
					url: `${this.httpUrl}/adminapi/UploadFile/file_upload`,
					filePath: file.path,
					name: 'files',
					success: (uploadRes) => {
						const {code,data} = JSON.parse(uploadRes.data)
						if(code) {
							this.fileList.push({
								tempFile: data,
								tempFilePath: data.url,
								res: JSON.parse(uploadRes.data)
							})
							// console.log(this.fileList)
							this.$emit('changeImageList',this.fileList)
						}

					}
				})
			},

			// 上传成功回调
			success(e) {
				console.log('上传成功', e)
			},

			// 上传失败回调
			fail(e) {
				console.log('上传失败', e)
			},

			// 上传进度回调
			progress(e) {
				console.log('上传进度', e)
			},
			clearImage(){
				this.fileList = []
			}
		}
	}
</script>

<style>
</style>