<template>
	<view class="problem">
		<view class="problem-form">
			<u--form labelPosition="left" :model="reportForm" :rules="rules" ref="reportForm">
				<u-form-item label="问题描述:" prop="content" labelWidth="80">
					<u--textarea v-model="reportForm.content" placeholder="请详细描述问题内容" count maxlength="300" height="80">
					</u--textarea>
				</u-form-item>
				<u-form-item label="上传图片:" labelWidth="80">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="10" uploadIcon="plus" uploadIconColor="rgba(0, 0, 0, 0.45)"
						uploadText="上传照片" width="104" height="104" bgColor="#fff"></u-upload>
				</u-form-item>
				<u-form-item label="" labelWidth="20">
					<u-button type="primary" text="提交" :loading="loading" @click="submitForm"></u-button>
				</u-form-item>
			</u--form>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				reportForm: {
					content: '',
					fileList: []
				},
				rules: {
					content: [
						{
							type: 'string',
							required: true,
							message: '请详细描述问题内容',
							trigger: ['blur']
						}
					]
				},
				fileList1: [],
				loading: false
			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 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].thumb)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
					this.reportForm.fileList = this.fileList1.map(item => {
						return item.url.url
					})
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://wx.hw.hongweisoft.com/veterans/file/upload/single/minio',
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(JSON.parse(res.data)?.data)
							}, 1000)
						}
					});
				})
			},
			submitForm() {
				this.$refs.reportForm.validate().then(res => {
					console.log(this.reportForm)
					this.loading = true
					this.$refs.uToast.show({
						type: 'success',
						title: '提示',
						message: "提交成功",
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
					})
					setTimeout(() => {
						uni.$u.route({
							url: 'pages/mine/mine',
							type: 'switchTab'
						})
					}, 2000)
				}).catch(error => {
					
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.problem {
	padding: 20rpx 40rpx;
}
</style>
