<template>
  <view class="login-wrapper" :style="{ backgroundImage: `url(${loginBg})` }">
    <!-- 头部内容 -->
    <view class="login-header">
      <!-- 卡通形象 -->
      <image class="login-avatar" src="./images/dh.png" mode="widthFix"></image>
      <image
        class="login-title"
        src="./images/dhwq.png"
        mode="widthFix"
      ></image>

      <!-- 描述 -->
      <view class="login-desc">您好，欢迎来到朵花温泉！</view>
    </view>

    <!-- 按钮区域 -->
    <view class="login-btn-area">
      <!-- 同意隐私协议并获取手机号按钮 -->

      <button
        v-if="!isAgree"
        class="login-btn"
        type="primary"
        @click="handleAgreeAndGetPhone"
      >
        微信一键登录
      </button>
      <button
        v-if="isAgree"
        class="login-btn"
        open-type="getPhoneNumber"
        type="primary"
        @getphonenumber="onLogin"
      >
        微信一键登录
      </button>
    </view>

    <!-- 协议勾选 -->
    <view class="login-agreement">
      <CheckBox v-model="isAgree">
        <text class="login-agreement-text">阅读并同意</text>
        <text
          class="login-agreement-link"
          @click.stop="handleAgreeClick('service')"
          >《服务协议》</text
        >
        <text class="login-agreement-text">和</text>
        <text
          class="login-agreement-link"
          @click.stop="handleAgreeClick('privacy')"
          >《隐私协议》</text
        >
      </CheckBox>
    </view>

    <AgreePopup
      ref="agreePopup"
      :visible="visible"
      :agreement="computedAgreement"
      @close="visible = false"
    />
  </view>
</template>

<script setup>
import { ref, computed } from "vue";
import { loginAuth, bindPhone, checkPhone } from "@/manager/LoginManager";
import {
  getServiceAgreement,
  getPrivacyAgreement,
} from "@/request/api/LoginApi";
import { goHome } from "@/hooks/useGoHome";
import loginBg from "./images/bg.png";
import CheckBox from "@/components/CheckBox/index.vue";
import AgreePopup from "./components/AgreePopup/index.vue";

const isAgree = ref(false);
const visible = ref(false);
const serviceAgreement = ref("");
const privacyAgreement = ref("");
// 协议类型
const AgreeType = ref("service");

// 同意隐私协议并获取手机号
const handleAgreeAndGetPhone = () => {
  if (!isAgree.value) {
    uni.showToast({
      title: "请先同意服务协议和隐私协议",
      icon: "none",
    });
    return;
  }
};

const onLogin = (e) => {
  const { code } = e.detail;
  console.error("onLogin code", code);

  loginAuth()
    .then(async () => {
      // 检测是否绑定手机号，已绑定则直接跳转首页，未绑定则获取手机号并绑定
      const res = await checkPhone();

      if (res.data) {
        return goHome();
      }

      const { data } = await bindPhone({
        wechatPhoneCode: code,
        clientId: "2",
      });

      if (data) {
        goHome();
      }
    })
    .catch((err) => {
      console.error("登录失败", err);
    });
};

// 处理同意协议点击事件
const handleAgreeClick = (type) => {
  visible.value = true;
  AgreeType.value = type;
};

// 计算协议类型
const computedAgreement = computed(() => {
  if (AgreeType.value === "service") {
    return serviceAgreement.value;
  } else {
    return privacyAgreement.value;
  }
});

// 获取服务协议数据
const getServiceAgreementData = async () => {
  const { data } = await getServiceAgreement();
  serviceAgreement.value = data;
};

getServiceAgreementData();

// 获取隐私协议数据
const getPrivacyAgreementData = async () => {
  const { data } = await getPrivacyAgreement();
  privacyAgreement.value = data;
};

getPrivacyAgreementData();
</script>

<style lang="scss" scoped>
@import "./styles/index.scss";
</style>