<!-- 订单-评价 -->
<template>
<!-- 1,4 -->
	<!-- 产品 -->
	<view class="card" style="padding: 40rpx 30rpx;">
		<view class="title">{{state.orderInfo.productName}}</view>
		<evaluate-star v-model="state.product.score" title="评价得分"></evaluate-star>
		<textarea class="input-box" v-model="state.product.content" placeholder-class="text-hint" :maxlength="500"
			style="height: 260rpx;" placeholder="从多角度评价，可以帮助我们提升服务质量。">
		</textarea>
		<view class="upload-img">
			<form-image @backImg="backImg($event)" @removeImg="removeImg" :isUrl="false" style="width: 100%;" :retract="-15"></form-image>
		</view>
	</view>
<!-- 权益产品评价 -->
	<view class="card" v-if='state.haveInterestsProduct' style="padding: 40rpx 30rpx;">
		<view class="title">权益产品评价</view>
		<evaluate-star v-model="state.interest.score" title="评价得分"></evaluate-star>
		<textarea class="input-box" v-model="state.interest.content" placeholder-class="text-hint" :maxlength="500"
			style="height: 260rpx;" placeholder="从多角度评价，可以帮助我们提升服务质量。">
		</textarea>
		<view class="upload-img">
			<form-image @backImg="backImgInterest($event)" @removeImg="removeImgInterest" :isUrl="false" style="width: 100%;" :retract="-15"></form-image>
		</view>
	</view>

	<view class="btn">
		<submit-button @submit="publish" title="发布"></submit-button>
	</view>

	<!-- 选择标签弹窗 -->
	<u-popup v-model="state.showTagPop" mode="bottom" height="60%">
		<view v-if="state.showTagPop">
			<tag-popup :selTags="state.user.tagList" :tagAllList="state.tagAllList" @cancel="state.showTagPop = !state.showTagPop" @confirm="confirmSelectTag"></tag-popup>
		</view>
	</u-popup>
</template>

<script setup lang="ts">
	import evaluateStar from "./components/evaluate-star.vue"
	import {reactive} from "vue";
	import {confirm, isBlank, msg} from "@/utils/utils";
	import tagPopup from "./components/popup-order-evaluate-tag";
	import { request } from "@/utils/network/request";
	import { orderDetail, orderEvaluate,orderEvaluateTag,saleMessage,addEvaluation,judageQuanProduct } from "@/utils/network/api";
	import {onLoad} from "@dcloudio/uni-app";
	import { stringToJson } from "@/utils/network/encryption";
	import { getItem, StorageKeys } from "@/utils/storage";

	const state = reactive({
		orderInfo:{} as any, //订单信息
		showTagPop: false, //显示标签选择弹窗
		product: { //产品
			score: 0, //星级得分 最高5分，0.5小数点 2.5分及以下为差评 2.5以上4.0以下为中评 4.0及4.0以上为好评（必选）
			content: '', //评价内容
			imageList: [], //支持拍照上传最多上传9张
		},
		interest: { //产品
			score: 0, //星级得分 最高5分，0.5小数点 2.5分及以下为差评 2.5以上4.0以下为中评 4.0及4.0以上为好评（必选）
			content: '', //评价内容
			imageList: [], //支持拍照上传最多上传9张
		},
		user: { //业务员
			score: 0, //星级得分 最高5分，0.5小数点 2.5分及以下为差评 2.5以上4.0以下为中评 4.0及4.0以上为好评（必选）
			content: '', //评价内容
			tagList: [], //已选择的评价标签列表
		},
		tagAllList:[],
		orderId:"",
		haveInterestsProduct:false,  //是否有权益产品
	})

	/* 确认选择tag */
	const confirmSelectTag = (selectTag) => {
		state.showTagPop = !state.showTagPop
		state.user.tagList = [...selectTag];
	}

	//选择图片
	const backImg = (e: any) => {
		state.product.imageList = e;
	}
	
	//删除图片
	const removeImg = (imgList) => {
		state.product.imageList = imgList
	}
   //选择图片权益产品
	const backImgInterest = (e: any) => {
		state.interest.imageList = e;
	}
	
	//删除图片
	const removeImgInterest = (imgList) => {
		state.interest.imageList = imgList
	}
	/* 删除评价标签 */
	const removeTag = (item: any) => {
		state.user.tagList.map((tag, index) => {
			if (tag.id === item.id) {
				state.user.tagList.splice(index, 1)
			}
		})
	}

	/* 发布 */
	const publish = (e) => {
		addProduct();
	}
	const addProduct=()=>{
		if (state.product.score === 0) {
			msg('请对产品进行打分！');
			return;
		}
		//若不填评价内容，根据打分规则自动填充评价内容 
		//差评展示：默认差评 2.5分及以下
		//中评展示：默认中评 2.5以上4.0以下 
		//好评展示：默认好评 4.0及4.0以上
		if (!state.product.content) {
			if (state.product.score <= 2.5) {
				state.product.content = "默认差评";
			} else if (state.product.score > 2.5 && state.product.score < 4) {
				state.product.content = "默认中评";
			} else {
				state.product.content = "默认好评";
			}
		}
		const options = {
			type: 2,
			data: {
				"orderId":state.orderId,
				"serviceType":"product",
				"score":state.product.score,
				"pictureUrl":state.product.imageList.join(";"),
				"message":'',
				"suggestion":state.product.content
			},
			 method: 'POST',
			 showLoading: true,
		}
		console.log("options",options)
		debugger;
		request(addEvaluation,options).then((res)=>{
			if(state.haveInterestsProduct){
				addProductInterest();
			}else{
				confirm('您的评价已发布成功！',()=>{
					uni.$emit('refreshOrder');
					uni.navigateBack();
				},'发布成功',false);
			}
		})
	}
	const addProductInterest=()=>{
		if (state.interest.score === 0) {
			msg('请对权益产品进行打分！');
			return;
		}
		//若不填评价内容，根据打分规则自动填充评价内容 
		//差评展示：默认差评 2.5分及以下
		//中评展示：默认中评 2.5以上4.0以下 
		//好评展示：默认好评 4.0及4.0以上
		if (!state.interest.content) {
			if (state.interest.score <= 2.5) {
				state.interest.content = "默认差评";
			} else if (state.interest.score > 2.5 && state.interest.score < 4) {
				state.interest.content = "默认中评";
			} else {
				state.interest.content = "默认好评";
			}
		}
		const options = {
			type: 2,
			data: {
				"orderId":state.orderId,
				"serviceType":"equity",
				"score":state.interest.score,
				"pictureUrl":state.interest.imageList.join(";"),
				"message":'',
				"suggestion":state.interest.content
			},
			 method: 'POST',
			 showLoading: true,
		}
		console.log("options",options)
		request(addEvaluation,options).then((res)=>{
			confirm('您的评价已发布成功！',()=>{
				uni.$emit('refreshOrder');
				uni.navigateBack();
			},'发布成功',false);
		})
	}
	/* 获取业务员标签ID */
	const getTagIds = () => {
		const list = [];
		state.user.tagList.map((tag)=>{
			list.push(tag.id);
		});
		return list;
	}
