<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="700px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备名称" prop="deviceName">
            <el-input v-model="formData.deviceName" placeholder="请输入设备名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备分类" prop="deviceCategory">
            <el-input v-model="formData.deviceCategory" placeholder="请输入设备分类" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="采购地方" prop="purchasePlace">
            <el-input v-model="formData.purchasePlace" placeholder="请输入采购地方" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采购供应商" prop="supplier">
            <el-input v-model="formData.supplier" placeholder="请输入采购供应商" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="联系方式" prop="contactInfo">
            <el-input v-model="formData.contactInfo" placeholder="请输入联系方式" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采购价格" prop="purchasePrice">
            <el-input-number
              v-model="formData.purchasePrice"
              :precision="2"
              :min="0"
              placeholder="请输入采购价格"
              class="!w-full"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="产品参数" prop="productParams">
        <el-input
          v-model="formData.productParams"
          type="textarea"
          :rows="3"
          placeholder="请输入产品参数"
        />
      </el-form-item>
      <el-form-item label="产品要素" prop="productElements">
        <el-input
          v-model="formData.productElements"
          type="textarea"
          :rows="3"
          placeholder="请输入产品要素"
        />
      </el-form-item>
      <el-form-item label="使用部件" prop="usedParts">
        <el-input v-model="formData.usedParts" placeholder="请输入使用部件" />
      </el-form-item>
      <el-form-item label="设备图片" prop="picUrl">
        <UploadImg v-model="formData.picUrl" :height="'120px'" :width="'120px'" />
      </el-form-item>
      <el-form-item label="备注信息" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          :rows="3"
          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 { DeviceInfoApi, DeviceInfoVO } from '@/api/iot/device/deviceInfo'
import { UploadImg } from '@/components/UploadFile'

/** 设备信息库表单 */
defineOptions({ name: 'DeviceInfoForm' })

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

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  deviceName: undefined,
  deviceCategory: undefined,
  purchasePlace: undefined,
  supplier: undefined,
  contactInfo: undefined,
  purchasePrice: undefined,
  productParams: undefined,
  productElements: undefined,
  usedParts: undefined,
  picUrl: undefined,
  remark: undefined
})
const formRules = reactive({
  deviceName: [{ required: true, message: '设备名称不能为空', 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 DeviceInfoApi.getDeviceInfo(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 DeviceInfoVO
    if (formType.value === 'create') {
      await DeviceInfoApi.createDeviceInfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await DeviceInfoApi.updateDeviceInfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    deviceName: undefined,
    deviceCategory: undefined,
    purchasePlace: undefined,
    supplier: undefined,
    contactInfo: undefined,
    purchasePrice: undefined,
    productParams: undefined,
    productElements: undefined,
    usedParts: undefined,
    picUrl: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}
</script>
