<template>
    <div class="join-wrap">
        <div class="join-main-box">
            <p class="join-title">
                <span>
                    {{$t('cusMgt.join.title')}}
                </span>
            </p>
            <div v-show="isJoin" style="margin-bottom:50px">
                <p class="join-title">
                    <span>
                        {{$t('cusMgt.join.title')}}
                    </span>
                </p>
                <Icon type="md-checkmark-circle-outline" color="#19be6b" size="120" />
                <p class="success-tips">{{userName}}, {{$t('cusMgt.join.hasJoin')}}</p>
                
            </div>
            <p v-show="!isJoin" class="course-name">{{cusName || $t('cusMgt.join.errorInfo')}}</p>
            <div style="width:fit-content;margin: auto;">
                <p class="info-item" v-show="isJoin">
                    <span class="info-lable">
                        {{$t('cusMgt.join.cusLabel')}}
                    </span>
                    <span class="info-value">
                        {{cusName}}
                    </span>
                </p>
                <p class="info-item">
                    <span class="info-lable">
                        {{$t('cusMgt.join.teacherLabel')}}
                    </span>
                    <span class="info-value">
                        {{tName}}
                    </span>
                </p>
                <p class="info-item">
                    <span class="info-lable">
                        {{$t('cusMgt.join.listLabel')}}
                    </span>
                    <span class="info-value">
                        {{listName}}
                    </span>
                </p>
                <router-link v-show="isJoin" :to="{name:'login', query:{code}}" style="margin-top:50px;display:block" href="#">{{$t('cusMgt.join.toTeammodel')}}</router-link>
            </div>

            <div class="join-btn" @click="toLogin()" v-show="!isJoin">
                {{$t('cusMgt.join.joinBtn')}}
            </div>

        </div>
        <!-- <div v-show="isJoin" class="join-main-box">
            <p class="join-title">
                <span>
                    {{$t('cusMgt.join.title')}}
                </span>
            </p>
            <Icon type="md-checkmark-circle-outline" color="#19be6b" size="120" />
            <p class="success-tips">{{$t('cusMgt.join.hasJoin')}}</p>
        </div> -->
    </div>
