#!/bin/bash

# 简单的审批测试 - 使用curl直接调用API

BASE_URL="http://localhost:5010"

echo "=========================================="
echo "简单审批测试"
echo "=========================================="

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

TOKEN=$(echo ${LOGIN_RESPONSE} | grep -o '"token":"[^"]*"' | cut -d'"' -f4)

if [ -z "$TOKEN" ]; then
    echo "❌ 登录失败"
    echo "${LOGIN_RESPONSE}"
    exit 1
fi

echo "✅ 登录成功"
echo ""

# 2. 获取待审批订单
echo "2. 查询待审批订单..."
PENDING=$(curl -s -X GET "${BASE_URL}/admin/order/pending?pageNum=1&pageSize=5" \
  -H "Authorization: Bearer ${TOKEN}")

ORDER_NO=$(echo ${PENDING} | grep -o '"orderNo":"[^"]*"' | head -1 | cut -d'"' -f4)

if [ -z "$ORDER_NO" ]; then
    echo "⚠️  没有待审批订单"
    exit 0
fi

echo "找到订单: ${ORDER_NO}"
echo ""

# 3. 执行审批
echo "3. 执行审批..."
RESULT=$(curl -s -X POST "${BASE_URL}/admin/order/approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d "{\"orderNo\":\"${ORDER_NO}\",\"status\":2,\"adminRemark\":\"快速测试\"}")

echo "响应: ${RESULT}"
echo ""

# 4. 验证结果
sleep 1
echo "4. 验证结果..."
VERIFY=$(curl -s -X GET "${BASE_URL}/admin/order/list?pageNum=1&pageSize=5" \
  -H "Authorization: Bearer ${TOKEN}")

STATUS=$(echo ${VERIFY} | grep -A 20 "\"orderNo\":\"${ORDER_NO}\"" | grep -o '"status":[0-9]*' | head -1 | cut -d':' -f2)

if [ "$STATUS" = "3" ]; then
    echo "✅ 成功！订单状态 = 3"
else
    echo "❌ 失败！订单状态 = ${STATUS} (期望3)"
fi
