<template>
	<view class="page1">
		<view class="evaluate-fixed">
			<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'}">
						{{isMerchant == 2 ? '配送评价' : '商家评价'}}
					</view>
				</view>
			</view>
			<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>
		<view class="evaluate-fixed-holder"></view>
		<view class="content">
			<scroll-view class="comment-scroll" scroll-y @scrolltolower="onReachPage">
				<view class="comment-item" 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 class="goods-list" v-if="item.goodsList && item.goodsList.length > 0">
							<view class="goods-card" v-for="(goods,goodsIndex) in item.goodsList" :key="goodsIndex">
								<image class="goods-img" :src="goods.productPicture" mode="aspectFill"></image>
								<view class="goods-info">
									<view class="goods-name">{{goods.productName}}</view>
									<view class="goods-spec" v-if="goods.specs">{{goods.specs}}</view>
									<view class="goods-bottom">
										<text>￥{{goods.price}}</text>
										<text>x{{goods.quantity}}</text>
									</view>
								</view>
							</view>
						</view>
						<view style="display: flex;height: 50rpx;line-height: 50rpx;font-size: 24rpx;">
							
							<view style="padding: 12rpx 0 0 10rpx;">
								<uni-rate :disabled="true" disabledColor="rgba(255, 184, 84, 1)" :value="item.score" size="12" />
							</view>
						</view>
						<view class="eval-text">
							{{item.remark}}
						</view>
						<view class="eval-img" v-if="item.picture">
							<image v-for="(pic,picIndex) in getPictureList(item.picture)" :key="picIndex"
								@tap="largeImg(item.picture, picIndex)" :src="pic" mode="aspectFill"></image>
						</view>
						<view class="reply-card" v-for="(item1,index1) in item.comments" :key="index1">
							<view class="reply-header">
								<view class="reply-content">
									<text>{{item1.createByName}} : {{ item1.remark }}</text>
									<image :src="item1.picture" @tap="largeImg(item1.picture)" v-if="item1.picture" class="upload-img" mode="aspectFill" style="margin-top: 10px;border-radius: 10px;"></image>
								</view>
							</view>
						</view>
						<view class="" v-if="(isMerchant == 1 || isMerchant == 2)">
							<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: rgba(247, 248, 248, 0.6);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: rgba(247, 248, 248, 0.6);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>
			</scroll-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>
		<common-loading />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				replyText:'',
				isMerchant:0,
				replyPicture:"",
				menuButtonInfo: {},
				isPintuan: true,
				bigImg: '',
				shopItem:{},
				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
			if (option.item) {
				try {
					this.shopItem = JSON.parse(decodeURIComponent(option.item))
				} catch (e) {
					try {
						this.shopItem = JSON.parse(option.item)
					} catch (e2) {
						this.shopItem = {}
					}
				}
			}
			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.searchForm.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
							if(!that.shopComments[i].goodsList){
								that.shopComments[i].goodsList = []
							}
						}
						that.total = res.result.pages; // pages = 总页数
						that.$forceUpdate()
						if(this.searchAll){
							that.menuList[0].num =  res.result.total
						}
					} else {
						that.tui.toast(res.message)
						return
					}
					uni.hideLoading()
				}).catch((res) => {})
			},
			onReachPage(){
				if (this.searchForm.pageNum >= this.total) return;
				this.searchForm.pageNum++;
				this.getshopComments(this.shopItem.id);
			},
			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:this.isMerchant == 1?'商家':'配送员',
					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) => {});
			},
			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.searchAll = true
					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.searchForm.pageNum = 1
				this.shopComments = []
				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)
			},
			//查看大图
			getPictureList(picture) {
				if (!picture) return []
				if (Array.isArray(picture)) return picture.filter(Boolean)
				return String(picture).split(',').map(item => item.trim()).filter(Boolean)
			},
			largeImg(picture, currentIndex = 0) {
				const urls = this.getPictureList(picture)
				if (!urls.length) return
				uni.previewImage({
					urls,
					current: urls[currentIndex] || urls[0]
				})
			},
			back() {
				uni.navigateBack()
			}
		}
	}
</script>

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

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

	.evaluate-fixed {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 1000;
		background: #F5F8F5;
	}

	.evaluate-fixed-holder {
		height: 450rpx;
	}

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

	.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 {
		width: 95%;
		margin-left: 2.5%;
		height: calc(100vh - 450rpx);
		box-sizing: border-box;
	}

	.comment-scroll {
		width: 100%;
		height: 100%;
		box-sizing: border-box;
		padding-bottom: 32rpx;
	}

	.comment-item {
		padding: 24rpx 0;
		border-bottom: 1px solid rgba(0, 35, 28, 0.06);
	}

	.evaluate-fixed .shop-rate,
	.evaluate-fixed .menu-list {
		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: 120rpx;
		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;
	}

	.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;
	}

	.goods-list {
		margin: 4rpx 0 16rpx;
	}

	.goods-card {
		display: flex;
		padding: 16rpx;
		margin-bottom: 12rpx;
		background: rgba(247, 248, 248, 0.9);
		border-radius: 16rpx;
	}

	.goods-img {
		width: 104rpx;
		height: 104rpx;
		border-radius: 12rpx;
		background: #eef1ee;
		flex-shrink: 0;
	}

	.goods-info {
		flex: 1;
		min-width: 0;
		margin-left: 16rpx;
		line-height: 36rpx;
	}

	.goods-name {
		font-size: 26rpx;
		font-weight: 700;
		color: #00231C;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.goods-spec {
		margin-top: 6rpx;
		font-size: 22rpx;
		color: #999;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.goods-bottom {
		display: flex;
		justify-content: space-between;
		margin-top: 8rpx;
		font-size: 24rpx;
		color: #777;
	}

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

		image {
			width: 100rpx;
			height: 100rpx;
			border-radius: 10rpx;
			margin-right: 20rpx;
			flex-shrink: 0;
		}
	}

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

	.reply-header {
		display: flex;
		justify-content: space-between;
		align-items: flex-start;
		margin-bottom: 16rpx;
	}

	/* 回复内容基础样式 */
	.reply-content {
		font-size: 26rpx;
		color: #555;
		padding: 12rpx;
		flex: 1;
		display: block;
		height: auto;
		padding-bottom: 10px;
		overflow: visible;
	}
	.eval-num{
		    padding-bottom: 30rpx;
		    border-bottom: 1px solid #eee;
	}
	.upload-img {
		width: 160rpx;
		height: 160rpx;
	}
</style>