<template>
	<SiteAppPlanSelectorDialog
		v-model="showDialog"
		:app="app"
		:currentPlan="currentPlan"
		@plan-select="onPlanSelect"
	/>
</template>

<script>
import { getToastErrorMessage } from '../../utils/toast';
import SiteAppPlanSelectorDialog from './SiteAppPlanSelectorDialog.vue';
import { toast } from 'vue-sonner';

export default {
	components: {
		SiteAppPlanSelectorDialog
	},
	props: ['app', 'currentPlan'],
	emits: ['plan-changed', 'plan-selected'],
	data() {
		return {
			showDialog: true
		};
	},
	resources: {
		changeAppPlan: {
			url: 'jcloud.api.marketplace.change_app_plan'
		}
	},
	methods: {
		onPlanSelect(plan) {
			// if current plan is specified it's for changing plan
			// else it's for selecting plan while installing app
			if (this.currentPlan)
				toast.promise(
					this.$resources.changeAppPlan.submit({
						subscription: this.app.subscription.name,
						new_plan: plan.name
					}),
					{
						loading: '正在更改计划...',
						success: () => {
							this.$emit('plan-changed', plan);
							return '计划更改成功';
						},
						error: e => getToastErrorMessage(e)
					}
				);
			else this.$emit('plan-selected', plan);
		}
	}
};
</script>