<template>
	<div
		class="flex h-screen w-screen items-center justify-center"
		v-if="$resources.signupSettings.loading"
	>
		<Spinner class="mr-2 w-4" />
		<p class="text-gray-800">加载中</p>
	</div>
	<div class="flex h-screen overflow-hidden sm:bg-gray-50" v-else>
		<div class="w-full overflow-auto">
			<LoginBox
				title="几分钟内开始使用"
				subtitle="14天免费试用，无需信用卡。"
				:class="{ 'pointer-events-none': $resources.signup?.loading }"
				:logo="saasProduct?.logo"
			>
				<template v-slot:default>
					<form
						class="flex flex-col"
						@submit.prevent="this.$resources.signup.submit()"
					>
						<!-- 字段 -->
						<FormControl
							label="国家"
							type="select"
							placeholder="选择您的国家"
							autocomplete="country"
							variant="outline"
							:options="countries"
							v-model="country"
							required
						/>
						<div class="mt-5 flex flex-row gap-5">
							<FormControl
								label="名字"
								type="text"
								placeholder="John"
								variant="outline"
								v-model="first_name"
								required
							/>
							<FormControl
								label="姓氏"
								type="text"
								placeholder="Doe"
								variant="outline"
								v-model="last_name"
								required
							/>
						</div>
						<FormControl
							class="mt-5"
							label="邮箱"
							type="email"
							placeholder="email@example.com"
							variant="outline"
							autocomplete="email"
							v-model="email"
							required
						/>
						<div class="mt-5 text-base">
							<label class="leading-6 tracking-normal">
								<FormControl
									type="checkbox"
									v-model="terms_accepted"
									class="mr-0.5 py-1 align-baseline"
								/>
								我同意 Jingrow&nbsp;
								<Link href="https://jingrow.com/terms" target="_blank">
									服务条款 </Link
								>,&nbsp;
								<Link href="https://jingrow.com/privacy" target="_blank">
									隐私政策
								</Link>
								&nbsp;和&nbsp;
								<Link
									href="https://jingrow.com/cookie-policy"
									target="_blank"
								>
									Cookie 政策
								</Link>
							</label>
						</div>
						<!-- 错误信息 -->
						<ErrorMessage
							class="mt-2"
							:message="this.$resources?.signup?.error"
						/>
						<!-- 按钮 -->
						<div class="mt-8 flex flex-col items-center gap-3">
							<Button
								:loading="$resources.signup?.loading"
								variant="solid"
								class="w-full font-medium"
								type="submit"
							>
								创建账户
							</Button>
							<p v-if="isGoogleOAuthEnabled">或</p>
							<Button
								v-if="isGoogleOAuthEnabled"
								:loading="$resources.signupWithOAuth?.loading"
								@click="$resources.signupWithOAuth.submit()"
								variant="subtle"
								class="w-full font-medium"
								type="button"
							>
								<div class="flex flex-row items-center gap-1">
									<GoogleIconSolid class="w-4" />
									使用 Google 注册
								</div>
							</Button>
						</div>
					</form>
					<div class="mt-6 text-center">
						<router-link
							class="text-center text-base font-medium text-gray-900 hover:text-gray-700"
							:to="{
								name: 'SaaSLogin',
								params: $route.params,
							}"
						>
							已有账户？登录。
						</router-link>
					</div>
				</template>
			</LoginBox>
		</div>
	</div>
</template>
<script>
import { Spinner } from 'jingrow-ui';
import LoginBox from '../../components/auth/SaaSLoginBox.vue';
import GoogleIconSolid from '@/components/icons/GoogleIconSolid.vue';

export default {
	name: 'SaaSSignup',
	props: ['productId'],
	components: {
		LoginBox,
		Spinner,
		GoogleIconSolid,
	},
	data() {
		return {
			email: '',
			first_name: '',
			last_name: '',
			country: null,
			terms_accepted: false,
		};
	},
	computed: {
		saasProduct() {
return this.$resources.signupSettings.data?.product_trial;
		},
		countries() {
			return this.$resources.signupSettings.data?.countries || [];
		},
		isGoogleOAuthEnabled() {
			return this.$resources.signupSettings.data?.enable_google_oauth || false;
		},
	},
	resources: {
		signup() {
			return {
				url: 'jcloud.api.product_trial.signup',
				params: {
					email: this.email,
					first_name: this.first_name,
					last_name: this.last_name,
					country: this.country,
					product: this.productId,
					referrer: this.getReferrerIfAny(),
					terms_accepted: this.terms_accepted,
				},
				validate() {
					if (!this.terms_accepted) {
						throw new Error('请接受服务条款');
					}
				},
				onSuccess(account_request) {
					this.$router.push({
						name: 'SaaSSignupVerifyEmail',
						query: {
							email: this.email,
							account_request: account_request,
						},
					});
				},
			};
		},
		signupSettings() {
			return {
				url: 'jcloud.api.account.signup_settings',
				params: {
					product: this.productId,
					fetch_countries: true,
					timezone: window.Intl
						? Intl.DateTimeFormat().resolvedOptions().timeZone
						: null,
				},
				auto: true,
				onSuccess(res) {
					if (res && res.country) {
						this.country = res.country;
					}
				},
			};
		},
		signupWithOAuth() {
			return {
				url: 'jcloud.api.google.login',
				params: {
					product: this.productId,
				},
				auto: false,
				onSuccess(url) {
					window.location.href = url;
				},
			};
		},
	},
	methods: {
		getReferrerIfAny() {
			const params = location.search;
			const searchParams = new URLSearchParams(params);
			return searchParams.get('referrer');
		},
	},
};
</script>