index.vue
2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<script setup lang="ts">
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { EnableRuleFormType, getFormSchemas } from './config';
  import { nextTick, ref, unref } from 'vue';
  import CustomRule from './CustomRule.vue';
  import { ScheduleTypeEnum } from '/@/enums/linkedgeEnum';
  import { EnableRuleFormModalParamsType, ScheduleType } from './type';
  import { useSceneLinkageDrawerContext } from '../SceneLinkageDrawer/sceneLinkageDrawerContext';
  const { disabledDrawer } = useSceneLinkageDrawerContext();
  const emit = defineEmits(['register', 'settingComplete']);
  const scheduleItemKey = ref<string>();
  const [registerModal, { closeModal }] = useModalInner(
    ({ record }: EnableRuleFormModalParamsType) => {
      resetFields();
      const { type, schedule, key } = record;
      scheduleItemKey.value = key;
      handleSetFieldsValue({ ...schedule, type });
      clearValidate();
    }
  );
  const [register, { getFieldsValue, validate, setFieldsValue, resetFields, clearValidate }] =
    useForm({
      schemas: getFormSchemas(),
      showActionButtonGroup: false,
      layout: 'vertical',
    });
  const customRuleElRef = ref<Nullable<InstanceType<typeof CustomRule>>>();
  const handleGetFieldsValue = async (): Promise<ScheduleType> => {
    await unref(customRuleElRef)?.validate?.();
    await validate();
    const { type, timeRange, timezone, daysOfWeek } = getFieldsValue() as EnableRuleFormType;
    const customValue = unref(customRuleElRef)?.getFieldsValue();
    if (type === ScheduleTypeEnum.ANY_TIME) return { type };
    if (type === ScheduleTypeEnum.SPECIFIC_TIME)
      return {
        type,
        timezone,
        daysOfWeek,
        ...timeRange,
      };
    return {
      type,
      timezone,
      items: customValue,
    };
  };
  const handleSetFieldsValue = async (schedule: ScheduleType) => {
    const { type, startsOn, endsOn, timezone, daysOfWeek } = schedule;
    if (type === ScheduleTypeEnum.ANY_TIME) return setFieldsValue({ type });
    if (type === ScheduleTypeEnum.SPECIFIC_TIME)
      return setFieldsValue({ type, timezone, daysOfWeek, timeRange: { startsOn, endsOn } });
    setFieldsValue(schedule);
    await nextTick();
    unref(customRuleElRef)?.setFieldsValue(schedule.items || []);
  };
  const handleOk = async () => {
    const result = await handleGetFieldsValue();
    emit('settingComplete', result, unref(scheduleItemKey));
    closeModal();
  };
</script>
<template>
  <BasicModal
    @register="registerModal"
    title="启用规则"
    :width="600"
    @ok="handleOk"
    :cancel-text="disabledDrawer ? '关闭' : '取消'"
    :show-ok-btn="!disabledDrawer"
  >
    <BasicForm @register="register" :disabled="disabledDrawer">
      <template #custom="{ model, field }">
        <CustomRule ref="customRuleElRef" v-model:value="model[field]" />
      </template>
    </BasicForm>
  </BasicModal>
</template>