<template>
  <view class="page">
    <view class="status-box">
      <view v-t="'card.Info.s11'"></view>
      <image src="/static/images/card/img2.png" mode="aspectFit" />
      <view class="status-box-btn">
        <view v-t="'card.Btn.b7'" class="btn-apply" :class="{ disabled: !isAuthVerified || isLoading }"
          @click="handleApply"></view>
      </view>
      <view v-if="!isAuthVerified && !isLoading" class="auth-tip">
        {{ t('pages.card.authTip') || '请先完成身份认证' }}
      </view>
      <view v-t="'card.Info.s12'" class="a-title"></view>
      <view class="step-box">
        <view class="step-item">
          <view class="ids">1</view>
          <view v-t="'card.Info.s13'" class="k"></view>
          <view v-t="'card.Info.s14'" class="v"></view>
        </view>
        <view class="step-item">
          <view class="ids">2</view>
          <view v-t="'card.Info.s15'" class="k"></view>
          <view v-t="'card.Info.s16'" class="v"></view>
        </view>
        <view class="step-item">
          <view class="ids">3</view>
          <view v-t="'card.Info.s17'" class="k"></view>
          <view v-t="'card.Info.s18'" class="v"></view>
        </view>
        <view class="step-item">
          <view class="ids">4</view>
          <view v-t="'card.Info.s19'" class="k"></view>
          <view v-t="'card.Info.s20'" class="v"></view>
        </view>
      </view>
      <view v-t="'card.Info.s21'" class="f"></view>
    </view>
    <!-- <view class="cwg-button">
            <van-button type="primary" block @click="handleApply">{{ t('cards.p4') }}</van-button>
        </view> -->
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from "vue";
import { useI18n } from "vue-i18n";
import useRouter from "@/hooks/useRouter";
import { userApi } from "@/api/user";
import useUserStore from "@/stores/use-user-store";
import { userToken } from "@/composables/config";

const router = useRouter();
const { t } = useI18n();
const userStore = useUserStore();
const userInfo = computed(() => userStore.userInfo);

// 认证状态
const isAuthVerified = ref(false);
const isLoading = ref(true);

// 检查认证状态
function checkAuthStatus(userData: any) {
  // approveStatus == 2 或 kycStatus == 2 表示认证通过
  if (userData?.approveStatus == 2) {
    isAuthVerified.value = true;
  } else {
    isAuthVerified.value = false;
  }
}

// 获取用户详情
async function getUserSingle() {
  if (!userToken.value) {
    isLoading.value = false;
    isAuthVerified.value = false;
    return;
  }

  try {
    isLoading.value = true;
    const res = await userApi.getUserSingle();
    if (res.code === 200 && res.data) {
      // 更新用户信息到 store
      userStore.saveUserInfo(res.data);
      // 检查认证状态
      checkAuthStatus(res.data);
    } else {
      isAuthVerified.value = false;
    }
  } catch (error: any) {
    console.error('获取用户详情失败:', error);
    isAuthVerified.value = false;
  } finally {
    isLoading.value = false;
  }
}

function handleApply() {
  if (!isAuthVerified.value || isLoading.value) {
    uni.showToast({
      title: t('pages.card.authTip') || '请先完成身份认证',
      icon: 'none'
    });
    return;
  }
  router.push("/pages/card/select");
}

onMounted(async () => {
  // 如果 store 中已有用户信息，先检查认证状态
  if (userInfo.value) {
    checkAuthStatus(userInfo.value);
    isLoading.value = false;
  }
  // 调用接口获取最新用户信息
  await getUserSingle();
});
</script>

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

.page {
  // margin: 0 px2rpx(24) px2rpx(100) px2rpx(24);
}

.imgs {
  margin-left: px2rpx(74);
  margin-bottom: px2rpx(40);
}

.apply-card-steps {
  width: 100%;
  margin-bottom: px2rpx(52);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.steps-top {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  position: relative;
  max-width: px2rpx(600);
  margin: 0 auto;
}

.step {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  z-index: 2;
  flex: 0 0 auto;
}

.step-circle {
  width: px2rpx(28);
  height: px2rpx(28);
  color: var(--main-yellow);
  border-radius: 50%;
  font-size: px2rpx(14);
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--main-yellow);
  margin-bottom: px2rpx(8);
  transition: all 0.3s ease;
}

