<template>
  <view class="box defaultBgc">
    <view style="background-color: #fff;">
      <uni-segmented-control 
        :current="current"
        :values="tabList.map(e => e.label)"
        @clickItem="changeControl"
        styleType="text"
        activeColor="#00B760"
      ></uni-segmented-control>

      <!-- 条件搜索 -->
      <uni-search-bar
        v-model="query.name"
        radius="5"
        placeholder="投递人姓名"
        cancelButton="none"
        :focus="false"
				@clear="handleSearch('name', null)"
        @confirm="handleSearch('name', query.name)"
      ></uni-search-bar>
      <view style="padding: 0 10px;">
		<FilterList :list="filterList" idValue="label" labelWidth="93%" :paddingBottom="10" @change="handleSearch"></FilterList>
	  </view>
    </view>

    <scroll-view class="scrollBox" :scroll-y="true" @scrolltolower="loadingMore" style="position:relative;">
			<CardItem v-if="items?.length" :items="items" :current="tabList[current].value" @refresh="handleRefresh" />
			<uni-load-more :status="more" />
		</scroll-view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { timesTampChange } from '@/utils/date'
import { dealDictObjData } from '@/utils/position'
import CardItem from './item.vue'
import FilterList from '@/components/FilterList'
import { getInterviewInvitePage } from '@/api/interview'
import { getPersonCvPage, personCvUnfitPage } from '@/api/resume'
import { getJobFairList } from '@/api/jobFair'
import { getJobAdvertised } from '@/api/search'
import { onLoad } from '@dcloudio/uni-app'

const filterList = ref([
  { label: '投递职位',key: 'jobId', dataLabel: 'name', dataValue: 'id', api: getJobAdvertised, isFormatText: true },
  { label: '招聘会', key: 'jobFairId', dataLabel: 'title', dataValue: 'id', isRichText: true, api: getJobFairList },
  { label: '最高学历', dictType: 'menduner_education_type', key: 'eduType' },
  { label: '工作经验', dictType: 'menduner_exp_type', key: 'expType' },
  { label: '求职状态', dictType: 'menduner_job_seek_status', key: 'jobStatus' }
])

const current = 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 more = ref('more')
const total = ref(0)
const items = ref([])
const query = ref({
  pageNo: 1,
  pageSize: 10,
  type: null,
  status: null,
  name: null
})

// 获取牛人列表
const getList = async () => {
  more.value = 'loading'

  const api = tabList.value[current.value].api
  if (current.value !== 0) {
    query.value.conversationStatus = tabList.value[current.value].status
    delete query.value.status
    delete query.value.type
  }

  try {
    const { data } = await api(query.value)
    const { list, total: number } = data

    if (!list.length) {
      more.value = 'noMore'
      return
    }

    total.value = number
    const arr = list.map(e => {
      let obj = e
      if (e.person) obj.person = Object.assign(e.person, dealDictObjData({}, e.person))
      obj.job = Object.assign(e.job, dealDictObjData({}, e.job))
      obj.createTime = timesTampChange(e.createTime, 'Y-M-D h:m')
      return obj
    })
    items.value = items.value.concat(arr)

    if (items.value.length === +number) {
      more.value = 'noMore'
      return
    }
  } catch {
    query.value.pageNo--
    more.value = 'more'
  }
}

onLoad((options) => {
  if (options?.jobId) {
    query.value.jobId = options.jobId
    const item = filterList.value.find(e => e.key === 'jobId')
    item.value = options.jobId
    item.name = decodeURIComponent(options.jobName)
  }
  if (options?.jobFairId) {
    query.value.jobFairId = options.jobFairId
    const item = filterList.value.find(e => e.key === 'jobFairId')
    item.value = options.jobFairId
    item.name = decodeURIComponent(options.jobFairName)
  }
  query.value.pageNo = 1
  getList()
})

const handleSearch = (key, value) => {
	query.value.pageNo = 1
	query.value[key] = value

	items.value = []
	total.value = 0
	getList()
}

const handleRefresh = () => {
  items.value = []
  total.value = 0
  query.value.pageNo = 1
  getList()
}

const changeControl = (e) => {
  current.value = e.currentIndex
  handleRefresh()
}

// 加载更多
const loadingMore = () => {
	if (more.value === 'noMore') return
  more.value = 'loading'
  query.value.pageNo++
	getList()
}
</script>

<style scoped lang="scss">
.box {
  height: 100vh;
  overflow: hidden;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}
.scrollBox{
	flex: 1;
  padding-bottom: 24rpx;
  box-sizing: border-box;
	height: 0 !important;
}
</style>
