<template>
  <view class="login-prompt" v-if="showPrompt">
    <view class="mask" :class="{ 'mask-show': showAnimation }"></view>
    <view class="prompt-content" :class="{ 'slide-up': showAnimation }">
      <text class="prompt-title">登录以获得更好的体验</text>
      <button class="login-btn" @click="goLogin">登录</button>
      <button class="visitor-btn" @click="continueAsVisitor">
        以访客身份继续
      </button>
      <text class="prompt-title-small" @click="goRegister"
        >如果您还没有账号，点击注册</text
      >
    </view>
  </view>
</template>

<script setup>
import { ref, nextTick, onMounted, watch } from "vue";
import { useUserStore } from "../stores/modules/userInfo";
import { useDeviceStore } from "../stores/modules/deviceInfo";
import { useLoginStore } from "../stores/modules/login";
import { LoginApi } from "../api/start/login";
const deviceId = ref("");

const userStore = useUserStore();
const deviceStore = useDeviceStore();
const loginStore = useLoginStore();
// 初始化
onMounted(() => {
  if (!userStore.userInfo) {
    show();
  }
}),
  // watch(
  //   () => loginStore.loginInfo,
  //   (newVal, oldVal) => {
  //     console.log("登录状态改变");
  //     if (newVal === "false") {
  //       console.log("登录失败");
  //       show();
  //       loginStore.setLoginInfo("true");
  //     }
  //   }
  // );

  loginStore.$subscribe(() => {
    if (loginStore.loginInfo === "false") {
      console.log("登录失败");
      show();
    }
  });

// 定义响应式数据
const showPrompt = ref(false);
const showAnimation = ref(false);

// 显示弹窗
const show = () => {
  showPrompt.value = true;
  // 在下一帧触发动画
  nextTick(() => {
    setTimeout(() => {
      showAnimation.value = true;
    }, 10);
  });
};

// 隐藏弹窗
const hide = () => {
  showAnimation.value = false;
  // 等待动画结束后再隐藏
  setTimeout(() => {
    showPrompt.value = false;
  }, 300);
};

// 跳转到登录页面
const goLogin = () => {
  uni.navigateTo({
    url: "/pages/start/login/login",
  });
  hide();
};
// 跳转到登录页面
const goRegister = () => {
  uni.navigateTo({
    url: "/pages/start/Registration/Registration",
  });
  hide();
};

// 以访客身份继续
const continueAsVisitor = async () => {
  // 设置访客模式
  const res = await LoginApi({
    loginType: "VISITOR", //登录方式EMAIL,PHONE,DCCODE,APPLE,GOOGLE,VISITOR
    account: "", //登陆账号  手机号/邮箱/dccode
    verifyCode: "", //验证码
    password: "", //密码
    useCode: "", //是否使用验证码 true/false
    idToken: "", //第三方登录idToken
    deviceId: deviceStore.deviceInfo.deviceId,
  });

  if (res.code === 200) {
    userStore.setUserInfo(res.data);
    console.log("0loginStore.loginInfo", loginStore.loginInfo);
    hide();
    
    // 发送游客登录成功事件，通知首页重新加载
    uni.$emit('visitorLoginSuccess', {
      userInfo: res.data
    });
  }
};

// 暴露方法给外部使用
defineExpose({
  show,
  hide,
});
</script>

<style scoped>
.login-prompt {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
}

.mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.8);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.mask.mask-show {
  opacity: 1;
}

.prompt-content {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 400rpx;
  border-radius: 20rpx 20rpx 0 0;
  background-color: white;
  padding: 20rpx 70rpx;
  transform: translateY(100%);
  transition: transform 0.3s ease;
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.3);
}

.prompt-content.slide-up {
  transform: translateY(0);
}

.prompt-title {
  display: block;
  text-align: center;
  font-size: 40rpx;
  font-weight: 700;
  color: #000000;
  margin-top: 30rpx;
  margin-bottom: 40rpx;
}

.login-btn {
  width: 100%;
  height: 80rpx;
  background-color: rgb(0, 0, 0);
  color: white;
  font-size: 32rpx;
  line-height: 80rpx;
  border-radius: 40rpx;
  margin-bottom: 20rpx;
}

.visitor-btn {
  width: 100%;
  height: 80rpx;
  background-color: #f5f5f5;
  color: #333;
  font-size: 32rpx;
  line-height: 80rpx;
  border-radius: 40rpx;
}
.prompt-title-small {
  display: block;
  text-align: center;
  font-size: 24rpx;
  color: #6a6a6a;
  margin-top: 30rpx;
  margin-bottom: 40rpx;
  line-height: 15.5px;
  letter-spacing: 0.3px;
  font-style: normal;
  font-family: "PingFang SC";
}
</style>