<template>
  <div>
    <div class="text-end">
      <v-btn v-if="tab === '0'" color="primary" :disabled="selected.length ? false : true" variant="tonal" @click="handleAction('all', 0)">不合适</v-btn>
      <v-btn v-if="tab === '1'" color="primary" :disabled="selected.length ? false : true" variant="tonal" @click="handleAction('all', 1)">入职</v-btn>
    </div>
    <v-data-table
      class="mt-3"
      v-model="selected"
      :items="items"
      :headers="headers"
      hover
      show-select
      height="60vh"
      item-value="id"
    >
      <template #bottom></template>
      <template v-slot:item.name="{ item }">
        <div class="d-flex align-center cursor-pointer" @click="handleToPersonDetail(item)">
          <v-badge
            bordered
            offset-y="6"
            :color="badgeColor(item)"
            :icon="badgeIcon(item)">
            <v-avatar size="40" :image="item.person.avatar || 'https://minio.citupro.com/dev/menduner/7.png'"></v-avatar>
          </v-badge>
          <span class="defaultLink ml-3">{{ item?.person?.name }}</span>
        </div>
      </template>
      <template v-slot:item.actions="{ item }">
        <div v-if="tab === '0'">
          <v-btn color="primary" variant="text" @click="previewFile(item.url)">查看简历</v-btn>
          <v-btn color="primary" variant="text" @click="handleAction('', 0, item)">不合适</v-btn>
        </div>
        <div v-if="tab === '1'">
          <v-btn color="primary" variant="text" @click="handleAction('', 1, item)">入职</v-btn>
        </div>
      </template>
    </v-data-table>
  </div>
</template>

<script setup>
defineOptions({ name: 'table-page'})
import { ref, computed } from 'vue'
import { previewFile } from '@/utils'
import { joinEliminate, personEntryByEnterprise } from '@/api/enterprise'
import { useI18n } from '@/hooks/web/useI18n'
import Snackbar from '@/plugins/snackbar'

const { t } = useI18n()
const emit = defineEmits(['refresh'])
const props = defineProps({
  tab: String,
  items: Array
})
const badgeColor = computed(() => (item) => {
  return (item.person && item.person.sex) ? (item.person.sex === '1' ? '#1867c0' : 'error') : 'error'
})

const badgeIcon = computed(() => (item) => {
  return (item.person && item.person.sex) ? (item.person.sex === '1' ? 'mdi-gender-male' : 'mdi-gender-female') : 'mdi-gender-female'
})

const selected = ref([])
const defaultHeaders = [
  { title: '牛人', value: 'name' },
  { title: '应聘职位', value: 'job.name' },
  { title: '求职状态', key: 'person.jobStatusName' },
  { title: '工作经验', key: 'person.expName' },
  { title: '最高学历', key: 'person.eduName' },
  { title: '岗位薪资', key: 'job', value: item => `${item.job.payFrom}-${item.job.payTo}/${item.job.payName}`},
  { title: '操作', value: 'actions' }
]
const headers = ref(defaultHeaders)
if (props.tab === '5614') headers.value.splice(headers.value.length - 1, 0, { title: '推荐人', key: 'recommend' })
else if (props.tab === '5615') headers.value.splice(headers.value.length - 1, 0, { title: '已结算赏金', key: 'settlementBounty' })
else headers.value = defaultHeaders

// 人才详情
const handleToPersonDetail = ({ userId, id }) => {
  if (!userId || !id) return
  window.open(`/enterprise/talentPool/details/${userId}?id=${id}`)
}

const apiList = [
  joinEliminate, // 不合适
  personEntryByEnterprise // 入职
]

// 不合适、入职
const handleAction = async (type, index, item) => {
  const ids = type ? selected.value : [item?.id]
  if (!ids) return
  await apiList[index](ids)
  Snackbar.success(t('common.operationSuccessful'))
  emit('refresh')
}
</script>

<style scoped lang="scss">
:deep(.v-table > .v-table__wrapper > table > thead) {
  background-color: #f7f8fa !important;
}
:deep(.v-selection-control__input) {
  // color: var(--v-primary-base) !important;
  color: #767778;
}
</style>
