<template>
  <a-button class="ele-btn-icon" @click="confirmRefresh">
    修正主体企业归属
  </a-button>
</template>

<script lang="ts" setup>
  import { createVNode } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import { refreshCreditCompanyId } from '@/api/credit/companyId';

  const props = withDefaults(
    defineProps<{
      /** 后端模块路径片段，如: credit-breach-of-trust */
      module: string;
      /** 默认 true，仅更新 companyId 为空/0 的数据（由后端实现决定） */
      onlyNull?: boolean;
      /** 可选，限制处理条数 */
      limit?: number;
      /** 确认弹窗文案 */
      content?: string;
    }>(),
    {
      onlyNull: true,
      content:
        '将按记录名称匹配企业名称并回填 companyId（默认仅更新 companyId 为0的数据）确定要执行吗？'
    }
  );

  const emit = defineEmits<{
    (e: 'done'): void;
  }>();

  const confirmRefresh = () => {
    Modal.confirm({
      title: '提示',
      content: props.content,
      icon: createVNode(ExclamationCircleOutlined),
      maskClosable: true,
      onOk: () => {
        const hide = message.loading('请求中..', 0);
        return refreshCreditCompanyId(props.module, {
          onlyNull: props.onlyNull,
          limit: props.limit
        })
          .then((msg) => {
            hide();
            message.success(msg || '操作成功');
            emit('done');
          })
          .catch((e) => {
            hide();
            message.error(e.message);
          });
      }
    });
  };
</script>

