<!-- 精英管理 -->
<template>
  <v-card class="pa-3 card-box">
    <div class="d-flex justify-space-between">
      <v-tabs v-model="tab" align-tabs="start" color="primary" bg-color="#f7f8fa">
        <v-tab v-for="k in tabList" :value="k.value" :key="k.value">{{ k.label }}</v-tab>
      </v-tabs>
      <TextInput v-model="textItems.value" :item="textItems" @appendInnerClick="handleSearch" @enter="handleSearch"></TextInput>
    </div>
    <Screen :tab="tab" @search="handleScreen" @reset="handleScreenReset" @select="handleSelect" @change="handleChangeBounty"></Screen>

    <v-window v-model="tab" class="mt-1">
      <v-window-item v-for="k in tabList" :value="k.value" :key="k.value">
        <TablePage :items="items" :total="total"  :pageInfo="query" :tab="k.value" :statusList="statusList" @refresh="getList" @page="handleChangePage"></TablePage>
      </v-window-item>
    </v-window>
  </v-card>
</template>

<script setup>
defineOptions({ name: 'enterprise-elite-management'})
import { ref } from 'vue'
import { getPersonCvPage } from '@/api/enterprise'
import { personCvUnfitPage } from '@/api/recruit/enterprise/personnel'
import { dealDictObjData } from '@/utils/position'
import { getDict } from '@/hooks/web/useDictionaries'
import { getInterviewInvitePage } from '@/api/recruit/enterprise/interview'
import TablePage from './components/table.vue'
import Screen from './components/screen.vue'

const total = ref(0)
const query = ref({
  pageNo: 1,
  pageSize: 10,
  status: null,
  type: null
})
const tab = ref(0)
const tabList = ref([
  { label: '投递简历', value: 0, api: getPersonCvPage, status: null },
  { label: '已邀约', value: 1, api: getInterviewInvitePage, status: '0' },
  { label: '已入职', value: 2, api: getInterviewInvitePage, status: '1' },
  { label: '已结算', value: 3, api: getInterviewInvitePage, status: '2' },
  { label: '不合适', value: 4, api: personCvUnfitPage },
])
const textItems = ref({
  type: 'text',
  value: '',
  width: 250,
  label: '搜索姓名',
  clearable: true,
  appendInnerIcon: 'mdi-magnify'
})

// 状态字典
const statusList = ref([])
getDict('menduner_interview_invite_status').then(({data}) => {
  if (data && data.length) statusList.value = data
})

// 获取牛人列表
const items = ref([])
const getList = async () => {
  const api = tabList.value[tab.value].api
  if (tab.value !== 0) {
    query.value.conversationStatus = tabList.value[tab.value].status
    delete query.value.status
    delete query.value.type
  }

  const { list, total: number } = await api(query.value)
  if (!list.length) {
    items.value = []
    total.value = 0
    return
  }
  total.value = number
  items.value = list.map(e => {
    let obj = e
    obj.jobClosed = e?.job?.status === '1' || false // 职位已关闭
    obj.person = Object.assign(e.person, dealDictObjData({}, e.person))
    obj.job = Object.assign(e.job, dealDictObjData({}, e.job))
    return obj
  })
}
getList()

// 分页
const handleChangePage = (i) => {
  query.value.pageNo = i
  getList()
}

// 牛人姓名检索
const handleSearch = () => {
  if (textItems.value.value) query.value.name = textItems.value.value
  else delete query.value.name
  query.value.pageNo = 1
  getList()
}

// 下拉筛选
const handleScreen = ({ value, key }) => {
  if (value) query.value[key] = value
  else delete query.value[key]
  getList()
}

// 下拉筛选重置
const handleScreenReset = () => {
  query.value = {
    pageSize: 10,
    pageNo: 1,
    status: tab.value
  }
  if (tab.value === 0) {
    query.value.status = null
    query.value.type = null
  }
  if (textItems.value.value) query.value.name = textItems.value.value
  getList()
}

const handleSelect = (e) => {
  query.value.pageNo = 1
  query.value.status = e
  getList()
}

// 赏金职位
const handleChangeBounty = (e) => {
  query.value.type = e ? 1 : 0
  query.value.pageNo = 1
  getList()
}
</script>

<style scoped lang="scss">
</style>
