<template>
    <div>
        <div class="EntBlo" v-loading="loading">
            <!-- <div style="text-align: center;margin-bottom: 60px;">
                小学部
            </div> -->
            <div v-if="CampusList.length" class="EntBloConList">
                <div class="EntBloCon" @click="loginLiyuan(i)" v-for="(i,index) in CampusList" :key="index">
                    <img style="width: 26px;margin-bottom: 10px;" :src="JSON.parse(i.json).basics.logo" alt="">
                    <div>{{ i.name }}</div>
                </div>
            </div>

            <div v-else class="EntBloCon2" @click="loginBen()">
                <img style="width: 26px;margin-bottom: 10px;" :src="fromL.basics.logo ? fromL.basics.logo : require('../../../assets/img/moren.png')" alt="">
                <div>{{ fromL.basics.title }}</div>
            </div>
        </div>

        <!-- <div class="EntBlo">
            <div style="text-align: center;margin-bottom: 60px;">
                初中部
            </div>
            <div class="EntBloConList">
                <div class="EntBloCon" v-for="i in 1" :key="i">
                    <img style="width: 18px;margin-bottom: 10px;" src="../../../assets/img/tese.svg" alt="">
                    <div>通新岭校区</div>
                </div>
            </div>
        </div> -->
    </div>
</template>

<script>
import { mapGetters } from 'vuex';
import { API_CONFIG } from "@/common/apiConfig";
import store from '../../../store'
import { addrrole } from "@/api/user";

    export default {
        data(){
            return{
                CampusList:[],
                loading:false
            }
        },
        computed: {
            ...mapGetters(['userinfo','fromL']),
        },
        methods:{
            // 跳转本校校区
            loginBen(){
                this.$router.push('/');
            },
            // 跳转校区
            async loginLiyuan(val){
                this.loading = true
                try{
                    let email = ''
                    let userName = JSON.parse(JSON.stringify(this.userinfo.accountNumber))
                    const regEmail = new RegExp("^[A-Za-z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
                    
                    // // 判断用户输入账户带不带后缀
                    if (!regEmail.test(userName)) {
                        console.log('111');
                    } else {
                        const parts = userName.split('@');
                        email = parts[0] + `-${val.code}@${val.code}.com`;
                    }

                    let params = [
                        {
                            functionName: API_CONFIG.ajax_roleUser.functionName, // 调用存储过程的名称
                            uname: email
                        },
                    ];
                    console.log('params',params);
                    
                    
                    // 发起异步请求
                    const res = await this.$ajax.post(API_CONFIG.baseUrl, params);
                    // console.log('res.data[0]',res.data[0]);
                    if (res.data[0].length) {
                        await store.commit('user/SET_USERINFO2', res.data[0][0])

                        await store.commit('user/SET_AppSIGN', '')

                        this.loading = false

                        this.$router.push('/');
                    }else{
                        this.addRoleZh(email, val)
                    }
                } catch (err) {
                    this.loading = false

                    console.error("请求失败，错误信息：", err);
                }
            },
            // 创建校区管理员账号
            async addRoleZh(email, school){
                let params = {
                        username: email,
                        userpassword: 'Coco1234',
                        alias: this.userinfo.username,
                        org: school.orgid,
                        oid: school.schoolid,
                        ph: '',
                        sid: '',
                        cid: "",
                        typ: 1,
                        rol: 1
                    }


                let res = await addrrole(params)
                if (res.data.success != 1) {
                    this.$message.error('创建账号失败')
                }else{
                    this.loginLiyuan(school)
                }
                this.loading = false
            },
            // 获取校区数据
            getData(){
                this.loading = true
                let params = [
                    {
                        functionName: API_CONFIG.select_liYuanCampus.functionName, // 调用存储过程的名称
                        org: this.userinfo.org, //组织id
                    },
                ];
                
                // 发起请求
                this.$ajax
                    .post(API_CONFIG.baseUrl, params)
                    .then((res) => {
                        let data = res.data[0]
                        console.log(data);
                        
                        this.CampusList = data
                        this.loading = false
                    })
                    .catch((err) => {
                        this.loading = false
                        console.error("请求失败，错误信息：", err);
                    });
            }
        },
        mounted(){
            this.getData()
        }
    }
</script>

<style  scoped>
.EntBlo{
    width: 90%;
    margin: auto;
    margin-bottom: 80px;
}
.EntBloConList{
    grid-template-columns: repeat(5, 1fr);
    display: grid;
    gap: 10px;
}


.EntBloCon{
    cursor: pointer;
    padding: 40px 20px;
    background: #fff;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
}
.EntBloCon2{
    cursor: pointer;
    padding: 40px 20px;
    background: #fff;
    border-radius: 10px;
    display: flex;
    width: 200px;
    flex-direction: column;
    align-items: center;
    margin: auto;
}

</style>