<template>
  <view class="page-real-name-verification">
    <view
      v-if="pageStatus==='realNameUnderway'"
      class="real-name-underway"
    >
      <view class="top-bg">
        <view class="bgc">
          <view class="tit1">
            未实名认证
          </view>
          <view class="tit2">
            提升账号安全，保障合法权益
          </view>
          <image
            class="img"
            src="../../static/images/bg-id-card.png"
          />
        </view>
      </view>
      <view class="content">
        <view class="info-item">
          <view class="tit">
            姓名
          </view>
          <view class="con">
            <input
              v-model="name"
              type="text"
              placeholder="请输入您的姓名"
            >
          </view>
        </view>
        <view class="info-item">
          <view class="tit">
            身份证号
          </view>
          <view class="con">
            <input
              v-model="identityNo"
              type="text"
              placeholder="请输入您的身份证号"
            >
          </view>
        </view>
      </view>
      <view class="footer">
        <view
          class="confirm-btn"
          @tap="onSubmit"
        >
          下一步
        </view>
      </view>
    </view>

    <view
      v-if="pageStatus==='realNameSuccess'"
      class="real-name-success"
    >
      <view class="icon">
        <image
          src="../../static/images/realname-success.png"
          class="img"
        />
      </view>
      <view class="tips">
        恭喜，认证成功!
      </view>
      <view class="second-tip">
        专享更多安全保障，更多优惠权益
      </view>
      <view
        class="btn"
        @tap="onRefund('success')"
      >
        返回
      </view>
    </view>
    <view
      v-if="pageStatus==='realNameFailure'"
      class="real-name-failure"
    >
      <view class="icon">
        <image
          src="../../static/images/realname-failure.png"
          class="img"
        />
      </view>
      <view class="tips">
        认证未通过!
      </view>
      <view class="second-tip">
        实名信息与注册信息不匹配！
      </view>
      <view
        class="btn"
        @tap="onRefund('failure')"
      >
        重新验证
      </view>
    </view>
  </view>
</template>

<script setup>
const { debounce } = util
const name = ref('') // 姓名
const identityNo = ref('') // 身份证号
// realNameUnderway：实名认证中；realNameSuccess：实名成功；realNameFailure：实名失败
const pageStatus = ref('realNameUnderway')
const allinpayMemberInfo = reactive({}) // 通联会员信息

/**
 * 生命周期函数--监听页面加载
 */
onLoad(() => {
  wx.setNavigationBarTitle({
    title: '实名认证'
  })
})

onShow(() => {
  // 获取实名认证状态
  onGetMemberInfo()
})

const onGetMemberInfo = () => {
  const params = {
    url: '/mall4cloud_user/user_extension/get_info',
    method: 'GET'
  }
  http.request(params).then(data => {
    // 已经实名认证则返回上一页
    if (data.allinpayRealNameSet) {
      uni.showToast({
        title: '您已实名认证',
        duration: 1500,
        icon: 'none'
      })
      setTimeout(() => {
        uni.navigateBack() // 返回上一页
      }, 1500)
    }
  })
}
// 提交前校验
const onCheckBeforeSubmit = () => {
  if (!(name.value && name.value.trim())) {
    uni.showToast({
      title: '请输入姓名',
      duration: 1500,
      icon: 'none'
    })
    return false
  }
  if (!(identityNo.value && identityNo.value.trim())) {
    uni.showToast({
      title: '请输入身份证号',
      duration: 1500,
      icon: 'none'
    })
    return false
  }
  if (!util.checkIDCard(identityNo.value)) {
    uni.showToast({
      title: '请输入正确的身份证号',
      duration: 1500,
      icon: 'none'
    })
    return false
  }
  return true
}

/**
 * 确认提交
 */
const onSubmit = debounce(function () {
  // 校验
  if (!onCheckBeforeSubmit()) {
    return
  }
  uni.showLoading()
  const params = {
    url: '/mall4cloud_payment/allinpay/member/set_real_name',
    method: 'POST',
    data: {
      identityNo: identityNo.value,
      name: name.value
    },
    dontTrunLogin: true
  }
  http.request(params).then(() => {
    uni.hideLoading()
    uni.showToast({
      title: '实名认证成功',
      duration: 1500,
      icon: 'none'
    })
    if (!allinpayMemberInfo.isPhoneChecked) {
      // 未绑定手机号前往绑定手机号
      setTimeout(() => {
        uni.navigateTo({
          url: '/package-user/pages/bind-mobile/bind-mobile?isRealnameVerification=1'
        })
      }, 1500)
    } else {
      // 已绑定手机号返回上一页
      setTimeout(() => {
        uni.navigateBack() // 返回上一页
      }, 1500)
    }
  }).catch(() => {
    uni.hideLoading()
    pageStatus.value = 'realNameFailure'
  })
}, 1500)

const onRefund = (status) => {
  // 状态为success时，返回首页；状态为failure时，重新验证
  if (status === 'failure') {
    pageStatus.value = 'realNameUnderway'
  } else if (status === 'success') {
    uni.navigateBack() // 返回上一页
  }
}

</script>

<style lang="scss" scoped>
@use "./realname-verification.scss";
</style>
