<?php
// api/get_kyc.php - 檢查實名認證狀態
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
error_reporting(0); // 防呆：關閉警告干擾
require_once 'db.php';

$user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;
if ($user_id <= 0) { echo json_encode(["status" => "error", "message" => "未登入"]); exit; }

try {
    $stmt = $pdo->prepare("SELECT is_verified, id_photo FROM users WHERE id = ?");
    $stmt->execute([$user_id]);
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$row) {
        echo json_encode(["status" => "success", "kyc_status" => "unsubmitted"]); exit;
    }

    $kyc_status = 'unsubmitted'; // 尚未上傳
    if ($row['is_verified'] == 1) {
        $kyc_status = 'verified'; // 已認證放行
    } else if (!empty($row['id_photo'])) {
        $kyc_status = 'pending'; // 審核中
    }

    echo json_encode(["status" => "success", "kyc_status" => $kyc_status]);
} catch (Exception $e) {
    echo json_encode(["status" => "error", "message" => "資料庫讀取失敗"]);
}
?>