index.vue 4.2 KB
<script lang="ts" setup>
  import { ref, unref } from 'vue';
  import { AggregateDataEnum, formSchema, QueryWay, SchemaFiled } from './config';
  import { useForm } from '/@/components/Form';
  import { useModalInner } from '/@/components/Modal';
  import { useGridLayout } from '/@/hooks/component/useGridLayout';
  import { ColEx } from '/@/components/Form/src/types';
  import { BasicForm } from '/@/components/Form';
  import { BasicModal } from '/@/components/Modal';
  import { nextTick } from 'vue';
  import {
    getPacketIntervalByRange,
    getPacketIntervalByValue,
  } from '/@/views/device/localtion/cpns/TimePeriodForm/helper';

  const emit = defineEmits(['register', 'getAlarmForm', 'getHistoryForm']);
  // const emit = defineEmits<{
  //   (event: 'getAlarmForm', data: WidgetDataType): void;
  // }>();

  const fontId = ref('');
  const [registerModal, { closeModal }] = useModalInner(async (data) => {
    fontId.value = unref(data).record.frontId;
    method.updateSchema([
      {
        field: SchemaFiled.PAGE_SIZE,
        ifShow: unref(fontId) !== 'StatisticsComponent7',
      },
    ]);
    if (unref(fontId) !== 'StatisticsComponent7') return;
    await nextTick();
    method.appendSchemaByField(
      {
        field: SchemaFiled.AGG,
        label: '数据聚合功能',
        component: 'Select',
        defaultValue: AggregateDataEnum.NONE,
        componentProps: {
          getPopupContainer: () => document.body,
          options: [
            { label: '最小值', value: AggregateDataEnum.MIN },
            { label: '最大值', value: AggregateDataEnum.MAX },
            { label: '平均值', value: AggregateDataEnum.AVG },
            { label: '求和', value: AggregateDataEnum.SUM },
            { label: '计数', value: AggregateDataEnum.COUNT },
            { label: '空', value: AggregateDataEnum.NONE },
          ],
        },
      },
      SchemaFiled.DATE_RANGE
    );
    method.appendSchemaByField(
      {
        field: SchemaFiled.INTERVAL,
        label: '分组间隔',
        component: 'Select',
        dynamicRules: ({ model }) => {
          return [
            {
              required: model[SchemaFiled.AGG] !== AggregateDataEnum.NONE,
              message: '分组间隔为必填项',
              type: 'number',
            },
          ];
        },
        ifShow({ values }) {
          return values[SchemaFiled.AGG] !== AggregateDataEnum.NONE;
        },
        componentProps({ formModel, formActionType }) {
          const options =
            formModel[SchemaFiled.WAY] === QueryWay.LATEST
              ? getPacketIntervalByValue(formModel[SchemaFiled.START_TS])
              : getPacketIntervalByRange(formModel[SchemaFiled.DATE_RANGE]);
          if (formModel[SchemaFiled.AGG] !== AggregateDataEnum.NONE) {
            formActionType.setFieldsValue({ [SchemaFiled.LIMIT]: null });
          }
          return {
            options,
            getPopupContainer: () => document.body,
          };
        },
      },
      SchemaFiled.AGG
    );
  });

  const [register, method] = useForm({
    schemas: formSchema(),
    baseColProps: useGridLayout(1) as unknown as ColEx,
    showSubmitButton: false,
    showResetButton: false,
    rowProps: {
      gutter: 10,
    },
    labelWidth: 120,
    fieldMapToTime: [
      [SchemaFiled.DATE_RANGE, [SchemaFiled.START_TS, SchemaFiled.END_TS], 'YYYY-MM-DD HH:ss:mm'],
    ],
  });

  const handleCancel = () => {
    // destory()
  };
  const handleSubmit = async () => {
    const values = method.getFieldsValue();
    if (unref(fontId) == 'StatisticsComponent7') {
      emit('getHistoryForm', values);
    } else {
      emit('getAlarmForm', values);
    }

    await nextTick();
    closeModal();
  };
</script>

<template>
  <BasicModal
    @register="registerModal"
    @cancel="handleCancel"
    @ok="handleSubmit"
    :destroy-on-close="true"
    :show-ok-btn="true"
    cancel-text="关闭"
    width="40%"
    title="历史趋势"
  >
    <section
      class="flex flex-col p-4 h-full w-full min-w-7/10"
      style="color: #f0f2f5; background-color: #f0f2f5"
    >
      <section class="bg-white my-3 p-2">
        <BasicForm @register="register" />
      </section>
    </section>
  </BasicModal>
</template>

<style scoped></style>