<template>
	<view class="feedback">
		<view class="content">
			<view class="mg-bot-10"><text class="light-gray fs22">此投诉为本小程序自有投诉渠道，非微信官方投诉渠道。</text></view>
			<u--textarea v-model="content" height='488rpx' maxlength="-1" placeholder="请输入您的意见与反馈..."></u--textarea>
			<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="1" multiple
				:maxCount="1"></u-upload>
		</view>

		<view class="submitFeedback" @click="submitFeedback">
			提交
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content: '',
				fileList: [],
			}
		},
		methods: {

			//提交问题反馈
			submitFeedback() {
				let feedbackImage = []
				this.fileList.map(item => {
					feedbackImage.push(item.url)
				})
				this.$http.post('/api/client/userInfo/addFeedback', {
					feedbackContent: this.content,
					feedbackImageList: feedbackImage
				}).then(res => {
					console.log(res)
					uni.showToast({
						icon: 'none',
						title: res.data.msg
					})
					if (res.data.code == 200) {
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					}
				})
			},

			skipUp() {
				uni.navigateBack({
					delta: 1
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList`].length
				lists.map((item) => {
					this[`fileList`].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`][fileListLen]
					this[`fileList`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				console.log(url, "url");
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: 'https://server.mycsh.cc/prod-api/client/api/client/upload', // 真实接口地址
						filePath: url,
						header: {
							'Content-Type': 'application/json;charset=UTF-8',
							Authorization: uni.getStorageSync('token')
						},
						name: 'file',
						success: (res) => {
							try {
								// 解析返回的数据
								const data = JSON.parse(res.data);
								if (data && data.data && data.data.url && data.data.url) {
									resolve(data.data.url);
								} else {
									reject(new Error('Invalid response format'));
								}
							} catch (error) {
								reject(new Error('Failed to parse response data'));
							}
						},
						fail: (error) => {
							// 处理上传失败的情况
							reject(error);
						}
					});
				});
			}
		}
	}
</script>
<style>
	page {
		background: #FAFAFA;
	}
</style>
<style lang="scss" scoped>
	.content {
		box-sizing: border-box;
		padding: 24rpx;
		margin-top: 20rpx;
		width: 750rpx;
		background: #FFFFFF;
		border-radius: 0rpx 0rpx 0rpx 0rpx;

		.u-upload {
			padding: 10rpx 23rpx;
		}
	}

	.submitFeedback {
		width: 702rpx;
		height: 88rpx;
		background: linear-gradient(90deg, #FF4502 0%, #FF8903 100%);
		border-radius: 44rpx 44rpx 44rpx 44rpx;
		font-weight: 400;
		font-size: 28rpx;
		color: #FFFFFF;
		text-align: center;
		line-height: 88rpx;
		margin: 40rpx auto;
	}
</style>