<template>
  <div>
    <v-data-table
      class="mt-3"
      :items="items"
      :headers="headers"
      hover
      :disable-sort="true"
      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="handlePreviewResume(item)">查看附件</v-btn>
          <v-btn color="primary" variant="text" @click="handleInterviewInvite(item)">邀请面试</v-btn>
        </div>
        <v-btn v-if="tab === 0 || tab === 1" color="primary" variant="text" @click="handleEliminate(item)">不合适</v-btn>
        <div v-if="tab === 1">
          <v-btn color="primary" variant="text" @click="handleEnterByEnterprise(item)">入职</v-btn>
        </div>
        <v-btn v-if="tab === 3" color="primary" variant="text" @click="handleCancelEliminate(item)">取消不合适</v-btn>
      </template>
    </v-data-table>

    <!-- 邀请面试 -->
    <CtDialog :visible="showInvite" :widthType="2" titleClass="text-h6" title="面试信息" @close="handleEditClose" @submit="handleEditSubmit">
      <InvitePage v-if="showInvite" ref="inviteRef" :itemData="itemData"></InvitePage>
    </CtDialog>
  </div>
</template>

<script setup>
defineOptions({ name: 'table-page'})
import { ref, computed, watch } from 'vue'
import { previewFile } from '@/utils'
import { personJobCvLook, joinEliminate, personEntryByEnterprise, personCvUnfitCancel } from '@/api/recruit/enterprise/personnel'
import { saveInterviewInvite } from '@/api/recruit/enterprise/interview'
import { useI18n } from '@/hooks/web/useI18n'
import Snackbar from '@/plugins/snackbar'
import InvitePage from './invite.vue'

const { t } = useI18n()
const emit = defineEmits(['refresh'])
const props = defineProps({
  tab: Number,
  items: Array,
  statusList: 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 inviteRef = ref()
const showInvite = ref(false)
const headers = ref([
  { title: '姓名', value: 'name', sortable: false },
  { title: '应聘职位', value: 'job.name', sortable: false },
  { title: '求职状态', key: 'person.jobStatusName', sortable: false },
  { title: '工作经验', key: 'person.expName', sortable: false },
  { title: '最高学历', key: 'person.eduName', sortable: false },
  { title: '岗位薪资', key: 'job', value: item => item.job.payFrom && item.job.payTo ? `${item.job.payFrom ? item.job.payFrom + '-' : ''}${item.job.payTo}${item.job.payName ? '/' + item.job.payName : ''}` : '面议', sortable: false },
  { title: '状态', key: 'status', sortable: false, value: item => item.status ? props.statusList.find(i => i.value === item.status).label : '' },
  { title: '操作', value: 'actions', sortable: false }
])
const unfit = { title: '类型', key: 'unfitType', sortable: false, value: item => item.type === '0' ? '简历不合适' : '面试不合适' }
const delivery = { title: '类型', key: 'deliveryType', sortable: false, value: item => item.status === '0' ? '新投递' : '已查看' }

const list = [0, 3]
watch(
  () => props.tab,
  (val) => {
    if (list.indexOf(val) !== -1) {
      headers.value.splice(-1, 0, val === 0 ? delivery : unfit)
    } else {
      const index = headers.value.indexOf(item => item.key === val === 0 ? 'deliveryType' : 'unfitType')
      if (index !== -1) headers.value.splice(index, 1)
    }
  },
  { immediate: true }
)

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

// 入职
const handleEnterByEnterprise = async (item) => {
  if (!item.id) return
  await personEntryByEnterprise(item.id)
  Snackbar.success(t('common.operationSuccessful'))
  emit('refresh')
}

// 不合适
const handleEliminate = async (item) => {
  if (!item.id || !item?.job?.id) return
  const query = {
    bizId: item.id,
    jobId: item.job.id,
    userId: item.userId,
    type: props.tab === 0 ? '0' : '1' // 投递简历0 已邀约1
  }
  await joinEliminate(query)
  Snackbar.success(t('common.operationSuccessful'))
  emit('refresh')
}

// 取消不合适
const handleCancelEliminate = async (item) => {
  if (!item.id) return
  await personCvUnfitCancel(item.id)
  Snackbar.success(t('common.operationSuccessful'))
  emit('refresh')
}

// 查看简历
const handlePreviewResume = async ({ url, id }) => {
  if (!url || !id) return
  await personJobCvLook(id)
  previewFile(url)
}

// 邀请面试
const itemData = ref({})
const handleInterviewInvite = (item) => {
  itemData.value = item
  showInvite.value = true
}

const handleEditClose = () => {
  showInvite.value = false
  itemData.value = {}
}

const handleEditSubmit = async () => {
  const { valid } = await inviteRef.value.CtFormRef.formRef.validate()
  if (!valid) return
  const query = inviteRef.value.getQuery()
  if (!query?.time) return Snackbar.warning('请选择面试时间')
  await saveInterviewInvite(query)
  Snackbar.success(t('common.operationSuccessful'))
  handleEditClose()
  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;
}
</style>
