export function useOperations() {
  const formPropsMap = new Map([
    [
      'coal',
      {
        schemas: schemasCoalAlarm,
        submitFunc: (res) => (res.id ? updateCoalSeamAlarmRule(res) : addCoalSeamAlarmRule(res)),
        fetchRecord({ id, mineCode }) {
          return getCoalSeamAlarmRule({ coalSeamId: id, mineCode: mineCode }).then((r) => r[r.length - 1]);
        },
      },
    ],
    [
      'goaf',
      {
        schemas: schemasGoafLimit,
        submitFunc: (res) => (res.id ? updateGoafDataLimit(res) : addGoafDataLimit(res)),
        fetchRecord({ id }) {
          return getGoafDataLimit({ goafId: id }).then((r) => r[r.length - 1]);
        },
      },
    ],
  ]);
  const modalPropsMap = new Map<string, Partial<ModalProps>>([
    ['coal', { title: '预警参数设置', visible: true, loading: true }],
    ['goaf', { title: '超限预警设置', visible: true, loading: true }],
  ]);
  const submitResolver = ref<(res: any) => Promise<void>>();
  // 点击编辑后，获取对应的表单和弹窗配置
  async function handleEdit(record, sign: string) {
    if (!modalPropsMap.has(sign)) return;
    if (!formPropsMap.has(sign)) return;
    setModalProps(modalPropsMap.get(sign) as ModalProps);
    const { schemas, fetchRecord, submitFunc } = formPropsMap.get(sign)!;
    const res = await fetchRecord(record);
    console.log('debug before resetSchema');
    await resetSchema(schemas);
    console.log('debug after resetSchema');

    await nextTick();

    console.log('debug before setFieldsValue');
    if (res.id) {
      await setFieldsValue(res);
    } else {
      await resetFields();
    }
    console.log('debug after setFieldsValue');

    await nextTick();

    console.log('debug before setFormProps');
    // 不要使用setFormProps因为它会错误的触发submit方法
    // await setFormProps({ submitFunc });
    submitResolver.value = submitFunc;
    console.log('debug after setFormProps');

    setModalProps({ loading: false });
    // await setFormProps({ model: res });
  }
  async function handleAdd(record, sign: string) {
    if (!modalPropsMap.has(sign)) return;
    if (!formPropsMap.has(sign)) return;
    setModalProps(modalPropsMap.get(sign) as ModalProps);
    const { schemas, submitFunc } = formPropsMap.get(sign)!;
    await resetSchema(schemas);

    await nextTick();

    await resetFields();

    await nextTick();

    // 不要使用setFormProps因为它会错误的触发submit方法
    // await setFormProps({ submitFunc });
    submitResolver.value = submitFunc;

    setModalProps({ loading: false });
    // await setFormProps({ model: res });
  }

  const deletionPropsMap = new Map<string, { api: (id: string) => Promise<void> }>([
    ['coal', { api: (id) => deleteCoalSeamAlarmRule({ id }) }],
    ['goaf', { api: (id) => deleteGoafDataLimit({ id }) }],
  ]);

  function handleDelete(record, sign: string) {
    if (!deletionPropsMap.has(sign)) return;
    deletionPropsMap.get(sign)?.api(record.id);
  }

  const [registerModal, { setModalProps, closeModal }] = useModal();
  const [registerForm, { setProps: setFormProps, resetFields, setFieldsValue, validate, submit, resetSchema }] = useForm({
    model: {},
    schemas: schemasCoalAlarm,
    showResetButton: false,
    showSubmitButton: false,
    colon: false,
    compact: true,
  });

  function handleSubmit() {
    return validate().then((res) => {
      submitResolver && submitResolver(res);
    });
    submit().then(() => {
      closeModal();
    });
  }
}
