<template>
  <div>
    <div class="d-flex align-center mt-5" style="width: 600px; margin: auto;">
      <Autocomplete v-model="query.jobId" :item="selectItems" @change="handleChange"></Autocomplete>
    </div>
    <CtTable
      class="mt-3"
      :items="items"
      :headers="headers"
      :loading="loading"
      :elevation="0"
      :isTools="false"
      :showPage="true"
      :total="total"
      :page-info="query"
      itemKey="id"
      @pageHandleChange="handleChangePage"
    >
      <template #name="{ item }">
        <div class="d-flex align-center cursor-pointer" @click="handleToPersonDetail(item)">
          <v-badge
            v-if="item?.sex === '1' || item?.sex === '2'"
            bordered
            offset-y="6"
            :color="badgeColor(item)"
            :icon="badgeIcon(item)">
            <v-avatar size="40" :image="getUserAvatar(item.avatar, item.sex)"></v-avatar>
          </v-badge>
          <v-avatar v-else size="40" :image="getUserAvatar(item.avatar, item.sex)"></v-avatar>
          <span class="defaultLink ml-3 mt-2">{{ item?.name }}</span>
        </div>
      </template>
      <template #actions="{ item }">
        <v-btn color="primary" variant="text" @click="handleCommunicate(item)">立即沟通</v-btn>
      </template>
    </CtTable>
  </div>
</template>

<script setup>
defineOptions({ name: 'searchRecommend' })
import { ref, computed } from 'vue'
import { getPersonRecommendPage, getJobAdvertised } from '@/api/enterprise'
import { timesTampChange } from '@/utils/date'
import { talkToUser, defaultTextEnt } from '@/hooks/web/useIM'
import { useRouter } from 'vue-router'
import { dealDictArrayData } from '@/utils/position'
import { getUserAvatar } from '@/utils/avatar'

const router = useRouter()
const query = ref({
  pageNo: 1,
  pageSize: 10,
  jobId: null
})
const selectItems = ref({
  label: '推荐的职位',
  placeholder: '请选择要进行推荐的职位',
  clearable: true,
  width: 600,
  items: []
})
const total = ref(0)
const items = ref([])
const loading = ref(false)
const headers = ref([
  { title: '姓名', key: 'name', sortable: false },
  { title: '求职状态', key: 'jobStatusName', sortable: false },
  { title: '工作年限', key: 'expName', sortable: false },
  { title: '最高学历', key: 'eduName', sortable: false },
  { title: '所在城市', key: 'areaName', sortable: false },
  { title: '户籍地', key: 'regName', sortable: false },
  { title: '婚姻状况', key: 'maritalStatusName', sortable: false },
  { title: '首次工作时间', key: 'firstWorkTime', sortable: false, value: item => timesTampChange(item.firstWorkTime, 'Y-M-D') },
  { title: '操作', key: 'actions', sortable: false }
])

// 职位列表
const getJobList = async () => {
  const data = await getJobAdvertised({})
  if (data.length) {
    const list = dealDictArrayData([], data)
    selectItems.value.items = list.map(e => {
      return { label: `${e.name}${e.areaName ? '_' + e.areaName : ''} ${e.payFrom ? e.payFrom + '-' : ''}${e.payTo}${e.payName ? '/' + e.payName : ''}`, value: e.id }
    })
  }
}
getJobList()

// 列表
const getData = async () => {
  loading.value = true
  try {
    const res = await getPersonRecommendPage(query.value)
    if (!res.list.length) {
      items.value = []
      total.value = 0
      return
    }
    items.value = dealDictArrayData([], res.list)
    total.value = res.total
  } finally {
    loading.value = false
  }
}
getData()

// 推荐
const handleChange = () => {
  if (query.value.jobId) {
    query.value.pageNo = 1
    getData()
  }
}

// 分页
const handleChangePage = (e) =>{
  query.value.pageNo = e
  getData()
}

// 立即沟通
const handleCommunicate = async (item) => {
  const userId = item.userId
  await talkToUser({userId, text: defaultTextEnt})
  let url = `/recruit/enterprise/chatTools?id=${userId}`
  router.push(url)
}

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

const badgeColor = computed(() => (item) => {
  return (item && item.sex) ? (item.sex === '1' ? '#1867c0' : 'error') : 'error'
})
const badgeIcon = computed(() => (item) => {
  return (item && item.sex) ? (item.sex === '1' ? 'mdi-gender-male' : 'mdi-gender-female') : 'mdi-gender-female'
})
</script>

<style scoped lang="scss">

</style>
