<template>
	<view class="content">
		<u-form :model="form" ref="form" label-width="140">
			<u-form-item label="评价星级" prop="level" required>
				<u-rate :count="5" v-model="form.level" :active-color="mainColor"></u-rate>
			</u-form-item>
			<u-form-item label="评价内容" prop="estimateContent" required>
				<u-input v-model="form.estimateContent" placeholder="请输入评价内容" type="textarea" auto-height :height="100" />
			</u-form-item>
		</u-form>
		<view class="handle-fix-box">
			<u-button type="warning" shape="circle" :ripple="true" :custom-style="customStyle" @click="submitForm()">提交</u-button>
		</view>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		computed: {
			...mapGetters([
				'mainColor',
				'customStyle',
			])
		},
		data() {
			return {
				classId: '',
				form: {
					level: 0,
					estimateContent: '',
				},
				rules: {
					level: [{
						required: true,
						message: '请选择评价星级',
						trigger: 'change'
					}],
					estimateContent: [{
						required: true,
						message: '请输入评价内容',
						trigger: 'change'
					}],
				},
			}
		},
		onLoad(options) {
			this.classId = options.id
		},
		onReady() {
			this.$refs.form.setRules(this.rules);
		},
		methods: {
			//  提交表单
			submitForm() {
				this.$refs.form.validate(valid => {
					if (valid) {
						NET.request(API.submitEvaluateForm, {
							classId: this.classId,
							...this.form
						}, 'POST').then(res => {
							this.$refs.uTips.show({
								title: '评价成功',
								type: 'success',
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						}).catch(error => {
							this.$refs.uTips.show({
								title: '评价失败',
								type: 'warning',
							})
						})
					}
				});
			},
		},
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		position: relative;
	}
</style>
<style lang="scss" scoped>
	@import "@/static/css/themes.scss";
	.content {
		width: 100%;
		float: left;
		padding: 0 15px 60px 15px;
		box-sizing: border-box;
	}
</style>
