import WeValidator from 'we-validator';
const cardRules = {
  rules: {
    relationShip: {
      required: true
    },
    defaultCard: {
      required: true
    },
    cardNum: {
      required: true
    },
    name: {
      required: true
    },
    idCardNo: {
      required: true // idcard: true,

    },
    phoneNumber: {
      required: true // mobile: true,

    },
    verifyCode: {
      required: true
    }
  },
  messages: {
    relationShip: {
      required: '请选择关系'
    },
    defaultCard: {
      required: '请选择是否默认'
    },
    cardNum: {
      required: '请输入就诊卡号'
    },
    name: {
      required: '请输入就诊人姓名',
      mobile: '手机号格式不正确'
    },
    idCardNo: {
      required: '请输入身份证号码' // idcard: '身份证号码格式错误',

    },
    phoneNumber: {
      required: '请输入手机号' // mobile: '手机号格式错误',

    },
    verifyCode: {
      required: '请输入验证码'
    }
  }
};

const getRules = fieldArray => {
  const ret = {
    rules: {},
    messages: cardRules.messages
  };

  for (let i = 0; i < fieldArray.length; i++) {
    const field = fieldArray[i];

    if (cardRules.rules[field]) {
      ret.rules[field] = cardRules.rules[field];
    }
  }

  return ret;
};

const initValidator = (type, filter) => {
  // type: 1,// 0=新建 1=绑定
  let rule = null;

  if (type === 0) {
    rule = getRules(filter(['relationShip', 'name', 'idCardNo', 'phoneNumber', 'verifyCode', 'defaultCard']));
  } else {
    rule = getRules(filter(['relationShip', 'cardNum', 'name', 'idCardNo', 'phoneNumber', 'verifyCode', 'defaultCard']));
  }

  return new WeValidator(rule);
};

export { initValidator };