<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-info">
						<view class="rate-label">综合评分</view>
						<view class="rate-star">
							<uni-rate :disabled="true" size="20" disabledColor="rgba(255, 184, 84, 1)" :value="shopItem.shopScore" />
						</view>
					</view>
				</view>
			</view>
			<view class="menu-wrap">
				<view class="menu-list">
					<view class="menu1" :class="{'menu1-active': item.checked}" @tap="checkTab(index)" v-for="(item,index) in menuList" :key="index">
						<view class="menu-text">
							{{item.name}} {{item.num}}
						</view>
					</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 class="eval-rate-row">
							<uni-rate :disabled="true" disabledColor="rgba(255, 184, 84, 1)" :value="item.score" size="12" />
						</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-if="item.comments && item.comments.length">
							<view class="reply-title">回复</view>
							<view class="reply-list">
								<view class="reply-content" v-for="(item1,index1) in item.comments" :key="index1" v-show="!item.isCommentFold || index1 == 0">
									<view>
										<text class="reply-name">{{item1.createByName}}：</text>
										<text>{{ item1.remark }}</text>
									</view>
									<view class="reply-time" v-if="item1.createTime">
										{{item1.createTime | formatTime}}
									</view>
									<image :src="item1.picture" @tap="largeImg(item1.picture)" v-if="item1.picture" class="upload-img reply-img" mode="aspectFill"></image>
								</view>
							</view>
							<view class="reply-fold" v-if="item.comments.length > 1" @tap="toggleCommentFold(index)">
								{{item.isCommentFold ? '展开全部 ' + item.comments.length + ' 条回复' : '收起回复'}}
							</view>
						</view>
						<view class="reply-operate" v-if="(isMerchant == 1 || isMerchant == 2)">
							<view @tap="changeStatus(index)" class="reply-action" v-if="!item.isReply">
								回复
							</view>
							<view class="reply-editor" v-else>
								<view class="reply-editor-box">
									<view class="eval-num">
										<textarea name="" v-model="replyText" id="" cols="30" rows="10" placeholder="有什么想说的吗？可以写下来，提交给我们" class="reply-textarea"></textarea>
									</view>
									<view class="reply-upload-row">
										<view class="upload-img upload-add" @tap="pictureAdd()">
											<uni-icons type="camera" size="28" color="#777"></uni-icons>
										</view>
										<view class="reply-preview-list">
											<view v-if="replyPicture !=''" :key="index" class="reply-preview">
												<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="reply-buttons">
									<view @tap="submit(index)" class="reply-confirm">
										确认
									</view>
									<view class="reply-cancel" @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) {
			  if (!value) return '';
			  const timeText = String(value).replace('T', ' ');
			  if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(timeText)) {
				  return timeText.slice(0, 19);
			  }
			  const date = new Date(value);
			  if (isNaN(date.getTime())) return '';
			  
			  // 获取年份
			  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
							that.shopComments[i].isCommentFold = true
							if(!that.shopComments[i].goodsList){
								that.shopComments[i].goodsList = []
							}
							if(!that.shopComments[i].comments){
								that.shopComments[i].comments = []
							}
						}
						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()
			},
			toggleCommentFold(v){
				this.shopComments[v].isCommentFold = !this.shopComments[v].isCommentFold
				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: #F7F8FA;
		color: #00231C;
	}

	.page1 {
		width: 100%;
		min-height: 100vh;
		font-size: 24rpx;
		position: relative;
		background: linear-gradient(180deg, #F2FFF8 0%, #F7F8FA 360rpx, #F7F8FA 100%);
	}

	.evaluate-fixed {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 1000;
		background: linear-gradient(180deg, #EEFFF6 0%, #F7F8FA 100%);
		box-shadow: 0 12rpx 28rpx rgba(0, 35, 28, 0.05);
	}

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

	.title {
		width: 100%;
		height: 210rpx;
		background:
			radial-gradient(circle at 16% 20%, rgba(115, 255, 199, 0.35) 0, rgba(115, 255, 199, 0) 210rpx),
			linear-gradient(135deg, #F3FFF7 0%, #EAFFF4 48%, #FFF8EC 100%);
	}

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

	.back-btn {
		position: absolute;
		left: 24rpx;
		width: 64rpx;
		height: 64rpx;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		background: rgba(255, 255, 255, 0.78);
		box-shadow: 0 10rpx 24rpx rgba(0, 35, 28, 0.08);
	}

	.title-name {
		padding-top: 110rpx;
		font-size: 34rpx;
		font-weight: 700;
		flex: 1;
		text-align: center;
		line-height: 64rpx;
		letter-spacing: 2rpx;
	}

	.content {
		width: 100%;
		height: calc(100vh - 470rpx);
		box-sizing: border-box;
		padding: 0 24rpx;
	}

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

	.comment-item {
		padding: 28rpx;
		margin-bottom: 22rpx;
		border-radius: 28rpx;
		background: #FFFFFF;
		box-shadow: 0 16rpx 40rpx rgba(0, 35, 28, 0.06);
	}

	.evaluate-fixed .shop-rate,
	.evaluate-fixed .menu-list {
		width: calc(100% - 48rpx);
		margin-left: 24rpx;
	}

	.shop-rate {
		display: flex;
		margin-top: 4rpx;
		padding: 28rpx 30rpx;
		box-sizing: border-box;
		border-radius: 30rpx;
		background: #FFFFFF;
		box-shadow: 0 16rpx 40rpx rgba(255, 132, 34, 0.12);
	}

	.rate-left {
		display: flex;
		align-items: center;
		flex: 1;
	}

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

	.rate-info {
		margin-left: 24rpx;
	}

	.rate-label {
		margin-bottom: 12rpx;
		font-size: 24rpx;
		color: #7A8782;
		font-weight: 700;
	}

	.rate-star {
		line-height: 36rpx;
	}

	.menu-list {
		height: 110rpx;
		display: flex;
		align-items: center;
		overflow-x: auto;
		overflow-y: hidden;
	}

	.menu1 {
		min-width: 176rpx;
		height: 64rpx;
		margin-right: 18rpx;
		line-height: 64rpx;
		border-radius: 999rpx;
		text-align: center;
		font-size: 28rpx;
		color: #6D7874;
		background: rgba(255, 255, 255, 0.78);
		box-shadow: inset 0 0 0 1rpx rgba(0, 35, 28, 0.06);
		flex-shrink: 0;
	}

	.menu1-active {
		color: #00231C;
		font-weight: 700;
		background: linear-gradient(135deg, #B5FFDD 0%, #FDF4B9 100%);
		box-shadow: 0 10rpx 24rpx rgba(0, 180, 126, 0.14);
	}

	.menu-text {
		padding: 0 24rpx;
	}

	.eval-title {
		width: 100%;
		display: flex;
		align-items: center;
	}

	.eval-icon {
		height: 82rpx;
		width: 82rpx;
		border-radius: 50%;
		overflow: hidden;
		flex-shrink: 0;
		background: #EAF5EF;
		box-shadow: 0 8rpx 18rpx rgba(0, 35, 28, 0.08);

		img {
			width: 100%;
			height: 100%;
			display: block;
		}
	}

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

	.name1 {
		font-size: 28rpx;
		line-height: 42rpx;
		font-weight: 700;
		color: #142D26;
	}

	.eval-time {
		margin-top: 4rpx;
		font-size: 22rpx;
		line-height: 32rpx;
		color: #87918E;
	}

	.eval-rate-row {
		display: flex;
		align-items: center;
		margin-top: 22rpx;
	}

	.eval-content {
		margin-left: 102rpx;
		font-size: 28rpx;
		color: #5C6662;
		line-height: 46rpx;
	}

	.eval-text {
		margin-top: 12rpx;
		color: #4E5A56;
		word-break: break-all;
	}

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

	.goods-card {
		display: flex;
		padding: 16rpx;
		margin-bottom: 12rpx;
		background: #F7F8FA;
		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%;
		flex-wrap: wrap;
		margin-top: 22rpx;
		gap: 16rpx;

		image {
			width: 150rpx;
			height: 150rpx;
			border-radius: 18rpx;
			background: #EEF1EE;
		}
	}

	.reply-card {
		border-radius: 20rpx;
		background-color: #F8FAF9;
		width: 100%;
		box-sizing: border-box;
		margin-top: 20rpx;
		padding: 18rpx 20rpx;
		border: 1rpx solid rgba(0, 35, 28, 0.06);
	}

	.reply-title {
		position: relative;
		padding-left: 18rpx;
		margin-bottom: 10rpx;
		font-size: 24rpx;
		line-height: 34rpx;
		color: #17352D;
		font-weight: 700;
	}

	.reply-title::before {
		content: '';
		position: absolute;
		left: 0;
		top: 8rpx;
		width: 6rpx;
		height: 18rpx;
		border-radius: 999rpx;
		background: #20D38B;
	}

	.reply-list {
		display: flex;
		flex-direction: column;
	}

	.reply-content {
		position: relative;
		font-size: 26rpx;
		color: #5A6460;
		padding: 14rpx 0;
		flex: 1;
		display: block;
		height: auto;
		overflow: visible;
		line-height: 42rpx;
	}

	.reply-content + .reply-content {
		border-top: 1rpx solid rgba(0, 35, 28, 0.06);
	}

	.reply-name {
		color: #17352D;
		font-weight: 700;
	}

	.reply-time {
		margin-top: 4rpx;
		font-size: 22rpx;
		line-height: 32rpx;
		color: #99A19E;
	}

	.reply-operate {
		margin-top: 18rpx;
	}

	.reply-action {
		text-align: right;
		color: #00AEEF;
		font-size: 26rpx;
		font-weight: 700;
		padding: 10rpx 0;
	}

	.reply-editor-box {
		margin-top: 12rpx;
		border-radius: 22rpx;
		border: 1rpx solid rgba(0, 35, 28, 0.08);
		background: #FAFBFA;
		overflow: hidden;
	}

	.eval-num {
		border-bottom: 1rpx solid rgba(0, 35, 28, 0.06);
	}

	.reply-textarea {
		width: 100%;
		height: 200rpx;
		box-sizing: border-box;
		padding: 26rpx;
		font-size: 28rpx;
		line-height: 42rpx;
		color: #243832;
	}

	.reply-upload-row {
		display: flex;
		padding: 22rpx 26rpx;
	}

	.upload-img {
		width: 160rpx;
		height: 160rpx;
		border-radius: 18rpx;
		display: block;
	}

	.upload-add {
		display: flex;
		align-items: center;
		justify-content: center;
		background: #F0F3F2;
		border: 1rpx dashed #C8D2CE;
	}

	.reply-preview-list {
		flex: 1;
		display: flex;
	}

	.reply-preview {
		width: 160rpx;
		height: 160rpx;
		margin-left: 20rpx;
	}

	.reply-img {
		margin-top: 16rpx;
	}

	.reply-fold {
		margin-top: 8rpx;
		padding-top: 12rpx;
		border-top: 1rpx solid rgba(0, 35, 28, 0.06);
		font-size: 24rpx;
		line-height: 34rpx;
		color: #00AEEF;
		font-weight: 700;
		text-align: right;
	}

	.reply-buttons {
		display: flex;
		flex-direction: row-reverse;
		align-items: center;
		margin-top: 18rpx;
	}

	.reply-confirm,
	.reply-cancel {
		min-width: 108rpx;
		height: 58rpx;
		line-height: 58rpx;
		border-radius: 999rpx;
		text-align: center;
		font-size: 26rpx;
	}

	.reply-confirm {
		margin-left: 18rpx;
		color: #FFFFFF;
		font-weight: 700;
		background: linear-gradient(135deg, #20D38B 0%, #00BFFF 100%);
		box-shadow: 0 10rpx 22rpx rgba(0, 191, 255, 0.18);
	}

	.reply-cancel {
		color: #7A8782;
		background: #F1F3F2;
	}
</style>
