<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<view class="one_1">
					<view class="text1">{{info.goods.name}}{{info.specs[0].name}}</view>
					<view class="text2">好评率 <text>{{info.rate||100}}%</text></view>
				</view>
				<view class="one_2">
					<view class="one_2_1" v-for="(item,index) in btnlist" :key="index" @tap="toSelect(item)">
						<text>{{item.name}}</text><text>({{item.total||0}})</text>
					</view>
				</view>
			</view>
			<view class="two">
				<scroll-view scroll-y="true" class="scroll-view">
					<view class="list-scroll-view">
						<view class="list" v-for="(item, index) in list" :key="index">
							<image v-if="item.customer.icon&&item.customer.icon.length>0" class="image" :src="item.customer.icon&&item.customer.icon.length>0?item.customer.icon[0].url:''" mode="">
							</image>
							<text v-else class="iconfont icon-top"></text>
							<view class="list_1">
								<view class="other">
									<text>{{item.customer?item.customer.phone:'暂无'}}</text>
									<text>{{item.reply[0].time||'暂无'}}</text>
								</view>
								<view class="flie">
									<view v-for="(tag, indexs) in item.reply[0].file" :key="indexs">
										<image @click="toLarge(item,indexs)" class="images" :src="tag.url" mode="">
										</image>
									</view>
								</view>
								<view class="stars">
									<uni-rate size="18" :readonly="true" :value="item.goods_score" />
								</view>
								<view class="other1"><text>{{item.reply[0].content||'暂无'}}</text></view>
								<view class="other2">规格：{{item.goodsSpec.name}}</view>
								<view class="other2" v-for="(itm, indexx) in item.reply" :key="indexx">
									<view class="other2_1">
										<view :class="[item.customer._id!=user._id?'reply_2':'reply']">
											<text>用户评论：{{itm.content||'暂无'}}</text>
											<text v-if="itm.reply">商家回复：{{itm.reply||'暂无'}}</text>
										</view>
										<view v-if="item.customer._id==user._id" @tap="toReply(item,indexx)">回复</view>
									</view>
									<uni-easyinput v-if="reply.reply&&reply.customer._id==user._id&&reply._id==item._id&&reply.index==indexx" class="input" maxlength=-1 type="textarea" @change="confirm" placeholder="评论" />
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	import moment from 'moment';
	export default {
		data() {
			return {
				id: '',
				user: {},
				btnlist: [{
						name: '全部好评',
						total: 0,
						code: '0'
					},
					{
						name: '好评',
						total: 0,
						code: '1'
					},
					{
						name: '中评',
						total: 0,
						code: '2'
					},
					{
						name: '差评',
						total: 0,
						code: '3'
					}
				],
				info: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
				one: [],
				two: [],
				thr: [],
				// 回复数据
				reply: {},
				// 列表
				code: ''
			};
		},
		onLoad: function(e) {
			const that = this;
			that.$set(that, `id`, e.id || '');
			that.watchLogin()
		},
		methods: {
			// 监听用户是否登录
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						let user = that.$jwt(res.data);
						that.$set(that, `user`, user);
						that.search()
					},
					fail: function(err) {
						uni.navigateTo({
							url: `/pages/login/index`
						})
					}
				})
			},
			// 查询列表
			async search() {
				const that = this;
				let user = that.user;
				let res;
				res = await that.$api(`/viewGoods/goodsDetail`, `POST`, {
					id: that.id
				});
				if (res.errcode == '0') {
					that.$set(that, `info`, res.data)
				}
				let info = {
					goods: that.id
				}
				res = await that.$api(`/goodsRate`, `GET`, {
					...info,
				})
				if (res.errcode == '0') {
					let list = res.data;
					for (let val of list) {
						if (val.customer && val.customer.phone) {
							val.customer.phone = val.customer.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
						}
					}
					that.$set(that, `list`, list)
					that.$set(that, `total`, res.total)
					that.$set(that.btnlist[0], `total`, res.total)
					let one = list.filter(item => item.goods_score == 5);
					that.$set(that, `one`, one)
					that.$set(that.btnlist[1], `total`, one.length);
					let two = list.filter(item => item.goods_score < 5 && item.goods_score > 2)
					that.$set(that, `two`, two)
					that.$set(that.btnlist[2], `total`, two.length);
					let thr = list.filter(item => item.goods_score <= 2)
					that.$set(that, `thr`, thr)
					that.$set(that.btnlist[3], `total`, thr.length);
				} else {
					uni.showToast({
						title: res.errmsg,
					});
				}
			},
			async confirm(e) {
				const that = this;
				if (e) {
					let reply = that.reply
					let obj = {
						content: e,
						time: moment().format('YYYY-MM-DD HH:mm:ss')
					}
					reply.reply.push(obj)
					const arr = await that.$api(`/goodsRate/${that.reply._id}`, 'POST', reply)
					if (arr.errcode == '0') {
						uni.showToast({
							title: `回复成功`,
							icon: 'success',
						});
						that.$set(that, `reply`, {});
					} else {
						uni.showToast({
							title: arr.errmsg,
							icon: 'none',
						})
					}
				} else {
					that.$set(that, `reply`, {});
				}

			},
			// 回复
			toReply(e, index) {
				const that = this;
				that.$set(that, `reply`, e);
				that.$set(that.reply, `index`, index);
			},
			// 放大
			toLarge(e, index) {
				let url = e.reply[0].file.map(item => item.url)
				uni.previewImage({
					current: index,
					urls: url
				})
			},
			// 选择
			toSelect(e) {
				const that = this;
				that.$set(that, `code`, e.code);
				if (e.code == '0') {
					that.search()
				} else if (e.code == '1') {
					that.$set(that, `list`, that.one);
				} else if (e.code == '2') {
					that.$set(that, `list`, that.two);
				} else {
					that.$set(that, `list`, that.thr);
				}
			},
		}
	}
