<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-5-20
 */
require_once dirname(__DIR__) . '/core/lib/session.php';
cmsStartSession();
header('Content-Type: application/json; charset=utf-8');

require_once __DIR__ . '/../core/db/db.php';
require_once __DIR__ . '/../core/lib/common.php';
require_once __DIR__ . '/../core/lib/refund_service.php';

$db = \Core\Db\DB::getInstance();
$memberId = intval($_SESSION['member_id'] ?? 0);
if ($memberId <= 0) {
    echo json_encode(['code' => 401, 'msg' => '请先登录'], JSON_UNESCAPED_UNICODE);
    exit;
}

$action = trim((string) ($_POST['action'] ?? $_GET['action'] ?? ''));
if ($action === 'apply') {
    if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
        http_response_code(405);
        echo json_encode(['code' => 405, 'msg' => '璇锋敹鍒板悎鍙厑璁?POST'], JSON_UNESCAPED_UNICODE);
        exit;
    }
    requireMemberCsrfToken(true);
    $result = \Core\Lib\RefundService::apply($db, $memberId, intval($_POST['order_id'] ?? 0), $_POST['reason'] ?? '');
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}

echo json_encode(['code' => 404, 'msg' => '未知操作'], JSON_UNESCAPED_UNICODE);
