<template>
	<view>
		<view class="">
			<form @submit="formSubmit">
				<form-item ref="formItem" :gopay="gopay" :gopaytwo="gopaytwo" :orderSubmitInfo="orderSubmitInfoData"></form-item>
				<view class="uni-btn-v">
					<button form-type="submit" class="btn">提交</button>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	import App from '@/common/js/app.js';
	import formItem from '@/components/template/formItem.vue';
	export default {
		data() {
			return {
				diyformid: '',
				formInfo: {},
				template_type: '',
				activities_id: '',
				gopay:true,//变化触发表单必填项查询
				orderSubmitInfoData:{},
				gopaytwo:true,
			}
		},
		components: {
			formItem
		},
		onLoad(e) {
			this.template_type = e.template_type;
			this.activities_id = e.activities_id;
			this.diyformid = e.diyformid;
			this.gopaytwo = false;
			this.getJoin();
		},
		methods: {
			formSubmit: function(e) {
				this.gopay = false;
				this.submitJoin();
			},
			// 获取报名表单
			getJoin(){
				let _this = this,
				data = {
					diyformid: this.diyformid
				}
				App._post_form('&p=enroll&do=getDiyForm', data, res => {
					this.orderSubmitInfoData = res.data;
					this.formInfo = res.data.diyform;
					this.gopaytwo = true;
				})
			},
			// 提交报名表单
			submitJoin(){
				let _this = this,
				data = {
					diyformid: this.diyformid,
					// form_data: JSON.stringify(this.formInfo),
					template_type: this.template_type,
					activities_id: this.activities_id
				}
				
				
				console.log(this.$refs.formItem);
				setTimeout(()=>{
					let datas;
					if(_this.$refs.formItem && _this.orderSubmitInfoData.diyform){
						if( _this.$refs.formItem.ifrequired == false){
							uni.showToast({
								icon: 'none',
								title: '请完善必填信息'
							});
							return
						}
						datas = _this.$refs.formItem.formList;
						// console.log(JSON.stringify(datas), 'JSON.stringify(datas)--------')
						data.form_data = JSON.stringify(datas);
					}
					
					uni.showModal({
						title: '温馨提示',
						content: '请确认是否报名',
						success(res) {
							if(res.confirm){
								App._post_form('&p=enroll&do=saveDiyForm', data, res => {
									uni.showToast({
										title: res.message
									})
									this.gopay = true;
									setTimeout(()=>{
										uni.navigateBack({
											delta: 1
										})
									}, 1000)
								})
							}
						}
					})
				})
				
			},
		}
	}
</script>

<style scoped>
.uni-form-item .title {
	padding: 20rpx 0;
}
.btn{
	color: #fff;
	background-color: #82d0f7;
	border-radius: 100px;
	margin: 10px 24rpx;
}
</style>
