<template>
  <a-space style="margin-bottom: 20px">
    <SelectStaff
      v-if="hasRole('superAdmin') || hasRole('admin')"
      :placeholder="`添加成员`"
      @done="addAssetsDevUser"
    />
    <a-button>邀请添加</a-button>
  </a-space>
  <div class="content">
    <table class="ele-table">
      <thead>
        <tr>
          <th>用户ID</th>
          <th>角色</th>
          <th>昵称</th>
          <th>加入时间</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(user, index) in data.users" :key="index">
          <td>
            <span style="padding-left: 5px">{{ user.userId }}</span>
          </td>
          <td>
            <span style="padding-left: 5px">{{ user.nickname }}</span>
          </td>
          <td>
            <span v-if="user.role === 20">项目成员</span>
            <span v-if="user.role === 30">管理员</span>
          </td>
          <td>{{ user.createTime }}</td>
          <td>
            <div v-if="user.role !== 30">
              <a
                @click="removeUser(user)"
                v-if="hasRole('superAdmin') || hasRole('admin')"
              >
                移除
              </a>
            </div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>
<script lang="ts" setup>
  import { User } from '@/api/system/user/model';
  import { Assets } from '@/api/oa/assets/model';
  import { hasRole } from '@/utils/permission';
  import { AssetsUser } from '@/api/oa/assets/user/model';
  import { addAssetsUser, removeAssetsUser } from '@/api/oa/assets/user';
  import { message } from 'ant-design-vue';

  const props = defineProps<{
    assetsId: any;
    data: Assets;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 添加开发成员
  const addAssetsDevUser = (data: User) => {
    addAssetsUser({
      userId: data.userId,
      assetsId: props.data?.assetsId,
      role: 20,
      nickname: data.nickname
    })
      .then((msg) => {
        message.success(msg);
        emit('done');
      })
      .catch((e) => {
        message.error(e.message);
      });
  };

  // 移除成员
  const removeUser = (data: AssetsUser) => {
    removeAssetsUser(data.id)
      .then((msg) => {
        message.success(msg);
        emit('done');
      })
      .catch((e) => {
        message.error(e.message);
      });
  };
</script>
