<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="分类名称" prop="classifyName">
        <el-input v-model="formData.classifyName" disabled />
      </el-form-item>
      <el-form-item label="设备编码" prop="deviceName" v-if="ifDevice">
        <el-input v-model="formData.deviceName" disabled />
      </el-form-item>
      <el-form-item label="告警属性" prop="alarmProperty">
<!--        <el-input v-model="formData.alarmProperty" placeholder="请输入告警属性" />-->
        <el-select v-model="formData.alarmProperty" placeholder="请选择告警属性" clearable filterable @change="propertyChange">
          <el-option
            v-for="dict in specs"
            :key="dict.identifier"
            :label="dict.modelName"
            :value="dict.modelName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="属性标识" prop="propertyCode">
        <el-input v-model="formData.propertyCode" placeholder="请输入属性标识" disabled />
      </el-form-item>
      <el-form-item label="上限值" prop="maxValue">
        <el-input v-model="formData.maxValue" placeholder="请输入上限" />
      </el-form-item>
      <el-form-item label="下限值" prop="minValue">
        <el-input v-model="formData.minValue" placeholder="请输入下限" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { IotAlarmSettingApi, IotAlarmSettingVO } from '@/api/pms/alarm'
import {IotDeviceApi} from "@/api/pms/device";
import {DICT_TYPE, getStrDictOptions} from "@/utils/dict";

/** 告警设置 表单 */
defineOptions({ name: 'IotAlarmSettingForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const specs = ref([])
const formData = ref({
  id: undefined,
  classifyId: undefined,
  classifyName: undefined,
  deviceId: undefined,
  deviceName: undefined,
  maxValue: undefined,
  minValue: undefined,
  alarmProperty: undefined,
  propertyCode: undefined,
})
const formRules = reactive({
  classifyName: [{ required: true, message: '分类不能为空', trigger: 'blur' }],
  alarmProperty: [{ required: true, message: '告警属性不能为空', trigger: 'blur' }],
  propertyCode: [{ required: true, message: '属性标识不能为空', trigger: 'blur' }],
  // maxValue: [{ required: true, message: '上限不能为空', trigger: 'blur' }],
  // minValue: [{ required: true, message: '下限不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref
const ifDevice = ref(false)

/** 打开弹窗 */
const open = async (type: string, id?: number,classId:any, className:any,deviceId:any, deviceName:any) => {
  debugger
  dialogVisible.value = true
  dialogTitle.value = '新增设置'
  formType.value = type
  resetForm()
  formData.value.classifyId = classId;
  formData.value.classifyName = className;
  formData.value.deviceId = deviceId;
  formData.value.deviceName = deviceName;
  ifDevice.value = !(deviceId === '' || deviceId === undefined);
  // 修改时，设置数据
  if (id) {
    dialogTitle.value = '编辑设置'
    formLoading.value = true
    try {
      formData.value = await IotAlarmSettingApi.getIotAlarmSetting(id)
    } finally {
      formLoading.value = false
    }
  }
  if (deviceId) {
    await IotDeviceApi.getIotDeviceTds(deviceId).then(res => {
      specs.value = res
      specs.value = specs.value.sort((a, b) => {
        return b.modelOrder - a.modelOrder
      })
    })
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as IotAlarmSettingVO
    if (formType.value === 'create') {
      await IotAlarmSettingApi.createIotAlarmSetting(data)
      message.success(t('common.createSuccess'))
    } else {
      await IotAlarmSettingApi.updateIotAlarmSetting(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success',ifDevice.value?'2':'1')
  } finally {
    formLoading.value = false
  }
}

const propertyChange = (value) =>{
  debugger
  const model = specs.value.filter((item) => item.modelName === value)
  formData.value.propertyCode = model[0].identifier
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    classifyId: undefined,
    classifyName: undefined,
    deviceId: undefined,
    deviceName: undefined,
    maxValue: undefined,
    minValue: undefined,
    alarmProperty: undefined,
  }
  formRef.value?.resetFields()
}
</script>
