<template>
  <div class="vh100">
    <!-- Add Toast component -->
    <Toast ref="toast" />
    <!-- 导航栏 -->
    <div class="nav-bar">
      <div class="back-btn" @click="$router.back()">
        <i class="arrow-left"></i>
      </div>
      <div class="title">实名认证</div>
    </div>

    <!-- 已认证状态 -->
    <div v-if="userInfo.if_real === 1" class="auth-success">
      <div class="success-icon">
        <svg viewBox="64 64 896 896" data-icon="check-circle" width="80px" height="80px" fill="#52c41a" aria-hidden="true">
          <path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm193.5 301.7l-210.6 292a31.8 31.8 0 01-51.7 0L318.5 484.9c-3.8-5.3 0-12.7 6.5-12.7h46.9c10.2 0 19.9 4.9 25.9 13.3l71.2 98.8 157.2-218c6-8.3 15.6-13.3 25.9-13.3H699c6.5 0 10.3 7.4 6.5 12.7z"></path>
        </svg>
      </div>
      <div class="success-text">您已完成实名认证</div>
    </div>

    <!-- 未认证状态 -->
    <div v-else>
      <!-- 认证表单 -->
      <div class="flex bgwhite alcenter radius4 pl10 pt10 bdb_blue3">
        <div class="flex between h40 lh40 pr10 tr flex1">
          <span>证件认证</span>
          <span>身份证</span>
        </div>
      </div>

      <div class="flex bgwhite alcenter radius4 pl10 pt10 bdb_blue3">
        <span>证件姓氏：</span>
        <input 
          type="text" 
          class="h40 lh40 pr10 tr flex1 input-uni" 
          placeholder="姓名"
          v-model="formData.realName"
        >
      </div>

      <div class="flex bgwhite alcenter radius4 pl10 mt10 bdb_blue3">
        <span>证件号码：</span>
        <input 
          type="text" 
          class="h40 lh40 pr10 tr flex1 input-uni" 
          placeholder="输入证件号码"
          v-model="formData.idCard"
        >
      </div>

      <div 
        class="mt30 h40 lh40 tc white bgBlue radius28 ft14 w90 mauto"
        @click="handleSubmit"
      >
        确 认
      </div>

      <div class="flex alcenter mt10">
        <div class="bottom">
          <ul>
            <li style="height: 45px;">为保证您的权益请认真查看以下条例，如有疑问请及时联系在线客服</li>
            <li>身份证件拍摄不完整有反光</li>
            <li>提交信息和身份证件不一致</li>
            <li style="height: 45px;">为了您的信息安全审核期间由AI机器人全程负责我们无法干预</li>
            <li style="height: 45px;">使用虚假信息的用户我们将采取相应措施,包含且不限于封号、冻结资产、限制登录等</li>
          </ul>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import Toast from '@/components/Toast.vue';
import { setReal, getUserInfo } from '@/api/home';

export default {
  name: 'RealAuthentication',
  components: {
    Toast
  },
  data() {
    return {
      userInfo: {},
      formData: {
        realName: '',
        idCard: ''
      }
    }
  },
  created() {
    this.getUserInfo();
  },
  methods: {
    async getUserInfo() {
      try {
        const res = await getUserInfo();
        this.userInfo = res.data;
      } catch (error) {
        this.$refs.toast.show('获取用户信息失败', 'error');
      }
    },
    async handleSubmit() {
      if (!this.formData.realName) {
        this.$refs.toast.show('请输入姓名', 'warning');
        return;
      }
      if (!this.formData.idCard) {
        this.$refs.toast.show('请输入证件号码', 'warning');
        return;
      }

      try {
        const params = {
          type: '身份证',
          name: this.formData.realName,
          idCard: this.formData.idCard
        };

        const res = await setReal(params);

        if (res.code === 1) {
          this.$refs.toast.show('认证信息提交成功', 'success');
          setTimeout(() => {
            this.$router.back();
          }, 1500);
        } else {
          this.$refs.toast.show(res.msg || '认证失败，请重试', 'error');
        }
      } catch (error) {
        this.$refs.toast.show('网络错误，请稍后重试', 'error');
      }
    }
  }
}
</script>

<style scoped>
.vh100 {
  min-height: 100vh;
  background: #fff;
}

.nav-bar {
  height: 44px;
  background-color: #ed4b39;
  color: #fff;
  display: flex;
  align-items: center;
  position: relative;
  padding: 0 15px;
}

.back-btn {
  width: 24px;
  height: 44px;
  display: flex;
  align-items: center;
  cursor: pointer;
}

.arrow-left {
  width: 12px;
  height: 12px;
  border-left: 2px solid #fff;
  border-bottom: 2px solid #fff;
  transform: rotate(45deg);
}

.title {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  font-size: 16px;
  font-weight: 500;
}

.flex { display: flex; }
.between { justify-content: space-between; }
.alcenter { align-items: center; }
.tc { text-align: center; }
.tr { text-align: right; }
.mt10 { margin-top: 10px; }
.mt30 { margin-top: 30px; }
.pl10 { padding-left: 10px; }
.pr10 { padding-right: 10px; }
.pt10 { padding-top: 10px; }
.h40 { height: 40px; }
.lh40 { line-height: 40px; }
.ft14 { font-size: 14px; }
.white { color: #fff; }
.bgwhite { background: #fff; }
.flex1 { flex: 1; }
.w90 { width: 90%; }
.mauto { margin: 0 auto; }

.radius4 { border-radius: 4px; }
.radius28 { border-radius: 28px; }

.bgBlue {
  margin-top: 20px;
  background: #ed4b39;
  cursor: pointer;
}

.bgBlue:active {
  opacity: 0.9;
}

.bdb_blue3 {
  border-bottom: 1px solid rgba(85, 89, 105, 0.12);
}

.input-uni {
  border: none;
  outline: none;
  background: transparent;
}

.bottom {
  padding: 0 20px;
  width: 100%;
}

.bottom ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.bottom li {
  color: #666;
  font-size: 12px;
  line-height: 1.5;
  margin-bottom: 10px;
  padding-left: 15px;
  position: relative;
}

.bottom li::before {
  content: '';
  position: absolute;
  left: 0;
  top: 8px;
  width: 4px;
  height: 4px;
  background: #666;
  border-radius: 50%;
}

.auth-success {
  padding: 40px 20px;
  text-align: center;
}

.success-icon {
  margin-bottom: 20px;
}

.success-text {
  font-size: 20px;
  color: #52c41a;
  font-weight: 500;
  margin-bottom: 30px;
}

.auth-info {
  background: #f8f8f8;
  border-radius: 8px;
  padding: 20px;
  text-align: left;
}

.info-item {
  margin-bottom: 15px;
  font-size: 14px;
  line-height: 1.5;
}

.info-item:last-child {
  margin-bottom: 0;
}

.info-item .label {
  color: #666;
}

.info-item .value {
  color: #333;
  margin-left: 5px;
}
</style> 