.step-circle:hover {
  transform: scale(1.1);
  box-shadow: 0 0 10px rgba(73, 247, 166, 0.3);
}

.step-dash {
  flex: 1;
  height: px2rpx(2);
  background: repeating-linear-gradient(to right,
      var(--main-yellow),
      var(--main-yellow) px2rpx(6),
      transparent px2rpx(6),
      transparent px2rpx(12));
  min-width: px2rpx(18);
  position: relative;
  top: -px2rpx(16);
}

.step-label {
  color: var(--white);
  font-size: px2rpx(14);
  font-weight: 500;
  text-align: center;
  line-height: 2;
}

.status-box {
  width: 100%;
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  margin-bottom: px2rpx(20);
  gap: px2rpx(16);

  .status {
    width: 100%;
    color: #333333;
    font-size: px2rpx(26);
    font-family: PingFang SC;
    font-weight: 500;
    word-wrap: break-word;
    text-align: start;
  }

  view {
    width: 100%;
    text-align: start;
    color: #6f6f6f;
    font-size: px2rpx(18);
    font-family: PingFang SC;
    font-weight: 400;
    word-wrap: break-word;
    line-height: px2rpx(32);
    padding: px2rpx(20) px2rpx(30) px2rpx(20) 0;
    margin: 0;
  }

  .a-title {
    color: #333333;
    font-size: px2rpx(18);
    font-family: PingFang SC;
    font-weight: 500;
    word-wrap: break-word;
    margin-top: px2rpx(44);
  }

  .step-box {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    margin-top: px2rpx(20);
    padding: 0 0;

    .step-item {
      width: 100%;
      padding: 0 0 px2rpx(10) px2rpx(60);
      text-align: center;
      position: relative;

      &::after {
        content: "";
        position: absolute;
        top: 0;
        left: px2rpx(15);
        width: 0;
        height: 100%;
        border-left: 1px dashed #ccc;
        z-index: 1;
      }

      &:last-child {
        &::after {
          display: none;
        }
      }

      .ids {
        position: absolute;
        left: 0;
        top: 0;
        width: px2rpx(30);
        height: px2rpx(30);
        padding: 0;
        position: absolute;
        color: #000;
        line-height: px2rpx(30);
        background-color: #fff;
        text-align: center;
        border-radius: px2rpx(9999);
        border: 1px #ccc dashed;
        z-index: 12;
      }

      .k {
        font-size: px2rpx(14);
        color: #333333;
        font-weight: bold;
        padding: 0;
      }

      .v {
        font-size: px2rpx(14);
        color: #6f6f6f;
        padding: 0;
      }
    }
  }

  .f {
    margin: px2rpx(20) 0 0 px2rpx(60);
    color: #333333;
    font-size: px2rpx(14);
    font-family: PingFang SC;
    font-weight: 500;
    word-wrap: break-word;
  }
}

.status-box-btn {
  margin-top: px2rpx(20);
  width: 100%;
  display: flex;
  justify-content: flex-start;

  .btn-apply {
    width: px2rpx(193);
    height: px2rpx(40);
    background: #eb3f57;
    border-radius: px2rpx(4);
    text-align: center;
    color: white;
    font-size: px2rpx(16);
    font-family: Roboto;
    font-weight: 600;
    line-height: px2rpx(40);
    cursor: pointer;
    user-select: none;
    padding: 0;
    transition: all 0.3s ease;

    &.disabled {
      background: #ccc;
      cursor: not-allowed;
      opacity: 0.6;
    }
  }
}

.status-box-btn1 {
  justify-content: center;
}

.auth-tip {
  margin-top: px2rpx(12);
  width: 100%;
  color: #ff6b6b;
  font-size: px2rpx(14);
  text-align: left;
  padding: 0 px2rpx(30) 0 0;
}
</style>
