<template>
	<view class="content">
		<u-form :model="form" ref="form" label-width="140">
			<u-form-item label="补课学员" prop="content" required label-position="top">
				<u-grid :col="3" :border="false">
					<u-grid-item v-for="(item, index) in userList" :key="index">
						<u-avatar :src="item.url" mode="circle" size="128" show-level level-icon="checkbox-mark" :level-bg-color="mainColor"></u-avatar>
						<view class="grid-text">{{item.name}}</view>
					</u-grid-item>
					<u-grid-item @click.native="goToSelectStudent()">
						<view class="slot-btn">
							<u-icon name="plus" size="60" color="#999999"></u-icon>
						</view>
					</u-grid-item>
				</u-grid>
			</u-form-item>
			<u-form-item label="补课地点" prop="address" required>
				<u-input v-model="form.address" placeholder="请输入补课地点" />
			</u-form-item>
			<u-form-item label="备注" prop="remark" required>
				<u-input v-model="form.remark" type="textarea" placeholder="请输入备注" 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 {
				userList: [],
				form: {
					address: '',
					remark: '',
				},
				rules: {
					address: [{
						required: true,
						message: '请输入补课地点',
						trigger: 'change'
					}],
					remark: [{
						required: true,
						message: '请输入备注',
						trigger: 'change'
					}],
				},
			}
		},
		onLoad() {},
		onReady() {
			this.$refs.form.setRules(this.rules);
		},
		onShow() {
			this.userList = uni.getStorageSync('extraLessonsUserList')
		},
		methods: {
			//  提交表单
			submitForm() {
				this.$refs.form.validate(valid => {
					if (valid) {
						NET.request(API.submitExtraLessonsForm, {
							...this.form,
							studentIdList: this.userList.map(site => {
								return {
									id: site.id
								}
							})
						}, 'POST').then(res => {
							this.$refs.uTips.show({
								title: '提交成功',
								type: 'success',
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						}).catch(error => {
							this.$refs.uTips.show({
								title: error.message,
								type: 'warning',
							})
						})
					}
				});
			},
			//  跳转选择学生页面
			goToSelectStudent() {
				uni.navigateTo({
					url: '/pagesClass/extraLessonsStudentList'
				});
			},
		},
	}
</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;

		/deep/.u-avatar__level {
			left: 50%;
			transform: translateX(-50%);
			bottom: -6px !important;
		}

		.grid-text {
			line-height: 18px;
		}

		.slot-btn {
			width: 128rpx;
			height: 128rpx;
			border-radius: 50%;
			border: 1px solid #999999;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>
