<!-- 产品的物模型表单 -->
<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="ThingModelFormRules"
      label-width="100px">
      <el-form-item label="功能类型" prop="type">
        <el-radio-group v-model="formData.type">
          <el-radio-button
            v-for="dict in getIntDictOptions(DICT_TYPE.IOT_THING_MODEL_TYPE)"
            :key="dict.value"
            :value="dict.value">
            {{ dict.label }}
          </el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="功能名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入功能名称" />
      </el-form-item>
      <el-form-item label="标识符" prop="identifier">
        <el-input v-model="formData.identifier" placeholder="请输入标识符" />
      </el-form-item>
      <!-- 属性配�?-->
      <ThingModelProperty
        v-if="formData.type === IoTThingModelTypeEnum.PROPERTY"
        v-model="formData.property" />
      <!-- 服务配置 -->
      <ThingModelService
        v-if="formData.type === IoTThingModelTypeEnum.SERVICE"
        v-model="formData.service" />
      <!-- 事件配置 -->
      <ThingModelEvent
        v-if="formData.type === IoTThingModelTypeEnum.EVENT"
        v-model="formData.event" />
      <el-form-item label="描述" prop="description">
        <el-input
          v-model="formData.description"
          :maxlength="200"
          :rows="3"
          placeholder="请输入属性描述"
          type="textarea" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确定</el-button>
      <el-button @click="dialogVisible = false">取消</el-button>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
import ThingModelProperty from './ThingModelProperty.vue'
import ThingModelService from './ThingModelService.vue'
import ThingModelEvent from './ThingModelEvent.vue'
import { ThingModelCommon, ThingModelData, ThingModelFormRules } from '@/api/iot/thingmodel'
import { IoTDataSpecsDataTypeEnum, IoTThingModelTypeEnum } from '@/views/iot/utils/constants'
import { cloneDeep } from 'lodash-es'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { isEmpty } from '@/utils/is'

/** IoT 物模型数据表�?*/
defineOptions({ name: 'IoTThingModelForm' })

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

const dialogVisible = ref(false) // 弹窗的是否展�?
const dialogTitle = ref('') // 弹窗的标�?
const formLoading = ref(false) // 表单的加载中�?）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref<ThingModelData>({
  type: IoTThingModelTypeEnum.PROPERTY,
  dataType: IoTDataSpecsDataTypeEnum.INT,
  property: {
    dataType: IoTDataSpecsDataTypeEnum.INT,
    dataSpecs: {
      dataType: IoTDataSpecsDataTypeEnum.INT
    }
  },
  service: {},
  event: {}
})

const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ThingModelCommon.getCommonThingModel(id)
      // 情况一：属性初始化
      if (isEmpty(formData.value.property)) {
        formData.value.dataType = IoTDataSpecsDataTypeEnum.INT
        formData.value.property = {
          dataType: IoTDataSpecsDataTypeEnum.INT,
          dataSpecs: {
            dataType: IoTDataSpecsDataTypeEnum.INT
          }
        }
      }
      // 情况二：服务初始�?
      if (isEmpty(formData.value.service)) {
        formData.value.service = {}
      }
      // 情况三：事件初始�?
      if (isEmpty(formData.value.event)) {
        formData.value.event = {}
      }
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open, close: () => (dialogVisible.value = false) })

/** 提交表单 */
const emit = defineEmits(['success'])
const submitForm = async () => {
  await formRef.value.validate()
  formLoading.value = true
  try {
    const data = cloneDeep(formData.value) as ThingModelData

    fillExtraAttributes(data)
    if (formType.value === 'create') {
      await ThingModelCommon.createCommonThingModel(data)
      message.success(t('common.createSuccess'))
    } else {
      await ThingModelCommon.updateCommonThingModel(data)
      message.success(t('common.updateSuccess'))
    }
    // 关闭弹窗
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 填写额外的属性（处理不同类型的情况） */
const fillExtraAttributes = (data: any) => {
  // 属�?
  if (data.type === IoTThingModelTypeEnum.PROPERTY) {
    removeDataSpecs(data.property)
    data.dataType = data.property.dataType
    data.property.identifier = data.identifier
    data.property.name = data.name
    delete data.service
    delete data.event
  }
  // 服务
  if (data.type === IoTThingModelTypeEnum.SERVICE) {
    removeDataSpecs(data.service)
    data.dataType = data.service.dataType
    data.service.identifier = data.identifier
    data.service.name = data.name
    delete data.property
    delete data.event
  }
  // 事件
  if (data.type === IoTThingModelTypeEnum.EVENT) {
    removeDataSpecs(data.event)
    data.dataType = data.event.dataType
    data.event.identifier = data.identifier
    data.event.name = data.name
    delete data.property
    delete data.service
  }
}

/** 处理 dataSpecs 为空的情�?*/
const removeDataSpecs = (val: any) => {
  if (isEmpty(val.dataSpecs)) {
    delete val.dataSpecs
  }
  if (isEmpty(val.dataSpecsList)) {
    delete val.dataSpecsList
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    type: IoTThingModelTypeEnum.PROPERTY,
    dataType: IoTDataSpecsDataTypeEnum.INT,
    property: {
      dataType: IoTDataSpecsDataTypeEnum.INT,
      dataSpecs: {
        dataType: IoTDataSpecsDataTypeEnum.INT
      }
    },
    service: {},
    event: {}
  }
  formRef.value?.resetFields()
}
</script>
