<?php
include_once('./_common.php');
header('Content-Type: application/json; charset=UTF-8');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
if (!$is_member) { echo json_encode(['result'=>'fail','message'=>'로그인이 필요합니다.']); exit; }
$od_id = isset($_GET['od_id']) ? trim((string)$_GET['od_id']) : '';
if (!$od_id) { echo json_encode(['result'=>'fail','message'=>'주문번호가 필요합니다.']); exit; }
$safe_mb = sql_escape_string($member['mb_id']);
$safe_od = sql_escape_string($od_id);
$order = sql_fetch(
    "SELECT od_id, od_fx_type FROM g5_shoppoint_order WHERE od_id='{$safe_od}' AND mb_id='{$safe_mb}' LIMIT 1",
    false
);
if (!$order) { echo json_encode(['result'=>'fail','message'=>'주문을 찾을 수 없습니다.']); exit; }
$raw_fx3 = trim($order['od_fx_type'] ?? '');
if (strlen($raw_fx3) === 6 && strtoupper(substr($raw_fx3, 0, 3)) === 'KRW') {
    $od_currency3 = strtoupper(substr($raw_fx3, 3));
} else {
    $od_currency3 = ($raw_fx3 !== '') ? strtoupper($raw_fx3) : 'KRW';
}
function _oi_parse_accts($txt) {
    $codes = ['KRW','CNY','USD','EUR','JPY','VND'];
    $r = []; foreach ($codes as $c) $r[$c] = [];
    $fb = [];
    foreach (explode("\n", trim((string)$txt)) as $ln) {
        $ln = trim($ln); if ($ln === '') continue;
        $hit = false;
        foreach ($codes as $c) {
            if (stripos($ln, '['.$c.']') !== false) { $r[$c][] = trim(str_ireplace('['.$c.']','',$ln)); $hit=true; break; }
        }
        if (!$hit) $fb[] = $ln;
    }
    if (empty($r['KRW']) && !empty($fb)) $r['KRW'] = $fb;
    return $r;
}
$def3 = sql_fetch("SELECT de_bank_account FROM g5_shop_default LIMIT 1", false);
$all3 = _oi_parse_accts($def3['de_bank_account'] ?? '');
$platform_accts3 = isset($all3[$od_currency3]) ? $all3[$od_currency3] : $all3['KRW'];
$res = sql_query(
    "SELECT dn_created_at, transfer_datetime, dn_timezone, used_platform_account,
            alt_bank_name, alt_bank_account, alt_bank_holder, alt_depositor_name,
            alt_deposit_amount, memo, dn_status
     FROM g5_deposit_notify
     WHERE od_id='{$safe_od}' AND mb_id='{$safe_mb}'
     ORDER BY dn_id DESC",
    false
);
$list = [];
while ($r = sql_fetch_array($res)) {
    $amt = rtrim(rtrim((string)$r['alt_deposit_amount'], '0'), '.');
    if ($amt === '' || $amt === '.') $amt = '0';
    $list[] = [
        'created_at'        => $r['dn_created_at'],
        'transfer_datetime' => $r['transfer_datetime'],
        'timezone'          => $r['dn_timezone'],
        'platform_account'  => (bool)(int)$r['used_platform_account'],
        'alt_bank'          => $r['alt_bank_name'],
        'alt_account'       => $r['alt_bank_account'],
        'alt_holder'        => $r['alt_bank_holder'],
        'depositor'         => $r['alt_depositor_name'],
        'amount'            => $amt,
        'memo'              => (string)($r['memo'] ?? ''),
        'status'            => $r['dn_status'],
    ];
}
echo json_encode(['result'=>'success','list'=>$list,'platform_accounts'=>$platform_accts3], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
