<template>
  <el-dialog v-model.trim="addShow" :title="title" width="660px" 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="userName">
        <el-input v-model.trim="form.userName" placeholder="请输入姓名" clearable maxlength="30" />
      </el-form-item>
      <el-form-item label="身份证：" prop="IDCard">
        <el-input v-model.trim="form.IDCard" placeholder="请输入身份证" clearable maxlength="30" />
      </el-form-item>
      <el-form-item label="性别：" prop="sex">
        <el-select v-model.trim="form.sex" placeholder="请选择性别" clearable filterable style="width:100%;">
          <el-option v-for="item in sexInfo.list" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="联系方式：" prop="contactgWays">
        <el-input v-model.trim="form.contactgWays" placeholder="请输入联系方式" clearable maxlength="30" />
      </el-form-item>
      <el-form-item label="所属岗位：" prop="position">
        <el-select v-model.trim="form.position" placeholder="请选择所属岗位" clearable filterable style="width:100%;">
          <el-option v-for="item in positionIfo.list" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="系统角色：" prop="roleType">
        <el-select v-model.trim="form.roleType" placeholder="请选择系统角色" clearable filterable style="width:100%;">
          <el-option v-for="item in roleTypeInfo.list" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </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 = ref()
const form = reactive({
  userName: '',
  IDCard: '',
  sex: '',
  contactgWays:'',
  position:'',
  roleType: ''
})

const roleTypeInfo = reactive({
  list: [{

    value: 'Option1',
    label: 'Option1',
  },
  {
    value: 'Option2',
    label: 'Option2',

  }]
})
const positionIfo = reactive({
  list: [{

    value: 'Option1',
    label: 'Option1',
  },
  {
    value: 'Option2',
    label: 'Option2',

  }]
})
const sexInfo = reactive({
  list: [{

    value: 'Option1',
    label: 'Option1',
  },
  {
    value: 'Option2',
    label: 'Option2',

  }]
})
 


// const validatorLink = (rule: any, value: any, callback: any) => {
//   if (linkReg.test(value)) {
//     callback()
//   } else {
//     callback(new Error("请输入正确格式的跳转链接"))
//   }
// }
const formRules = reactive<FormRules<any>>({
  userName: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
  ],
  IDCard: [
    { required: true, message: '请输入身份证', trigger: 'blur' },
  ],
  sex: [
    { required: true, message: '请选择性别', trigger: 'blur' },
  ],
  contactgWays: [
    { required: true, message: '请输入联系方式', trigger: 'blur' },
  ],
  position: [
    { required: true, message: '请选择所属岗位', trigger: 'blur' },
  ],
  roleType: [
    { 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>
