<template>
  <div class="mt-4">
    <div class="d-flex justify-center">
      <TextInput v-model="query.content" :item="textItem" @enter="handleConfirm" @appendInnerClick="handleConfirm"></TextInput>
    </div>
    <!-- <div style="margin: auto; text-align: center; width: 70%"> -->
    <div>
      <Position :selectData="position" @select="val => position = val"></Position>
      <Area @select="val => handleSearch('areaIds', val)"></Area>
      <CommonPage class="my-3" dictType="menduner_education_type" title="最高学历" @select="val => handleSearch('eduType', val)"></CommonPage>
      <CommonPage dictType="menduner_exp_type" title="工作经验" @select="val => handleSearch('expType', val)"></CommonPage>
      <v-divider class="mt-1 mb-3"></v-divider>
      <div>
        <div>
          <v-chip v-for="k in position" :key="k.id" label class="mr-3" closable @click:close="handleClose(k)">{{ k.nameCn }}</v-chip>
        </div>
        <div v-if="position.length" class="text-end font-size-15 color-999 cursor-pointer color-primary" @click="handleClear">清除选择</div>
      </div>
      <div class="text-center mt-3">
        <v-btn class="half-button" color="primary" @click="handleConfirm">搜 索</v-btn>
      </div>
    </div>
  </div>

  <div class="mt-10">
    <v-divider></v-divider>
    <CtTable
      :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: 'searchRetrieval' })
import { ref, computed } from 'vue'
import CommonPage from '../components/common.vue'
import Position from './components/position.vue'
import Area from './components/area.vue'
import { getPersonSearchPage } from '@/api/enterprise.js'
import { dealDictArrayData } from '@/utils/position'
import { timesTampChange } from '@/utils/date'
import { getUserAvatar } from '@/utils/avatar'
import { talkToUser, defaultTextEnt } from '@/hooks/web/useIM'
import { useRouter } from 'vue-router'

const router = useRouter()
const textItem = ref({
  type: 'text',
  width: 600,
  value: '',
  label: '输入关键字',
  clearable: true,
  appendInnerIcon: 'mdi-magnify'
})
const query = ref({
  pageNo: 1,
  pageSize: 10,
  content: null,
  positionIds: [],
  areaIds: [],
  expType: '',
  eduType: ''
})

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: 'phone', sortable: false },
  // { title: '常用邮箱', key: 'email', sortable: false },
  { title: '操作', key: 'actions', sortable: false }
])

const getData = async () => {
  loading.value = true
  try {
    const res = await getPersonSearchPage(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
  }
}

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

const position = ref([])
const handleSearch = (key, value) => {
  query.value.pageNo = 1
  if (value === -1) query.value[key] = null
  else query.value[key] = value
}

const handleConfirm = () => {
  const positionIds = position.value.map(k => k.id)
  query.value.positionIds = positionIds || []
  query.value.pageNo = 1
  getData()
}

const handleClose = (item) => {
  position.value = position.value.filter(k => k.id !== item.id)
}

const handleClear = () => {
  position.value = []
}

// 立即沟通
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>
