Tea.context(function () {
	this.sorting = false; // 是否正在排序

	this.columns = [
		{
			title: '',
			key: 'handle',
			dataIndex: 'handle',
			width: '50px',
			scopedSlots: { customRender: 'handleSlot' }
		},
		{
			title: '分组名称',
			dataIndex: 'name',
			key: 'name',
			scopedSlots: { customRender: 'nameSlot' }
		},
		{
			title: '服务数量',
			dataIndex: 'countServers',
			key: 'countServers',
			align: 'center',
			width: '120px',
			scopedSlots: { customRender: 'countSlot' }
		},
		{
			title: '操作',
			key: 'action',
			width: '150px',
			scopedSlots: { customRender: 'actionSlot' }
		}
	];

	this.handleSort = function (groupIds) {
		// 防止重复请求
		if (this.sorting) {
			return;
		}

		setTimeout(() => {
			this.sorting = true;

			this.$post("/servers/groups/sort")
				.params({
					groupIds: groupIds
				})
				.success(function () {
					teaweb.successToast("排序保存成功");
					window.location.reload();
				})
				.fail(() => {
					teaweb.failToast("排序保存失败，请重试");
				})
				.finally(() => {
					this.sorting = false;
				});
		}, 100)
	};

	this.createGroup = function () {
		teaweb.popup("/servers/groups/createPopup", {
			title: '创建分组',
			callback: function () {
				teaweb.success("保存成功", function () {
					teaweb.reload()
				})
			}
		})
	}

	this.deleteGroup = function (groupId) {
		let that = this
		teaweb.confirm("确定要删除这个分组吗？", function () {
			that.$post("/servers/groups/group/delete")
				.params({
					groupId: groupId
				})
				.success(function () {
					teaweb.success("删除成功", function () {
						teaweb.reload()
					})
				})
		})
	}
})