<template>
  <!-- 列表 -->
  <ContentWrap>
    <div style="text-align: end;">
      <el-button
        type="primary"
        plain
        @click="handleAdd"
      >
        <Icon icon="ep:plus" class="mr-5px" /> 新增
      </el-button>
    </div>
    <el-table v-loading="loading" :data="list" :stripe="true">
      <el-table-column label="招聘会" align="center" prop="title" />
      <el-table-column label="企业全称" align="center" prop="name" />
      <el-table-column label="企业别称" align="center" prop="anotherName" />
      <el-table-column label="企业logo" align="center" prop="logoUrl">
        <template #default="scope">
          <el-image style="width: 80px; height: 80px" :src="scope.row.logoUrl" />
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center" prop="contact" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleRemoveWhiteList(scope.row.id)"
          >
            移出白名单
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <JobFairForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { JobFairWhiteApi } from '@/api/menduner/system/jobFair/white'
import JobFairForm from './jobFairForm.vue'

/** 招聘会 列表 */
defineOptions({ name: 'JobFair' })

const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await JobFairWhiteApi.getJobFairWhiteList()
    if (!data) return list.value = []
    list.value = data.enterpriseList.map(e => {
      return { ...e, title: data.fair.title }
    })
  } finally {
    loading.value = false
  }
}

const formRef = ref()
const handleAdd = () => {
  formRef.value.open()
}

const handleRemoveWhiteList = async (id: number) => {
  try {
    await message.confirm('确定要将该企业移出白名单吗？')
    await JobFairWhiteApi.removeJobFairWhiteList([id])
    message.success('移出成功')
    getList()
  } catch (err) {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>