<template>
	<PlansCards v-model="currentPlan" :plans="plans" />
</template>

<script>
import PlansCards from './PlansCards.vue';
import { getPlans } from '../data/plans';

export default {
	name: 'SitePlansCards',
	props: [
		'modelValue',
		'isPrivateBenchSite',
		'isDedicatedServerSite',
		'selectedCluster',
		'selectedApps',
		'selectedVersion',
		'hideRestrictedPlans',
	],
	emits: ['update:modelValue'],
	components: {
		PlansCards,
	},
	computed: {
		currentPlan: {
			get() {
				return this.modelValue;
			},
			set(value) {
				this.$emit('update:modelValue', value);
			},
		},
		plans() {
			let plans = getPlans();

			if (this.isPrivateBenchSite) {
				plans = plans.filter((plan) => plan.private_benches);
			}
			if (this.isPrivateBenchSite && this.isDedicatedServerSite) {
				plans = plans.filter((plan) => plan.dedicated_server_plan);
			} else {
				plans = plans.filter((plan) => !plan.dedicated_server_plan);
			}
			if (this.selectedCluster) {
				plans = plans.map((plan) => {
					return {
						...plan,
						disabled:
							plan.disabled ||
							(plan.clusters.length == 0
								? false
								: !plan.clusters.includes(this.selectedCluster)),
					};
				});
			}
			if (this.selectedApps) {
				plans = plans.map((plan) => {
					return {
						...plan,
						disabled:
							plan.disabled ||
							(plan.allowed_apps.length == 0
								? false
								: !this.selectedApps.every((app) =>
										plan.allowed_apps.includes(app.app),
									)),
					};
				});
			}
			if (this.selectedVersion) {
				plans = plans.map((plan) => {
					return {
						...plan,
						disabled:
							plan.disabled ||
							(plan.bench_versions.length == 0
								? false
								: !plan.bench_versions.includes(this.selectedVersion)),
					};
				});
			}
			if (this.hideRestrictedPlans) {
				plans = plans.filter((plan) => !plan.restricted_plan);
			}

			return plans.map((plan) => {
				return {
					...plan,
					features: [
						{
							label: '数据库',
							condition: !plan.name.includes('Unlimited'),
							value: this.$format.bytes(plan.max_database_usage, 0, 2),
						},
						{
							label: '硬盘',
							condition: !plan.name.includes('Unlimited'),
							value: this.$format.bytes(plan.max_storage_usage, 1, 2),
						},
						{
							value: plan.name.includes('Unlimited - Low')
								? '在此分配较少的资源（为其他工作台分配更多资源）'
								: '',
						},
						{
							value: plan.database_access ? '数据库访问' : '',
						},
						{
							value: plan.offsite_backups ? '异地备份' : '',
						},
						{
							value: plan.monitor_access ? '高级监控' : '',
						},
						{
							value: plan.support_included ? '售后支持' : '',
						},
					].filter((feature) => feature.condition ?? true),
				};
			});
		},
	},
};
</script>