<template>
	<Dialog
		:options="{
			title: '将站点移动到另一个站点分组',
			actions: [
				{
					label: '更改站点分组',
					loading: $resources.changeGroup.loading,
					disabled: !$resources.changeGroupOptions?.data?.length,
					variant: 'solid',
					onClick: () =>
						$resources.changeGroup.submit({
							skip_failing_patches: skipFailingPatches,
							group: targetGroup.value,
							name: site
						})
				},
				{
					label: '克隆当前站点分组',
					onClick: () => {
						$emit('update:modelValue', false);
						showCloneBenchDialog = true;
					}
				}
			]
		}"
		v-model="show"
	>
		<template #body-content>
			<LoadingIndicator
				class="mx-auto h-4 w-4"
				v-if="$resources.changeGroupOptions.loading"
			/>
			<div
				v-else-if="$resources.changeGroupOptions.data.length > 0"
				class="space-y-4"
			>
				<FormControl
					variant="outline"
					label="选择站点分组"
					type="autocomplete"
					:options="
						$resources.changeGroupOptions.data.map(group => ({
							label: group.title || group.name,
							description: group.name,
							value: group.name
						}))
					"
					v-model="targetGroup"
				/>
				<FormControl
					label="跳过失败的补丁（如果有）"
					type="checkbox"
					v-model="skipFailingPatches"
				/>
			</div>
			<p v-else-if="!errorMessage" class="text-md text-base text-gray-800">
				您没有其他可移动此站点的站点分组。
				您可以克隆此站点分组以移动站点。
			</p>
			<ErrorMessage class="mt-3" :message="errorMessage" />
		</template>
	</Dialog>
	<Dialog
		:options="{
			title: '克隆站点分组',
			actions: [
				{
					label: '克隆站点分组',
					variant: 'solid',
					loading: $resources.cloneGroup.loading,
					onClick: () =>
						$resources.cloneGroup.submit({
							name: site,
							new_group_title: newGroupTitle,
							server: selectedServer
						})
				}
			]
		}"
		v-model="showCloneBenchDialog"
	>
		<template #body-content>
			<FormControl label="新站点分组名称" v-model="newGroupTitle" />
			<FormControl
				v-if="$resources.serverOptions.data.length > 0"
				class="mt-4"
				label="选择服务器"
				type="select"
				:options="$resources.serverOptions.data"
				v-model="selectedServer"
			/>
			<ErrorMessage :message="$resources.cloneGroup.error" />
		</template>
	</Dialog>
</template>

<script>
import { toast } from 'vue-sonner';

export default {
	props: ['site'],
	data() {
		return {
			show: true,
			targetGroup: {
				label: '',
				value: ''
			},
			newGroupTitle: '',
			skipFailingPatches: false,
			showCloneBenchDialog: false,
			selectedServer: null
		};
	},
	computed: {
		errorMessage() {
			return (
				this.$resources.changeGroupOptions.error ||
				this.$resources.changeGroup.error ||
				this.$resources.cloneGroup.error
			);
		}
	},
	resources: {
		changeGroup() {
			return {
				url: 'jcloud.api.site.change_group',
				onSuccess() {
					const destinationGroupTitle =
						this.$resources.changeGroupOptions.data.find(
							group => group.name === this.targetGroup.value
						).title;

					toast.success(
						`站点已成功安排移动到 ${destinationGroupTitle} 站点分组。`
					);

					this.$router.push({
						name: 'Site Jobs',
						params: {
							name: this.site
						}
					});

					this.targetGroup = {
						label: '',
						value: ''
					};
					this.show = false;
				}
			};
		},
		changeGroupOptions() {
			return {
				url: 'jcloud.api.site.change_group_options',
				params: {
					name: this.site
				},
				initialData: [],
				onSuccess(data) {
					if (data.length > 0) this.targetGroup.value = data[0].name;
				},
				auto: true
			};
		},
		cloneGroup() {
			return {
				url: 'jcloud.api.site.clone_group',
				onSuccess(data) {
					toast.success(
'当前站点分组已成功克隆。正在重定向到新的站点分组...'
					);
					this.showCloneBenchDialog = false;

					this.$router.push({
						name: 'Deploy Candidate',
						params: {
							name: data.bench_name,
							id: data.candidate_name
						}
					});
				}
			};
		},
		serverOptions() {
			return {
				type: 'list',
				pagetype: 'Server',
				fields: ['name', 'title'],
				auto: true,
				transform(data) {
					return data.map(server => ({
						label: server.title,
						value: server.name
					}));
				}
			};
		}
	}
};
</script>