<template>
	<!-- #ifdef H5 -->
	<zj-page-layout :hasFooter="true">
		<view style="padding: 20rpx 20rpx 150rpx;">
			<view class="form-container">
				<view class="top">
					<view class="left">您对订单满意吗？</view>
					<view class="right" @tap="isAnonymous = !isAnonymous">
						<text class="iconfont icon-danxuan" v-if="!isAnonymous"></text>
						<text class="iconfont icon-danxuan2 active" v-if="isAnonymous"></text>
						<text>匿名评价</text>
					</view>
				</view>
				<view class="rate">
					<view class="item">
						<view class="left">商品质量：<u-rate :count="5" v-model="rateVal1"></u-rate></view>
						<view class="right">{{rateVal1 | rateFilter}}</view>
					</view>
					<view class="item">
						<view class="left">服务质量：<u-rate :count="5" v-model="rateVal2"></u-rate></view>
						<view class="right">{{rateVal2 | rateFilter}}</view>
					</view>
					<view class="item">
						<view class="left">配送质量：<u-rate :count="5" v-model="rateVal3"></u-rate></view>
						<view class="right">{{rateVal3 | rateFilter}}</view>
					</view>
				</view>
				<view class="title" v-if="tagList.length > 0">选择评价标签：</view>
				<view class="tags" v-if="tagList.length > 0">
					<block v-for="(item, index) in tagList" :key="index">
						<view class="item" :class="item.checked ? 'current':''" @tap="changeTag(item, item.checked, index)">{{item.tag}}</view>
					</block>
				</view>
				<view class="title">评价内容：</view>
				<view class="content">
					<textarea maxlength="120" v-model="content"></textarea>
					<view class="length">{{content.length > 120 ? 120 : content.length}}/120</view>
				</view>
				<view class="title">上传图片：<span>(最多9张)</span></view>
				<view class="images">
					<block v-for="(item, index) in images" :key='index'>
						<view class="img">
							<image :src="item.url" mode="aspectFill"></image>
							<text @tap="delImage(index)">x</text>
						</view>
					</block>
					<view class="add"  @tap="addImage" v-if="images.length < 9">
						<image src="@/static/icon/camera.png"></image>
						<text>点击上传</text>
					</view>
				</view>
			</view>
		</view>

		<template slot="footer">
			<view class="footer-btn-group">
				<u-button type="primary" text="提交" @click="submitForm"></u-button>
			</view>
		</template>
	</zj-page-layout>
	<!-- #endif -->
	<!-- #ifndef H5 -->
	<web-view :src="webViewHref(`/packageMine/pages/order/evaluate`, pam,crossPagePam)" @message="crossPage.$listener"></web-view>
	<!-- #endif -->
</template>

