<template>
  <view class="authentication-page">
    <u-navbar title="身份认证"> </u-navbar>

    <view class="title">请填写您的真实信息</view>

    <view class="form-container">
      <view class="input-item">
        <text>姓名</text>
        <input
          v-model="name"
          maxlength="20"
          type="text"
          placeholder="请输入"
          placeholder-class="placeholder"
        />
      </view>

      <view class="input-item">
        <text>身份证号</text>
        <input
          v-model="idCard"
          maxlength="18"
          type="text"
          placeholder="请输入"
          placeholder-class="placeholder"
        />
      </view>
    </view>

    <view class="btn-container">
      <button class="next-btn" @click="handleNext">下一步</button>
    </view>

    <u-toast ref="uToast" />
  </view>
</template>

<script>
import test from "@/uview-ui/libs/function/test.js";

export default {
  data() {
    return {
      name: "",
      idCard: "",
    };
  },
  methods: {
    handleNext() {
      // 调用后端API获取token
      this.getTokenFromBackend();

      if (!this.name) {
        return this.$refs.uToast.show({
          title: "请输入姓名",
          type: "warning",
        });
      }
      if (!this.idCard) {
        return this.$refs.uToast.show({
          title: "请输入身份证号",
          type: "warning",
        });
      }
      // 验证身份证号码格式
      if (!test.idCard(this.idCard)) {
        return this.$refs.uToast.show({
          title: "请输入正确的身份证号码",
          type: "warning",
        });
      }
      // 处理下一步逻辑
      // console.log("提交信息", this.name, this.idCard);
    },

    // 获取token
    getTokenFromBackend() {
      this.$u.api.getAPIToken().then((res) => {
        const token = res.verifyToken

        // 跳转到百度人脸核验页面
        this.redirectToBaiduVerification(token);
      });
    },

    redirectToBaiduVerification(token) {
      // 获取当前域名
      const currentDomain = window.location.origin;

      // 编码回调URL
      // const callbackUrl = encodeURIComponent(`${currentDomain}/success.html`);
      const callbackUrl = "/pages/login/recognitionResult/recognitionResult";

      // 构建跳转URL
      const verifyUrl = `https://brain.baidu.com/face/print/verify/verify?token=${token}&successUrl=${callbackUrl}&failedUrl=${callbackUrl}`;

      const verifyObj = {
        url: verifyUrl,
        name:'身份认证'
      };

      // 在uni-app中跳转
      this.$u.route({
        url: `/pages/webview/index?data=${encodeURIComponent(
          JSON.stringify(verifyObj)
        )}`,
      });
    },
  },
};
</script>

<style lang="scss">
.authentication-page {
  padding: 0 30rpx;

  .title {
    font-family: PingFang SC, PingFang SC;
    font-weight: bold;
    font-size: 32rpx;
    color: rgba(0, 0, 0, 0.9);
    line-height: 52rpx;
    text-align: left;
    margin-top: 30rpx;
    margin-bottom: 40rpx;
  }

  .form-container {
    background-color: #ffffff;
    border-radius: 16rpx;
    padding: 0 30rpx;
    margin-bottom: 60rpx;

    .input-item {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      padding: 30rpx 0;
      border-bottom: 1px solid rgba(0, 0, 0, 0.05);

      &:last-child {
        border-bottom: none;
      }

      text {
        font-size: 28rpx;
        color: rgba(0, 0, 0, 0.9);
      }

      input {
        flex: 1;
        text-align: right;
        font-size: 28rpx;
      }

      .placeholder {
        color: rgba(0, 0, 0, 0.3);
      }
    }
  }

  .btn-container {
    .next-btn {
      background-color: #45b5ff;
      color: #ffffff;
      height: 90rpx;
      line-height: 90rpx;
      border-radius: 45rpx;
      font-size: 32rpx;
      font-weight: 500;
      border: none;
    }
  }
}
</style>
