<!-- 精英管理 -->
<template>
  <v-card class="pa-5 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"></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" :tab="k.value" @refresh="getList"></TablePage>
        <CtPagination
          v-if="total > 0"
          :total="total"
          :page="query.pageNo"
          :limit="query.pageSize"
          @handleChange="handleChangePage"
        ></CtPagination>
      </v-window-item>
    </v-window>
  </v-card>
</template>

<script setup>
defineOptions({ name: 'enterprise-personnelManagement-management'})
import { ref } from 'vue'
import { getPersonCvPage } from '@/api/enterprise'
import { dealDictObjData } from '@/utils/position'
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: 0
})
const tab = ref(0)
const tabList = ref([
  { label: '投递简历', value: 0, api: getPersonCvPage, status: null },
  { label: '已邀约', value: 1, api: getInterviewInvitePage, status: '0' },
  { label: '已发offer', value: 2, api: getInterviewInvitePage, status: '1' },
  { label: '已入职', value: 3, api: getInterviewInvitePage, status: '2' },
  { label: '不合适', value: 4 },
])
const textItems = ref({
  type: 'text',
  value: '',
  width: 250,
  label: '搜索姓名',
  clearable: true,
  appendInnerIcon: 'mdi-magnify'
})

// 获取牛人列表
const items = ref([])
const getList = async () => {
  if (tab.value === 4) return
  const api = tabList.value[tab.value].api
  query.value.status = tab.value === 0 ? null : tabList.value[tab.value].status
  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.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 (textItems.value.value) query.value.name = textItems.value.value
  getList()
}
</script>

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