<script>
  // #ifdef H5
  import { uploadImgFull } from '@/common/utils/util.js'

	export default {
		filters: {
			rateFilter(val) {
				if(!val) {
					return '';
				}else if(val === 5) {
					return '满意';
				}else if(val >= 3) {
					return '中评';
				}else if(val >= 1) {
					return '不满意';
				}
			}
		},

		data() {
			return {
				orderId: null, // 订单id
				isAnonymous: false,
				tagList: [],
				rateVal1: 1,
				rateVal2: 1,
				rateVal3: 1,
				content: '',
				images: [],
			}
		},

		onLoad({orderId}) {
			this.orderId = orderId;
			this.getTagList();
			this.rateVal1 = 0;
			this.rateVal2 = 0;
			this.rateVal3 = 0;
		},

		methods: {
			getTagList() {
				this.$api.get('/order/comment/tag/list')
        .then(res => {
					if(res.data && res.data.length > 0) {
						res.data.forEach(item => {
							item.checked = false;
						})
						this.tagList = res.data;
					}else {
						this.tagList = [];
					}

				})
			},

			changeTag(item, checked, index) {
				checked = !checked;
				this.$set(this.tagList, index, {...item, checked });
			},

			// 删除图片
			delImage(index) {
				this.images.splice(index, 1);
			},

      async addImage() {
      	uni.chooseImage({
      		count: 9 - this.images.length,
      		sizeType: ['original'],
      		success: res => {
      			uni.showLoading();
      			res.tempFilePaths.forEach(async item => {
      				let data = await uploadImgFull(item);
      				this.images.push(data);
      			});
      			uni.hideLoading();
      		},
      		fail: err => {}
      	})
      },

			// 提交
			submitForm() {
				if(!this.rateVal1 || !this.rateVal2 || !this.rateVal3) {
					return this.$toast('请对该订单进行评分');
				}
				if(!this.content) {
					return this.$toast('请填写评价内容');
				}
				let tags = [];
				this.tagList.forEach(item => {
					if(item.checked) {
						tags.push(item.tag);
					}
				})
				if(tags.length <= 0 && this.tagList.length > 0) {
					return this.$toast('请选择评价标签');
				}
				let imgIds = [];
				this.images.forEach(item => {
					imgIds.push(item.id);
				})
				this.$api.postJson('/order/comment/add', {
          orderId: this.orderId,
          commentGoods: this.rateVal1,
          commentService: this.rateVal2,
          commentExpress: this.rateVal3,
          tags: tags,
          content: this.content,
          fileIds: imgIds,
          isAnonymous: this.isAnonymous,
				}).then(res => {
					this.$successToast('提交成功');
					this.crossPage.$emit('refreshOrderList', res.data);
					this.crossPage.$emit('refreshOrderDetail', res.data);
					setTimeout(() => {
						this.$navToPage({
							delta: 1
						}, 'navigateBack')
					}, 1000)
				})
			},
		}
	}
  // #endif
  // #ifndef H5
  export default {
    data() {
      return {
        pam: {},
      }
    },

    onLoad(pam) {
      this.pam = pam;
    },
  }
  // #endif
</script>

<style lang="scss" scoped>
	.form-container {
		padding: 30rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		.top {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20rpx;
			.left {
				font-size: 32rpx;
				font-weight: 500;
			}
			.right {
				display: flex;
				align-items: center;
				color: #666666;
				.iconfont {
					font-size: 32rpx;
					color: #999999;
					&.active {
						color: $theme-color;
					}
				}
			}
		}
		.rate {
			.item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 10rpx 0;
				.left {
					display: flex;
					align-items: center;
          ::v-deep .u-rate__content {
            display: flex;
          }
				}
			}
		}
		.title {
			margin-top: 30rpx;
			span {
				color: #999999;
			}
		}
		.tags {
			display: flex;
			flex-wrap: wrap;
			.item {
				height: 48rpx;
				padding: 0 30rpx;
				border-radius: 48rpx;
				border: 1px solid #eaeaea;
				font-size: 24rpx;
				color: #333333;
				line-height: 48rpx;
				margin-right: 20rpx;
				margin-top: 20rpx;
				&.current {
					background: $theme-color;
					color: #FFFFFF;
					border: 1px solid $theme-color;
				}
			}
		}
		.content {
			border: 1px solid #eaeaea;
			border-radius: 10rpx;
			position: relative;
			margin-top: 20rpx;
			textarea {
				width: 100%;
				height: 200rpx;
				padding: 20rpx;
				box-sizing: border-box;
			}
			.length {
				position: absolute;
				right: 20rpx;
				bottom: 20rpx;
				font-size: 24rpx;
				color: #999999;
				line-height: 24rpx;
			}
		}
		.images {
			display: flex;
			flex-wrap: wrap;
			margin-top: 20rpx;
			.add {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				width: 146rpx;
				height: 146rpx;
				border: 2rpx dashed #dadada;
				border-radius: 10rpx;
				image {
					width: 48rpx;
					height: 34rpx;
					display: block;
					margin-bottom: 8rpx;
				}
				text {
					font-size: 22rpx;
					color: #999999;
					line-height: 24rpx;
				}
			}
			.img {
				position: relative;
				margin-right: 20rpx;
				margin-bottom: 20rpx;
				image {
					width: 150rpx;
					height: 150rpx;
					border-radius: 10rpx;
					overflow: hidden;
					display: block;
				}
				text {
					position: absolute;
					right: -10rpx;
					top: -10rpx;
					width: 40rpx;
					height: 40rpx;
					border-radius: 50%;
					background: #FF3F42;
					font-size: 28rpx;
					color: #FFFFFF;
					text-align: center;
					line-height: 36rpx;
					display: block;
				}
			}
		}
	}

</style>
