<?php
include_once 'config.php';
check_auth();

$user_id = $_SESSION['user_id'];
$account = get_account($user_id);
$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $real_name = $_POST['real_name'] ?? '';
    $id_number = $_POST['id_number'] ?? '';
    
    // In a real app, we would handle file uploads here. 
    // For this prototype, we'll just save the paths as placeholders.
    $id_front = 'uploads/kyc/' . $user_id . '_front.jpg';
    $id_back = 'uploads/kyc/' . $user_id . '_back.jpg';
    
    if (empty($real_name) || empty($id_number)) {
        $error = '请填写完整信息';
    } else {
        $stmt = db()->prepare("UPDATE accounts SET real_name = ?, id_number = ?, kyc_status = 'PENDING' WHERE id = ?");
        $stmt->execute([$real_name, $id_number, $account['id']]);
        $success = '认证资料已提交，请等待审核';
        $account['kyc_status'] = 'PENDING';
    }
}

include 'header.php';
?>
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="glass-card p-4">
                <h3 class="text-white mb-4"><i class="bi bi-shield-check text-warning me-2"></i> 实名认证 (KYC)</h3>
                
                <?php if ($success): ?>
                    <div class="alert alert-success"><?php echo $success; ?></div>
                    <div class="text-center py-5">
                        <i class="bi bi-clock-history display-1 text-warning"></i>
                        <p class="mt-3 text-secondary">审核中，通常在 24 小时内完成</p>
                        <a href="profile.php" class="btn btn-warning mt-3">返回个人中心</a>
                    </div>
                <?php elseif ($account['kyc_status'] === 'VERIFIED'): ?>
                    <div class="text-center py-5">
                        <i class="bi bi-check-circle-fill display-1 text-success"></i>
                        <h4 class="mt-4">您已完成实名认证</h4>
                        <p class="text-secondary">感谢您的信任，您现在可以享受完整交易权限</p>
                        <a href="profile.php" class="btn btn-warning mt-3">返回个人中心</a>
                    </div>
                <?php elseif ($account['kyc_status'] === 'PENDING'): ?>
                    <div class="text-center py-5">
                        <i class="bi bi-hourglass-split display-1 text-warning"></i>
                        <h4 class="mt-4">认证审核中</h4>
                        <p class="text-secondary">我们正在快马加鞭为您处理，请耐心等待</p>
                        <a href="profile.php" class="btn btn-warning mt-3">返回个人中心</a>
                    </div>
                <?php else: ?>
                    <?php if ($error): ?>
                        <div class="alert alert-danger"><?php echo $error; ?></div>
                    <?php endif; ?>
                    
                    <form method="POST">
                        <div class="mb-3">
                            <label class="form-label text-secondary small">真实姓名</label>
                            <input type="text" name="real_name" class="form-control bg-dark border-secondary text-white" placeholder="请输入您的真实姓名" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label text-secondary small">身份证号 / 护照号</label>
                            <input type="text" name="id_number" class="form-control bg-dark border-secondary text-white" placeholder="请输入证件号码" required>
                        </div>
                        
                        <div class="row mb-4">
                            <div class="col-6">
                                <label class="form-label text-secondary small">证件正面</label>
                                <div class="border border-secondary border-dashed rounded p-4 text-center" style="border-style: dashed !important; cursor: pointer;">
                                    <i class="bi bi-plus-lg text-secondary"></i>
                                    <div class="small text-secondary mt-1">上传正面</div>
                                </div>
                            </div>
                            <div class="col-6">
                                <label class="form-label text-secondary small">证件反面</label>
                                <div class="border border-secondary border-dashed rounded p-4 text-center" style="border-style: dashed !important; cursor: pointer;">
                                    <i class="bi bi-plus-lg text-secondary"></i>
                                    <div class="small text-secondary mt-1">上传反面</div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="p-3 mb-4 rounded" style="background: rgba(252, 213, 53, 0.1); border: 1px solid rgba(252, 213, 53, 0.2);">
                            <div class="d-flex">
                                <i class="bi bi-info-circle text-warning me-2"></i>
                                <div class="small text-secondary">
                                    请确保上传的图片清晰可见，文字无遮挡。您的个人信息将受到最高级别的安全保护。
                                </div>
                            </div>
                        </div>
                        
                        <button type="submit" class="btn btn-warning w-100 fw-bold py-3">提交认证</button>
                    </form>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php include 'footer.php'; ?>