<template>
	<div class="mx-auto max-w-2xl rounded-lg border-0 px-2 sm:border sm:p-8">
		<div class="prose prose-sm max-w-none">
			<h1 class="text-2xl font-semibold">欢迎来到 今果 Jingrow</h1>
			<p>
				Jingrow是一站式通用数字化平台，以"一切皆页面"的革新理念，帮助您快速构建从个人工作到团队协作的一站式数字化解决方案。通过AI Agent智能体、可视化工作流编排、零代码可视化数据建模、自动化任务、团队协作、角色与权限管理、文件库、知识库、笔记，会议，待办事项，通知、智能报表等模块，引领数字化协作新趋势。
			</p>
		</div>
		<p class="mt-6 text-base text-gray-800">
			从这里开始，让一切数字化、系统化、智能化、自动化。
		</p>
		<div class="mt-6 space-y-6">
			<!-- 步骤 1 - 账户已创建 -->
			<div class="rounded-md">
				<div>
					<div class="flex items-center justify-between space-x-2">
						<div class="flex items-center space-x-2">
							<TextInsideCircle>1</TextInsideCircle>
							<span class="text-base font-medium"> 账户已创建 </span>
						</div>
						<div
							class="grid h-4 w-4 place-items-center rounded-full bg-green-500/90"
						>
							<i-lucide-check class="h-3 w-3 text-white" />
						</div>
					</div>
				</div>
			</div>
			
			<!-- 步骤 2 - 添加支付方式 -->
			<div class="rounded-md">
				<div v-if="!$team.pg.payment_mode">
					<div class="flex items-center space-x-2">
						<TextInsideCircle>2</TextInsideCircle>
						<span class="text-base font-medium"> 余额充值 </span>
					</div>

					<div class="mt-4 pl-7">
						<div class="mt-2 w-full">
							<div class="mt-10">
								<BuyPrepaidCreditsForm
									:isOnboarding="true"
									@success="onBuyCreditsSuccess"
								/>
							</div>
						</div>
					</div>
				</div>
				<!-- 支付方式已添加 -->
				<div v-else>
					<div class="flex items-center justify-between space-x-2">
						<div class="flex items-center space-x-2">
							<TextInsideCircle>2</TextInsideCircle>
							<span
								class="text-base font-medium"
								v-if="$team.pg.payment_mode === 'Card'"
							>
								自动扣费设置已完成
							</span>
							<span
								class="text-base font-medium"
								v-if="$team.pg.payment_mode === 'Prepaid Credits'"
							>
								余额支付已开通
							</span>
						</div>
						<div
							class="grid h-4 w-4 place-items-center rounded-full bg-green-500/90"
						>
							<i-lucide-check class="h-3 w-3 text-white" />
						</div>
					</div>
					<div
						class="mt-1.5 pl-7 text-p-base text-gray-800"
						v-if="$team.pg.payment_mode === 'Prepaid Credits'"
					>
						账户余额: {{ $format.userCurrency($team.pg.balance) }}
					</div>
				</div>
			</div>
			
			<!-- 步骤 3 - 创建站点 -->
			<div v-if="pendingSiteRequest" class="rounded-md">
				<div class="flex items-center justify-between space-x-2">
					<div class="flex items-center space-x-2">
						<TextInsideCircle>3</TextInsideCircle>
						<span
							class="text-base font-medium"
							v-if="pendingSiteRequest.status == 'Error'"
						>
							创建您的 {{ pendingSiteRequest.title }} 试用站点时出错
						</span>
						<span class="text-base font-medium" v-else>
							创建您的 {{ pendingSiteRequest.title }} 试用站点
						</span>
					</div>
				</div>
				<div class="mt-2 pl-7" v-if="pendingSiteRequest.status == 'Error'">
					<p class="mt-2 text-p-base text-gray-800">
						请点击下方按钮联系 今果 Jingrow支持团队。
					</p>
					<Button class="mt-2" link="/support"> 联系支持 </Button>
				</div>
				<div class="mt-2 pl-7" v-else>
					<p class="mt-2 text-p-base text-gray-800">
						您可以点击下方按钮免费试用 {{ pendingSiteRequest.title }} 应用程序。
					</p>
					<Button
						class="mt-2"
						:route="{
							name: 'SignupSetup',
							params: { productId: pendingSiteRequest.product_trial },
							query: {
								account_request: pendingSiteRequest.account_request,
							},
						}"
					>
						继续
					</Button>
				</div>
			</div>
			<div v-else-if="trialSite" class="rounded-md">
				<div class="flex items-center justify-between space-x-2">
					<div class="flex items-center space-x-2">
						<TextInsideCircle>3</TextInsideCircle>
						<span class="text-base font-medium">
							您的试用站点已准备就绪
						</span>
					</div>
					<div
						class="grid h-4 w-4 place-items-center rounded-full bg-green-500/90"
					>
						<i-lucide-check class="h-3 w-3 text-white" />
					</div>
				</div>
				<div class="pl-7">
					<div class="mt-2">
						<a
							class="flex items-center text-base font-medium underline"
							:href="`https://${trialSite.host_name || trialSite.name}`"
							target="_blank"
						>
							https://{{ trialSite.host_name || trialSite.name }}
							<i-lucide-external-link class="ml-1 h-3.5 w-3.5 text-gray-800" />
						</a>
					</div>
					<p class="mt-2 text-p-base text-gray-800">
						您的试用将于
						<span class="font-medium">
							{{ $format.date(trialSite.trial_end_date, 'LL') }} </span
						> 到期。立即设置账单以确保您的站点访问不受影响。
					</p>
				</div>
			</div>
			<div v-else class="rounded-md">
				<div class="flex items-center space-x-2">
					<TextInsideCircle>3</TextInsideCircle>
					<div class="text-base font-medium">创建您的第一个数字化平台</div>
				</div>

				<Button class="ml-7 mt-4" :route="'/sites/new'">
					创建
				</Button>
			</div>
		</div>
	</div>
</template>
<script>
import { defineAsyncComponent } from 'vue';
import TextInsideCircle from './TextInsideCircle.vue';

export default {
	name: 'Onboarding',
	emits: ['payment-mode-added'],
	components: {
		BuyPrepaidCreditsForm: defineAsyncComponent(
			() => import('./BuyPrepaidCreditsForm.vue'),
		),
		TextInsideCircle,
	},
	methods: {
		onBuyCreditsSuccess() {
			this.$team.reload();
			this.$emit('payment-mode-added');
		},
	},
	computed: {
		pendingSiteRequest() {
			return this.$team.pg.pending_site_request;
		},
		trialSite() {
			return this.$team.pg.trial_sites?.[0];
		},
	},
};
</script>