<template>
  <div v-loading="loading" class="policy-container flx-center">
    <div class="container">
      <img :src="baseURL + '/' + policyData.posterUrl" height="560" style="width: 100%" />
      <el-autocomplete
        v-model="searchValue"
        value-key="FTITLE"
        :fetch-suggestions="querySearchAsync"
        placeholder="搜索关键词"
        @select="search"
      >
        <template #suffix>
          <el-icon size="24" style="cursor: pointer" @click="querySearchAsync"
            ><i-ep-Search
          /></el-icon>
        </template>
      </el-autocomplete>

      <div class="flx-justify-between" style="margin-bottom: 64px; align-items: start">
        <policy
          title="南山就业政策公告"
          :list="policyData.noticeData"
          :params="policyData.noticeParams"
          is-more
          @change="handleChange"
          @see-more="seeMore(0)"
        />
        <policy
          title="南山就业活动"
          :list="policyData.activityData"
          :params="policyData.activityParams"
          is-more
          @change="handleChange"
          @see-more="seeMore(1)"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { baseURL } from '@/services'
import { getPolicy, getPolicyList } from '@/services/api/policy'
import policy from '@/components/policy.vue'

const router = useRouter()

const searchValue = ref()
const loading = ref(true)
const queryList = ref({ pageIndex: 1, pageSize: 10 })
const policyData: any = reactive({
  posterUrl: '',
  noticeData: [] as any,
  noticeParams: { pageIndex: 1, pageSize: 0, total: 10 },
  activityData: [] as any,
  activityParams: { pageIndex: 1, pageSize: 0, total: 10 }
})
let timeout: ReturnType<typeof setTimeout>

const querySearchAsync = async (queryString: string, cb: (arg: any) => void) => {
  const res: any = await getPolicyList({
    pageIndex: 1,
    pageSize: 10,
    Type: 2,
    FTITLE: searchValue.value
  })
  if (res.code === 200) {
    let results = res.data || []
    clearTimeout(timeout)
    timeout = setTimeout(() => {
      cb(results)
    }, 500)
  }
}

const search = (item: Record<string, any>) => {
  if (!searchValue.value) return
  router.push({
    path: '/commonDetail',
    query: { FID: item.FID, FormType: 'B', FTITLE: item.FTITLE }
  })
}

const seeMore = async (Type: number) => {
  if (Type && policyData.activityParams.pageSize <= policyData.activityParams.total) {
    policyData.activityParams.pageSize += 10
    const res: any = await getPolicyList({ ...policyData.activityParams, ...{ Type } })
    if (res.code === 200) {
      policyData.activityData = res.data
      policyData.activityParams.total = res.total
    }
  }
  if (!Type && policyData.noticeParams.pageSize <= policyData.noticeParams.total) {
    policyData.noticeParams.pageSize += 10
    const res: any = await getPolicyList({ ...policyData.noticeParams, ...{ Type } })
    if (res.code === 200) {
      policyData.noticeData = res.data
      policyData.noticeParams.total = res.total
    }
  }
}

const handleChange = (row: any) => {
  router.push({ path: '/commonDetail', query: { FID: row.FID, FormType: 'B', FTITLE: row.FTITLE } })
}

const init = async () => {
  const res: any = await getPolicy(queryList.value)
  if (res.code === 200) {
    policyData.posterUrl = res.data?.HbData?.[0].FPOSTERPICTURE
    seeMore(0)
    seeMore(1)
    loading.value = false
  }
}

init()
</script>

<style lang="scss" scoped>
@import url(./index.scss);
</style>
