<template>
  <div v-loading="loading" class="policy-container flx-center">
    <div class="container">
      <img :src="baseURL + '/' + policyData.posterUrl" height="560" style="width: 100%" />
      <el-input v-model="searchValue" placeholder="搜索关键词" @change="search">
        <template #suffix>
          <el-icon size="24" style="cursor: pointer" @click="search"><i-ep-Search /></el-icon>
        </template>
      </el-input>
      <div class="flx-justify-between" style="margin-bottom: 64px; align-items: start">
        <policy
          title="南山就业政策公告"
          :list="policyData.noticeData"
          is-more
          @change="handleChange"
          @see-more="seeMore(0)"
        />
        <policy
          title="南山就业活动"
          :list="policyData.activityData"
          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: 10, total: 10 },
  activityData: [] as any,
  activityParams: { pageIndex: 1, pageSize: 10, total: 10 }
})

const search = () => {
  console.log(searchValue.value)
}

const seeMore = async (Type: number) => {
  if (Type && policyData.activityParams.pageSize <= policyData.activityParams.total) {
    policyData.activityParams.pageSize = 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 = 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) {
    console.log(res)
    policyData.posterUrl = res.data?.HbData?.[0].FPOSTERPICTURE
    policyData.noticeData = res.data?.GgData
    policyData.activityData = res.data?.HdData
    loading.value = false
  }
}

init()
</script>

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