<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 style="color: #fff;font-size: 14px;padding: 40px 20px;font-weight: bold;">
        实名认证<br/>
        获得红旗资产18000元
      </div>
      <!-- 认证表单 -->
      <div class="form-container">
        <div class="form-item">
          <div class="form-label">姓名</div>
          <input 
            type="text" 
            class="form-input" 
            placeholder="请输入您的姓名"
            v-model="formData.realName"
          >
        </div>

        <div class="form-item">
          <div class="form-label">身份证号码</div>
          <input 
            type="text" 
            class="form-input" 
            placeholder="请输入身份证号码"
            v-model="formData.idCard"
          >
          <div style="display: flex;justify-content: center;align-items: center;">
            <button 
          class="submit-btn"
          @click="handleSubmit"
        >
          提交实名认证
        </button>
          </div>
        </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 formData = new FormData();
        formData.append('type', '身份证');
        formData.append('name', this.formData.realName);
        formData.append('idCard', this.formData.idCard);

        const res = await setReal(formData);

        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: url('../assets/dabag.png') no-repeat ;
  background-size: 100% 100%;
}

.nav-bar {
  height: 44px;
  background-color: transparent;
  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;
}

.form-container {
  padding: 0 20px;
}

.form-item {
  background: #fff;
  padding: 18px;
}

.form-label {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
}

.form-input {
  width: 100%;
  height: 44px;
  border: none;
  background: #fff;
  font-size: 14px;
  padding: 0;
  color: #333;
}

.form-input::placeholder {
  color: #999;
}

.form-input:focus {
  outline: none;
  border-bottom-color: #ed4b39;
}

.submit-btn {
  width: 60%;
  height: 44px;
  background: #FF6B6B;
  border-radius: 22px;
  border: none;
  color: #fff;
  font-size: 16px;
  font-weight: 500;
  margin-top: 30px;
  cursor: pointer;
  
}

.submit-btn:active {
  opacity: 0.9;
  
}
</style> 