<template>
	<view class="default_page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='填写所在团队信息'></cus-header>
		<cus-team-info-fill @handleConfirm="handleConfirm" :confirmText="confirmText" :qtype="qtype"></cus-team-info-fill>
	</view>
</template>

<script>
	import CusTeamInfoFill from '@/components/CusTeamInfoFill/index.vue'
	export default {
		components:{ CusTeamInfoFill },
		data(){
			return {
				title:'',
				type:'',
				next:'',
				questionnaireId:'',
				confirmText:'下一步',
				qtype:''
			}
		},
		onLoad(options) {
			this.title = options.title||'';
			this.type = options.type;
			this.qtype = options.qtype||'';
			this.questionnaireId = options.questionnaireId||'';
			this.confirmText = options.type?'确定':'下一步';
			this.next = options.next;
			if(this.next) this.confirmText = '下一步';
		},
		methods:{
			handleConfirm(team){
				team.questionnaireId = this.questionnaireId;
				team.coachId = JSON.parse(uni.getStorageSync('userInfo')).id;
				this.$api.post('/core/user/team',team).then(({data:res})=>{
					if(res.code!==0) return this.$showToast(res.msg)
					if(this.next){
						this.$api.post('/core/team/questionnaire/publish',{
							answerSetting:1,
							coachId:JSON.parse(uni.getStorageSync('userInfo')).id,
							startTime:new Date().Format('yyyy-MM-dd hh:mm:ss'),
							endTime:new Date(new Date().setDate(new Date().getDate()+30)).Format('yyyy-MM-dd hh:mm:ss'),
							questionnaireId:this.questionnaireId,
							teamId:res.data.teamId,
							type:1
						}).then(({data:res2})=>{
							if(res2.code!==0) return this.$showToast(res2.msg)
							this.$showToast('保存成功，即将填写问卷')
							setTimeout(()=>{
								uni.removeStorageSync('newUser')
								uni.navigateTo({
									url:'/pagesPublish/questionnaireFill?teamQuestionnaireId='+res2.data+'&teamId='+res.data.teamId+'&type='+this.type+'&title='+this.title
								})	
							},1500)	
						})	
					}else{
						this.$showToast('团队新增成功')
						setTimeout(()=>{
							this.getOpenerEventChannel().emit('saveTeamInfo')
							uni.navigateBack()
						},1500)
					} 
				})	
			}
		}
	}
</script>

<style scoped lang="scss">
	.default_page{
		box-sizing: border-box;
	}
</style>