<template>
	<view class="imgs-upload-container">
    <u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" multiple :maxCount="1"
      width="275" height="150">
      <image src="https://cdn.uviewui.com/uview/demo/upload/positive.png" mode="widthFix"
        style="width: 670rpx;height: 150px;"></image>
    </u-upload>
    
	</view>
</template>

<script>
export default {
		props:{
			filelist:{
				type:Array
			},
			value:{
				type:Number
			}
		},
		data() {
			return {
				fileList: [],
				img_upload_list: [],
			}
		},
		onLoad(option) {
		},
		mounted() {
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.img_upload_list.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				const { index, file} = event
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: `http://192.168.2.21:7001/upload`, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							Authorization: localStorage.getItem('App-Token')
						},
						formData: {},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
								this.img_upload_list.push(JSON.parse(res.data).data.relativePath)
								this.$emit('update:filelist', this.img_upload_list)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.upload-textbox {
		margin-top: 10rpx;

		.upload-img {
			width: 80rpx;
			height: 80rpx;
		}

		.span-box {}
	}
</style>
