<template>
	<!-- <u-loadmore v-if="statusFlag" status="loading" :load-text="loadText" color="#C4CCD8" font-size="24" /> -->
	<view v-else class="container-main">
		<view class="enterprise-header">
			<view class="enterprise-img">
				<image :src="baseImgUrl + 'liao-ning/icon_qyzhcx.png'" mode=""></image>
			</view>
			<view class="enterprise-name">
				<view class="name-text">{{ query.productName || query.enterpriseName ||query.name}}</view>
				<view class="score">
					<uni-rate allow-half :value="gradeNum" :size="18" color="#C4CCD8" activeColor="#ED7043" readonly />
					<text v-if="gradeNum == 0">暂无评价</text>
					<text v-else>{{ gradeNum }}分</text>
				</view>
			</view>
		</view>
		<view class="ping-fen">
			<view class="lable">
				<text style="color: #eb5b47; font-size: 40rpx; margin-right: 10rpx">*</text>
				<text style="font-size: 32rpx; font-weight: 400; color: #34373b">评分</text>
			</view>
			<view class="value">
				<uni-rate allow-half v-model="query.grade" :size="25" color="#C4CCD8" activeColor="#ED7043" />
			</view>
		</view>
		<view class="ping-fen" style="
        flex-direction: column;
        justify-content: flex-start;	
        align-items: flex-start;
      ">
			<view class="lable">
				<text style="color: #eb5b47; font-size: 40rpx; margin-right: 10rpx">*</text>
				<text style="font-size: 32rpx; font-weight: 400; color: #34373b">评价内容</text>
			</view>
			<view class="value" style="width: 100%">
				<u-input v-model="query.content" type="textarea" height="200" maxlength='1000'
					placeholderStyle="padding-left:20rpx;color:#C4CCD8;font-size:30rpx" :border="false"
					:auto-height='true' placeholder="请输入建议意见" />
			</view>
		</view>
		<fixedButton @click="addlist" :flag="status" />
	</view>
</template>

<script>
	//登录的api
	import {
		ControllerLogin,
	} from "@/api/login";
	import {
		getSelfInfo
	} from "@/api/system/user.js";
	//存token
	import {
		setToken
	} from "@/utils/auth";

	import {
		addAppraise
	} from "@/api/shianliaoning/enterpriseSEARCH.js";
	import fixedButton from "@/components/fixedButton/fixedButton.vue";
	import {
		enterpriseGrade,
	} from '@/api/shianliaoning/enterpriseSEARCH.js'
	import {
		gardeFood,
	} from "@/api/shianliaoning/foodMsg.js";
	export default {
		components: {
			fixedButton,
		},
		data() {
			return {
				baseImgUrl: getApp().globalData.config.iamgeBaseUrl,
				status: false,
				statusFlag: true,
				loadText: {
					nomore: "已经到底了~",
					loading: "努力加载中",
				},
				query: {
					content: "",
					grade: 0,
				},
				gradeNum: 0,
				//登录账号密码
				loginForm: {
					// pdma: "MTExMTEx",
					pdma: "UEBzc3dvcjY",
					urne: "2100002310090",
				},
			};
		},

		onLoad(e) {
			this.loginGetToken()
			//评价人
			this.query.writer = uni.getStorageSync('USER_DATA').userDto.realName
			if (e.productId) {
				this.query.productId = e.productId;
				this.query.productName = e.productName;
				this.query.type = 1
				this.fdGrade()
			} else if (e.code) {
				this.query.code = e.code;
				this.query.enterpriseName = e.enterpriseName;
				this.query.type = 2
				this.epGrade()
			}
		},
		methods: {
			//模仿登录
			loginGetToken() {
				ControllerLogin(this.loginForm)
					.then((res) => {
						// 保存token
						setToken(res.data.result.userToken);
						// 获取登录人信息
						getSelfInfo().then(msg => {
							uni.setStorage({
								key: "USER_DATA",
								data: msg.result,
							});
						})
					})
					.catch(() => {});
			},
			//获取企业的评价分数
			epGrade() {
				enterpriseGrade(this.query.code).then(msg => {
					this.gradeNum = msg.data;
					this.statusFlag = false
				})
			},
			//获取产品的评价分数
			fdGrade() {
				gardeFood(this.query.productId).then(msg => {
					this.gradeNum = msg.data;
					this.statusFlag = false
				});
			},
			addlist() {
				console.log(this.query);
				if (this.query.content && this.query.grade > 0) {
					addAppraise(this.query).then((res) => {
						console.log(res);
						this.status = true;
						uni.showToast({
							title: "评价成功",
							duration: 1000,
						});
						setTimeout(() => {
							window.history.go(-1)
						}, 1500);
					});
				} else if (!this.query.content) {
					uni.showToast({
						title: "请输入评价内容",
						icon: "none",
						duration: 2000,
					});
				} else {
					uni.showToast({
						title: "请选择评分分数",
						icon: "none",
						duration: 2000,
					});
				}
			},
		},
	};
</script>

<style lang="scss" scoped>
	.container-main {
		box-sizing: border-box;
		padding: 20rpx;

		.enterprise-header {
			background: #ffffff;
			border-radius: 16rpx;
			display: flex;
			align-items: center;
			box-sizing: border-box;
			padding: 20rpx;

			.enterprise-img {
				height: 100rpx;
				width: 100rpx;

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

			.enterprise-name {
				flex: 1;
				box-sizing: border-box;
				padding-left: 20rpx;

				.name-text {
					font-size: 36rpx;
					font-weight: 550;
					color: #34373b;
					margin-bottom: 15rpx;
				}

				.score {
					display: flex;
					align-items: center;

					text {
						margin-left: 20rpx;
						font-size: 26rpx;
						font-weight: 600;
						color: #ed7043;
					}
				}
			}
		}

		.ping-fen {
			margin-top: 20rpx;
			border-radius: 16rpx;
			background-color: #ffffff;
			box-sizing: border-box;
			padding: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
	}
</style>