<template>
  <el-dialog :title="title" v-model="dialogOpen" width="600px" append-to-body>
    <el-form :model="form" :rules="rules" ref="deviceRef" label-width="80px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="设备名称" prop="deviceName">
            <el-input v-model="form.deviceName" placeholder="请输入设备名称" maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备型号" prop="model">
            <el-input v-model="form.model" placeholder="请输入设备型号" maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="ip地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入ip地址" maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio :value="'0'">正常</el-radio>
              <el-radio :value="'1'">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入备注内容"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { addPest, updatePest } from '@/api/pest'

const props = defineProps({
  open: { type: Boolean, required: true },
  form: { type: Object, required: true },
  title: { type: String, required: true }
})

const emits = defineEmits(['update:open', 'success'])

// 创建本地状态来响应open属性的变化
const dialogOpen = ref(props.open)

// 表单验证规则
const rules = {
  deviceName: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }],
  address: [{ required: true, message: 'ip地址不能为空', trigger: 'blur' }],
  model: [{ required: true, message: '设备型号不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '请选择状态', trigger: 'blur' }]
}

// 创建表单引用
const deviceRef = ref(null)

const { proxy } = getCurrentInstance()

onMounted(() => {
  // 重置表单
  setTimeout(() => {
    deviceRef.value?.resetFields()
  }, 0)
})

// 监听props.open的变化，同步到本地状态
watch(
  () => props.open,
  (newValue) => {
    dialogOpen.value = newValue
  }
)

// 监听本地状态的变化，通知父组件更新
watch(
  dialogOpen,
  (newValue) => {
    if (newValue !== props.open) {
      emits('update:open', newValue)
    }
  }
)

// 移除或修改相关函数中的emits调用
// 提交表单
async function submitForm() {
  try {
    await deviceRef.value.validate()

    if (props.form.id) {
      await updatePest(props.form)
      proxy.$model.msgSuccess('修改成功')
    } else {
      await addPest(props.form)
      proxy.$model.msgSuccess('新增成功')
    }

    dialogOpen.value = false
    emits('success')
  } catch (error) {
    // 表单验证失败不提示错误
    if (error !== 'cancel') {
      proxy.$model.msgError('操作失败: ' + (error.message || '未知错误'))
    }
  }
}

// 取消按钮操作
function cancel() {
  dialogOpen.value = false
  deviceRef.value?.resetFields()
}
</script>