<template></template>

<script lang="ts" setup>
import { ref, vueVersion } from "../../TUIKit/adapter-vue";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { TUITranslateService } from "@tencentcloud/chat-uikit-engine";
import Link from "../../utils/link";
import { genTestUserSig } from "../../TUIKit/debug";
import { isPC, isH5, isApp } from "../../TUIKit/utils/env";
import Icon from "../../TUIKit/components/common/Icon.vue";
import logo from "../../static/logo-back.svg";
import { loginChat } from "../../loginChat";
const privateAgree = ref(false);
const inputValue = ref("");

const userSigs = ref();
onLoad((options) => {
  let personId = getQueryString("personId");
  let type = getQueryString("type");
  let openid = getQueryString("openid");
  // 获取跳转的token和userId
  let token = getQueryString("token");
  let id = getQueryString("id");
  uni.setStorageSync("personId", personId);
  uni.setStorageSync("type", type);
  uni.setStorageSync("token", token);
  uni.setStorageSync("userId", id);
  uni.setStorageSync("openid", openid);
  let userSig = getQueryString("userSig");
  uni.setStorageSync("userSig", userSig);
  userSigs.value = userSig;
  let params = {
    token: token,
    id: id,
  };
  init(params);
  console.log("userSigsuserSigsuserSigs", userSigs.value);
});

function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
}

function init(params) {
  inputValue.value = params.id;
  handleLoginInfo();
}
const onAgreePrivateProtocol = () => {
  privateAgree.value = !privateAgree.value;
};

const handleLoginInfo = () => {
  // const options = genTestUserSig({
  //   SDKAppID: uni.$chat_SDKAppID,
  //   secretKey: uni.$chat_secretKey || userSigs.value,
  //   userID: inputValue.value,
  // });
  const loginInfo = {
    SDKAppID: uni.$chat_SDKAppID,
    userID: inputValue.value,
    userSig: userSigs.value,
    useUploadPlugin: true,
    framework: `vue${vueVersion}`,
    TIMPush: uni.$TIMPush, // register TencentCloud-TIMPush
    pushConfig: {
      androidConfig: uni.$TIMPushConfigs, // Android timpush-configs.json
      iOSConfig: {
        iOSBusinessID: "", // iOS Certificate ID
      },
    },
  };
  login(loginInfo);
};

const login = (loginInfo: any) => {
  loginChat(loginInfo).catch(() => {
    uni.showToast({
      title: TUITranslateService.t("Login.登录失败"),
      icon: "none",
    });
  });
};

const openFullPlatformLink = (link: string) => {
  if (isPC || isH5) {
    window.open(link);
  } else if (isApp) {
    plus?.runtime?.openURL(link);
  }
};
</script>

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

.icon {
  display: inline;
}

.btn {
  background: none;
  border: none;
}

.icon-unselected {
  display: inline-block;
  width: 12px;
  height: 12px;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 8px;
}

.selected-icon {
  width: 14px;
  height: 14px;
}

.icon-default {
  margin: 7px 6px 0 0;
}

.login-input-uniapp {
  border: 1px solid #ccc;
  border-radius: 4px;
  outline: none;
  height: 40px;
  padding: 0 0 0 14px;
}

.logo-back-png {
  width: 4.61rem;
  height: 3.23rem;
}

.private-content-link {
  display: inline-block;
}
</style>
