<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="设备ID" prop="deviceId">
        <el-input v-model="formData.deviceId" placeholder="请输入设备ID" />
      </el-form-item>
      <el-form-item label="计量编号" prop="measurementNumber">
        <el-input v-model="formData.measurementNumber" placeholder="请输入计量编号" />
      </el-form-item>
      <el-form-item label="计量范围" prop="measurementRange">
        <el-input v-model="formData.measurementRange" placeholder="请输入计量范围" />
      </el-form-item>
      <el-form-item label="允许误差" prop="allowableError">
        <el-input v-model="formData.allowableError" placeholder="请输入允许误差" />
      </el-form-item>
      <el-form-item label="计量点位" prop="measurementPoint">
        <el-input v-model="formData.measurementPoint" placeholder="请输入计量点位" />
      </el-form-item>
      <el-form-item label="管理等级" prop="manageLeavel">
        <el-input v-model="formData.manageLeavel" placeholder="请输入管理等级" />
      </el-form-item>
      <el-form-item label="仪器精度" prop="instrumentPrecision">
        <el-input v-model="formData.instrumentPrecision" placeholder="请输入仪器精度" />
      </el-form-item>
      <el-form-item label="计量方式" prop="measurementMethod">
        <el-input v-model="formData.measurementMethod" placeholder="请输入计量方式" />
      </el-form-item>
      <el-form-item label="上次计量日期" prop="lastMeasurementDate">
        <el-date-picker
          v-model="formData.lastMeasurementDate"
          type="date"
          value-format="x"
          placeholder="选择上次计量日期"
        />
      </el-form-item>
      <el-form-item label="下次计量日期" prop="nextMeasurementDate">
        <el-date-picker
          v-model="formData.nextMeasurementDate"
          type="date"
          value-format="x"
          placeholder="选择下次计量日期"
        />
      </el-form-item>
      <el-form-item label="计量周期" prop="measurementCycle">
        <el-input v-model="formData.measurementCycle" placeholder="请输入计量周期" />
      </el-form-item>
      <el-form-item label="提醒时间" prop="reminderTime">
        <el-date-picker
          v-model="formData.reminderTime"
          type="date"
          value-format="x"
          placeholder="选择提醒时间"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="responsiblePerson">
        <el-input v-model="formData.responsiblePerson" placeholder="请输入负责人" />
      </el-form-item>
      <el-form-item label="说明" prop="description">
        <Editor v-model="formData.description" height="150px" />
      </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 { MeasurementApi, Measurement } from '@/api/device/measurement'

/** 设备计量信息 表单 */
defineOptions({ name: 'MeasurementForm' })

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 formData = ref({
  id: undefined,
  deviceId: undefined,
  measurementNumber: undefined,
  measurementRange: undefined,
  allowableError: undefined,
  measurementPoint: undefined,
  manageLeavel: undefined,
  instrumentPrecision: undefined,
  measurementMethod: undefined,
  lastMeasurementDate: undefined,
  nextMeasurementDate: undefined,
  measurementCycle: undefined,
  reminderTime: undefined,
  responsiblePerson: undefined,
  description: undefined,
})
const formRules = reactive({
  deviceId: [{ required: true, message: '设备ID不能为空', trigger: 'blur' }],
})
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 MeasurementApi.getMeasurement(id)
    } finally {
      formLoading.value = false
    }
  }
}
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 Measurement
    if (formType.value === 'create') {
      await MeasurementApi.createMeasurement(data)
      message.success(t('common.createSuccess'))
    } else {
      await MeasurementApi.updateMeasurement(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    deviceId: undefined,
    measurementNumber: undefined,
    measurementRange: undefined,
    allowableError: undefined,
    measurementPoint: undefined,
    manageLeavel: undefined,
    instrumentPrecision: undefined,
    measurementMethod: undefined,
    lastMeasurementDate: undefined,
    nextMeasurementDate: undefined,
    measurementCycle: undefined,
    reminderTime: undefined,
    responsiblePerson: undefined,
    description: undefined,
  }
  formRef.value?.resetFields()
}
</script>