<!-- 设备属性模型表单（property 项） -->
<template>
  <el-form-item
    :rules="[{ required: true, message: '请选择数据类型', trigger: 'change' }]"
    label="数据类型"
    prop="selectOptions.type"
  >
    <el-select v-model="selectOptions.type" placeholder="请选择数据类型" @change="handleChange">
      <!-- ARRAY 和 STRUCT 类型数据相互嵌套时，最多支持递归嵌套 2 层（父和子） ${option.value}(${option.label}) -->
      <el-option
        v-for="option in getDataTypeOptions"
        :key="option.value"
        :label="`${option.label}`"
        :value="option.value"
      />
    </el-select>
  </el-form-item>
  <!-- 数值型配置 -->
  <DeviceAttrModelNumberDataSpecs
    v-if="
      [DataSpecsDataType.INT, DataSpecsDataType.DOUBLE, DataSpecsDataType.FLOAT].includes(
        selectOptions.type || ''
      )
    "
    v-model="selectOptions.dataSpecs"
  />
  <!-- 枚举型配置 -->
  <DeviceAttrModelEnumDataSpecs
    v-if="selectOptions.type === DataSpecsDataType.ENUM"
    v-model="selectOptions.dataSpecsList"
  />
  <!-- 布尔型配置 -->
  <el-form-item v-if="selectOptions.type === DataSpecsDataType.BOOL" label="布尔值">
    <template v-for="(item, index) in selectOptions.dataSpecsList" :key="item.value">
      <div class="flex items-center justify-start w-1/1 mb-5px">
        <span>{{ item.value }}</span>
        <span class="mx-2">-</span>
        <el-form-item
          :prop="`selectOptions.dataSpecsList[${index}].name`"
          :rules="[
            { required: true, message: '枚举描述不能为空' },
            { validator: validateBoolName, trigger: 'blur' }
          ]"
          class="flex-1 mb-0"
        >
          <el-input
            v-model="item.name"
            :placeholder="`如：${item.value === 0 ? '关' : '开'}`"
            class="w-255px!"
          />
        </el-form-item>
      </div>
    </template>
  </el-form-item>
  <!-- 文本型配置
  <el-form-item
    v-if="selectOptions.type === DataSpecsDataType.TEXT"
    label="数据长度"
    prop="selectOptions.dataSpecs.length"
  >
    <el-input v-model="selectOptions.dataSpecs.length" class="w-255px!" placeholder="请输入文本字节长度">
      <template #append>字节</template>
    </el-input>
  </el-form-item>
  -->
  <!-- 时间型配置 -->
  <el-form-item v-if="selectOptions.type === DataSpecsDataType.DATE" label="时间格式" prop="date">
    <el-input class="w-255px!" disabled placeholder="String 类型的 UTC 时间戳（毫秒）" />
  </el-form-item>
  <!-- 数组型配置-->
  <DeviceAttrModelArrayDataSpecs
    v-if="selectOptions.type === DataSpecsDataType.ARRAY"
    v-model="selectOptions.dataSpecs"
  />
  <!-- Struct 型配置-->
  <DeviceAttrModelStructDataSpecs
    v-if="selectOptions.type === DataSpecsDataType.STRUCT"
    v-model="selectOptions.dataSpecsList"
  />
  <el-form-item v-if="!isStructDataSpecs && !isParams" label="是否必填" prop="selectOptions.requiredFlag">
    <el-radio-group v-model="selectOptions.requiredFlag">
      <el-radio :label="DeviceAttrModelRequired.REQUIRED.value">
        {{ DeviceAttrModelRequired.REQUIRED.label }}
      </el-radio>
      <el-radio :label="DeviceAttrModelRequired.NOT_REQUIRED.value">
        {{ DeviceAttrModelRequired.NOT_REQUIRED.label }}
      </el-radio>
    </el-radio-group>
  </el-form-item>
</template>

<script lang="ts" setup>
import { useVModel } from '@vueuse/core'
import {
  DataSpecsDataType,
  dataTypeOptions,
  DeviceAttrModelRequired,
  validateBoolName
} from './config'
import {
  DeviceAttrModelArrayDataSpecs,
  DeviceAttrModelEnumDataSpecs,
  DeviceAttrModelNumberDataSpecs,
  DeviceAttrModelStructDataSpecs
} from '../dataSpecs'
import { DeviceTemplateAttrs } from '@/api/pms/deviceattrmodel'
import { isEmpty } from '@/utils/is'

/** 设备属性模板 属性 */
defineOptions({ name: 'AttrTemplateModelProperty' })

const props = defineProps<{ modelValue: any; isStructDataSpecs?: boolean; isParams?: boolean }>()
const emits = defineEmits(['update:modelValue'])
const selectOptions = useVModel(props, 'modelValue', emits) as Ref<DeviceTemplateAttrs>
const getDataTypeOptions = computed(() => {
  return !props.isStructDataSpecs
    ? dataTypeOptions
    : dataTypeOptions.filter(
        (item) =>
          !([DataSpecsDataType.STRUCT, DataSpecsDataType.ARRAY] as any[]).includes(item.value)
      )
}) // 获得数据类型列表

/** 属性值的数据类型切换时初始化相关数据 */
const handleChange = (dataType: any) => {
  selectOptions.value.dataSpecs = {}
  selectOptions.value.dataSpecsList = []
  // 不是列表型数据才设置 dataSpecs.dataType
  ![DataSpecsDataType.ENUM, DataSpecsDataType.BOOL, DataSpecsDataType.STRUCT].includes(dataType) &&
    (selectOptions.value.dataSpecs.dataType = dataType)
  switch (dataType) {
    case DataSpecsDataType.ENUM:
      selectOptions.value.dataSpecsList.push({
        dataType: DataSpecsDataType.ENUM,
        name: '', // 枚举项的名称
        value: undefined // 枚举值
      })
      break
    case DataSpecsDataType.BOOL:
      for (let i = 0; i < 2; i++) {
        selectOptions.value.dataSpecsList.push({
          dataType: DataSpecsDataType.BOOL,
          name: '', // 布尔值的名称
          value: i // 布尔值
        })
      }
      break
  }
}

// 默认选中 是否必填
watch(
  () => selectOptions.value.requiredFlag,
  (val: string) => {
    if (props.isStructDataSpecs || props.isParams) {
      return
    }
    isEmpty(val) && (selectOptions.value.requiredFlag = DeviceAttrModelRequired.REQUIRED.value)
  },
  { immediate: true }
)

</script>

<style lang="scss" scoped>
:deep(.el-form-item) {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
