<!-- 录入成绩和证书 -->
<template>
	<view class="achievement" id="achievement">
		<u-form :model="form" ref="uForm" class="achievement-form">
			<u-form-item label="考试名称:" label-position="top" :required="true" prop="examName">
				<u-input v-model="form.examName" placeholder="请输入内容" />
			</u-form-item>
			<u-form-item label="成绩:" label-position="top" :required="true">
				<view class="achievement-form-result">
					<u-form-item label="得分" label-width="70" class="item" :border-bottom="false" prop="score">
						<u-input v-model="form.score" border type="number" placeholder="得分" />
					</u-form-item>
					<u-form-item label="满分" label-width="70" class="item" :border-bottom="false" prop="fullScore">
						<u-input v-model="form.fullScore" border type="number" placeholder="满分" />
					</u-form-item>
				</view>
			</u-form-item>
			<u-form-item label="通过日期:" label-position="top" :required="true" prop="passDate">
				<u-input v-model="form.passDate" placeholder="选择通过日期" type="select" @click="dateShow = true" border />
			</u-form-item>
			<u-form-item label="上传证书:" label-position="top" :border-bottom="false">
				<u-form-item label="证书名称" label-width="140" :border-bottom="false">
					<u-input v-model="form.certificateName" placeholder="请输入内容" border />
				</u-form-item>
				<u-form-item label="证书相片" label-width="140" :border-bottom="false">
					<u-upload :action="upload.action" :custom-btn="true" :limitType="['png', 'jpg']" :max-count="2" @on-success="uploadSuccess" @on-remove="uploadRemove">
						<view class="upload-button" slot="addBtn">
							<view class="upload-button-icon">
								<u-image src="../../../static/img/upload-add.png" mode="aspectFill" width="62"
									height="64" />
							</view>
							<view>上传相片</view>
						</view>
					</u-upload>
					<view class="tips">支持jpg.png格式</view>
				</u-form-item>
			</u-form-item>
		</u-form>
		<view class="achievement-submit">
			<view class="achievement-submit-btn" @click="submit">提交</view>
		</view>
		<u-calendar v-model="dateShow" mode="date" @change="dateChange"></u-calendar>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					skillTrainId: '',
					examName: '',
					score: '',
					fullScore: '',
					passDate: '',
					certificateName: '',
					imgUrl: '',
					picList: []
				},
				rules: {
					examName: [
						{ 
							required: true, 
							message: '请输入考试名称', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change','blur'],
						}
					],
					score: [
						{
							required: true, 
							message: '请输入得分', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change','blur'],
						}
					],
					fullScore: [
						{
							required: true, 
							message: '请输入满分', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change','blur'],
						}
					],
					passDate: [
						{
							required: true, 
							message: '请选择通过日期', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change','blur'],
						}
					]
				},
				dateShow: false,
				upload: {
					action: this.config.uploadUrl
				}
			}
		},
		onLoad(page) {
			if (page.id) {
				this.form.skillTrainId = page.id
			}
		},
		methods: {
			/**
			 * 日历选择器选中触发
			 * @param {Object} e
			 */
			dateChange(e) {
				this.form.passDate = e.result;
			},
			/**
			 * 图片上传成功触发
			 * @param {Object} res
			 */
			uploadSuccess(res) {
				if (res.code === 200) {
					this.form.picList.push(res.data.url)
				} else {
					this.$refs.uToast.show({
						title: res.msg,
						type: 'error'
					})
				}
			},
			/**
			 * 移除图片下标
			 * @param {Object} index
			 */
			uploadRemove(index) {
				this.form.picList.splice(index, 1)
			},
			/**
			 * 提交
			 */
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						const form = {
							...this.form
						}
						form.imgUrl = form.picList.join(',');
						delete form.picList;
						this.$u.api.skillTraining.addCertificateApi(form).then(res => {
							console.log(res)
							if (res.code === 200) {
								this.$refs.uToast.show({
									title: '录入成功!',
									type: 'success',
									back: true
								})
							} else {
								this.$refs.uToast.show({
									title: res.msg,
									type: 'error'
								})
							}
						})
					}
				})
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style lang="scss" scoped>
	@import './addAchievementCertificate.scss';
</style>
