<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="102px" :rules="formRules" style="padding: 0 80px 0px 0;">
      <el-form-item label="企业类型：" prop="enterprise">
        <el-input v-model.trim="form.enterprise" placeholder="请输入企业类型" clearable maxlength="30" />
      </el-form-item>
      <el-form-item label="是否启用：" prop="isEnable">
        <el-switch v-model.trim="form.isEnable" :before-change="beforeChange" />
      </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({
  enterprise: '',
  isEnable: ''
})
// const validatorLink = (rule: any, value: any, callback: any) => {
//   if (linkReg.test(value)) {
//     callback()
//   } else {
//     callback(new Error("请输入正确格式的跳转链接"))
//   }
// }
const formRules = reactive<FormRules<any>>({
  enterprise: [
    { required: true, message: '请输入企业类型', trigger: 'blur' },
  ],
 
 
})

const publishFn = () => {
  if (!ruleFormRef) return
  ruleFormRef.value.validate((valid: any) => {
    if (valid) {
      if (form.linkUrl && linkReg.test(form.linkUrl)) {
        ElMessage({
          type: 'warning',
          message: "请输入正确格式的跳转链接"
        })
      }
      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 beforeChange = (ev: any) => {
  return new Promise((resolve, reject) => {
    resolve(true)
  })

}



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

const edit = () => {
  title.value = "编辑"
  addShow.value = true
}

defineExpose({
  open,
  edit
})

</script>

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