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

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

	this.createRecord = function () {
		utils.popup(".createPopup?domainId=" + this.domain.id, {
			height: "36em",
			callback: function () {
				utils.success("保存成功", function () {
					utils.reload()
				})
			}
		})
	}

	this.updateRecord = function (recordId) {
		utils.popup(".updatePopup?recordId=" + recordId, {
			height: "36em",
			callback: function () {
				utils.success("保存成功", function () {
					utils.reload()
				})
			}
		})
	}

	this.deleteRecord = function (recordId) {
		let that = this
		utils.confirm("确定要删除此记录吗？", function () {
			that.$post(".delete")
				.params({
					recordId: recordId
				})
				.success(function () {
					utils.reload()
				})
		})
	}

	this.updateRecordHealthCheck = function (recordId) {
		utils.popup(".healthCheckPopup?recordId=" + recordId, {
			callback: function () {
				utils.success("设置成功", function () {
					utils.reload()
				})
			}
		})
	}

	this.upRecord = function (record) {
		record.isUp = true

		this.$post(".updateUp")
			.params({
				recordId: record.id
			})
			.success(function () {
				utils.success("操作成功", function () {
					utils.reload()
				})
			})
	}

	this.searchWithName = function (keyword) {
		this.keyword = "name:\"" + keyword + "\""
		this.$delay(function () {
			this.$refs.recordsSearchForm.submit()
		})
	}

	this.searchWithValue = function (keyword) {
		this.keyword = "value:\"" + keyword + "\""
		this.$delay(function () {
			this.$refs.recordsSearchForm.submit()
		})
	}

	this.showStat = function (recordId) {
		utils.popup(".statPopup?recordId=" + recordId, {

		})
	}

	this.formatTTL = function (ttl) {
		if (ttl % 86400 == 0) {
			let days = ttl / 86400
			return days + "天"
		}
		if (ttl % 3600 == 0) {
			let hours = ttl / 3600
			return hours + "小时"
		}
		if (ttl % 60 == 0) {
			let minutes = ttl / 60
			return minutes + "分钟"
		}
		return ttl + "秒"
	}
}