<template>
  <a-table :columns="columns" :data-source="data">
    <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'operation'">
          <a>编辑</a>
        </template>
    </template>
  </a-table>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { message } from 'ant-design-vue'
import { callUser } from '@/apis/call.js'
const columns = [
  {
    title: '名称',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '编码',
    dataIndex: 'code',
    key: 'code',
  },
  {
    title: '标识',
    dataIndex: 'tag',
    key: 'tag',
  },
];
const data = ref([]);
const allOrgs = [];
const allRoles = [];
const allStaffRoles = [];
const allStaffs = [];
onMounted(() => {
  callUser("/organizations/list", {},"get").then((res) => {
    if (res.code != 200) {
      message.error(res.message)
      return
    }
    allOrgs.push(...res.data);
    callUser("/roles/list", {},"get").then((res) => {
        if(res.code){
          if (res.code!= 200) {
            message.error(res.message)
            return
          }
          allRoles.push(...res.data);
        }
        else{
          allRoles.push(...res);
        }
        callUser("/staffRoles/list", {},"get").then((res) => {
          if (res.code!= 200) {
            message.error(res.message)
            return
          }
          allStaffRoles.push(...res.data);
          callUser("/staff/list", {},"get").then((res) => {
            if(res.code){
              if (res.code!= 200) {
                message.error(res.message)
                return
              }
              allStaffs.push(...res.data);
            }
            else{
              allStaffs.push(...res);
            }
            for (let i = 0, len = allOrgs.length; i < len; i++) {
              const item = {
                name: allOrgs[i].orgName,
                code: "",
                tag: "",
                key: allOrgs[i].orgId,
                children: [], 
              }
              const myRoles = allRoles.filter(item => item.orgId == allOrgs[i].orgId);
              for (let j = 0, len = myRoles.length; j < len; j++) {
                const item2 = {
                  name: myRoles[j].roleName,
                  code: myRoles[j].roleCode,
                  tag: myRoles[j].isSystemRole,
                  key: myRoles[j].roleId,
                  children: [], 
                }
                const myStaffRoles = allStaffRoles.filter(item => item.roleId == myRoles[j].roleId);
                for (let k = 0, len = myStaffRoles.length; k < len; k++) {
                  const myStaff = allStaffs.filter(item => item.staffId == myStaffRoles[k].staffId);
                  const item3 = {
                    name: myStaff[k].username,
                    code: myStaff[k].phone,
                    tag: myStaff[k].isSuperadmin,
                    key: myStaff[k].staffId,
                  }
                  item2.children.push(item3);
                }
                item.children.push(item2);
              }
              data.value.push(item);
            }
          })
      })
    })
  })
})
</script>