</script>

<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;
		background-color: var(--f1Color);

		.one {
			margin: 0 0 2vw 0;

			.one_1 {
				display: flex;
				justify-content: space-around;
				background-color: var(--mainColor);
				margin: 0 0 0.5vw 0;
				padding: 2vw;

				.text1 {
					width: 65vw;
					font-size: var(--font16Szie);
				}

				.text2 {
					font-size: var(--font16Szie);
					color: var(--f85Color);

					text {
						margin: 0 0 0 1vw;
						color: var(--ff0Color);
					}
				}
			}

			.one_2 {
				display: flex;
				justify-content: space-around;
				padding: 2vw;
				background-color: var(--mainColor);
			}
		}

		.two {
			position: relative;
			flex-grow: 1;

			.list {
				display: flex;
				justify-content: space-around;
				align-items: flex-start;
				background-color: var(--mainColor);
				margin: 0 0 2vw 0;
				padding: 2vw;

				.image {
					width: 10vw;
					height: 10vw;
					border-radius: 10vw;

				}

				.iconfont {
					font-size: 30px;
				}

				.list_1 {
					flex-grow: 1;
					width: 85vw;
					margin: 0 0 0 2vw;
					font-size: var(--font14Size);

					.other {
						display: flex;
						justify-content: space-between;

						text:last-child {
							font-size: var(--font12Size);
							color: var(--f85Color);
						}
					}

					.other1 {
						margin: 1vw 0 0 0;
						word-break: break-all;
						word-wrap: break-word;
					}

					.flie {
						display: flex;
						flex-wrap: wrap;
						margin: 1vw 0;

						.images {
							width: 20vw;
							height: 20vw;
							margin: 0 1vw 0 0;
						}
					}

					.other2 {
						font-size: var(--font12Size);
						color: var(--f85Color);
						margin: 2vw 0 0 0;

						.other2_1 {
							display: flex;
							justify-content: space-between;
						}

						.reply {
							display: flex;
							flex-direction: column;
							width: 70vw;
							word-wrap: break-word;
							word-break: break-all;
						}

						.reply_2 {
							display: flex;
							flex-direction: column;
							word-wrap: break-word;
							word-break: break-all;
						}
					}
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}
</style>
