<template>
	<view class="app-container">
		<view class="form-container">
			<view class="top">
				<view class="left">您对订单满意吗？</view>
				<view class="right" @tap="isAnonymous = !isAnonymous">
					<image src="@/static/icon/select_0.png" v-if="!isAnonymous"></image>
					<image src="@/static/icon/select_1.png" v-if="isAnonymous"></image>
					<text>匿名评价</text>
				</view>
			</view>
			<view class="rate">
				<view class="item">
					<view class="left">商品质量：<uni-rate :size="18" :margin="4" v-model="rateVal1" /></view>
					<view class="right">{{rateVal1 | rateFilter}}</view>
				</view>
				<view class="item">
					<view class="left">服务质量：<uni-rate :size="18" :margin="4" v-model="rateVal2" /></view>
					<view class="right">{{rateVal2 | rateFilter}}</view>
				</view>
				<view class="item">
					<view class="left">配送质量：<uni-rate :size="18" :margin="4" v-model="rateVal3" /></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 class="bottom-container">
			<view class="button" @tap="submitForm">提交</view>
		</view>
	</view>
</template>

<script>
	import { base_url } from '@/utils/config.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.$axios({
					url: '/order/comment/tag/list',
					method: 'get',
				}).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() {
				const files = await new Promise((resolve,reject)=>{
					uni.chooseImage({
						count: 9 - this.images.length,
						success:res=>{
							resolve(res.tempFilePaths)
						},fail:err=>{
							reject()
						}
					})
				});
				uni.showLoading({
					title:'上传中'
				})
				let arr = [];
				files.forEach(item=>{
					arr.push(this.uploadFile(item))
				});
				Promise.all(arr).then(res=>{
					this.images = this.images.concat(res.map(item=>{
						return JSON.parse(item).data;
					}))
					console.log(this.images);
				}).catch(err=>{
					uni.showModal({
						title:'上传失败'
					})
				}).finally(()=>{
					uni.hideLoading();
				})
			},
			
			// 上传图片
			async uploadFile(file) {
				const result = new Promise((resolve, reject) => {
					uni.uploadFile({
						url: base_url + '/common/upload',
						header: {
							"Content-Type": 'multipart/form-data',
							"x-token": uni.getStorageSync('token')
						},
						name: 'file',
						filePath: file,
						success(res) {
							resolve(res.data)
						}
					})
				})
				return result;
			},
			
			// 提交
			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) {
					return this.$toast('请选择评价标签');
				}
				let imgIds = [];
				this.images.forEach(item => {
					imgIds.push(item.id);
				})
				this.$axios({
					url: '/order/comment/add',
					type: 'application/json',
					params: {
						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('提交成功');
					// uni.$emit('refreshOrderList', res.data);
					uni.$emit('refreshOrderDetail', res.data);
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 1000)
				})
			},
		}
	}
</script>

<style lang="scss">
	.app-container {
		background: #F4F2F2;
		padding: 20rpx 20rpx 150rpx;
		box-sizing: border-box;
	}
	.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;
				image {
					width: 28rpx;
					height: 28rpx;
					margin-right: 16rpx;
				}
			}
		}
		.rate {
			.item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 10rpx 0;
				.left {
					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: #FE781F;
					color: #FFFFFF;
					border: 1px solid #FE781F;
				}
			}
		}
		.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;
				}
			}
		}
	}
	
	.bottom-container {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 0 20rpx;
		box-sizing: border-box;
		height: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		background: #FFFFFF;
		border-top: 1px solid #F4F2F2;
		.button {
			width: 100%;
			height: 70rpx;
			line-height: 70rpx;
			text-align: center;
			border-radius: 70rpx;
			background: linear-gradient(-90deg,#ff3f42 0%, #fe781f 100%);
			font-size: 32rpx;
			color: #FFFFFF;
			
		}
	}
</style>
