<template>
	<view>
		<view class="img_box">
			<u-upload
				@on-list-change="change"
				upload-text="上传图片"
				width="139"
				height="139"
				:max-size="10 * 1024 * 1024"
				max-count="6"
				ref="uUpload"
				:action="action"
				:auto-upload="false"
			></u-upload>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
export default {
	data() {
		return {
			uploadlist: [], //上传图片数组
			value: '', //提交图片数组
			action: 'https://xj.chuanghai-tech.com/patrol-app/v1/file/upload'
		}
	},
	created() {},
	methods: {
		change() {
			this.uploadlist = []
			this.value = ''
			this.$nextTick(() => {
				let lists = this.$refs.uUpload.lists
				lists.forEach((val) => {
					this.upload(val.url)
				})
			})
		},
		//上传图片
		upload(item) {
			uni.uploadFile({
				url: 'https://xj.chuanghai-tech.com/patrol-app/v1/file/upload', //仅为示例，非真实的接口地址
				filePath: item,
				name: 'file',
				success: (uploadFileRes) => {
					let data = JSON.parse(uploadFileRes.data)
					let showUrl = data.data
					this.uploadlist.push(showUrl)
					this.$nextTick(() => {
						let value = ''
						let len = this.uploadlist.length
						if (len > 0) {
							if (len > 1) {
								value = this.uploadlist[0]
								for (let i = 1; i < len; i++) {
									value = value + ',' + this.uploadlist[i]
								}
							} else {
								value = this.uploadlist[0]
							}
							this.value = value
							this.$nextTick(() => {
								this.$emit('changeImg', this.value)
							})
						}
					})
				},
				fail(data) {
					this.$refs.uToast.show({
						title: data.message,
						type: 'warning'
					})
				}
			})
		},
		//提交
		submit() {
			let newarr = JSON.parse(JSON.stringify(this.value))
			let item = {
				id: this.scan_id,
				value: JSON.stringify(newarr)
			} //提交数据
			this.$store.state.user.items.push(item)
		},
		reset() {
			this.$nextTick(() => {
				let len = this.$refs.uUpload.lists.length
				this.$refs.uUpload.lists.splice(0, len)
			})
		}
	}
}
</script>

<style></style>
