<template>
	<view class="page1">
		<view class="title">
			<view class="title-sreach">
				<view class="back-btn" @tap="back" :style="{'top': menuButtonInfo.top +'px'}">
					<uni-icons type="left" size="28"></uni-icons>
				</view>
				<view class="title-name" :style="{'padding-top': menuButtonInfo.top +'px'}">
					商家评价
				</view>
			</view>
		</view>
		<view class="content">
			<view class="shop-rate">
				<view class="rate-left">
					<view class="rate-num">
						{{shopItem.shopScore}}
					</view>
					<view class="rate-star">
						<uni-rate :disabled="true" size="20" disabledColor="rgba(255, 184, 84, 1)" :value="shopItem.shopScore" />
					</view>
				</view>
			</view>
			<view class="">
				<view class="menu-list">
					<view class="menu1" @tap="checkTab(index)" v-for="(item,index) in menuList" :key="index"
						:style="{'font-size':item.checked?'40rpx':'30rpx','color':item.checked?'rgba(0, 35, 28, 1)':'#777','width':index == 0?'160rpx':'180rpx'}">
						<view :style="{'width':index == 0?'160rpx':'180rpx'}" style="text-align: left;">
							{{item.name}} {{item.num}}
						</view>
						<img v-if="item.checked" class="checked-img"
							src="https://jewel-shop.oss-cn-beijing.aliyuncs.com/4bb5fc7725cb4a7a84d1d3a15f05b39c.png"
							alt="" />
					</view>
				</view>
			</view>
			<view class="evaluate-list" v-for="(item,index) in shopComments" :key="index">
				<view class="eval-title">
					<view class="eval-icon">
						<img :src="item.createByIcon"
							alt="" />
					</view>
					<view class="eval-name">
						<view class="name1">
							{{item.createByName}}
						</view>
						<view class="eval-time">
							{{item.createTime | formatTime}}
						</view>
					</view>
				</view>
				<view class="eval-content">
					<view style="display: flex;height: 50rpx;line-height: 50rpx;font-size: 24rpx;">
						商品
						<view style="padding: 12rpx 0 0 10rpx;">
							<uni-rate v-model="item.score" size="12" />
						</view>
					</view>
					<view class="eval-text">
						{{item.remark}}
					</view>
					<view class="eval-img" v-if="item.picture">
						<img @tap="largeImg" :src="item.picture" alt="">
					</view>
					<view class="reply-card" :class="{'expanded': isExpanded}" v-for="(item1,index1) in item.comments" :key="index1">
						<!-- 标题行：商家回复 + 展开/收起按钮 -->
						<view class="reply-header">
							<!-- 回复内容区域（动态类控制展开/折叠） -->
							<view class="reply-content" :class="{ collapsed: !isExpanded, expanded: isExpanded }">
								<text>{{item1.createByName}} : {{ item1.remark }}</text>
								<img :src="item1.picture" alt="" class="upload-img" style="margin-top: 10px;border-radius: 10px;">
							</view>
							<view class="reply-expand-btn" @click="toggleReply">
								<text v-if="!isExpanded">展开</text>
								<text v-else>收起</text>
							</view>
						</view>
					</view>
					<view class="" v-if="isMerchant == 1">
						<view @tap="changeStatus(index)" class="reply-card" v-if="!item.isReply" style="text-align: right;padding-right: 10px;color: #00BFFF;">
							回复
						</view>
						<view class="" v-else>
							<view class="" style="background: #f5f5f5;border-radius: 10px;border: 1px solid #777;margin-top: 10px;">
								<view class="eval-num" style="border: none;">
									<textarea name="" v-model="replyText" id="" cols="30" rows="10" placeholder="有什么想说的吗？可以写下来，提交给我们" style="width: 100%;font-size: 28rpx;padding: 30rpx;height: 200rpx;"></textarea>
								</view>
								<view class="eval-num" style="border: none;padding: 0 30rpx;display: flex;margin-bottom: 10px;">
									<view class="upload-img" @tap="pictureAdd()"
										style="background: #eee;text-align: center;line-height: 160rpx;">
										<uni-icons type="camera" size="28" color="#777"></uni-icons>
									</view>
									<view style="flex: 1;display: flex;">
										<view v-if="replyPicture !=''"
											:key="index" style="width: 160rpx;height: 160rpx;margin-left: 20rpx;">
											<img :src="replyPicture" alt="" class="upload-img">
										</view>
									</view>
								</view>
							</view>
							<!-- <textarea type="text" v-model="replyText" placeholder="要说什么写下来吧" style="background: #eee;border-radius: 10px;height: 100px;margin: 10px 0;padding: 10px;" /> -->
							<view class="" style="display: flex;flex-direction: row-reverse;padding-right: 10px;margin-top: 10px;">
								<view @tap="submit(index)" class="" style="margin-left: 20px;color: #00BFFF;">
									确认
								</view>
								<view class="" @tap="changeStatus(index)">
									取消
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 查看大图弹出层 -->
		<uni-popup ref="imgPopup" background-color="#fff">
			<view class="img-popup-content">
				<img :src="bigImg" alt="" style="width: 100%;height: 100%;">
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				replyText:'',
				isMerchant:0,
				replyPicture:"",
				isExpanded: false,
				menuButtonInfo: {},
				isPintuan: true,
				bigImg: '',
				shopItem:{},
				pageNum:1,
				searchAll:true,
				total:0,
				shopComments:[],
				searchForm:{
					shopId:'',
					pageNum: 1,
					score:0,
					picture:0,
					pageSize: '10'
				},
				menuList: [{
					name: '全部',
					num: "",
					checked: true
				}, {
					name: '有图/视频',
					num: '',
					checked: false
				}, {
					name: '中差评',
					num: '',
					checked: false
				}]
			}
		},
		components: {

		},
		filters:{
			formatTime(value) {
			  const date = new Date(value);
			  
			  // 获取年份
			  const year = date.getFullYear();
			  
			  // 获取月份 (getMonth 返回 0-11，所以需要 +1)，并补齐0
			  const month = String(date.getMonth() + 1).padStart(2, '0');
			  
			  // 获取日 (getDate)，并补齐0
			  const day = String(date.getDate()).padStart(2, '0');
			  
			  // 获取小时 (getHours)，并补齐0
			  const hour = String(date.getHours()).padStart(2, '0');
			  
			  // 获取分钟 (getMinutes)，并补齐0
			  const minute = String(date.getMinutes()).padStart(2, '0');
			  
			  // 获取秒 (getSeconds)，并补齐0
			  const second = String(date.getSeconds()).padStart(2, '0');
			  
			  // 拼接格式：年-月-日 时:分:秒
			  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
			}
		},
		onLoad(option) {
			this.isMerchant = option.isMerchant
			this.shopItem = JSON.parse(option.item)
			if(option.isCha == 1){ //待回复差评进来的
				this.searchForm.score = 1
				this.searchForm.hasAnswer= 0
				this.menuList[0].checked = false
				this.menuList[2].checked = true
			}
			this.getshopComments(this.shopItem.id)
		},
		onShow() {
			this.menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			
		},
		methods: {
			getshopComments(id){
				let that = this
				this.searchForm.shopId = id
				that.tui.request("/app/comment/getCommentList", "POST", this.searchForm, false, false).then((res) => {
					if (res.code == 200) {
						if (that.pageNum == 1) {
							that.shopComments = res.result.records
						} else {
							that.shopComments = [...that.shopComments, ...res.result.records]
						}
						for(let i=0;i<that.shopComments.length;i++){
							that.shopComments[i].isReply=false
						}
						that.$forceUpdate()
						if(this.searchAll){
							that.menuList[0].num =  res.result.total
						}
					} else {
						that.tui.toast(res.message)
						return
					}
					uni.hideLoading()
				}).catch((res) => {})
			},
			submit(v){
				let that = this;
				let data = [{ //店铺
					parentId:this.shopComments[v].id,
					leve:1,
					remark:this.replyText,
					picture:this.replyPicture,
					createBy:this.shopComments[v].shopId,
					createByName:'商家',
					createByIcon:'https://ooo.0o0.ooo/2019/04/28/5cc5a71a6e3b6.png',
					orderId:this.shopComments[v].orderId,
					shopId:this.shopComments[v].shopId,
					
				}]
				that.tui.request("/app/comment/save", "POST", data, false, false).then((res) => {
					if (res.code == 200) {
						uni.showToast({
							title: "评论成功",
							icon: 'none'
						})
						setTimeout(res => {
							this.back()
						}, 1000)
					} else {
						that.tui.toast(res.message);
					}
					uni.hideLoading();
				}).catch((res) => {});
			},
			toggleReply() {
				this.isExpanded = !this.isExpanded;
			},
			changeStatus(v){
				this.shopComments[v].isReply = !this.shopComments[v].isReply
				this.$forceUpdate()
			},
			checkTab(index) {
				for (let i = 0; i < this.menuList.length; i++) {
					if (i == index) {
						this.menuList[i].checked = true
					} else {
						this.menuList[i].checked = false
					}
				}
				if(index == 0){
					this.searchForm.picture = 0
					this.searchForm.score = 0
				}else if(index ==1){
					this.searchAll = false
					this.searchForm.picture = 1
					this.searchForm.score = 0
				}else{
					this.searchAll = false
					this.searchForm.picture = 0
					this.searchForm.score = 1
				}
				this.getshopComments(this.shopItem.id)
			},
			pictureAdd() {
				let that = this
				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['camera', 'album'],
					success(res) {
						uni.showLoading({
							title: '加载中...',
							mask: true
						})
						for (let i = 0; i < res.tempFiles.length; i++) {
							that.upLoadFile(res.tempFiles[i].tempFilePath)
						}
			
					}
				})
			},
			async upLoadFile(path) {
				let that = this;
				let hiver_token = uni.getStorageSync("hiver_token")
				await uni.uploadFile({
					url: that.tui.interfaceUrl() + '/upload/file',
					filePath: path,
					name: 'file',
					header: {
						"content-type": "multipart/form-data",
						'accessToken': hiver_token
					},
					formData: {},
					success: (uploadFileRes) => {
						let pathData = JSON.parse(uploadFileRes.data)
						
						this.replyPicture = pathData.result
						
						that.$forceUpdate()
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showToast({
							title: JSON.stringify(err),
							icon: 'none'
						})
					}
				});
				await setTimeout(res => {
					uni.hideLoading();
				}, 1000)
			},
			//查看大图
			largeImg(img) {
				this.bigImg = 'https://jewel-shop.oss-cn-beijing.aliyuncs.com/00b1f011787c4549bbea650d95a4eb39.png'
				this.$refs.imgPopup.open()
			},
			back() {
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss">
	page {
		width: 100%;
		height: 100%;
		font-size: 24rpx;
		background: #F5F8F5;
		color: #00231C;
	}

	.page1 {
		width: 100%;
		height: 100%;
		font-size: 24rpx;
		position: relative;
	}

	.title {
		background: url('https://jewel-shop.oss-cn-beijing.aliyuncs.com/8bc15960c2dc40268e295d6dd23aecce.png') no-repeat;
		width: 100%;
		height: 54%;
	}

	.title-sreach {
		width: 100%;
		display: flex;
		height: 200rpx;
		position: relative;
	}

	.back-btn {
		position: absolute;
		bottom: 0;
		left: 0;
	}

	.title-name {
		padding-top: 110rpx;
		font-size: 36rpx;
		font-weight: 700;
		flex: 1;
		text-align: center;
	}

	.content {
		position: absolute;
		top: 200rpx;
		width: 95%;
		margin-left: 2.5%;
	}

	.shop-rate {
		display: flex;
		padding-bottom: 30rpx;
	}

	.rate-left {
		display: flex;
		flex: 1;
	}

	.rate-num {
		font-size: 86rpx;
		font-weight: 700;
		background: linear-gradient(90deg, rgba(255, 77, 0, 1), rgba(255, 184, 84, 1));
		background-clip: text;
		color: transparent;
	}

	.rate-right {
		display: flex;
		width: 160rpx;
		height: 100rpx;
		line-height: 40rpx;
		font-size: 36rpx;
		text-align: center;
		font-weight: 700;
		border-left: 1px solid #eee;
	}

	.rate-star {
		height: 100rpx;
		padding: 32rpx 20rpx;
	}

	.right1 {
		margin: 20rpx 0 0 5%;
		width: 45%;
	}

	.menu-list {
		height: 100rpx;
		width: 100%;
		display: flex;
		overflow-y: hidden;
		padding-top: 30rpx;
		border-top: 1px solid #eee;
	}

	.checked-img {
		width: 100%;
		height: 20rpx;
		position: absolute;
		bottom: 0;
		left: 0;
	}

	.menu1 {
		width: 180rpx;
		height: 70rpx;
		position: relative;
		margin-right: 20rpx;
		line-height: 70rpx;
		flex: 1;
	}

	.evaluate-list {
		margin-top: 40rpx;
	}

	.eval-title {
		width: 100%;
		height: 80rpx;
		display: flex;
	}

	.eval-icon {
		height: 80rpx;
		width: 80rpx;

		img {
			width: 100%;
			height: 100%;
			background-size: 100%;
		}
	}

	.eval-name {
		margin-left: 20rpx;
	}

	.name1 {
		font-size: 28rpx;
		line-height: 50rpx;
		font-weight: 700;
	}

	.eval-rate {
		display: flex;
		margin: 20rpx 0 0 100rpx;
		height: 48rpx;
		line-height: 40rpx;
	}

	.eval-content {
		margin-left: 100rpx;
		font-size: 28rpx;
		color: #777;
		line-height: 48rpx;
	}

	.eval-img {
		display: flex;
		width: 100%;
		overflow-x: scroll;
		margin-top: 20rpx;

		img {
			width: 100rpx;
			height: 100rpx;
			background-size: 100%;
			margin-right: 20rpx;
		}
	}

	/* 卡片整体样式（可根据需要调整外间距） */
	.reply-card {
		border-radius: 12rpx;
		background-color: #eee;
		width: 100%;
		box-sizing: border-box;
		margin-top:20rpx;
	}

	/* 标题行：左标签 + 右按钮 */
	.reply-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 16rpx;
	}

	.reply-label {
		font-size: 28rpx;
		font-weight: 500;
		color: #333;
	}

	.reply-expand-btn {
		font-size: 24rpx;
		padding: 4rpx 12rpx;
	}

	/* 回复内容基础样式 */
	.reply-content {
		font-size: 26rpx;
		color: #555;
		padding: 12rpx;
		transition: all 1s;
		height: 70rpx;
		flex: 1;
	}

	/* 折叠状态：单行省略 */
	.reply-content.collapsed {
		display: -webkit-box;
		-webkit-line-clamp: 1;
		-webkit-box-orient: vertical;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	/* 展开状态：固定高度100px，可滚动 */
	.reply-content.expanded {
		display: block;
		height: auto;
		/* 固定高度100px */
		overflow-y: auto;
		/* 内容超长时滚动 */
		/* 重置折叠相关属性 */
		-webkit-line-clamp: unset;
		-webkit-box-orient: unset;
		text-overflow: clip;
		padding-bottom: 10px;
	}

	/* 滚动条美化（可选） */
	.reply-content.expanded::-webkit-scrollbar {
		width: 12rpx;
	}

	.reply-content.expanded::-webkit-scrollbar-thumb {
		background-color: #ccc;
		border-radius: 20rpx;
	}
	.eval-num{
		    padding-bottom: 30rpx;
		    border-bottom: 1px solid #eee;
	}
	.upload-img {
		width: 160rpx;
		height: 160rpx;
	}
</style>