<template>
  <el-dialog v-model="visible" width="50%" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" center>
    <template #title>
      <div style="margin: 0 auto;width:100%;text-align:center;font-size:18px;font-weight:bold;">
        登录角色选择
      </div>
    </template>
    <div style="margin: 0 auto;width:100%;text-align:center;font-size:18px;font-weight:bold;">
      <el-radio-group v-model="radio">
        <el-radio-button v-for="(item,index) in allRole"  :key="index" :label="item.roleCode" @click.native="handleChangeRole(item.roleCode)">{{item.roleName}}</el-radio-button>
      </el-radio-group>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import {listAllRole} from '/@/api/admin/role'
import {Local, Session} from '/@/utils/storage';
import {useMessage} from "/@/hooks/message";
// import {querySchoolName} from "/@/api/admin/tenant"

const visible=ref(false)
const radio=ref('')
const allRole = reactive<any[]>([])
const schoolName=ref('')

const open = () => {
  listAllRole().then(res => {
    Object.assign(allRole, res.data)
    radio.value = Local.get('roleCode')
    visible.value = true
  })
}

// 必须选择角色后才能关闭，禁止点击遮罩/ESC 关闭（不调用 done 即不关闭）
const handleBeforeClose = (_done: () => void) => {
  useMessage().warning('请先选择登录角色')
}

const handleChangeRole = (label: any) => {
  let obj:any=allRole.find((v:any) => v.roleCode == label)
  Local.set("roleCode",obj.roleCode)
  Local.set("roleName",obj.roleName)
  Local.set("roleId",obj.roleId)
  useMessage().success("操作成功")
  setTimeout(()=>{
    window.location.reload()
  },500)
}

// const handleQuerySchoolName=()=>{
//   querySchoolName({id:Session.get("tenantId")}).then((res:any)=>{
//     schoolName.value=res.data
//   })
// }

defineExpose({
  open
})
</script>

<style scoped>

</style>