<template>
  <BasicModal
    v-bind="$attrs"
    title="告警详情"
    @register="registerModal"
    :canFullscreen="false"
    :footer="null"
  >
    <BasicForm @register="registerForm" />
    <div class="flex justify-end">
      <a-button
        type="primary"
        class="mr-4"
        @click="handleAlarm"
        v-if="alarmStatus !== 'ACTIVE_ACK' && alarmStatus !== 'CLEARED_ACK'"
        >处理</a-button
      >
      <a-button
        danger
        type="primary"
        @click="clearAlarm"
        v-if="alarmStatus !== 'CLEARED_UNACK' && alarmStatus !== 'CLEARED_ACK'"
        >清除</a-button
      >
    </div>
  </BasicModal>
</template>

<script lang="ts">
  import { defineComponent, ref, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { alarmSchemasForm } from '../../config/detail.config';
  import { clearOrAckAlarm } from '/@/api/device/deviceManager';
  import { alarmLevel, statusType } from '../../config/detail.config';
  export default defineComponent({
    name: 'AlarmDetailModal',
    components: {
      BasicModal,
      BasicForm,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const [registerForm, { setFieldsValue, resetFields }] = useForm({
        showActionButtonGroup: false,
        schemas: alarmSchemasForm,
        labelCol: {
          span: 5,
        },
      });
      const alarmId = ref('');
      const alarmStatus = ref('');
      const [registerModal, { closeModal }] = useModalInner(async (data) => {
        await resetFields();
        await setFieldsValue({
          ...data,
          details: JSON.stringify(data?.details?.data).slice(
            1,
            JSON.stringify(data?.details?.data).length - 1
          ),
          severity: alarmLevel(data.severity),
          status: statusType(data.status),
        });
        alarmId.value = data.id;
        alarmStatus.value = data.status;
      });
      // 处理报警
      const handleAlarm = async () => {
        await clearOrAckAlarm(unref(alarmId), false);
        emit('success');
        closeModal();
      };
      // 清除报警
      const clearAlarm = async () => {
        await clearOrAckAlarm(unref(alarmId), true);
        emit('success');
        closeModal();
      };
      return {
        registerModal,
        registerForm,
        clearAlarm,
        handleAlarm,
        alarmStatus,
      };
    },
  });
</script>

<style lang="less" scoped></style>
