<template>
    <div class="add-teacher-container">
        <span class="to-train-mgt-add" @click="toTeachers">
            <Icon type="md-arrow-back" size="24"/>
        </span>
		
		<p>请输入手机号、醍摩豆ID或邮箱地址进行搜索</p>
		<Input v-model="serchText" placeholder=""/>
		<Button type="info" @click="doSearch()">搜索教师</Button>
		<div id="serchUserResult" v-if="serchModel == 'user'" key="2">
		    <div class="resultBox">
		        <div class="avatar-div">
		            <img v-if="searchInfo.picture" :src="searchInfo.picture" />
		            <img v-else src="@/assets/image/touxiang.png" />
		        </div>
		        <div class="basicInfo">
		            <h3>{{searchInfo.name}}</h3>
		            <p>{{searchInfo.email}}</p>
		        </div>
		        <div class="serchInfo-btns">
		            <Button class="serchInfoBtn" @click="sendInvite()" :loading="inviteLoading">{{ $t('teachermgmt.addTeacher.btn.sendInvite') }}</Button>
		            <Button class="serchInfoBtn" @click="backHome()">{{ $t('teachermgmt.addTeacher.btn.backHome') }}</Button>
		        </div>
		    </div>
		</div>
		
    </div>
</template>
<script>
export default {
    data(){
        return {
			inviteLoading:false,
			serchText:'',
			searchInfo:null,
			serchModel:'',
			userListImportSuccess:[]
        }
    },
    methods:{
        toTeachers(){
            this.$router.push({
                name:'teacher'
            })
        },
		doSearch(){
			this.searchIdErrorMsg = '';
			if (this.serchText) {
				if (this.serchText.indexOf(0) == 0) this.serchText = this.serchText.substr(1) //開頭有0要去0
				// 向CoreID取得使用者
				this.$store.dispatch('user/getUserFromCoreId', [this.serchText]).then(
					(res) => {
						if (res.code == 1 && res.data.length > 0) {
							this.userListImportSuccess = res.data;
							this.searchInfo = res.data[0];
							this.searchInfo.status = 'invite'; //學校邀請
							this.serchModel = 'user';
						}
						else {
							this.$Message.warning('未搜索到教师！')
							this.serchText = ""
						}
					},
					(err) => {
						this.$Message.error('user/getUserFromCoreId API error!')
					}
				);
			} else {
				this.$Message.warning({
					content: this.$t('teachermgmt.message.info11'),
					duration: 5
				});
			}
		},
		//將搜尋成功/匯入成功資料加入DB
		sendInvite() {
			this.inviteLoading = true
		        this.$store.dispatch('user/addSchoolUserToInvite', this.userListImportSuccess).then(
		        (res) => {
		            if (res.code == 1) {
		                if(this.userListImportSuccess.length > 1) {
		                    this.$Message.info({
		                        content: this.$t('teachermgmt.message.info12') +'。',
		                        duration: 5
		                    });
							this.inviteLoading = false
		                }
		                else {
		                    this.$Message.info({
		                        content: this.$t('teachermgmt.message.info13') +' ' + this.userListImportSuccess[0].name + ' ' + this.$t('teachermgmt.message.info14')+ '。',
		                        duration: 5
		                    });
		                }
						this.backHome()
		            }
		            else {
		                alert('Can not add user.')
		            }
		        },
		        (err) => {
		            this.$Message.error('user/addSchoolUser API error!')
		        })
		},
		backHome() {
		    this.serchText= ''
		    this.searchInfo = {}
		    this.serchModel = ''
		},
    }
}
</script>
<style lang="less">
@import './AddTeacher.less';
</style>