<script setup>
import { ref } from "vue";
import { onShow } from "@dcloudio/uni-app";
import Avatar from "@/components/Avatar.vue";
import SButton from "@/components/SButton.vue";
import { getMyDevicesAPI, loginAPI, getHomeData } from "@/apis";
import useStore from "@/store";
const store = useStore();
const { updateUser, updateDevice } = store;
const props = defineProps({
  noBg: {
    type: Boolean,
    default: false,
  },
  onClose: {
    type: Function,
    default: () => {},
  },
});
const agree = ref(false);
const avatarUrl = ref("");
const nickName = ref("");
const loading = ref(false);
const handleAgree = () => {
  agree.value = !agree.value;
};

function onChooseAvatar(e) {
  avatarUrl.value = e.detail.avatarUrl;
}

function onNicknameChange(e) {
  nickName.value = e.detail.value;
}

const handleLogin = () => {
  if (loading.value) return;
  if (!avatarUrl.value) {
    return uni.showToast({
      title: "请选择头像",
      icon: "none",
    });
  }
  if (!nickName.value) {
    return uni.showToast({
      title: "请输入昵称",
      icon: "none",
    });
  }
  if (!agree.value) {
    return uni.showToast({
      title: "请先同意协议",
      icon: "none",
    });
  }
  loading.value = true;
  uni.login({
    provider: "weixin",
    success: async (loginRes) => {
      const { code } = loginRes;
      const fileManager = uni.getFileSystemManager();
      const avatarBase64 = fileManager.readFileSync(avatarUrl.value, "base64");
      const base64Url = `data:image/png;base64,${avatarBase64}`;
      const result = await loginAPI(nickName.value, base64Url, code);
      const data = await getHomeData();
      if (data.user) updateUser(data.user);
      const devices = await getMyDevicesAPI();
      if (devices.bindings && devices.bindings.length) {
        updateDevice(
          devices.bindings[0].deviceId,
          devices.bindings[0].deviceName
        );
      }
      props.onClose();
    },
    fail: (err) => {
      loading.value = false;
      uni.showToast({
        title: "登录失败",
        icon: "none",
      });
      console.error("登录失败：", err);
    },
  });
};

const openServiceLink = () => {
  uni.navigateTo({
    url:
      "/pages/webview?url=" +
      encodeURIComponent(
        "https://static.shelingxingqiu.com/shootServiceAgreement.html"
      ),
  });
};

const openPrivacyLink = () => {
  uni.navigateTo({
    url:
      "/pages/webview?url=" +
      encodeURIComponent(
        "https://static.shelingxingqiu.com/shootPrivacyPolicy.html"
      ),
  });
};

onShow(() => {
  loading.value = false;
});
</script>

<template>
  <view class="container" :style="{ background: noBg ? '#fff' : 'none' }">
    <view class="avatar" :style="{ borderColor: noBg ? '#E3E3E3' : '#fff3' }">
      <text :style="{ color: noBg ? '#666' : '#fff' }">头像:</text>
      <button
        open-type="chooseAvatar"
        @chooseavatar="onChooseAvatar"
        class="login-btn"
        hover-class="none"
      >
        <Avatar v-if="avatarUrl" :src="avatarUrl" :size="30" />
        <text v-else :style="{ color: noBg ? '#666' : '#fff9' }">点击获取</text>
        <image src="../static/enter.png" mode="widthFix" />
      </button>
    </view>
    <view class="nickname" :style="{ borderColor: noBg ? '#E3E3E3' : '#fff3' }">
      <text :style="{ color: noBg ? '#666' : '#fff' }">昵称:</text>
      <input
        type="nickname"
        placeholder="请输入昵称"
        :placeholder-style="`color: ${noBg ? '#666' : '#fff9'} `"
        @change="onNicknameChange"
        @blur="onNicknameBlur"
        :style="{ color: noBg ? '#333' : '#fff' }"
      />
    </view>
    <SButton :rounded="20" width="80vw" :onClick="handleLogin">
      <block v-if="!loading">
        <image
          src="../static/wechat-icon.png"
          mode="widthFix"
          class="wechat-icon"
        />
        <text :style="{ color: '#000' }">登录/注册</text>
      </block>
      <block v-else>
        <image
          src="../static/btn-loading.png"
          mode="widthFix"
          class="loading"
        />
      </block>
    </SButton>
    <view class="protocol" @click="handleAgree">
      <view v-if="!agree" :style="{ borderColor: noBg ? '#E3E3E3' : '#fff' }" />
      <image v-if="agree" src="../static/checked.png" mode="widthFix" />
      <view>
        <text>已同意并阅读</text>
        <view
          @click.stop="openServiceLink"
          :style="{ color: noBg ? '#333' : '#fff' }"
          >《用户协议》</view
        >
        <text>及</text>
        <view
          @click.stop="openPrivacyLink"
          :style="{ color: noBg ? '#333' : '#fff' }"
          >《隐私协议》</view
        >
        <text>内容</text>
      </view>
    </view>
  </view>
</template>

<style scoped>
.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-top-left-radius: 24rpx;
  border-top-right-radius: 24rpx;
}
.avatar,
.nickname {
  width: 80%;
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  border-bottom: 1rpx solid #fff3;
}
.avatar {
  margin: 0;
}
.avatar > text,
.nickname > text {
  width: 20%;
  font-size: 14px;
  line-height: 55px;
}
.avatar > button > text {
  color: #fff9;
  font-size: 14px;
}
.nickname > input {
  flex: 1;
  font-size: 14px;
  line-height: 55px;
}
.wechat-icon {
  width: 24px;
  height: 24px;
  margin-right: 20px;
}
.protocol {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 13px;
  margin-top: 15px;
  color: #8a8a8a;
}
.protocol > image {
  width: 16px;
  height: 16px;
  margin-right: 10px;
}
.protocol > view:first-child {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  margin-right: 10px;
  border: 1rpx solid #fff;
}
.protocol > view:last-child {
  display: flex;
  align-items: center;
}
.login-btn {
  line-height: 55px;
  width: 80%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.login-btn > image {
  width: 28rpx;
  height: 28rpx;
}
.loading {
  width: 25px;
  height: 25px;
  background-blend-mode: darken;
  animation: rotate 1s linear infinite;
}
</style>
