<script setup lang="ts">

//引入MD5
import { md5 } from 'js-md5'

import { getPositionApi, userGroupApi, userRegisterApi } from '@/api/login';
import CpmdHeader from '@/components/CpmdHeader.vue';
import CpmdFooter from '@/components/CpmdFooter.vue'
import { ElMessage } from 'element-plus';

import { onMounted, onUnmounted, reactive, ref } from 'vue'

import { useRoute, useRouter } from 'vue-router'

// 加一个锁表示不能重复点击
const isLock = ref<boolean>(false)
//账号角色
const roleType = ref<string>('2')
//定义账号
const account = ref<string>('')
//定义密码
const password = ref<string>('')
//定义确认密码
const comPassword = ref<string>('')
//定义所选组织架构ID
const group = ref<string>('')
//定义组织架构的名字
const groupName = ref<string>('')
//定义用户姓名
const name = ref('')

const position = ref('')

//选择性别
const sex = ref('')

//定义所选组织架构的数据
const groupData = ref([])

//定义临时变量保存平铺的数据
const ppData = ref<any>([])


const listPosition = ref<any>([])



//此处应该是唯一的报告ID
const id = ref<string>('')
//持久化设置 菜单状态
//刚进入页面就将高度设置为页面需要的
onMounted(() => {
    userGroupFun()
    getPosition()
})

//调用查询接口职务的接口
const getPosition = async () => {
    let res: any = await getPositionApi()
    listPosition.value = res.data
}

