<template>
	<div class="flex items-center justify-between gap-1">
		<div>
			<h3 class="text-base font-medium">{{ props.actionLabel }}</h3>
			<p class="mt-1 text-p-base text-gray-600">{{ props.description }}</p>
		</div>
		<Button
			v-if="releaseGroup?.pg"
			class="whitespace-nowrap"
			@click="getBenchActionHandler(props.actionLabel)"
		>
			<p
				:class="
					group === 'Dangerous Actions' ? 'text-red-600' : 'text-gray-800'
				"
			>
				{{ props.buttonLabel }}
			</p>
		</Button>
	</div>
</template>

<script setup>
import { getCachedDocumentResource } from 'jingrow-ui';
import { toast } from 'vue-sonner';
import { confirmDialog } from '../../utils/components';
import router from '../../router';

const props = defineProps({
	benchName: { type: String, required: true },
	actionLabel: { type: String, required: true },
	method: { type: String, required: true },
	description: { type: String, required: true },
	buttonLabel: { type: String, required: true },
	group: { type: String, required: false }
});

const releaseGroup = getCachedDocumentResource(
	'Release Group',
	props.benchName
);

function getBenchActionHandler(action) {
	const actionHandlers = {
		'Rename Bench Group': onRenameBench,
		'Transfer Bench Group': onTransferBench,
		'Drop Bench Group': onDropBench
	};
	if (actionHandlers[action]) {
		actionHandlers[action].call(this);
	}
}

function onRenameBench() {
	confirmDialog({
		title: '重命名站点分组',
		fields: [
			{
				label: '输入新的站点分组名称',
				fieldname: 'newBenchName'
			}
		],
		onSuccess({ hide, values }) {
			if (values.newBenchName) {
				toast.promise(
					releaseGroup.setValue.submit(
						{
							title: values.newBenchName
						},
						{
							onSuccess() {
								hide();
							}
						}
					),
					{
						loading: '正在重命名站点分组...',
						success: '站点分组重命名成功',
						error: '重命名站点分组失败'
					}
				);
			} else {
				toast.error('请输入有效的站点分组名称');
			}
		}
	});
}

function onTransferBench() {
	confirmDialog({
		title: '转移站点分组所有权',
		fields: [
			{
				label: '输入接收站点分组所有权的团队邮箱地址',
				fieldname: 'email'
			},
			{
				label: '转移原因',
				fieldname: 'reason',
				type: 'textarea'
			}
		],
		primaryAction: {
			label: '转移',
			variant: 'solid',
			onClick: ({ hide, values }) => {
				if (!values.email) {
					throw new Error('请输入有效的邮箱地址');
				}

				return releaseGroup.sendTransferRequest
					.submit({ team_mail_id: values.email, reason: values.reason || '' })
					.then(() => {
						hide();
						toast.success(
							`转移请求已成功发送至 ${values.email}。`
						);
					});
			}
		}
	});
}

function onDropBench() {
	confirmDialog({
		title: '删除站点分组',
		message: `确定要删除站点分组 <b>${
			releaseGroup.pg.title || releaseGroup.name
		}</b> 吗？`,
		fields: [
			{
				label: '请输入站点分组名称以确认',
				fieldname: 'confirmBenchName'
			}
		],
		primaryAction: {
			label: '删除',
			theme: 'red',
			onClick: ({ hide, values }) => {
				if (releaseGroup.delete.loading) return;
				if (values.confirmBenchName !== releaseGroup.pg.title) {
					throw new Error('站点分组名称不匹配');
				}
				toast.promise(
					releaseGroup.delete.submit(null, {
						onSuccess: () => {
							hide();
							router.push({ name: 'Release Group List' });
						}
					}),
					{
						loading: '正在删除站点分组...',
						success: '站点分组删除成功',
						error: error =>
							error.messages.length
								? error.messages.join('\n')
								: '删除站点分组失败'
					}
				);
			}
		}
	});
}
</script>
