function noGenerator(object, entity, name, key, useDate, useKey, count, int) {
	var _count = !count ? 4 : count
	object.$z.execute('NOGenerator', {
		key: key,
		useDate: useDate,
		useKey: useKey,
		count: _count
	}, (ret) => {
		// if (int)
		// 	entity[name] = parseInt(ret)
		// else
		entity[name] = ret
	})
}

function compareDate(date1, date2) {
	if (date1 != "" && date2 != "") {
		var _date1 = new Date(date1).getTime();
		var _date2 = new Date(date2).getTime();
		if (_date1<=_date2)
			return false;
		else
			return true
	}
	return false
}


function formValids(_this, data, fname, errmsg) {
	if (errmsg)
		layer.msg(errmsg);
	_this.$nextTick(() => {
		data.zform.valids['_' + fname] = {
			errmsg: errmsg,
			valid: errmsg ? false : true,
		};
	});
}

function formValidsNoMsg(_this, data, fname, errmsg) {
	_this.$nextTick(() => {
		data.zform.valids['_' + fname] = {
			errmsg: errmsg,
			valid: errmsg ? false : true,
		};
	});
}

function fuzzyValids(_this, data, field) {
	var result = []
	var formData = data.zform.getAllDataForParams();
	var params = Object.assign({
		u_u: data.entity.fielddescs(field).option.uuid,
		k_k: data.entity[field],
		p_p: 0,
		pl_pl: 100
	}, formData);
	_this.$z.executeSync("ZSearchOption", params, (options) => {
		for (var i in options.data)
			result.push(options.data[i].v)
	});
	return result.indexOf(data.entity[field]) == -1
}

//////////////////////////////////////////////////////////
//websocket
function WSocket(group, onMessage, openedMsg) {
	if (typeof(WebSocket) === "undefined") {
		alert("您的浏览器不支持socket");
		return;
	}
	// this.group = group;
	var ishttps = 'https:' == document.location.protocol ? true : false;
	var ws = "ws"
	if (ishttps)
		ws = "wss"
	var url = ws + "://" + window.location.host + "/admin_api/ics-params-configuration/wsocket/" + group;
	// var url = "ws://localhost:18089/wsocket/"+group;

	this.websock = new WebSocket(url);
	this.websock.onopen = (e) => {
		console.log("WSocket onopened,e=", e);
		if (openedMsg)
			this.websock.send(openedMsg);
	};
	this.websock.onerror = function(e) {
		console.log("WSocket onerror,e=", e);
	};
	this.websock.onclose = (e) => {
		console.log("WSocket onclose,e=", e);
		if (e.code != 1000) {
			this.websock = openWSocket(group, onMessage, openedMsg)
		}
	};
	this.websock.onmessage = function(e) {
		onMessage(e.data)
	};

	// console.log("url=",url,"WSocket=",this.websock);
}

WSocket.prototype.close = function(msg) {
	if (typeof this.websock === "undefined") {
		return false;
	}
	// if (this.websock.readyState != 3) {
	// 	return false;
	// }
	if (this.websock.readyState == 1)
		this.websock.close(msg);
}

//广播消息
WSocket.prototype.broadcast = function(msg) {
	// console.log("broadcast this.websock",this.websock);

	if (typeof this.websock === "undefined") {
		// alert("websocket还没有连接，或者连接失败，请检测");
		console.log("websocket还没有连接，或者连接失败，请检测")
		return false;
	}
	if (this.websock.readyState == 3) {
		// alert("websocket已经关闭，请重新连接");
		console.log("websocket已经关闭，请重新连接")
		return false;
	}
	// console.log(this.websock);

	if (this.websock.readyState == 1)
		this.websock.send(msg);
}

function openWSocket(group, onMessage, openedMsg) {
	return new WSocket(group, onMessage, openedMsg);
}
//////////////////////////////////////////////////////////

var account = ''

export default {
	account,
	formValids,
	formValidsNoMsg,
	fuzzyValids,
	compareDate,
	noGenerator,
	openWSocket
}
