import { genNameValidator, genUserNameValidator } from '@/utils/validator'

export const defaultFormData = {
  // 表单初始数据
  id: '',
  username: '',
  mobile: '',
  nickname: '',
  state: '1',
  password: 'fujica123',
  confirmpassword: 'fujica123',
  remark: '',
  roleIds: [], // 角色ID列表
  deptId: '', // 组织ID
  deptName: '', // 组织名称（显示用）
  companyId: '' // 企业ID
}

export default {
  data() {
    //  检查手机号格式
    const checkmobile = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else if (!/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    // 检查密码格式
    const checkformat = (rule, value, callback) => {
      // 验证必须包含数字、字母、特殊符号中至少2种元素
      const format = /^(?![0-9]+$)(?![a-zA-Z]+$)(?![`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]]+$)[0-9A-Za-z`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]]{6,16}$/
      if (!format.test(value)) {
        callback(new Error('密码格式不正确'))
      } else {
        callback()
      }
    }
    // 检查确认密码
    const checkconfirmpassword = (rule, value, callback) => {
      if (value != this.formData.password) {
        callback(new Error('确认新密码不一致'))
      } else {
        callback()
      }
    }
    const checkRoleList = (rule, value, callback) => {
      if (value?.length) {
        callback()
      } else {
        callback(new Error('请分配角色'))
      }
    }
    return {
      defaultFormData: { ...defaultFormData }, // 表单初始数据（打开编辑框时传入）
      formData: { ...defaultFormData }, // 表单数据
      formOptions: [ // 表单配置
        {
          type: 'input',
          label: '账号',
          key: 'username',
          rules: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { validator: genUserNameValidator([5, 16]), trigger: 'blur' }
          ],
          labelWidth: '100px'
        },
        {
          type: 'input',
          label: '姓名',
          key: 'nickname',
          rules: [
            { required: true, message: '请输入姓名', trigger: 'blur' },
            { validator: genNameValidator([2, 16]), trigger: 'blur' }
          ],
          labelWidth: '100px'
        },
        {
          type: 'input',
          label: '手机号',
          key: 'mobile',
          rules: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { validator: checkmobile, trigger: 'blur' }
          ],
          labelWidth: '100px'
        },
        {
          type: 'slot',
          slotName: 'password',
          label: '密码',
          key: 'password',
          rules: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { validator: checkformat, required: true }
          ],
          labelWidth: '100px',
          mode: ['add']
        },
        {
          type: 'slot',
          slotName: 'confirmpassword',
          label: '确认密码',
          key: 'confirmpassword',
          rules: [
            { required: true, message: '请输入确认密码', trigger: 'blur' },
            { validator: checkconfirmpassword, trigger: 'blur' }
          ],
          labelWidth: '100px',
          mode: ['add']
        },
        {
          type: 'slot',
          slotName: 'deptId',
          label: '所属组织',
          key: 'deptName',
          prop: 'deptName',
          rules: [
            { required: true, message: '请选择所属组织', trigger: 'blur' }
          ],
          labelWidth: '100px'
        },
        {
          type: 'slot',
          slotName: 'roleList',
          label: '分配角色',
          key: 'roleList',
          rules: [
            { required: true, message: '请选择分配角色', trigger: 'blur' },
            { validator: checkRoleList, trigger: 'blur' }
          ],
          labelWidth: '100px'
        },
        {
          type: 'textarea',
          label: '备注',
          key: 'remark',
          labelWidth: '100px'
        }
      ]
    }
  },
  methods: {
    resetFormData() {
      console.log(`output->defaultFormData`, defaultFormData)
      this.defaultFormData = { ...defaultFormData }
      this.formData = { ...defaultFormData }
    }
  }
}
