<template>
	<view>
		<!-- 表单区域 -->
		<view class="form-container region mar-t-10">
			<view class="form-item">
				<view class="form-label">评分</view>
				<uni-rate size="30" v-model="score.score" />
			</view>

			<view class="form-item">
				<view class="form-label">评价内容</view>
				<view class="textarea-container">
					<textarea style="width: 100%;" v-model="score.content" placeholder="请详细描述申请说明(选填)"
						class="form-textarea" :maxlength="maxLength"></textarea>
					<view class="char-count">
						您还可以输入{{ remainingChars }} 字
					</view>
				</view>
			</view>

			<!-- 上传凭证 -->
			<!-- 媒体上传 -->
			<view class="upload-section">
				<text class="section-title">图片上传</text>
				<media-uploader v-model="mediaList" :maxCount="9" mediaType="image" :media="false"
					:uploadUrl="uploadUrl" :token="token" @change="handleMediaChange" />
			</view>
		</view>

		<!-- 提交按钮 -->
		<button class="submit-button" @click="submitForm('customForm')">提交评价</button>
	</view>
</template>

<script>
	import {
		UPLOAD_URL
	} from '@/common/config.js';
	import store from '@/store';
	import MediaUploader from '@/components/MediaUploader.vue';
	import {
		saveScore
	} from '@/config/api.js';

	export default {
		components: {
			MediaUploader
		},
		data() {
			return {
				mediaList: [], // 媒体文件列表
				uploadUrl: UPLOAD_URL, // 上传地址
				token: "",
				score: {
					imgList: [],
					score: 5,
					content: ""
				},
				token: '',
				imageValue: [],
				fileList: [],
				images: [],
				orderId: "",
				maxLength: 200, // 最大字数
			};
		},
		computed: {
			// 计算剩余字数
			remainingChars() {
				return this.maxLength - this.score.content.length;
			}
		},
		onLoad(op) {
			this.orderId = op.orderId;
			this.token = uni.getStorageSync('access_token');
		},
		methods: {
			// 处理媒体文件变化
			handleMediaChange(list) {
				console.log('媒体列表变化:', list);
				// list 中的每个项目都包含 url 和 type (image/video)
				this.mediaList = list;
				// 更新 score.imgList，只包含图片类型的URL
				this.score.imgList = list
					.filter(item => item.type === 'image')
					.map(item => item.url);
			},

			// 提交表单
			submitForm() {
				this.score.images = this.score.imgList.join(',');
				this.score.orderId = this.orderId
				saveScore(this.score).then((res) => {
					if (res.code == 200) {

						uni.showToast({
							title: '评价成功',
							icon: 'success'
						});
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1000)
					}
				})

			}
		}
	}
</script>

<style scoped>
	.page-title {
		font-size: 36rpx;
		font-weight: bold;
		text-align: center;
		padding: 20rpx 0;
		color: #333;
	}

	.form-container {
		padding: 20rpx;
	}

	.form-item {

		margin-bottom: 20rpx;
	}

	.form-label {
		font-size: 28rpx;
		color: #666;
		margin-bottom: 10rpx;
	}

	.textarea-container {
		position: relative;
	}

	.form-textarea {
		border: 1px solid #ccc;
		border-radius: 5rpx;
		padding: 10rpx;
		font-size: 28rpx;
		height: 150rpx;
		resize: none;
	}

	.char-count {
		position: absolute;
		bottom: 10rpx;
		right: 10rpx;
		font-size: 24rpx;
		color: #999;
	}

	.upload-container {
		margin-top: 10rpx;
	}

	.submit-button {
		background-color: #F95B5B;
		color: #fff;
		font-size: 32rpx;
		padding: 5rpx;
		border-radius: 10rpx;
		margin-left: 30rpx;
		margin-right: 30rpx;
		text-align: center;
		margin-top: 30rpx;
	}

	.videoUpload {
		border: 1rpx solid #999;

		height: 200rpx;
		width: 200rpx;
	}
</style>