<?php
require_once __DIR__ . '/../config.php';
$u = current_user();

$isRealname = 0; // 0未认证, 1已通过, 2审核中, 3已驳回
$realNameMasked = '';
$idCardMasked = '';
$realnameAt = '';
$rejectReason = '';

if (!empty($u['id'])) {
    try {
        $cols = db()->query('SHOW COLUMNS FROM users')->fetchAll(PDO::FETCH_COLUMN);
        if (in_array('is_realname', $cols)) {
            $stmt = db()->prepare('SELECT is_realname, real_name, id_card, realname_reject_reason, realname_at FROM users WHERE id = ? LIMIT 1');
            $stmt->execute([$u['id']]);
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            if ($row) {
                $isRealname = (int)($row['is_realname'] ?? 0);
                $rn = $row['real_name'] ?? '';
                $ic = $row['id_card'] ?? '';
                $rejectReason = $row['realname_reject_reason'] ?? '';
                $realNameMasked = (mb_strlen($rn) > 1) ? (mb_substr($rn, 0, 1) . str_repeat('*', mb_strlen($rn) - 1)) : $rn;
                $idCardMasked = (strlen($ic) === 18) ? (substr($ic, 0, 4) . '**********' . substr($ic, -4)) : $ic;
                $realnameAt = $row['realname_at'] ?? '';
            }
        }
    } catch (Exception $e) {}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover">
<meta name="apple-mobile-web-app-title" content="盼之交易">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="default">
<meta name="mobile-web-app-capable" content="yes">
<meta name="theme-color" content="#ff4d4f">
<link rel="apple-touch-icon" href="/static/picture/6a61a47f2fd8e_1784783999.webp">
<link rel="manifest" href="/manifest.json">
<meta name="referrer" content="no-referrer">
<title>实名认证中心 - 盼之账号</title>
<link rel="icon" type="image/webp" href="/static/picture/6a61a47f2fd8e_1784783999.webp">
<link rel="stylesheet" href="/static/css/front-fixed.css?v=20261005">
<style>
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
body { font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif; background: #f6f7f9; color: #1a1a1a; min-height: 100vh; }
.app-container { max-width: none; margin: 0 auto; background: #f6f7f9; min-height: 100vh; position: relative; }

/* 导航栏 */
.nav-bar { position: sticky; top: 0; left: 0; width: 100%; height: 48px; background: #fff; display: flex; align-items: center; justify-content: space-between; padding: 0 14px; z-index: 100; border-bottom: 1px solid #f0f0f0; }
.back-btn { width: 32px; height: 32px; display: flex; align-items: center; justify-content: center; border-radius: 50%; color: #333; text-decoration: none; border: none; background: transparent; cursor: pointer; }
.nav-title { font-size: 16px; font-weight: 600; color: #1a1a1a; }

/* 头部认证状态横幅 */
.auth-header-card { margin: 12px 14px; background: linear-gradient(135deg, #fff 0%, #fff9f9 100%); border-radius: 12px; padding: 18px 16px; display: flex; align-items: center; gap: 14px; box-shadow: 0 2px 8px rgba(0,0,0,0.03); border: 1px solid #ffe8e8; }
.auth-icon-wrap { width: 48px; height: 48px; border-radius: 50%; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.auth-icon-wrap.verified { background: #e6f7ea; color: #52c41a; }
.auth-icon-wrap.unverified { background: #fff1f0; color: #ff4d4f; }
.auth-status-title { font-size: 16px; font-weight: 700; color: #1a1a1a; margin-bottom: 4px; }
.auth-status-desc { font-size: 12px; color: #888; line-height: 1.4; }

/* 表单卡片 */
.form-card { margin: 12px 14px; background: #fff; border-radius: 12px; padding: 16px; box-shadow: 0 2px 8px rgba(0,0,0,0.03); }
.form-title { font-size: 14px; font-weight: 700; color: #1a1a1a; margin-bottom: 14px; display: flex; align-items: center; justify-content: space-between; }
.form-group { margin-bottom: 14px; }
.form-label { font-size: 13px; font-weight: 500; color: #333; margin-bottom: 6px; display: block; }
.form-input { width: 100%; height: 44px; border: 1px solid #e2e4e8; border-radius: 8px; padding: 0 12px; font-size: 14px; color: #1a1a1a; background: #fafbfc; outline: none; transition: border-color 0.2s; }
.form-input:focus { border-color: #ff4d4f; background: #fff; }
.form-input::placeholder { color: #bbb; }

/* 已认证详情展示 */
.info-cell { display: flex; justify-content: space-between; padding: 12px 0; border-bottom: 1px solid #f2f3f5; font-size: 14px; }
.info-cell:last-child { border-bottom: none; }
.info-label { color: #666; }
.info-value { color: #1a1a1a; font-weight: 500; }

/* 提交按钮 */
.submit-btn { width: 100%; height: 46px; background: linear-gradient(135deg, #ff4d4f 0%, #cf1322 100%); color: #fff; border: none; border-radius: 23px; font-size: 15px; font-weight: 600; cursor: pointer; display: flex; align-items: center; justify-content: center; margin-top: 10px; }
.submit-btn:active { opacity: 0.88; }
.submit-btn:disabled { background: #d9d9d9; cursor: not-allowed; }

/* 安全提示 */
.notice-card { margin: 14px; padding: 14px; background: #fff; border-radius: 12px; font-size: 12px; color: #888; line-height: 1.6; }
.notice-title { font-weight: 600; color: #555; margin-bottom: 6px; display: flex; align-items: center; gap: 4px; }

/* Toast */
.toast { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: rgba(0,0,0,0.85); color: #fff; padding: 10px 20px; border-radius: 8px; font-size: 13px; z-index: 9999; display: none; text-align: center; max-width: 80%; }
</style>
</head>
<body>

<div class="app-container">
  <!-- 顶部导航 -->
  <div class="nav-bar">
    <button class="back-btn" onclick="goBack()" aria-label="返回">
      <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 18 9 12 15 6"></polyline></svg>
    </button>
    <div class="nav-title">实名认证</div>
    <div style="width: 32px;"></div>
  </div>

  <!-- 认证状态卡片 -->
  <div class="auth-header-card">
    <?php if ($isRealname === 1): ?>
      <div class="auth-icon-wrap verified">
        <svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path><polyline points="22 4 12 14.01 9 11.01"></polyline></svg>
      </div>
      <div>
        <div class="auth-status-title" style="color: #52c41a;">已完成实名认证</div>
        <div class="auth-status-desc">您的身份信息已通过国家身份认证系统核验，享有账号交易官方保障</div>
      </div>
    <?php elseif ($isRealname === 2): ?>
      <div class="auth-icon-wrap" style="background:#fffbe6; color:#fa8c16;">
        <svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline></svg>
      </div>
      <div>
        <div class="auth-status-title" style="color: #fa8c16;">实名审核中</div>
        <div class="auth-status-desc">您的二要素实名信息已提交，官方客服通常在 10 分钟内完成核验</div>
      </div>
    <?php elseif ($isRealname === 3): ?>
      <div class="auth-icon-wrap unverified">
        <svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle><line x1="15" y1="9" x2="9" y2="15"></line><line x1="9" y1="9" x2="15" y2="15"></line></svg>
      </div>
      <div>
        <div class="auth-status-title" style="color: #ff4d4f;">实名审核未通过</div>
        <div class="auth-status-desc" style="color: #ff4d4f;"><?= htmlspecialchars($rejectReason ?: '二要素信息核验不一致，请仔细检查后重新提交') ?></div>
      </div>
    <?php else: ?>
      <div class="auth-icon-wrap unverified">
        <svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="16" rx="2"></rect><circle cx="9" cy="10" r="2"></circle><line x1="15" y1="8" x2="17" y2="8"></line><line x1="15" y1="12" x2="17" y2="12"></line><line x1="7" y1="16" x2="17" y2="16"></line></svg>
      </div>
      <div>
        <div class="auth-status-title">未实名认证</div>
        <div class="auth-status-desc">根据国家网络安全监管要求，账号交易发布须实名核验</div>
      </div>
    <?php endif; ?>
  </div>

  <?php if ($isRealname === 1): ?>
    <!-- 已实名认证信息卡片 -->
    <div class="form-card">
      <div class="form-title">
        <span>认证信息</span>
        <span style="font-size: 12px; color: #52c41a; font-weight: normal;">✓ 终身保障有效</span>
      </div>
      <div class="info-cell">
        <span class="info-label">真实姓名</span>
        <span class="info-value"><?= htmlspecialchars($realNameMasked) ?></span>
      </div>
      <div class="info-cell">
        <span class="info-label">身份证号</span>
        <span class="info-value"><?= htmlspecialchars($idCardMasked) ?></span>
      </div>
      <?php if (!empty($realnameAt)): ?>
      <div class="info-cell">
        <span class="info-label">认证时间</span>
        <span class="info-value" style="font-size: 12px; color: #888;"><?= htmlspecialchars($realnameAt) ?></span>
      </div>
      <?php endif; ?>
      <div class="info-cell">
        <span class="info-label">认证状态</span>
        <span class="info-value" style="color: #52c41a;">审核已通过</span>
      </div>
    </div>
  <?php elseif ($isRealname === 2): ?>
    <!-- 审核中状态卡片 -->
    <div class="form-card">
      <div class="form-title">
        <span>正在审核</span>
        <span style="font-size: 12px; color: #fa8c16; font-weight: normal;">审核受理中</span>
      </div>
      <div class="info-cell">
        <span class="info-label">已提交姓名</span>
        <span class="info-value"><?= htmlspecialchars($realNameMasked) ?></span>
      </div>
      <div class="info-cell">
        <span class="info-label">已提交身份证</span>
        <span class="info-value"><?= htmlspecialchars($idCardMasked) ?></span>
      </div>
      <div class="info-cell">
        <span class="info-label">提交时间</span>
        <span class="info-value" style="font-size: 12px; color: #888;"><?= htmlspecialchars($realnameAt ?: date('Y-m-d H:i')) ?></span>
      </div>
      <div style="margin-top:14px; padding:10px 12px; background:#fffbe6; border-radius:8px; font-size:12px; color:#d46b08; line-height:1.5;">
        管理员正在后台核验二要素真实性，请留意站内系统通知与个人中心状态刷新。
      </div>
    </div>
  <?php else: ?>
    <!-- 实名认证填写表单（未认证或已驳回重新提交） -->
    <div class="form-card">
      <div class="form-title">
        <span>二要素实名信息填写</span>
        <?php if ($isRealname === 3): ?>
          <span style="font-size: 12px; color: #ff4d4f;">请重新核实填写</span>
        <?php endif; ?>
      </div>
      <form id="realnameForm" onsubmit="submitRealname(event)">
        <div class="form-group">
          <label class="form-label">真实姓名</label>
          <input type="text" id="realNameInput" class="form-input" placeholder="请输入本人身份证上的真实姓名" required maxlength="20">
        </div>
        <div class="form-group">
          <label class="form-label">身份证号码</label>
          <input type="text" id="idCardInput" class="form-input" placeholder="请输入18位二代居民身份证号码" required maxlength="18" style="text-transform: uppercase;">
        </div>
        <button type="submit" class="submit-btn" id="submitBtn"><?= $isRealname === 3 ? '重新提交审核' : '立即提交认证' ?></button>
      </form>
    </div>
  <?php endif; ?>

  <!-- 认证提示说明 -->
  <div class="notice-card">
    <div class="notice-title">
      <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="16" x2="12" y2="12"></line><line x1="12" y1="8" x2="12.01" y2="8"></line></svg>
      实名认证声明与合规保障
    </div>
    <p>1. 本平台严格按照国家《网络安全法》及有关部门规定落实网络用户真实身份信息认证要求；</p>
    <p>2. 实名认证信息一经审核绑定，不可随意更换，仅用于账号交易防诈骗合规风控；</p>
    <p>3. 平台采用银行级敏感数据非对称加密存储技术，全方位保障您的个人隐私安全。</p>
  </div>
</div>

<div class="toast" id="toast"></div>

<script>
function showToast(msg, duration) {
  var t = document.getElementById('toast');
  if (!t) return;
  t.innerText = msg;
  t.style.display = 'block';
  setTimeout(function() { t.style.display = 'none'; }, duration || 2000);
}

function goBack() {
  if (document.referrer && document.referrer.indexOf(location.host) !== -1) {
    history.back();
  } else {
    location.href = '/me.php';
  }
}

function submitRealname(e) {
  e.preventDefault();
  var name = document.getElementById('realNameInput').value.trim();
  var idcard = document.getElementById('idCardInput').value.trim().toUpperCase();

  if (!name || name.length < 2) {
    showToast('请输入合法的真实姓名');
    return;
  }
  if (!/^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/.test(idcard)) {
    showToast('请输入合法的18位居民身份证号码');
    return;
  }

  var btn = document.getElementById('submitBtn');
  btn.disabled = true;
  var originalText = btn.innerText;
  btn.innerText = '正在核验中...';

  var fd = new FormData();
  fd.append('action', 'verify_realname');
  fd.append('real_name', name);
  fd.append('id_card', idcard);

  fetch('/api.php', { method: 'POST', body: fd })
    .then(function(r) { return r.json(); })
    .then(function(res) {
      if (res.success) {
        showToast(res.msg || '实名认证信息已提交，请等待后台管理审核通过！');
        setTimeout(function() { location.reload(); }, 1500);
      } else {
        alert(res.msg || '实名认证失败，请检查输入是否准确');
        btn.disabled = false;
        btn.innerText = originalText;
      }
    })
    .catch(function(err) {
      alert('网络连接异常，请重试');
      btn.disabled = false;
      btn.innerText = originalText;
    });
}
</script>

</body>
</html>
