import request from '@/utils/request';

export interface EmsClearAllConfig {
  moduleKey: string;
  entityName: string;
  permission?: string;
}

/**
 * 这里把原先散落在 mixin 里的“一键清空”能力收敛成组合函数，
 * 既保留老页面行为，也避免继续依赖仓库里缺失的 mixin 文件。
 */
export function useEmsClearAll(clearAllConfig: Ref<EmsClearAllConfig | null | undefined>, refreshList: () => Promise<unknown> | unknown) {
  const { proxy } = getCurrentInstance() as ComponentInternalInstance;

  const handleClearAll = async () => {
    const config = clearAllConfig.value;
    if (!config?.moduleKey || !config.entityName) {
      proxy?.$modal.msgError('清空配置缺失，无法执行操作');
      return;
    }

    await proxy?.$modal.confirm(`是否确认清空全部${config.entityName}数据？此操作不可恢复！`);
    await request({
      url: `/ems/common/clearAll/${config.moduleKey}`,
      method: 'delete'
    });
    proxy?.$modal.msgSuccess(`已清空${config.entityName}`);
    await refreshList();
  };

  return {
    handleClearAll
  };
}
