<template>
  <!-- 模态框遮罩层，点击空白处关闭模态框 -->
  <div class="modal-overlay" @click.self="close">
    <!-- 模态框容器 -->
    <div class="modal-container">
      <!-- 关闭按钮 -->
      <button class="close-button" @click="close">&times;</button>
      <!-- 模态框内容区域 -->
      <div class="modal-content">
        <!-- 根据 isLogin 状态动态切换登录/注册组件 -->
        <UserLogin v-if="isLogin" @toggleForm="toggleForm" @LoginSuccess="close"/>
        <UserRegister v-else @toggleForm="toggleForm" />
      </div>
    </div>
  </div>
</template>

<script setup lang="js" name="LoginRegisterModal">
import { ref } from 'vue';
// 导入登录和注册组件
import UserLogin from './Login.vue';
import UserRegister from './Register.vue';

const emit = defineEmits(['close']);

const isLogin = ref(true);

// 切换登录/注册表单的方法
const toggleForm = () => {
  isLogin.value = !isLogin.value;
};

// 关闭模态框的方法
const close = () => {
  emit('close');
};
</script>

<style scoped>
/* 模态框遮罩层样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

/* 模态框容器样式 */
.modal-container {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.15);
  position: relative;
  width: 90%;
  max-width: 480px;
  min-height: 500px;
}

/* 模态框内容区域样式 */
.modal-content {
  padding: 25px;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 关闭按钮样式 */
.close-button {
  position: absolute;
  top: 4px;
  right: 17px;
  background: none;
  border: none;
  font-size: 50px;
  color: #666;
  cursor: pointer;
  padding: 4px;
  width: 40px;
  height: 40px;
  z-index: 1;
}

/* 关闭按钮悬停效果 */
.close-button:hover {
  color: #111;
}
</style>