<template>
	<view class="evaluate-container">

		<!-- 评分项 -->
		<view class="evaluate-list">
			<view class="evaluate-row">
				<text class="evaluate-label">服务评价</text>
				<view class="evaluate-stars">
					<image v-for="i in 5" :key="i"
						:src="detailItem.serviceEvalua >= i ? '/static/ic_evaluate_select.png' : '/static/ic_evaluate_disselect.png'"
						class="evaluate-star"  @click.stop="setRating(i)" />
				</view>
			</view>
		</view>

		<!-- 输入框 -->
		<textarea class="evaluate-textarea" placeholder="说点什么吧..." v-model="detailItem.serviceEvaluaText" />

		<!-- 上传照片 -->
		<!-- 上传照片 -->
		<view class="repair-evaluate-section">
			<view class="add-repair-label">上传照片 <text class="add-repair-optional">(非必填，最多三张)</text></view>
			<u-upload :fileList="selectedImages" @delete="deletePic" name="upload" multiple maxCount="3" width="180"
				height="180" :autoUpload="false"></u-upload>
		</view>
		<!-- 提交按钮 -->
		<button class="evaluate-btn" @click="submit">提交</button>
	</view>
</template>

<script>
	// 导入MediaSelector和MediaType
	import MediaSelector, {
		MediaType
	} from '@/utils/mediaSelector';
	import {
		uploadFiles
	} from '@/common/upload.js';
	export default {
		data() {
			return {
				comment: '',
				selectedImages: [], // 存储已选图片
				detailItem: {} // 接收传递的详情项
			}
		},
		onLoad(options) {
			// 接收传递的detailItem参数
			if (options.detailItem) {
				try {
					this.detailItem.id = options.detailItem;
				} catch (e) {
					console.error('解析detailItem参数失败', e);
				}
			}
		},
		methods: {
      setRating(score) {
        if (!this.detailItem) {
          this.detailItem = {};
        }
        this.$set(this.detailItem, 'serviceEvalua', score);
        console.log('当前评分:', score);
      },
			// 删除图片
			deletePic(event) {
				this.selectedImages.splice(event.index, 1);
			},
			async submit() {
				if (this.selectedImages.length <= 0) {
					this.realSubmit()
					return
				}
				// 遍历selectedImages数组并处理图片路径
				const images = this.selectedImages.map(item => item.path.replace('file://', ''));
				const result = await uploadFiles({
					files: images,
					url: this.vuex_config.baseUrl + '/resource/oss/upload',
					name: 'file',
					vm: this // 关键：用于注入 token 等
				});

				if (result.code == 200) {
					// 遍历result获取data.url加上,分割
					const urls = result.map(item => item.data?.url || '').filter(url => url !== '');
					this.detailItem.imgUrl = urls.join(',');
					this.realSubmit()
				}
			},

			async realSubmit() {
        console.log(this.detailItem);
				let res = await this.$u.api.repair.updateOrder(this.detailItem);
				if (res.code == '200') {
					// 关闭页面前发送事件通知前页面刷新
					uni.$emit('refreshData', '');
					// 返回上一页
					uni.navigateBack();
				}
			}
		}
	}
</script>

<style scoped>
	.evaluate-container {
		min-height: 100vh;
		background: #fff;
		padding-bottom: 40rpx;
		/* 防止在iOS等设备上滑动到顶部或底部时出现回弹效果 */
		overscroll-behavior-y: contain;
	}


	.evaluate-list {
		margin: 0 53rpx 0 40rpx;
	}

	.evaluate-row {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 38rpx;
	}

	.evaluate-label {
		font-size: 30rpx;
		color: #222;
		width: 160rpx;
		text-align: left;
	}

	.evaluate-stars {
		display: flex;
		align-items: center;
	}

	.evaluate-star {
		width: 36rpx;
		height: 34rpx;
		margin-right: 36rpx;
	}


	.evaluate-tag {
		flex: 0 0 calc((100% - 60rpx) / 4);
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		height: 64rpx;
		border-radius: 32rpx;
		font-size: 28rpx;
		color: #666666;
		background: #F2F2F2;
	}

	.evaluate-tag.selected {
		background: #0090FF;
		color: #fff;
	}

	.evaluate-textarea {
		width: 85vw;
		min-height: 120rpx;
		background: #F5F6F7;
		border-radius: 10rpx;
		font-size: 26rpx;
		color: #222222;
		padding: 23rpx 27rpx 23rpx 27rpx;
		margin: 49rpx auto 0 auto;
		display: block;
	}

	.evaluate-btn {
		width: 80vw;
		height: 88rpx;
		background: #0090FF;
		color: #fff;
		font-size: 36rpx;
		border: none;
		border-radius: 44rpx;
		margin: 137rpx auto 0 auto;
		display: block;
		font-weight: bold;
		box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.18);
	}

	.repair-evaluate-section {
		background: #fff;
		border-radius: 12rpx;
		margin: 22rpx 0rpx 0 0rpx;
		padding: 24rpx 46rpx 24rpx 44rpx;
	}

	.add-repair-label {
		font-size: 32rpx;
		color: #000000;
		font-weight: 500;
		margin-bottom: 41rpx;
	}


	.add-repair-optional {
		color: #888;
		font-size: 24rpx;
		font-weight: 400;
	}
</style>