<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="40px"
    >
      <el-form-item label="职位" prop="title_zh">
        <el-input
          v-model="queryParams.title_zh"
          placeholder="请输入职位名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="酒店" prop="hotel_zh">
        <el-input
          v-model="queryParams.hotel_zh"
          placeholder="请输入酒店名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name_zh">
        <el-input
          v-model="queryParams.name_zh"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" row-key="id">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="name_zh" />
      <el-table-column label="职位" align="center" prop="title_zh" :show-overflow-tooltip="true" />
      <el-table-column label="酒店" align="center" prop="hotel_zh" :show-overflow-tooltip="true" />
      <el-table-column label="人才状态" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="scope.row.status === 'active' ? 'success' : 'danger'">
            {{ scope.row.status === 'active' ? '已启用' : '已禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="created_at" width="180" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm(scope.row)"
          >
            标注
          </el-button>
          <el-button
            v-if="scope.row.status === 'active'"
            link
            type="danger"
            @click="handleAction(scope.row.id, 'inactive')"
          >
            禁用
          </el-button>
          <el-button
            v-if="scope.row.status === 'inactive'"
            link
            type="success"
            @click="handleAction(scope.row.id, 'active')"
          >
            启用
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>

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

<script setup lang="ts" name="Tag">
import { talentLabelingApi } from '@/api/menduner/system/talentMap/labeling'
import LabelingForm from './LabelingForm.vue'

const loading = ref(false)
const list = ref([])
const queryParams = reactive({
  hotel_zh: undefined,
  name_zh: undefined,
  title_zh: undefined
})
const queryFormRef = ref() // 搜索的表单

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

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await talentLabelingApi.getCardList()
    list.value = data
  } finally {
    loading.value = false
  }
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (data: any) => {
  formRef.value.open(data)
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 禁用、启用按钮操作 */
const handleAction = async (id: number, status: string) => {
  try {
    // 二次确认
    await message.confirm(`是否确定${status === 'active' ? '启用' : '禁用'}？`)
    // 发起删除
    await talentLabelingApi.updateTalentStatus(id, { status })
    message.success('操作成功')
    // 刷新列表
    setTimeout(async () => {
      await getList()
    }, 0)
  } catch {}
}

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