<template>
	<view>
		<view class="cmain" style="padding-bottom: 85px">
			<view class="form_group">
				<view class="lable re">互助委托协议上传</view>
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple style="margin: 15px auto;"></u-upload>
			</view>
		</view>
		<view class="mfooter">
			<view class="flex">
				<view class="f"><button class="btn" @click="save()">提交</button></view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList1: [],
				imgList: [],
				id: null,
				importOrderList: {}
			}
		},
		onLoad(e) {
			this.fileList1 = []
			imgList: []
			if(e.id) {
				this.id = e.id
				this.http.request({
					url: '/level-one-server/app/TbImportOrder/getById',
					data: { id: this.id },
					success: res => {//this.http.ip
						this.importOrderList = res.data.data;
						console.log(JSON.parse(this.importOrderList.cooperEntrustImg))
						let data = JSON.parse(this.importOrderList.cooperEntrustImg)
						data.forEach((item) => {
							this.fileList1.push({url: this.http.ip+item});
							this.imgList.push({item});
						})
					}
				});
			}
		},
		methods: {
			save() {
				/* let rule = [
					{
						name: 'cardFront',
						checkType: 'notnull',
						errorMsg: '请上传互助委托协议'
					}
				];
				
				if (!this.verify.check(this.item, rule)) {
					uni.showModal({
						content: this.verify.error,
						showCancel: false
					});
					return false;
				} */
				this.http.request({
					url: '/level-one-server/app/TbImportOrder/update',
					data: { id: this.id, cooperEntrustImg: JSON.stringify(this.imgList) },
					method: 'POST',
					success: res => {
						uni.showToast({ title: '操作成功' });
						setTimeout(() => {
							uni.navigateBack();
						}, 1500);
					}
				});
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				if(`fileList${event.name}` == 'fileList1'){
					this.fileList1.splice(event.index,1)
					this.imgList.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: this.http.ip+result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.http.ip+'/sp-admin/upload/image',
						header:{
							"Authorization": uni.getStorageSync('user').token ? uni.getStorageSync('user').token : ''
						},
						filePath: url,
						name: 'file',
						success: (res) => {
							setTimeout(() => {
								this.imgList.push(JSON.parse(res.data).data);
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			
		}
	}
</script>

<style>

</style>