<template>
  <el-dialog
    v-model="dialogVisible"
    title="批量添加标签"
    width="500px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form :model="form" label-width="100px">
      <el-form-item label="选择标签">
        <el-select
          v-model="form.tags"
          multiple
          filterable
          allow-create
          default-first-option
          placeholder="请选择标签"
          style="width: 100%"
        >
          <el-option
            v-for="item in tagOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm" :loading="loading">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { successMessage, warningMessage } from '/@/utils/message';
import axios from 'axios';
import { Session } from '/@/utils/storage';

const props = defineProps({
  crudExpose: {
    type: Object,
    required: true
  }
});
const dialogVisible = ref(false);
const loading = ref(false);
const selectedRows = ref([]);
const tagOptions = ref([]);

const form = reactive({
  tags: [],
});

const emit = defineEmits(['success']);

// 获取标签列表
const getTagList = async () => {
  try {
    // 这里需要替换为实际的API调用
    const response = await axios.get(`${import.meta.env.VITE_API_URL}/api/system/talent_tag/`, {
      params: {
        page: 1,
        limit: 50,
        employment_status:1,
        tenant_id:Session.get('tenant_id')
      },
      headers:{
        'authorization':'JWT '+ Session.get('token'),
      },
    });
    if (response.data.code === 2000) {
      tagOptions.value = response.data.data.map((tag: any) => ({
        value: tag.id,
        label: tag.name
      }));
    }
  } catch (error) {
    console.error('获取标签列表失败:', error);
  }
};

// 打开对话框的方法
const open = (rows: any[]) => {
  selectedRows.value = rows;
  dialogVisible.value = true;
  form.tags = [];
};

// 确认添加标签
const handleConfirm = async () => {
  if (!form.tags.length) {
    warningMessage('请选择标签');
    return;
  }

  loading.value = true;
  try {
    // 这里需要替换为实际的API调用
    const response = await axios.post(`${import.meta.env.VITE_API_URL}/api/system/talent_pool/batch-bind-tags/`, {
      ids: selectedRows.value.map(row => row.id),
      tag_ids: form.tags,
      tenant_id: Session.get('tenant_id')
    },{
      headers:{
        'authorization':'JWT '+ Session.get('token'),
      },
    });

    if (response.data.code === 2000) {
      successMessage('标签添加成功');
      dialogVisible.value = false;
      emit('success');
      props.crudExpose.doRefresh();
    } else {
      warningMessage(response.data.message || '操作失败');
    }
  } catch (error) {
    console.error('批量添加标签失败:', error);
    warningMessage('操作失败');
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  getTagList();
});

defineExpose({
  open,
});
</script> 