<template>
  <el-dialog v-model.trim="addShow" :title="title" width="30%" draggable :close-on-click-modal="false">
    <el-form :model="form" ref="ruleFormRef" label-width="120px" :rules="formRules" style="padding: 0 80px 0px 0;" :readonly="readOnlyFlag">
      <el-form-item label="iot设备名称：" prop="deviceName">
        <el-input v-model.trim="form.deviceName" placeholder="请输入iot设备名称" clearable maxlength="30" />
      </el-form-item>
      <el-form-item label="规格型号：" prop="mode">
        <el-input v-model.trim="form.mode" placeholder="请输入规格型号" clearable maxlength="30" />
      </el-form-item>
      <el-form-item label="接入协议：" prop="aggreement">
        <el-input v-model.trim="form.aggreement" placeholder="请输入接入协议" clearable maxlength="30" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancelFn">取消</el-button>
        <el-button type="primary" @click="publishFn">
          保存
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>

import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'

const emits = defineEmits(['send'])
const addShow = ref(false)
let title = ref("新增")
const ruleFormRef: any = ref()
const form = reactive({
  deviceName: '',
  mode: '',
  aggreement: ''
})
// const validatorLink = (rule: any, value: any, callback: any) => {
//   if (linkReg.test(value)) {
//     callback()
//   } else {
//     callback(new Error("请输入正确格式的跳转链接"))
//   }
// }
const formRules = reactive<FormRules<any>>({
  deviceName: [
    { required: true, message: '请输入iot设备名称', trigger: 'blur' },
  ],
  mode: [
    { required: true, message: '请输入规格型号', trigger: 'blur' },
  ], aggreement: [
    { required: true, message: '请输入接入协议', trigger: 'blur' },
  ]
})

const publishFn = () => {
  if (!ruleFormRef) return
  ruleFormRef.value.validate((valid: any) => {
    if (valid) {
    
      console.log('submit!')
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const cancelFn = (formEl: FormInstance | undefined) => {
  if (!ruleFormRef) return
  ruleFormRef.value.resetFields()
  addShow.value = false
}

 


const open = (val: any) => {
  title.value = "新增"
  addShow.value = true
}

const edit = () => {
  title.value = "编辑"
  addShow.value = true
}
const readOnlyFlag =  ref(false)
const show = (val:any) => {
  title.value = val.title
  readOnlyFlag.value= true
  addShow.value = true
}

defineExpose({
  open,
  edit,
  show
})

</script>

<style scoped lang="scss"></style>
