#!/bin/bash

# 中介黑名单详细信息接口测试脚本
# 用于测试点击修改按钮时,后端接口是否正确返回中介类型信息

BASE_URL="http://localhost:8080"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo "========================================"
echo "中介黑名单详细信息接口测试"
echo "========================================"
echo ""

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

TOKEN=$(echo $LOGIN_RESPONSE | grep -o '"token":"[^"]*"' | sed 's/"token":"//' | sed 's/"//')

if [ -z "$TOKEN" ]; then
    echo -e "${RED}登录失败,无法获取token${NC}"
    exit 1
fi

echo -e "${GREEN}登录成功,获取到token${NC}"
echo ""

# 2. 查询列表获取一个中介ID
echo -e "${YELLOW}2. 查询中介列表,获取测试数据...${NC}"
LIST_RESPONSE=$(curl -s -X GET "${BASE_URL}/dpc/intermediary/list?pageNum=1&pageSize=10" \
  -H "Authorization: Bearer ${TOKEN}")

# 提取第一个中介ID
INTERMEDIARY_ID=$(echo $LIST_RESPONSE | grep -o '"intermediaryId":[0-9]*' | head -1 | sed 's/"intermediaryId"://')

if [ -z "$INTERMEDIARY_ID" ]; then
    echo -e "${RED}未找到中介数据${NC}"
    exit 1
fi

echo -e "${GREEN}找到中介ID: ${INTERMEDIARY_ID}${NC}"
echo ""

# 3. 测试获取详细信息
echo -e "${YELLOW}3. 获取中介详细信息 (ID: ${INTERMEDIARY_ID})...${NC}"
DETAIL_RESPONSE=$(curl -s -X GET "${BASE_URL}/dpc/intermediary/${INTERMEDIARY_ID}" \
  -H "Authorization: Bearer ${TOKEN}")

echo "响应内容:"
echo "$DETAIL_RESPONSE" | python -m json.tool 2>/dev/null || echo "$DETAIL_RESPONSE"
echo ""

# 4. 检查是否包含intermediaryType字段
echo -e "${YELLOW}4. 验证返回数据是否包含中介类型字段...${NC}"
if echo "$DETAIL_RESPONSE" | grep -q '"intermediaryType"'; then
    INTERMEDIARY_TYPE=$(echo $DETAIL_RESPONSE | grep -o '"intermediaryType":"[^"]*"' | sed 's/"intermediaryType":"//' | sed 's/"//')

    if [ "$INTERMEDIARY_TYPE" = "1" ]; then
        echo -e "${GREEN}✓ 包含中介类型字段,类型为: 个人 (1)${NC}"
    elif [ "$INTERMEDIARY_TYPE" = "2" ]; then
        echo -e "${GREEN}✓ 包含中介类型字段,类型为: 机构 (2)${NC}"
    else
        echo -e "${YELLOW}⚠ 包含中介类型字段,但值为: ${INTERMEDIARY_TYPE}${NC}"
    fi
else
    echo -e "${RED}✗ 缺少中介类型字段 (intermediaryType)${NC}"
    echo ""
    echo "这是导致前端表单无法正确反显的根本原因!"
    echo "前端EditDialog组件需要根据intermediaryType判断显示个人还是机构表单"
fi
echo ""

# 5. 检查其他关键字段
echo -e "${YELLOW}5. 验证其他关键字段...${NC}"
check_field() {
    FIELD_NAME=$1
    if echo "$DETAIL_RESPONSE" | grep -q "\"${FIELD_NAME}\""; then
        echo -e "${GREEN}  ✓ ${FIELD_NAME}: 存在${NC}"
    else
        echo -e "${RED}  ✗ ${FIELD_NAME}: 缺失${NC}"
    fi
}

check_field "intermediaryId"
check_field "name"
check_field "certificateNo"
check_field "status"
check_field "remark"
echo ""

# 6. 根据中介类型检查特定字段
if [ "$INTERMEDIARY_TYPE" = "1" ]; then
    echo -e "${YELLOW}6. 验证个人类型专属字段...${NC}"
    check_field "indivType"
    check_field "indivGender"
    check_field "indivCertType"
elif [ "$INTERMEDIARY_TYPE" = "2" ]; then
    echo -e "${YELLOW}6. 验证机构类型专属字段...${NC}"
    check_field "corpCreditCode"
    check_field "corpType"
    check_field "corpNature"
fi
echo ""

echo "========================================"
echo "测试完成"
echo "========================================"
