#!/bin/bash

# 审批订单测试脚本
# 用于测试管理员审批充值订单的完整流程

BASE_URL="http://localhost:5010"
ADMIN_TOKEN=""

echo "=========================================="
echo "审批订单测试脚本"
echo "=========================================="
echo ""

# 1. 管理员登录
echo "步骤1: 管理员登录..."
LOGIN_RESPONSE=$(curl -s -X POST "${BASE_URL}/admin/login" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "admin",
    "password": "admin123"
  }')

echo "登录响应: ${LOGIN_RESPONSE}"
echo ""

# 提取token
ADMIN_TOKEN=$(echo ${LOGIN_RESPONSE} | grep -o '"token":"[^"]*"' | cut -d'"' -f4)
echo "Token: ${ADMIN_TOKEN}"
echo ""

if [ -z "$ADMIN_TOKEN" ]; then
    echo "❌ 登录失败，无法获取token"
    exit 1
fi

echo "✅ 登录成功"
echo ""

# 2. 查询待审批订单
echo "步骤2: 查询待审批订单..."
PENDING_RESPONSE=$(curl -s -X GET "${BASE_URL}/admin/order/pending?pageNum=1&pageSize=10" \
  -H "Authorization: Bearer ${ADMIN_TOKEN}")

echo "待审批订单响应:"
echo "${PENDING_RESPONSE}" | python3 -m json.tool 2>/dev/null || echo "${PENDING_RESPONSE}"
echo ""

# 提取第一个订单号
ORDER_NO=$(echo ${PENDING_RESPONSE} | grep -o '"orderNo":"[^"]*"' | head -1 | cut -d'"' -f4)
echo "第一个待审批订单号: ${ORDER_NO}"
echo ""

if [ -z "$ORDER_NO" ]; then
    echo "⚠️  没有待审批订单，无法继续测试"
    exit 0
fi

# 3. 查询订单详情（审批前）
echo "步骤3: 查询订单详情（审批前）..."
ORDER_LIST_RESPONSE=$(curl -s -X GET "${BASE_URL}/admin/order/list?pageNum=1&pageSize=1" \
  -H "Authorization: Bearer ${ADMIN_TOKEN}")

echo "订单列表响应:"
echo "${ORDER_LIST_RESPONSE}" | python3 -m json.tool 2>/dev/null || echo "${ORDER_LIST_RESPONSE}"
echo ""

# 4. 执行审批操作
echo "=========================================="
echo "步骤4: 执行审批操作（通过）"
echo "=========================================="
echo "订单号: ${ORDER_NO}"
echo "审批状态: 2 (通过)"
echo ""

APPROVE_RESPONSE=$(curl -s -X POST "${BASE_URL}/admin/order/approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${ADMIN_TOKEN}" \
  -d "{
    \"orderNo\": \"${ORDER_NO}\",
    \"status\": 2,
    \"adminRemark\": \"测试审批通过\"
  }")

echo "审批响应: ${APPROVE_RESPONSE}"
echo ""

# 检查审批结果
if echo "${APPROVE_RESPONSE}" | grep -q '"code":"0"'; then
    echo "✅ 审批接口调用成功"
else
    echo "❌ 审批接口调用失败"
fi
echo ""

# 5. 等待几秒后查询订单状态
echo "步骤5: 等待2秒后查询订单状态..."
sleep 2

echo "查询订单详情（审批后）..."
VERIFY_RESPONSE=$(curl -s -X GET "${BASE_URL}/admin/order/list?pageNum=1&pageSize=10" \
  -H "Authorization: Bearer ${ADMIN_TOKEN}")

echo "订单列表响应:"
echo "${VERIFY_RESPONSE}" | python3 -m json.tool 2>/dev/null || echo "${VERIFY_RESPONSE}"
echo ""

# 查找刚才审批的订单
echo "验证订单 ${ORDER_NO} 的状态..."
ORDER_STATUS=$(echo ${VERIFY_RESPONSE} | grep -A 20 "\"orderNo\":\"${ORDER_NO}\"" | grep -o '"status":[0-9]*' | head -1 | cut -d':' -f2)
echo "订单状态: ${ORDER_STATUS}"
echo ""

if [ "$ORDER_STATUS" = "3" ]; then
    echo "✅✅✅ 测试成功！订单状态已更新为 3 (已完成)"
elif [ "$ORDER_STATUS" = "2" ]; then
    echo "⚠️  警告：订单状态仍为 2 (待确认)，审批可能未生效"
else
    echo "❌ 测试失败！订单状态异常: ${ORDER_STATUS}"
fi
echo ""

echo "=========================================="
echo "测试完成"
echo "=========================================="
echo ""
echo "请检查后端日志输出，查看详细的审批流程日志："
echo "  - [AdminController] 开头的日志"
echo "  - [FundService.approve] 开头的日志"
echo ""
