<template>
  <el-dialog v-model="visible" title="角色切换" width="50%">
    <el-form>
<!--      <el-form-item label="学校">-->
<!--        <el-tag>{{schoolName}}</el-tag>-->
<!--      </el-form-item>-->
      <el-form-item label="角色">
        <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>

      </el-form-item>
    </el-form>


    <template #footer>
<!--      <el-button type="primary" @click="handleChangeRole">切换</el-button>-->
      <el-button  @click="visible=false">关 闭</el-button>
    </template>
  </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([])
// const schoolName=ref('')

const open=()=>{
  visible.value=true
  // handleQuerySchoolName()
  listAllRole().then(res=>{
    Object.assign(allRole,res.data)
    radio.value=Local.get("roleCode")
    visible.value=true
  })
}
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>