<template>
	<u-upload :capture="['album', 'camera']" :multiple="true" :fileList="fileList"
		@afterRead="afterRead($event, 'fileList')" @delete="deletePic($event, 'fileList')" :maxCount="maxCount"
		:width="width" :height="height">
		<image :style="{'width': width + 'px', 'height': height + 'px'}" src="@/static/upload.png"></image>
	</u-upload>
</template>

<script>
	export default {
		props: {
			width: {
				type: String,
				default: '100'
			},
			height: {
				type: String,
				default: '100'
			},
			maxCount: {
				type: String,
				default: '9'
			},
			backUrl: { // 返回数组中哪个字段
				type: String,
				default: 'fullurl'
			},
			images: { // 重置fileList把images设为空即可
				type: String,
				default: ''
			}
		},
		data() {
			return {
				fileList: [],
				isManualUpdate: true
			}
		},
		model: {
			prop: 'images',
			event: 'upload'
		},
		watch: {
			// 只监听手动赋值，使用 deep 和 immediate
			// 只上传成功执行，给images赋值不执行
			images: {
				deep: true,
				immediate: true,
				handler(newVal, oldVal) {
					if (this.isManualUpdate) {
						this.fileList = [];
						// console.log(newVal)
						if (newVal) {
							const urlList = newVal.split(',');
							urlList.forEach((item) => {
								let url = this.removeUrl(item, this.$config.domain);
								const fullurl = url.indexOf('http') === -1 ? this.$config.domain + url : url;
								this.fileList.push({
									status: 'success',
									message: '',
									fullurl,
									url,
								});
							});
						}
						this.isManualUpdate = false;
					}
				},
			}
		},
		methods: {
			// 删除图片
			deletePic(event, type) {
				this.fileList.splice(event.index, 1)

				this.uploadParseData()
			},
			// 新增图片
			async afterRead(event, type) {
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length

				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.$wskj.uploadImg(lists[i].url)
					let item = this[[type]][fileListLen]

					// 这里需要根据后端接口返回的字段修改url，和fullurl的值
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result,
						fullurl: result
					}))
					fileListLen++
				}
				this.uploadParseData()
			},
			uploadParseData() {
				console.log(this.fileList)
				const images = this.fileList.map(item => item[this.backUrl]).join(",")
				console.log(images)
				// 标记不是手动更新 images, 
				this.isManualUpdate = false;
				// #ifndef MP-WEIXIN
				// this.$emit('upload', images)
				// #endif
				this.$emit('uploadImages', images)
				// 微信小程序不支持使用v-model更新父组件变量的方法，所以小程序需要传递一个方法到子组件
				// #ifdef MP-WEIXIN
				// #endif
			},
			removeUrl(str, urlToRemove) {
				// 使用正则表达式匹配要删除的URL，确保匹配整个字符串  
				// 注意：这里我们假设urlToRemove不包含查询字符串或路径，如果包含，需要调整正则表达式  
				const regex = new RegExp(urlToRemove, 'g');

				// 使用replace方法替换匹配的URL为空字符串，即删除它们  
				return str.replace(regex, '');
			},
			// 用于图片回显使用，因为小程序无法检测到v-modal的改变
			showImage(imgStr) {
				const urlList = imgStr.split(',');
				urlList.forEach((item) => {
					let url = this.removeUrl(item, this.$config.domain);
					const fullurl = url.indexOf('http') === -1 ? this.$config.domain + url : url;
					this.fileList.push({
						status: 'success',
						message: '',
						fullurl,
						url,
					});
				});
			}
		}
	}
</script>

<style scoped>

</style>