<!-- 牛人管理 -->
<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" @update:model-value="handleChangeTab">
        <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"></TablePage>
        <CtPagination
          :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 { getDict } from '@/hooks/web/useDictionaries'
import { dealDictObjData } from '@/views/recruit/position/components/dict'
import TablePage from './components/table.vue'
import Screen from './components/screen.vue'

const total = ref(0)
const query = ref({
  pageNo: 1,
  pageSize: 1,
  status: 0
})
const tab = ref('0')
const tabList = ref([])
const textItems = ref({
  type: 'text',
  value: '',
  width: 250,
  label: '搜索牛人姓名',
  appendInnerIcon: 'mdi-magnify'
})

// 获取tab列表项
const getTabData = () => {
  getDict('menduner_job_cv_status').then(({ data }) => {
    data = data?.length && data || []
    tabList.value = data
  })
}
getTabData()

// 获取牛人列表
const items = ref([])
const getList = async () => {
  const { list, total: number } = await getPersonCvPage(query.value)
  if (!list.length) {
    items.value = []
    total.value = 0
    return
  }
  total.value = number
  items.value = list.map(e => {
    let obj = e
    if (e.person) {
      obj = Object.assign(dealDictObjData({}, e.person), obj)
      if (e.person.workExpList && e.person.workExpList.length > 0) obj.jobExperience = e.person.workExpList[0]
      if (e.person.eduExpList && e.person.eduExpList.length > 0) obj.eduExperience = e.person.eduExpList[0]
    }
    return obj
  })
}
getList()

const handleChangeTab = () => {
  query.value.status = tab.value
  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>
