import Vue from "vue";
import {extend} from "vee-validate";
import {configure} from "vee-validate";
import {required, email} from "vee-validate/dist/rules";
import {ValidationProvider} from "vee-validate";
import {ValidationObserver} from "vee-validate";
//import {messages} from "vee-validate/dist/locale/en.json";
import {setInteractionMode} from "vee-validate";
import "./validator.css";
// import "./validator.less";

const nullMsg = {
	notNull: "必填",
	imperfect: "{_field_}信息不完善"
};
//过滤英文格式双引号
extend("not_doubleQuotes", {
	validate: value => {
		return value.indexOf("\"") < 0;
	},
	message: "{_field_}不能包含字符'\"'"
});
//过滤英文格式冒号
extend("not_colon", {
	validate: value => {
		return value.indexOf(":") < 0;
	},
	message: "{_field_}不能包含字符':'"
});
//过滤英文格式单引号
extend("not_apostrophe", {
	validate: value => {
		return value.indexOf("'") < 0;
	},
	message: "{_field_}不能包含字符'"
});
//判断合法包名加合法类名
extend("legal_class_name", {
	validate: value => {
		let reg = /^[a-zA-Z]+[0-9a-zA-Z_]*(\.[a-zA-Z]+[0-9a-zA-Z_]*)*\.[a-zA-Z]+[0-9a-zA-Z_]*$/g;
		return reg.test(value);
	},
	message: "java类名不合法"
});
//不允许为空
extend("not_null", {
	validate: value => {
		return value !== "" && value.length > 0;
	},
	message: "{_field_}信息不完善"
});
//最大长度
extend("maxLength", {
	validate: (value, {max}) => {
		return value.length <= max;
	},
	params: ["max"],
	message: "不允许超过{max}个字符"
});
//数值区间
extend("minMax", {
	validate: (value, {min, max}) => {
		value = parseInt(value);
		min = parseInt(min);
		max = parseInt(max);
		return min <= value && value <= max;
	},
	params: ["min", "max"],
	message: "{_field_}只能在{min}到{max}之间，且包含{min}和{max}"
});
//只能是数值
extend("is_number", {
	validate: value => {
		return new RegExp("^[0-9]*$").test(value);
	},
	message: "{_field_}只能是正整数"
});
//JavaScript语法校验
extend("javaScript_required", {
	validate: value => {
		let reg = /^function[\n\s]+(.+)[\n\s]*\((.*)\)[\n\s]*{((.|\n)*)}[\n\s]*$/g;
		return reg.test(value);
	},
	message: "{_field_}语法错误，请检查"
});
extend("required", {
	...required,
	message: nullMsg.notNull
});

extend("teamName_formatCheck",{
	validate: value => {
		let reg = /^[a-zA-Z0-9_\u4e00-\u9fa5]+$/g;
		return reg.test(value);
	},
	message: "只能输入字母、数字、汉字或下划线"
})

configure({
	classes: {
		valid: "is-valid ",
		invalid: "is-invalid",
		dirty: ["is-dirty", "is-dirty"] // multiple classes per flag!
		// ...
	}
});
Vue.component("ValidationProvider", ValidationProvider);
Vue.component("ValidationObserver", ValidationObserver);

