ImportInfoCard.tsx 6.53 KB
import { useEffect, useMemo } from 'react';
import { InfoCircleFilled } from '@ant-design/icons';
import { App } from 'antd';
import { UCard, UForm, UFormItem, USelect, UInput } from '@uj/ui';
import type { USelectProps } from '@uj/ui';
import type { StoreImportCheckVO } from '@/api/modules/store';

export interface ImportInfoValues {
  org?: string;
  scene?: string;
  categories?: string[];
  remark?: string;
}

const formItemLayout = {
  style: { marginBottom: 18 },
};

export interface ImportInfoCardProps {
  values?: Partial<ImportInfoValues>;
  /** check 接口返回结果(用于动态生成 3 个下拉的 options) */
  checkResult?: StoreImportCheckVO | null;
  onChange?: (values: ImportInfoValues) => void;
}

export function ImportInfoCard({ values, checkResult, onChange }: ImportInfoCardProps) {
  const { message } = App.useApp();
  const [form] = UForm.useForm<ImportInfoValues>();

  /**
   * 3 个下拉 options 完全由接口返回决定(不再硬编码):
   * - orgPath / businessScene 都是单值(名字里可能带 "/",是层级/组合名,不拆分)
   * - productCategories 是多值,用 "/" 分隔成多选数组
   */
  const orgOptions: USelectProps['options'] = useMemo(() => {
    if (!checkResult?.orgPath) return [];
    return [{ label: checkResult.orgPath, value: checkResult.orgPath }];
  }, [checkResult?.orgPath]);

  const sceneOptions: USelectProps['options'] = useMemo(() => {
    if (!checkResult?.businessScene) return [];
    return [{ label: checkResult.businessScene, value: checkResult.businessScene }];
  }, [checkResult?.businessScene]);

  const categoryOptions: USelectProps['options'] = useMemo(() => {
    if (!checkResult?.productCategories) return [];
    return checkResult.productCategories
      .split('/')
      .map((s) => s.trim())
      .filter(Boolean)
      .map((v) => ({ label: v, value: v }));
  }, [checkResult?.productCategories]);

  // 「只读」标记:只要任一3个下拉从接口回填过,即被锁定,禁止改动
  const readOnly = useMemo(
    () =>
      Boolean(
        values &&
        (values.org || values.scene || (values.categories && values.categories.length > 0)),
      ),
    [values],
  );

  // 受控回填:外部 values 变化时,把导入结果同步到表单
  useEffect(() => {
    if (values) {
      form.setFieldsValue(values);
    }
  }, [values, form]);

  const handleValuesChange = (changed: Partial<ImportInfoValues>, all: ImportInfoValues) => {
    // 只读模式下:所属组织/业务场景/产品分类一旦由接口回填确定,不允许再修改
    if (readOnly) {
      const lockedKeys: (keyof ImportInfoValues)[] = ['org', 'scene', 'categories'];
      const touched = lockedKeys.filter((k) => Object.prototype.hasOwnProperty.call(changed, k));
      if (touched.length > 0) {
        message.warning('该字段由模板解析结果自动回填,不可手动修改');
        // 立刻恢复原值(只读语义:展示原值恢复不改)
        form.setFieldsValue({
          org: values?.org ?? all.org,
          scene: values?.scene ?? all.scene,
          categories: values?.categories ?? all.categories,
        });
        // 回退 touched keys 恢复后,不向上游广播 change(只传 remark 的变化允许)
        const next: ImportInfoValues = { ...all };
        if (values?.org != null) next.org = values.org;
        if (values?.scene != null) next.scene = values.scene;
        if (values?.categories != null) next.categories = values.categories;
        onChange?.(next);
        return;
      }
    }
    onChange?.(all);
  };

  return (
    <UCard
      styles={{ body: { padding: 20 } }}
      style={{ marginBottom: 20, border: '1px solid #E5E7EB' }}
      title={
        <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <InfoCircleFilled style={{ color: '#2B5CE6', fontSize: 14 }} />
          <span style={{ fontSize: 15, fontWeight: 600 }}>导入信息</span>
        </div>
      }
    >
      <UForm
        form={form}
        layout="vertical"
        onValuesChange={handleValuesChange}
        onFinish={() => {
          onChange?.(form.getFieldsValue());
        }}
      >
        <div
          style={{
            display: 'grid',
            gridTemplateColumns: '1fr 1fr 1fr',
            gap: 20,
          }}
        >
          <UFormItem
            label={
              <span>
                所属组织<span style={{ color: '#E5484D', marginLeft: 2 }}>*</span>
              </span>
            }
            name="org"
            {...formItemLayout}
          >
            <USelect options={orgOptions} placeholder="请选择所属组织" />
          </UFormItem>

          <UFormItem
            label={
              <span>
                业务场景<span style={{ color: '#E5484D', marginLeft: 2 }}>*</span>
              </span>
            }
            name="scene"
            {...formItemLayout}
          >
            <USelect options={sceneOptions} placeholder="请选择业务场景" />
          </UFormItem>

          <UFormItem
            label={
              <span>
                产品分类<span style={{ color: '#E5484D', marginLeft: 2 }}>*</span>
              </span>
            }
            name="categories"
            {...formItemLayout}
          >
            <USelect
              mode="multiple"
              options={categoryOptions}
              placeholder="请选择产品分类"
              maxTagCount="responsive"
            />
          </UFormItem>
        </div>

        <div
          style={{
            display: 'grid',
            gridTemplateColumns: '1fr 1fr 1fr',
            gap: 20,
            marginTop: 4,
          }}
        >
          <div style={{ fontSize: 12, color: '#8A94A6', lineHeight: 1.6 }}>
            决定本次导入数据的数据可见范围
          </div>
          <div style={{ fontSize: 12, color: '#8A94A6', lineHeight: 1.6 }}>
            同一门店有多个业务场景时需拆分为独立行导入
          </div>
          <div style={{ fontSize: 12, color: '#8A94A6', lineHeight: 1.6 }}>
            决定模板中「产品分类」列的枚举范围
          </div>
        </div>

        <UFormItem
          label={<span style={{ marginTop: 18, display: 'inline-block' }}>备注</span>}
          name="remark"
          style={{ marginBottom: 8 }}
        >
          <UInput.TextArea
            rows={2}
            maxLength={200}
            showCount
            placeholder="选填,≤ 200 字,将在审批详情中展示给审批人"
          />
        </UFormItem>
      </UForm>
    </UCard>
  );
}