<template>
  <view class="recognition-result-page">
    <u-navbar title="认证结果" :is-back="false"></u-navbar>

    <view class="result-container">
      <image
        :src="isSuccess ? resultSuccess : resultError"
        class="result-image"
        mode="widthFix"
      ></image>
      <text class="result-text">{{
        isSuccess ? "认证通过" : "认证未通过"
      }}</text>
    </view>

    <view class="btn-container">
      <button class="next-btn" @click="handleNext">
        {{ isSuccess ? "进入系统" : "重新识别" }}
      </button>
    </view>
  </view>
</template>

<script>
import resultSuccess from "@/static/common/img/result-success.png";
import resultError from "@/static/common/img/result-error.png";

export default {
  data() {
    return {
      resultSuccess,
      resultError,
      isSuccess: true,
      token: null,
      userInfo: {
        realName: "", // 姓名
        // idCard: "", // 身份证号
      },
    };
  },
  onLoad(options) {
    // 获取token参数
    if (options && options.token) {
      this.token = options.token;
      console.log("获取到token:", this.token);
      // 使用token查询认证结果
      this.queryVerificationResult();
    }
  },
  methods: {
    // 查询认证结果
    queryVerificationResult() {
      if (!this.token) {
        console.log("token为空，无法查询认证结果");
        return;
      }

      // 调用API查询认证结果
      this.$u.api.getVerifyInfo(this.token).then((res) => {
        const result = res.result.idcard_confirm;
        this.userInfo.realName = result.name;
        // this.userInfo.idCard = result.idcard_number;
        // console.log("this.userInfo", this.userInfo);

        if (result.idcard_number) {
          this.getSchoolInfo(result.idcard_number);
          this.updateCardFn(result.idcard_number);
        } else {
          this.$u.vuex("vuex_userInfo", this.userInfo);
        }
      });
      // .catch((err) => {
      //   console.log("API调用过程中出错:", err);
      // });
    },

    // 根据身份证号获取学校信息
    getSchoolInfo(card) {
      this.$u.api
        .getInfoByCard({
          card,
          // card: "362429199209242513",
        })
        .then((res) => {
          // console.log("返回结果:", res);
          if (res.length > 0) {
            this.userInfo = {
              ...this.userInfo,
              ...res[0],
            };
          }

          this.$u.vuex("vuex_userInfo", this.userInfo);
        });
    },
    // 更新idCard
    updateCardFn(card) {
      this.$u.api
        .updateCard({
          userId: this.vuex_user.id,
          idCode: card,
        })
        .then((res) => {
          console.log("返回结果:", res);
        });
    },

    handleNext() {
      uni.navigateTo({
        url: "/pages/login/roleSelection",
      });
    },
  },
};
</script>

<style lang="scss">
.recognition-result-page {
  padding: 0 24rpx;

  .result-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 130rpx 0 36rpx;
    text-align: center;

    .result-image {
      width: 160rpx;
      height: 160rpx;
      margin-bottom: 36rpx;
    }

    .result-text {
      font-family: PingFang SC, PingFang SC;
      font-size: 32rpx;
      color: #000;
    }
  }

  .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>
