<template>
	<view class="content2">
		<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="1">
			<slot name="upLoadImg"></slot>
		</u-upload>
	</view>
</template>


<script>
	export default {
		data() {
			return {
				fileList1: [],
				imgList: [],
			}
		},
		components: {},
		onLoad() {

		},
		onShow() {

		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				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) {
				console.log(url, 'dfff')
				return new Promise((resolve, reject) => {
					this.$api.uploadImg(url,res=>{
						console.log(res,'=====')
						if(res.code == '1'){
							setTimeout(() => {
								resolve(res.data.data);
							}, 1000);
							// resolve(res.data);
							this.imgList= this.imgList.concat([res.data.url])
							this.$emit('imgList',res.data.url);
							console.log(res.data, this.imgList, '-----------')
						}else{ 
							uni.showToast({ title: res.msg , icon: 'fail' })
						}
					}) 
					console.log(this.imgList)
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		width: 100%;
	}
	.content {
		// padding: 20rpx 35rpx;
		background: #F5F5FA;
		height: 100%;
		overflow: auto;
	}
	.content2{
		height: 100%;
		width: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	::v-deep .u-upload__wrap__preview{
		margin: 0;
	}
	::v-deep .u-upload__wrap{
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
</style>