<template>
  <view class="mind">
	  <picker range-key="dictLabel" :disabled="daflag||studentlist.length<1" :range="studentlist"  :value="nameidx"  @change="bindDateChangea">
	  		  <view class="flexc mibox mb20">
	  			  <view class="tit" style="margin-bottom: 0;">人员</view>
	  			  <view class="f15 flex1 txr co16" v-if="name" >{{name}}</view>
	  			  <view class="f15 flex1 txr coa" v-else >请选择人员</view>
	  			  <image :src="rimg" class="rimg"></image>
	  		  </view>
	  </picker>
	  <picker range-key="dictLabel" :range="xlhealthrange"  :value="mindidx"  @change="bindDateChanged">
		  <view class="flexc mibox mb20">
			  <view class="tit" style="margin-bottom: 0;">心理健康状态</view>
			  <view class="f15 flex1 txr co16" v-if="mind" >{{mind}}</view>
			  <view class="f15 flex1 txr coa" v-else >请选择心理健康状态</view>
			  <image :src="rimg" class="rimg"></image>
		  </view>
	  </picker>
	  <view class='mibox'>
		<view class="tit">心理健康描述</view>
		<textarea placeholder="请输入心理健康描述详情…" v-model="psychologicalDescription" :auto-height="autoHeight"></textarea>
	  </view>
	  <view class="rebtn mt30" @click="getSubmit">确认</view>
  </view>
</template>

<script>
	// import tabSearch from "@/components/toptab/search.vue"
	import {getDictionaryFn} from '@/api/mine/register.js'
	import {getmindAddFn} from "@/api/mine/files.js"
  export default {
	data(){
		return{
			rimg:require("@/mine/static/score/rimg.png"),
			autoHeight:true,
			xlhealthrange:[],
			mindidx:0,
			mind:'',
			datainfo:{
				
			},
			roles:'',
			studentlist:[],
			name:'',
			nameidx:0,
			studentId:'',
			psychologicalDescription:'',
			daflag:false,
		}
	},
	onLoad: function(e) {
		this.init();
		if(e.type&&e.type=='daup'){
			this.name=e.name;
			this.studentId=e.id;
			this.daflag=true;
		}else{
			var roles=this.$store.state.user.choseroles;
			var newArr=this.$store.state.user.student;
			if(newArr&&newArr.length){
				if(newArr&&newArr.length>1){
					this.studentlist = newArr.map(v => {
						return {
							dictLabel:v.studentName,
							dictValue: v.id,
						}
					})
				}else{
					// 一个娃
					this.name=newArr[0].studentName
					this.studentId=newArr[0].id
				}
			}
			
		}
	},
	methods:{
		init(){
			//心理健康状态
			getDictionaryFn('psychology').then(res=>{
				if(res.code==200){
					this.xlhealthrange = res.data.map(v => {
						return {
							dictLabel: v.dictLabel,
							dictValue: v.dictValue
						}
					})
				}
			})
		},
		bindDateChangea(e){
			var idx=e.detail.value
			this.name=this.studentlist[idx].dictLabel
			this.studentId=this.studentlist[idx].dictValue
		},
		bindDateChanged(e){
		 	var idx=e.detail.value
		 	this.mind=this.xlhealthrange[idx].dictLabel
		},
		getSubmit(){
			// 学生 id
			var roles=this.$store.state.user.choseroles;
			
			var that=this;
			if(!this.studentId){
				this.$toast('请选择人员')
				return
			}
			if(!this.mind){
				this.$toast('请选择心理健康状态')
				return
			}
			var params={
				studentId:this.studentId,
				mind:this.mind,
				psychologicalDescription:this.psychologicalDescription
			}
			if(roles=='teacher'){
				params.createType='2'
			}else if(roles=='parents'){
				params.createType='1'
			}
			getmindAddFn(params).then(res => {
				if (res.code == 200) {
					that.$toast('新增成功')
					setTimeout(function() {
						if(that.daflag){
							uni.$emit('refreshdalist');
							uni.redirectTo({
								url:'/mine/pages/files/mindlist?type=da&id='+that.studentId+'&name='+that.name
							})
						}else{
							uni.$emit('refreshmindlist');
							uni.navigateBack({
								delta:1
							})
						}
					}, 1500)
				} else {
					this.$toast(res.msg)
				}
			})
		},
	},
	
  }
</script>

<style lang="scss" scoped>
.mind{
	min-height: 100vh;background: #ffffff;padding: 80rpx 60rpx;
	.mibox{
		.tit{font-size: 32rpx;font-weight: 500;color: #161616;margin-bottom: 40rpx;}
		textarea{border: 2rpx solid #FFB132;border-radius: 20rpx;width: 100%;min-height: 230rpx;padding:32rpx;box-sizing: border-box;}
		.rimg{width: 14rpx;height: 24rpx;flex: 0 0 auto;margin-left: 12rpx;}
	}
}
</style>
