<!-- 邀请记录 -->
<template>
  <div>
    <v-data-table
      :loading="loading"
      color="#00897B"
      :items="tableData"
      :headers="headers"
    >
      <template #bottom>
        <CtPagination
          v-if="total > 0"
          :total="total"
          :page="query.pageNo"
          :limit="query.pageSize"
          @handleChange="handleChangePage"
        ></CtPagination>
      </template>
    </v-data-table>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'; const { t } = useI18n()
import { timesTampChange } from '@/utils/date'
import { enterpriseInviteRecordPage } from '@/api/recruit/enterprise/enterpriseInvite.js'
defineOptions({name: 'groupAccount-component-record'})
const props = defineProps({
  inviteType: {
    type: [String, Number],
    default: '0' // 类型 (0 邀请同事 | 1 邀请子公司),示例值(2)
  }
})
const total = ref(0)
const tableData = ref([])
const loading = ref(false)
const query = ref({
  pageSize: 10,
  pageNo: 1,
  type: props.inviteType - 0
})
const headers = [
  { title: '企业名称', key: 'showInfo.currentAccountEnterpriseAnotherName', sortable: false },
  { title: '邀请人', key: 'showInfo.currentAccountUserName', sortable: false },
  { title: 'invited', key: 'showInfo.invited', sortable: false },
  { title: t('enterprise.userManagement.invitationTime'), key: 'showInfo.time', sortable: false },
]
headers.forEach(e => { if (e.title === 'invited') e.title = props.inviteType - 0 ? '受邀企业' : '受邀人' })

// 获取数据
const getData = async () => {
  loading.value = true
  try {
    const info = JSON.parse(localStorage.getItem('baseInfo')) || null
    const { list, total: number } = await enterpriseInviteRecordPage(query.value)
    const invitedNames = await Promise.all(list.map(async () => getInvited()))
    tableData.value = list.map((e, index) => {
      const showInfo = {
        currentAccountEnterpriseAnotherName: info?.enterpriseAnotherName || '--',
        currentAccountUserName: info?.name || '--',
        invited: invitedNames[index] || '--', // 使用预先获取的invited名称
        time: e.createTime ? timesTampChange(e.createTime) : '--',
      }
      return { showInfo, ...e }
    })
    console.log('tableData.value', tableData.value)
    total.value = number
  } finally {
    loading.value = false
  }
}

const getInvited = async (val) => {
  try {
    if (val) {
      const api = props.inviteType - 0 ? enterpriseInviteRecordPage: enterpriseInviteRecordPage
      const { data } = await api(query.value)
      return data.name
    } else {
      return '-占位-'
    }
  } catch (error) {
    console.log('error', error)
  }
}
getData()

const handleChangePage = (index) => {
  query.value.pageNo = index
  getData()
}
</script>
<style lang="scss" scoped>
</style>
