<template>
  <ContentWrap>
    <div class="flex">
      <div v-if="!fairId">
        <el-select v-model="queryParams.jobId" placeholder="招聘职位" class="!w-240px ml-10px" clearable @change="handleRefresh">
          <el-option
            v-for="item in jobList"
            :key="item.id"
            :label="formatName(item.name)"
            :value="item.id"
          />
        </el-select>
        <el-select v-model="queryParams.jobFairId" placeholder="招聘会" class="!w-240px ml-10px" clearable @change="handleRefresh">
          <el-option
            v-for="item in jobFairList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
          <template #footer v-if="jobFairList?.length < +jobFairTotal">
            <div class="text-center">
              <el-button type="primary" link @click="handleGetMore">加载更多</el-button>
            </div>
          </template>
        </el-select>
      </div>
      <el-select v-else v-model="queryParams.enterpriseId" placeholder="招聘企业" class="!w-240px ml-10px" clearable @change="handleRefresh">
        <el-option
          v-for="item in enterpriseList"
          :key="item.enterpriseId"
          :label="formatName(item.enterprise.anotherName || item.enterprise.name)"
          :value="item.enterpriseId"
        />
        <template #footer v-if="enterpriseList?.length < +enterpriseTotal">
          <div class="text-center">
            <el-button type="primary" link @click="handleGetEnterpriseMore">加载更多</el-button>
          </div>
        </template>
      </el-select>
      <el-button type="success" class="ml-10px" plain @click="handleRefresh"><Icon icon="ep:refresh" class="mr-5px" /> 刷新</el-button>
    </div>
  </ContentWrap>

  <ContentWrap>
    <el-table v-loading="loading" :data="tableData" :stripe="true" height="calc(100vh - 360px)">
      <el-table-column label="求职者" align="center" prop="person.name" />
      <el-table-column label="投递职位" align="center" prop="job.name">
        <template #default="{ row }">{{ formatName(row.job.name) }}</template>
      </el-table-column>
      <el-table-column v-if="fairId" label="发布企业" align="center" prop="enterprise.anotherName">
        <template #default="{ row }">{{ formatName(row.enterprise.anotherName || row.enterprise.name) }}</template>
      </el-table-column>
      <el-table-column v-if="isRecruitmentOfficer" label="职位发布人" align="center" prop="contact.name" />
      <el-table-column label="求职者联系电话" align="center" prop="person.phone" />
      <el-table-column v-if="!fairId" label="招聘会" align="center" prop="jobFair.title" />
      <el-table-column label="简历查看状态" align="center" prop="cvRel.status">
        <template #default="scope">
          <el-tag :type="scope.row.cvRel.status === '0' ? 'warning' : 'success'">
            {{ scope.row.cvRel.status === '0' ? '未查看' : '已查看' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="投递时间"
        align="center"
        prop="cvRel.createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" fixed="right" min-width="80">
        <template #default="scope">
          <el-link
            v-if="scope.row.cvRel?.url"
            type="primary"
            download
            :href="scope.row.cvRel.url"
            :underline="false"
            target="_blank"
          >
            附件下载
          </el-link>
        </template>
      </el-table-column>
    </el-table>

    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup>
defineOptions({ name: 'EnterpriseJobDeliveryResume'})
import { EnterpriseApi } from '@/api/menduner/system/enterprise/message'
import { formatName } from '@/utils'
import { dateFormatter } from '@/utils/formatTime'
import { JobFairManageApi } from '@/api/menduner/system/jobFair/manage'
import { statisticAnalysisApi } from '@/api/menduner/system/analysis/statisticAnalysis'
import { JobFairWhiteApi } from '@/api/menduner/system/jobFair/white'

const props = defineProps({
  id: String,
	fairId: String,
  userId: String,
  publishUserId: String,
  isRecruitmentOfficer: { type: Boolean, default: true }
})

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const loading = ref(false)
const tableData = ref([])
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
	jobId: undefined,
  userId: props.userId ?? undefined, // 求职者管理-详情-职位投递记录
  jobFairId: props.fairId ?? undefined, // 招聘企业-详情-职位投递记录
  enterpriseId: props.id, // 招聘企业-详情-职位投递记录
  publishUserId: props.publishUserId ?? undefined // 招聘官-详情-职位投递记录
})

// 职位投递记录
const getList = async () => {
  loading.value = true
  try {
    const data = await EnterpriseApi.getJobResumePage(queryParams)
    tableData.value = data.list ?? []
    total.value = data.total
  } finally {
    loading.value = false
  }
}
getList()

const handleRefresh = () => {
  queryParams.pageNo = 1
  getList()
}

// 获取招聘会列表
const jobFairTotal = ref(0)
const pageInfo = ref({
  pageNo: 1,
  pageSize: 10
})
const jobFairList = ref([])
const getJobFairList = async () => {
  try {
    const data = await JobFairManageApi.getJobFairPage(pageInfo.value)
    jobFairList.value = jobFairList.value.concat(data.list)
    jobFairTotal.value = data.total 
  } catch {}
}

const handleGetMore = () => {
  pageInfo.value.pageNo++
  getJobFairList()
}

// 获取招聘职位列表
const jobList = ref([])
const getJobList = async () => {
  const params = {
    enterpriseId: props.id
  }
  if (props.userId) params.userId = props.userId
  try {
    const data = await statisticAnalysisApi.getAnalysisJobAdvertisedList(params)
    jobList.value = jobList.value.concat(data)
  } catch {}
}

// 获取参加招聘会的企业列表（白名单）
const enterpriseTotal = ref(0)
const enterprisePageInfo = ref({
  pageNo: 1,
  pageSize: 20
})
const enterpriseList = ref([])
const getEnterpriseList = async () => {
  try {
    const data = await JobFairWhiteApi.getJobFairWhiteList(enterprisePageInfo.value)
    enterpriseList.value = enterpriseList.value.concat(data.list)
    enterpriseTotal.value = data.total 
  } catch {}
}

const handleGetEnterpriseMore = () => {
  enterprisePageInfo.value.pageNo++
  getEnterpriseList()
}

if (props.id || props.userId) {
  getJobFairList()
  getJobList()
}
if (props.fairId) getEnterpriseList()
</script>