import initCtx from "@/utils/ctx.js";

export default function () {
	const {utils} = initCtx(this)

	this.success = this.$NotifyReloadSuccess("保存成功")

	this.tab = "ipStat"

	this.changeTab = function () {
		if (this.tab == "events") {
			if (this.hasUnreadEvents) {
				this.hasUnreadEvents = false
				this.$post(".updateRead")
					.params({
						serverId: this.serverId
					})
			}
		}
	}

	this.createIPItem = function (listId, ip) {
		const expiresAt = Math.round(new Date().getTime() / 1000) + 86400
		utils.popup("/servers/iplists/createIPPopup?listId=" + listId + "&serverId=" + this.serverId + "&ip=" + ip + "&expiresAt=" + expiresAt + "&reason=疑似CC攻击&sourceCategory=cc", {
			height: utils.POPUP_HEIGHT_LG,
			callback: function () {
				utils.successRefresh("操作成功")
			}
		})
	}

	this.createIPMenu = function (ip) {
		return [
			{
				label: '拉黑IP',
				command: () => {
					this.createIPItem(this.denyListId, ip)
				},
				icon: "pi pi-ban"
			}
		]
	}

	this.createIPItemMenu = function (itemId) {
		return [
			{
				label: '解除拦截',
				command: () => {
					const newItems = []
					this.deniedIPItems.forEach(function (v) {
						if (v.id != itemId) {
							newItems.push(v)
						}
					})

					this.$post(".deleteIPItem")
						.params({itemId: itemId})
						.success(function () {
							utils.success("解除成功", function () {
								this.deniedIPItems = newItems
							})
						})
				},
				icon: "pi pi-ban"
			}
		]
	}
}