<template>
	<view>
		<doctor-login ref="doctorLogin" @need-select="openAccountPicker" />
		<account-switch-panel
			ref="accountSwitchPanel"
			mode="login"
			@confirm="onAccountPicked"
			@cancel="pendingAccountSelect = false"
		/>
	</view>
</template>

<script>
import DoctorLogin from '@/components/doctor-login/doctor-login.vue';
import AccountSwitchPanel from '@/components/account-switch/account-switch-panel.vue';
import { redirectIfLoggedIn } from '@/utils/loginSession.js';

export default {
	components: { DoctorLogin, AccountSwitchPanel },
	data() {
		return {
			pendingAccountSelect: false,
		};
	},
	onShow() {
		if (this.pendingAccountSelect) return;
		this.$refs.doctorLogin?.loadWxConfig?.();
		redirectIfLoggedIn();
	},
	methods: {
		openAccountPicker({ accounts, defaultAccountId, defaultAccountType }) {
			this.pendingAccountSelect = true;
			this.$refs.accountSwitchPanel.open(accounts, {
				defaultAccountId,
				defaultAccountType,
			});
		},
		onAccountPicked(account) {
			this.pendingAccountSelect = false;
			this.$refs.doctorLogin.onAccountPicked(account);
		},
	},
};
</script>

<style lang="scss" scoped>
</style>