//跳转到注册页面的方法
const router = useRouter()
const loginFun = () => {
    router.push({ name: 'login' })

}
const validatePassword = (password: string) => {
    const regex = /^[a-zA-Z0-9!@#~$%^&*(),.?":{}|<>]+$/;
    return regex.test(password);
}
//注册方法
const registerFun = async () => {
    if (isLock.value) {
        return
    }
    isLock.value = true
    //点击注册的方法
    //判断各个字段是否都在---或是否都为空
    if (group.value == '') {

        ElMessage({
            message: '请选择组织架构',
            type: 'warning'
        })
        isLock.value = false
        return
    }

    if (roleType.value == '1') {
        if (position.value == '') {
            ElMessage({
                message: '请选择职务',
                type: 'warning'
            })
            isLock.value = false
            return
        }
    }


    if (account.value == '') {
        ElMessage({
            message: '请输入工号',
            type: 'warning'
        })
        isLock.value = false
        return
    }
    if (!/^[A-Za-z0-9]{3,18}$/.test(account.value)) {
        ElMessage({
            message: '工号只允许输入数字和英文且长度为3到18位',
            type: 'warning'
        })
        isLock.value = false
        return
    }
    if (name.value == '') {
        ElMessage({
            message: '请填写姓名',
            type: 'warning'
        })
        isLock.value = false
        return
    }
    if (name.value.length > 20) {
        ElMessage({
            message: '姓名不能超过20位',
            type: 'warning'
        })
        isLock.value = false
        return
    }
    if (sex.value == '') {
        ElMessage({
            message: '请选择性别',
            type: 'warning'
        })
        isLock.value = false
        return
    }
    if (password.value == '') {
        ElMessage({
            message: '请填写密码',
            type: 'warning'
        })
        isLock.value = false
        return
    }
    if (!validatePassword(password.value)) {
        ElMessage({
            message: '密码规则为数字或英文或字符(@#~$%.,)，可任意组合。',
            type: 'warning'
        })
        isLock.value = false
        return
    }
    if (password.value.length < 6 || password.value.length > 18) {
        ElMessage({
            message: '密码为6到18位',
            type: 'warning'
        })
        isLock.value = false
        return
    }
    if (comPassword.value == '') {
        ElMessage({
            message: '请填写确认密码',
            type: 'warning'
        })
        isLock.value = false
        return
    }
    if (!validatePassword(comPassword.value)) {
        ElMessage({
            message: '密码规则为数字或英文或字符(@#~$%.,)，可任意组合。',
            type: 'warning'
        })
        isLock.value = false
        return
    }
    if (comPassword.value != password.value) {
        ElMessage({
            message: '两次密码输入不一致',
            type: 'warning'
        })
        isLock.value = false
        return
    }


    queryOrgName()
    //开始组织参数
    let params = {
        orgNo: group.value,
        orgName: groupName.value,
        userNo: account.value,
        userName: name.value,
        gender: sex.value,
        password: md5(password.value),
        position: roleType.value == '2' ? -1 : parseInt(position.value),
        roleType: roleType.value
    }
    //如果都通过开始调用方法注册的方法
    const res: any = await userRegisterApi(params)
    isLock.value = false
    if (res.code == 200) {
        ElMessage({
            message: res.msg,
            type: 'success'

        })
        // 跳转到登录页面吧
        router.push({ name: 'login' })

        //如果注册成功过得话
    } else {
        ElMessage({
            message: res.msg,
            type: 'error'

        })
    }

}

//根据id查询组织架构名字的方法
const queryOrgName = () => {
    for (let i = 0; i < ppData.value.length; i++) {
        if (group.value == ppData.value[i].orgNo) {
            groupName.value = ppData.value[i].orgName
        }
    }
}

//-----------------组织架构处理方法start---------------------
//获取组织架构list
const userGroupFun = async () => {
    let res: any = await userGroupApi()
    //数据的得到后新增数据格式的value字段 和id一样    新增数据格式的label  与name一致

    //增加字段的方法
    //先判断返回的是否有数据
    if (res.data.length > 0) {
        //判断当前是  教师还是学生

        let list = roleTypeFilter(res.data)

        let resAdd = addPro(list)
        ppData.value = JSON.parse(JSON.stringify(resAdd))
        let forRes = arrToTree(resAdd)
        let resultRes = deleteChildren(forRes)
        groupData.value = resultRes
    } else {
        groupData.value = []
    }

    //修改数据的字段
    //只取当前的id字段
    //得到值后开始组装数据

}

//过滤数据进行测试
const roleTypeFilter = (val: any) => {
    let data = JSON.parse(JSON.stringify(val))
    let list = []
    if (roleType.value == '1') {
        list = data.filter((item: any) => {
            return item.orgNo.length != 12 || (item.orgNo.length == 12 && item.extend != '-1')
        })
    } else
        if (roleType.value == '2') {
            list = data.filter((item: any) => {
                return (item.orgNo.length != 12) || item.orgNo.length == 12 && item.extend == '-1'
            })
        }

    return list
}

//给数据增加两个字段的方法
const addPro = (val: any) => {
    let data = JSON.parse(JSON.stringify(val))
    for (let i = 0; i < val.length; i++) {
        data[i].value = val[i].orgNo
        data[i].label = val[i].orgName
    }
    return data
}
//非递归方式：将平铺数据转换为树形结构数据
const arrToTree = (arr: any) => {
    let data = arr.filter((item: any) => {
        item.children = arr.filter((e: any) => {
            return item.orgNo === e.parentOrgNo
        })
        return !item.parentOrgNo
    })
    return data;
}


//去除转换树形结构数据后存在的空children
const deleteChildren = (arr: any) => {
    let childs = arr;
    for (let i = childs.length; i--; i > 0) {
        if (childs[i].children) {
            if (childs[i].children.length) {
                deleteChildren(childs[i].children);
            } else {
                delete childs[i].children;
            }
        }
    }
    return arr;
}

//-----------------组织架构处理方法结束---------------------

//轮旋切换页面的方法

//退出页面销毁 方法
onUnmounted(() => {

})
const roleTypeChange = (val: string) => {
    //切换角色时
    //当角色是2时要筛选角色是教师的组织架构
    //当角色是1时要筛选角色是学生的组织架构
    //清空选项
    group.value = ''
    userGroupFun()
}



</script>

<template>
    <div class="home_header_out">
        <div class=" home_header_inner">
            <CpmdHeader />
        </div>
        <div class="leave_message">
            <!-- <img class="img" src="../assets/zs/community.png" alt=""> -->
            <div class="font_blue"> 用户注册 </div>
            <img width="80px" style="position: absolute;left: 100px;bottom: 0;z-index: 11;"
                src="../assets/scale/guo.png" alt="">

        </div>
        <div class="kply">
            <div class="kply_inner">
                <div class="login_top">
                    <span class="des">用户注册</span>
                </div>
                <div style="padding: 10px 50px;">
                    <!-- <div style="margin-top:20px;display: flex;">


                        <div style="flex: 1;">
                            <div class="user_account" style="width: 100%;">用户类型:</div>
                            <div class="input_cus" style="width: 100%;"> <el-radio-group size="large"
                                    @change="roleTypeChange" v-model="roleType">
                                    <el-radio value="2" size="large">我是教师</el-radio>
                                    <el-radio value="1" size="large">我是学生</el-radio>
                                </el-radio-group>
                            </div>
                        </div>

                    </div> -->
                    <div style="margin-top:20px">
                        <div class="user_account">所属组织架构:</div>
                        <div class="input_cus">
                            <el-tree-select v-model="group" placeholder="请选择组织架构" :data="groupData" filterable
                                style="width:100%" />
                        </div>
                    </div>
                    <div style="margin-top:20px;display: flex;">
                        <div style="flex: 1;" v-show="roleType == '1'">
                            <div class="user_account">职务:</div>
                            <div class="input_cus">

                                <el-select v-model="position" placeholder="请选择职务" size="large">
                                    <el-option v-for="item in listPosition" :key="item.paramValue"
                                        :label="item.paramName" :value="item.paramValue" />
                                </el-select>
                            </div>
                        </div>
                        <div style="flex: 1;display: flex;flex-direction: column;justify-content: end;">
                            <div class="user_account"
                                :style="{ width: roleType == '1' ? '90%' : '100%', marginLeft: roleType == '1' ? '10%' : '0%' }">
                                工号:</div>
                            <div class="input_cus"> <el-input v-model="account"
                                    :style="{ width: roleType == '1' ? '90%' : '100%', marginLeft: roleType == '1' ? '10%' : '0%' }"
                                    placeholder="请输入工号" />
                            </div>
                        </div>

                    </div>
                    <div style="margin-top:20px;display: flex;">
                        <div style="flex: 1;">
                            <div class="user_account">姓名:</div>
                            <div class="input_cus"> <el-input v-model="name" style="width: 90%;" placeholder="请输入姓名" />
                            </div>
                        </div>

                        <div style="flex: 1;">
                            <div class="user_account" style="width: 90%;margin-left:10%">性别:</div>
                            <div class="input_cus" style="width: 90%;margin-left:10%"> <el-radio-group size="large"
                                    v-model="sex">
                                    <el-radio value="1" size="large">男</el-radio>
                                    <el-radio value="0" size="large">女</el-radio>
                                </el-radio-group>
                            </div>
                        </div>

                    </div>
                    <div style="margin-top:20px">
                        <div class="user_account">密码:</div>
                        <div><el-input v-model="password" type="password" style="width: 100%" placeholder="请输入密码"
                                show-password />
                        </div>
                    </div>
                    <div style="margin-top:20px">
                        <div class="user_account">确认密码:</div>
                        <div><el-input v-model="comPassword" type="password" style="width: 100%" placeholder="请输入密码"
                                show-password />
                        </div>
                    </div>
                    <div class="go_register">
                        <span @click="loginFun">
                            已有账号，去登录
                        </span>
                    </div>
                    <div class="start_button_out">
                        <div @click="registerFun" class="start_button_self">注册</div>
                    </div>


                </div>

            </div>

        </div>

    </div>

    <div class="home_footer_out">
        <CpmdFooter />
    </div>
</template>
<style></style>
<style lang="scss" scoped>
.home_footer_out {
    width: 100%;
    background-color: #1F2761;
}

:deep(.el-input__wrapper) {
    align-items: center;
    border-radius: 0px !important;
    border: 1px solid #4D6AD5 !important;
    padding-left: 20px;
    box-shadow: none
}

:deep(.el-input__wrapper .is-focus) {
    box-shadow: none
}

:deep(.el-input__inner) {
    height: 36px;
    font-size: 16px;
}

:deep(.el-select__wrapper.is-hovering:not(.is-focused)) {
    box-shadow: none !important
}

:deep(.el-select__placeholder) {
    color: var(--el-input-text-color, var(--el-text-color-regular));
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 100%;
    line-height: 30px;
    font-size: 16px;
}

:deep(.el-select__selection) {
    align-items: center;
    display: flex;
    flex: 1;
    flex-wrap: wrap;
    gap: 6px;
    min-width: 0;
    position: relative;
    height: 30px;

}

:deep(.el-select__wrapper) {
    border-radius: 0px;
    border: 1px solid #4D6AD5;
    padding-left: 20px;
    height: 40px;
    box-shadow: none;
}

:deep(.el-select__input) {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-color: transparent;
    border: none;
    color: var(--el-select-multiple-input-color);
    font-family: inherit;
    font-size: 20px;
    height: 36px;
    max-width: 100%;
    width: 100% !important;
    outline: none;
    padding: 0;
}

:deep(.el-tree) {
    --el-tree-node-content-height: 53px;
    --el-tree-node-hover-bg-color: var(--el-fill-color-light);
    --el-tree-text-color: var(--el-text-color-regular);
    --el-tree-expand-icon-color: var(--el-text-color-placeholder);
    background: var(--el-fill-color-blank);
    color: var(--el-tree-text-color);
    cursor: default;
    font-size: 27px !important;
    position: relative;
}

:deep(.el-radio.el-radio--large .el-radio__inner) {
    height: 20px;
    width: 20px;
}

:deep(.el-radio.el-radio--large .el-radio__label) {
    font-size: 16px;
}

:deep(.el-radio.el-radio--large) {
    height: 36px;
}

:deep(.el-radio__input.is-checked .el-radio__inner) {
    background: #4D6AD5;
    border-color: #B2F2D2;
}

:deep(.el-radio__input.is-checked+.el-radio__label) {
    // color: #48D68E;
}




.home_header_out {
    flex: 1;
    // position: relative;
    padding-bottom: 60px;
    width: 100%;
    min-width: 1200px;
    // background-image: url('../assets/home/bg_ty.png');
    background-repeat: no-repeat;
    background-size: contain;
    // background-color: #B3F1DA; //估计是需要动态
    // scroll-snap-align: start;

    //获取屏幕宽度home_header_out 这个div的宽度--然后宽度*1000再除1920即为当前div的宽度
    .home_header_inner {
        min-height: 1;
        left: 0;
        right: 0;
        margin: auto;
        // height: 100px;
        width: 100%;


        .report_top {
            width: 650px;
            // width: 860px; 210
            margin-left: 285px;
            margin-top: 20px;
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
            margin: auto;

            .xlts_img {
                width: 200px;
            }

            .report_jt {
                display: flex;
                flex-direction: column;
                font-size: 20px;
                font-weight: 700;
                letter-spacing: 3px;
            }
        }
    }

    .leave_message {
        position: relative;

        left: 0;
        right: 0;
        margin: auto;
        width: 650px;
        margin-bottom: 20px;
        margin-top: 20px;

        .font_blue {
            position: relative;
            color: #D31E1B;
            font-weight: 700;
            font-size: 22px;
            font-family: 'Rammetto One-Regular';
            padding-top: 30px;
            z-index: 10;
            background: url(../assets/zs/register1.png) no-repeat;
            background-size: auto 50px;
        }
    }


    .kply {
        width: 100%;
        margin-top: 20px;
        // background-color: #FAFAFA;

        .kply_inner {
            // padding: 20px 20px;
            left: 0;
            right: 0;
            margin: auto;
            width: 650px;
            min-height: 500px;
            background-color: #ffffff;
            border-radius: 5px;
            box-shadow: 0px 4px 32px 0px rgba(0, 0, 0, 0.17);

            .login_top {
                background-color: #4D6AD5;
                line-height: 50px;

                .des {
                    color: #ffffff;
                    font-weight: 700;
                    margin-left: 50px;
                    // background-image: url('../assets/zs/login_line.png');
                    background-repeat: no-repeat;
                    background-size: contain;
                    padding-bottom: 6px;
                    background-position: bottom;
                }
            }

            .user_account {
                font-size: 18px;
                // line-height: 70px;
                margin-bottom: 10px;
                letter-spacing: 5px;
            }


            .start_button_out {
                margin-top: 40px;
                margin-bottom: 20px;
                display: flex;
                flex-direction: row;
                justify-content: center;

                .start_button_self {
                    cursor: pointer;
                    // width: 100px;
                    border-radius: 4px;
                    border: 1px solid;
                    color: #ffffff;
                    background-color: #4D6AD5;
                    margin-right: 20px;
                    padding: 10px 50px;
                    cursor: pointer;
                    display: flex;
                    align-items: center;
                    font-size: 16px;

                }
            }

        }

        .go_register {
            text-align: right;
            margin-top: 20px;

            span {
                font-weight: 700;
                color: #4D6AD5;
                cursor: pointer;
                font-size: 16px;
            }
        }
    }


}



.home_mid {
    // background-color: blanchedalmond;
    width: 1200px;
    left: 0;
    right: 0;
    margin: auto;
    margin-top: 40px;
    // position: relative;




    .kepu_title {
        text-align: center;

        font-size: 50px;
        color: #111111;
        line-height: 95px;

        // width: 100%;
        // left: 0;
        // right: 0;
        // margin: auto
    }

    .kepu_title_sub {
        margin-top: 30px;
        text-align: center;
        font-size: 28px;
        color: #48D68E;
        line-height: 26px;
    }

    .man1_group {
        margin-top: 60px;
        height: 100%;
        display: flex;

        .man1 {
            position: relative;
            width: 327px;
            // height: 100%;

            // height: 100%
            .man1_img {
                position: absolute;
                bottom: 0;
                /* bottom: 0px; */
                /* height: auto; */
                width: 100%;
                left: 100px
            }

        }

        .des {
            border: #48D68E solid 5px;
            border-radius: 40px;
            padding: 20px;
            letter-spacing: 6px;
            flex: 1;

            .des_inner {
                border: 1px dashed #48D68E;
                border-radius: 40px;
                padding-bottom: 40px;
                padding-top: 30px;
                padding-left: 100px;
                padding-right: 20px;
                letter-spacing: 6px;
                font-weight: normal;
                font-size: 24px;
                color: #333333;
                line-height: 40px;
            }
        }
    }

    .man2_group {
        margin-top: 60px;
        height: 100%;
        display: flex;

        .man2 {
            position: relative;
            width: 327px;
            // height: 100%;

            // height: 100%
            .man2_img {
                position: absolute;
                bottom: 0;
                /* bottom: 0px; */
                /* height: auto; */
                width: 100%;
                left: -100px
            }

        }

        .des2 {
            border: #48D68E solid 5px;
            border-radius: 40px;
            padding: 20px;
            letter-spacing: 6px;
            flex: 1;

            .des2_inner {
                border: 1px dashed #48D68E;
                border-radius: 40px;
                padding-bottom: 40px;
                padding-top: 30px;
                padding-left: 20px;
                padding-right: 100px;
                letter-spacing: 6px;
                font-weight: normal;
                font-size: 24px;
                color: #333333;
                line-height: 40px;
            }
        }
    }

    .get_more {
        text-align: center;

        img {
            width: 300px;
            margin-top: 60px;
            margin-bottom: 60px;
        }
    }
}

// .home_footer_out {
//     width: 100%;
//     background-color: #000000;
// }</style>
