<template>
  <div class="hired-personnel-list">
    <h2 class="mb10">{{ props.title }} - {{ props.label }}</h2>

    <el-table :data="pageList.records" stripe border v-loading="loading" style="width: 100%">
      <el-table-column prop="userName" label="人员姓名" />
      <el-table-column prop="orgName" label="所属单位" />
      <el-table-column label="专业领域">
        <template #default="{ row }">
          <el-tag
            v-for="(label, index) in getFieldLabels(row.professionalField)"
            :key="index"
            type="success"
            class="mr5"
          >
            {{ label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="workExperience" label="工作经验" />
      <!-- 根据你的返回字段调整列 -->
    </el-table>

    <el-pagination
      class="mt20"
      background
      layout="prev, pager, next, total"
      :current-page="pageList.pageCurrent"
      :page-size="pageList.pageSize"
      :total="pageList.total"
      @current-change="handlePageChange"
    />
  </div>
</template>


<script setup>
import { ref, reactive, defineProps, watch } from "vue"
import { getApi } from "@/api/tobAcco_api.js"

const props = defineProps({
  orgCode: String,
  title: String,
  label: String,
  type: String,
})

const loading = ref(false)

let pageList = reactive({
  records: [],
  pageSize: 10,
  pageCurrent: 1,
  total: 0,
})

/** 请求接口 */
const getInfoList = () => {
  if (!props.orgCode) return
  loading.value = true
  getApi(
    {
      orgCode: props.orgCode,
      type: props.type,
      pageCurrent: pageList.pageCurrent,
      pageSize: pageList.pageSize,
    },
    "/mosty-jcgl/aqy/queryTalentPoolList"
  )
    .then((res) => {
      if (res) {
        pageList.records = res.records || []
        pageList.total = res.total || 0
      }
    })
    .finally(() => {
      loading.value = false
    })
}

/** 页码切换 */
const handlePageChange = (page) => {
  pageList.pageCurrent = page
  getInfoList()
}

const professionalFieldMap = {
  "01": "体系建设",
  "02": "消防安全",
  "03": "设备安全",
  "04": "危化品安全",
  "05": "交通安全",
  "06": "基建安全",
  "07": "电气安全",
  "08": "仓储安全",
  "09": "粉尘防爆",
  "10": "应急管理",
  "11": "职业健康",
  "12": "无"
};

// 将 01,02 转成 ['体系建设', '消防安全']
function getFieldLabels(fieldStr) {
  if (!fieldStr) return [];
  return fieldStr.split(',').map(code => professionalFieldMap[code] || code);
}

// props 变化时自动重新加载
watch(
  () => [props.orgCode, props.label],
  () => {
    pageList.pageCurrent = 1
    getInfoList()
  },
  { immediate: true }
)
</script>


<style scoped lang="scss">
.hired-personnel-list {
  padding: 10px;

  h2 {
    font-size: 18px;
    font-weight: bold;
  }

  .el-pagination {
    text-align: right;
  }
}
</style>