// 获取是否有权益产品
	const isInterestsProduct=(orderId)=>{
		const options = {
			type: 2,
			data: {"orderId": orderId},
			method: 'POST',
			showLoading: true,
		}
		request(judageQuanProduct, options).then((res) => {
			console.log("获取是否有业务员",stringToJson(res.bizContent))
			state.haveInterestsProduct = stringToJson(res.bizContent);
		})
	}
	/* 获取订单详情 */
	const getOrderDetails = (id) => {
		const options = {
			type: 2,
			data: {"id": id},
			method: 'POST',
			showLoading: true,
		}
		request(orderDetail, options).then((res) => {
			console.log("获取订单详情",stringToJson(res.bizContent))
			state.orderInfo = stringToJson(res.bizContent);
		})
	}
	
	onLoad((option) => {
		getOrderDetails(option.id);
		state.orderId=option.orderId
		isInterestsProduct(option.orderId);  //获取是否有权益产品
	})
</script>

<style>
	page {
		background-color: #EEF7F7;
		padding-bottom: 50rpx;
	}
</style>
<style lang="scss" scoped>
	.card {
		background-color: white;
		border-radius: 20rpx;
		box-shadow: 0px 0px 6rpx 2rpx rgba(223, 223, 223, 0.3);
		margin: 30rpx;

		.title {
			text-align: center;
			font-size: 32rpx;
			color: #333333;
			padding-bottom: 30rpx;
		}

		.upload-img {
			margin-top: 10rpx;
		}

		.rate {
			.rate-label {
				font-size: 28rpx;
				color: #333333;
				padding-right: 20rpx;
			}
		}

		.input-box {
			width: 100%;
			border: 1px solid #DCDCDC;
			border-radius: 10rpx;
			background-color: #F3F3F3;
			padding: 30rpx;
			box-sizing: border-box;
			-moz-box-sizing: border-box;
			/*Firefox*/
			-webkit-box-sizing: border-box;
			/*Safari*/
			margin-top: 30rpx;
		}

		.text-hine {
			color: #999999;
			font-size: 26rpx;
		}

		.title1 {
			font-size: 30rpx;
			color: #333333;
			border-bottom: 1px solid #DCDCDC;
			padding: 30rpx;
		}

		.user {
			margin-bottom: 40rpx;

			.avatar {
				width: 140rpx;
				height: 160rpx;
				background-color: #F3F3F3;
				border-radius: 10rpx;

			}

			.user-info {
				margin-left: 40rpx;
				margin-top: 28rpx;

				.name {
					font-size: 28rpx;
					color: #333333;

					&:last-child {
						margin-top: 40rpx;
					}
				}
			}
		}

		.center {
			margin-top: 40rpx;
			padding-bottom: 20rpx;
			border-bottom: 1rpx solid #DCDCDC;
			.tags {
				flex-wrap: wrap;

				.tag {
					border-radius: 28rpx;
					background-color: #F3F3F3;
					padding: 0rpx 20rpx;
					border: 1px solid #DCDCDC;
					margin-right: 18rpx;
					margin-bottom: 15rpx;
					color: #333333;
					font-size: 26rpx;
					display: flex;
					flex-direction: row;
					justify-content: center;
					align-items: center;
					height: 57rpx;

					&:last-child {
						margin-right: 0rpx;
						margin-bottom: 0px;
					}

					.tag-close {
						width: 32rpx;
						height: 32rpx;
						margin-left: 15rpx;
					}
				}
			}
		}
	}

	.btn {
		margin: 60rpx 40rpx 0rpx;
	}
</style>