</template>
<script>
import jwtDecode from 'jwt-decode'
export default {
    data() {
        return {
            isLogin: false,
            isJoin: false,
            tId: '',
            tName: '',
            listNo: '',
            listName: '',
            cusName: '',
            code: '',
            userId: '',
            userName: '',
            id_token: '',
            China: {
                srvAdr: 'China',
                clientID: 'c7317f88-7cea-4e48-ac57-a16071f7b884',
                accAPIUrl: 'https://account.teammodel.cn',
                coreAPIUrl: 'https://api2.teammodel.cn',
                domainUrl: [
                    {
                        station: 'product',
                        url: 'https://www.teammodel.cn'
                    },
                    {
                        station: 'test',
                        url: 'https://test.teammodel.cn'
                    },
                ]
            },
            Global: {
                srvAdr: 'Global',
                clientID: '531fecd1-b1a5-469a-93ca-7984e1d392f2',
                accAPIUrl: 'https://account.teammodel.net',
                coreAPIUrl: 'https://api2.teammodel.net',
                domainUrl: [
                    {
                        station: 'product',
                        url: 'https://www.teammodel.net'
                    },
                    {
                        station: 'test',
                        url: 'https://test.teammodel.net'
                    },
                ]
            }
        }
    },
    methods: {
        joinList() {
            this.$api.courseMgmt.qrCodeJoinList({
                stuListNo: this.listNo,
                tmdId: this.userId,
                id_token: this.id_token,
                school: undefined //加入行政班时需要传对应编码，个人名单不用传
            }).then(
                res => {
                    if (!res.error) {
                        this.$Message.success(this.$t('cusMgt.join.joinOk'))
                        this.isJoin = true
                    } else {
                        this.$Message.error(this.$t('cusMgt.join.joinErr'))
                    }
                },
                err => {
                    this.$Message.error(this.$t('cusMgt.join.joinErr'))
                }
            )
        },
        getServeAddr(){
            let hostname = window.location.hostname
            let addr
            let hostarray = hostname.split('.')
            if (hostarray[hostarray.length - 1] == 'net') {
                addr = 'Global'
            } else {
                addr = 'China'
            } 
            return addr
        },
        toLogin() {
            let addr = this.getServeAddr()
            // let type = process.env.NODE_ENV //product | development
            let clientId, accUrl
            //国际站
            if (addr == 'Global') {
                clientId = this.Global.clientID
                accUrl = this.Global.accAPIUrl
            }
            // 大陆站
            else {
                clientId = this.China.clientID
                accUrl = this.China.accAPIUrl
            }
            let callback = decodeURIComponent(window.location.href)
            let state = this.$jsFn.getBtwRandom(1000, 9999)
            let nonce = this.$jsFn.uuid()
            let loginUrl = `${accUrl}/oauth2/authorize?response_type=code&client_id=${clientId}&state=${state}&nonce=${nonce}&redirect_uri=${encodeURIComponent(callback)}`
            window.location.href = loginUrl
        }
    },
    created() {
        this.tId = this.$route.query.tId //教师id
        this.tName = this.$route.query.tName //教师姓名
        this.listNo = this.$route.query.listNo   //名单id
        this.listName = this.$route.query.listName //名单
        this.cusName = this.$route.query.cusName  //课程名称
        this.code = this.$route.query.code  //登录成功返回的code
        if (!this.listNo) {
            this.$Modal.error({
                title: this.$t('cusMgt.join.errorTile'),
                content: this.$t('cusMgt.join.errorContent')
            })
        } else if (this.code) {
            //获取登录信息
            let addr = this.getServeAddr()
            let host = addr == 'Global' ? this.Global.coreAPIUrl : this.China.coreAPIUrl
            let clientId = addr == 'Global' ? this.Global.clientID : this.China.clientID
            this.$api.service.getToken(host, {
                grant_type: "authorization_code",
                client_id: clientId,
                code: this.code
            }).then(
                res => {
                    if (!res.error) {
                        this.id_token = res.id_token
                        let tokenData = jwtDecode(res.id_token)
                        if (tokenData) {
                            this.userId = tokenData.sub
                            this.userName = tokenData.name
                            this.joinList()
                        } else {
                            this.$Message.error(this.$t('cusMgt.join.parseErr'))
                        }
                    } else {
                        this.$Message.error(this.$t('cusMgt.join.getErr'))
                    }
                },
                err => {
                    this.$Message.error(this.$t('cusMgt.join.getErr'))
                }
            )

        }
    }
}
</script>
<style scoped lang="less">
.success-tips {
    color: white;
    font-size: 16px;
    margin-top: 20px;
}
.join-wrap {
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: center;
    width: 100%;
    height: 100%;
    background-image: url("../../assets/image/bak_light.jpg");
}
.join-btn {
    cursor: pointer;
    width: 100%;
    margin: auto;
    margin-top: 60px;
    text-align: center;
    border: 1px solid rgba(25, 190, 107, 0.5);
    // color: rgba(25, 190, 107, 1);
    color: white;
    padding: 4px 30px;
    border-radius: 5px;
    font-size: 16px;
    letter-spacing: 2px;
    font-weight: 400;
    user-select: none;
    background: rgba(25, 190, 107, 0.5);
}
.course-name {
    color: white;
    margin-bottom: 15px;
    font-size: 30px;
    // font-family: cursive;
}
.join-main-box {
    max-width: 90%;
    width: fit-content;
    text-align: center;
    .info-item {
        margin-top: 20px;
        font-size: 15px;
        width: fit-content;
    }
    .info-lable {
        color: #a5a5a5;
    }
    .info-value {
        color: #eeeeee;
    }
}
.join-title {
    position: absolute;
    top: 15px;
    text-align: center;
    width: 100%;
    left: 0px;
    border-bottom: 1px dashed;
    padding-bottom: 8px;
}
</style>