#!/bin/bash

# 中介黑名单编辑功能修复测试脚本
# 用于验证修改按钮点击后数据是否正确反显

BASE_URL="http://localhost:8080"
USERNAME="admin"
PASSWORD="admin123"

echo "=========================================="
echo "中介黑名单编辑功能修复测试"
echo "=========================================="
echo ""

# 步骤1: 登录获取 token
echo "步骤1: 登录系统..."
LOGIN_RESPONSE=$(curl -s -X POST "${BASE_URL}/login/test" \
  -H "Content-Type: application/json" \
  -d "{\"username\":\"${USERNAME}\",\"password\":\"${PASSWORD}\"}")

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

# 提取 token (假设返回格式为 {"code":200,"data":{"token":"xxx"}})
TOKEN=$(echo ${LOGIN_RESPONSE} | grep -o '"token":"[^"]*' | sed 's/"token":"//')

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

echo "✅ 登录成功，Token: ${TOKEN:0:20}..."
echo ""

# 步骤2: 查询中介黑名单列表
echo "步骤2: 查询中介黑名单列表..."
LIST_RESPONSE=$(curl -s -X GET "${BASE_URL}/dpc/intermediary/list?pageNum=1&pageSize=10" \
  -H "Authorization: Bearer ${TOKEN}")

echo "列表响应: ${LIST_RESPONSE}" | head -c 500
echo ""
echo ""

# 步骤3: 获取个人中介详情
echo "步骤3: 测试个人中介详情查询..."
PERSON_ID_RESPONSE=$(curl -s -X GET "${BASE_URL}/dpc/intermediary/1" \
  -H "Authorization: Bearer ${TOKEN}")

echo "个人中介详情响应:"
echo "${PERSON_ID_RESPONSE}" | python -m json.tool 2>/dev/null || echo "${PERSON_ID_RESPONSE}"
echo ""

# 检查关键字段是否存在
if echo "${PERSON_ID_RESPONSE}" | grep -q '"intermediaryType":"1"'; then
  echo "✅ 个人中介类型字段正确"
else
  echo "❌ 个人中介类型字段缺失或错误"
fi

if echo "${PERSON_ID_RESPONSE}" | grep -q '"name"'; then
  echo "✅ 姓名字段存在"
else
  echo "❌ 姓名字段缺失"
fi

echo ""

# 步骤4: 获取机构中介详情（假设 ID 为 2）
echo "步骤4: 测试机构中介详情查询..."
ENTITY_ID_RESPONSE=$(curl -s -X GET "${BASE_URL}/dpc/intermediary/2" \
  -H "Authorization: Bearer ${TOKEN}")

echo "机构中介详情响应:"
echo "${ENTITY_ID_RESPONSE}" | python -m json.tool 2>/dev/null || echo "${ENTITY_ID_RESPONSE}"
echo ""

# 检查关键字段是否存在
if echo "${ENTITY_ID_RESPONSE}" | grep -q '"intermediaryType":"2"'; then
  echo "✅ 机构中介类型字段正确"
else
  echo "❌ 机构中介类型字段缺失或错误"
fi

if echo "${ENTITY_ID_RESPONSE}" | grep -q '"name"'; then
  echo "✅ 机构名称字段存在"
else
  echo "❌ 机构名称字段缺失"
fi

echo ""
echo "=========================================="
echo "测试完成"
echo "=========================================="
echo ""
echo "验证要点："
echo "1. 确保后端返回的数据包含 intermediaryType 字段"
echo "2. 确保返回的数据结构与前端表单字段匹配"
echo "3. 个人中介 (intermediaryType='1') 应包含 indivXXX 字段"
echo "4. 机构中介 (intermediaryType='2') 应包含 corpXXX 字段"
echo ""
echo "如需测试特定 ID，请修改脚本中的 ID 值"
