import { get } from 'sortablejs'
import Vue from 'vue'
export default {
  props: {
    label: {
      type: String,
      default: ''
    },
    prop: {
      type: String
    },
    required: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    clearable: {
      type: Boolean,
      default: true
    },
    readonly: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String
    },
    formModel: {
      type: Object
    },
    suffix: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: ''
    },
    validate: {
      type: String,
    },
    rules: {
      type: Array,
      default: () => [],
    },
    showLabel: {
      type: Boolean,
      default: true,
    },
    span: {
      type: Number,
      default: 8
    },
    trigger: {
      type: String,
      default: 'blur'
    },
    valueProp: {
      type: String,
      default: "value",
    },
    labelProp: {
      type: String,
      default: "label",
    },
    model: {
      type: Object
    }
  },
  data() {
    return {
      formRef: null
    }
  },
  methods: {
    requireRule(required, label, type) {
      const rules =  [{
        required: required,
        // message: `${label}不能为空`,
        message: this.$t('common.notInputAlertMessage',[label]),
        trigger: [this.trigger,'change']
      }]

      // const regRule = {message: `${label}格式不正确`, trigger: ['blur', 'change']}
      const regRule = {message: this.$t('common.errorFormatMessage',[label]), trigger: ['blur', 'change']}
      if (type === 'phone') {
        regRule.pattern = /^1(3|4|5|7|8|9)\d{9}$/
       // /^1[3|4|5|6|7|8|9][0-9]\d{8}$/
       // /^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$/
        rules.push(regRule)
      } else if (type === 'idCard') {
        regRule.pattern = /^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$/
        rules.push(regRule)
      }  else if (type === 'email') {
        regRule.pattern = /^[a-z0-9]+([._-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
        rules.push(regRule)
      }
      return rules
    }
  },
  created() {
    const getParent = e => e.validate ? e : getParent(e.$parent)
    const formRef = getParent(this.$parent)
    // 确保所有Form属性都是响应式的
    if(this.prop){
      Vue.set(formRef.model, this.prop, formRef.model[this.prop]);
    }

    this.formRef = this.model ? {model: this.model}: formRef
  },
}
