# 员工信息管理 API 测试脚本
# 需要使用 UTF-8 with BOM 编码保存
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

$BASE_URL = "http://localhost:8080"

Write-Host "========================================"  -ForegroundColor Cyan
Write-Host "员工信息管理 API 测试脚本"  -ForegroundColor Cyan
Write-Host "========================================"  -ForegroundColor Cyan
Write-Host ""

# 1. 登录获取 Token
Write-Host "[1] 正在登录..." -ForegroundColor Yellow
$loginBody = @{
    username = "admin"
    password = "admin123"
} | ConvertTo-Json

$loginResponse = Invoke-RestMethod -Uri "$BASE_URL/login/test" -Method Post -ContentType "application/json; charset=utf-8" -Body ([System.Text.Encoding]::UTF8.GetBytes($loginBody))
$TOKEN = $loginResponse.token

if ([string]::IsNullOrEmpty($TOKEN)) {
    Write-Host "[错误] 获取 Token 失败，请检查登录接口" -ForegroundColor Red
    Read-Host "按回车键退出"
    exit 1
}

Write-Host "登录成功，Token: $TOKEN" -ForegroundColor Green
Write-Host ""

# 2. 测试查询员工列表
Write-Host "[2] 测试查询员工列表..." -ForegroundColor Yellow
$headers = @{
    "Authorization" = "Bearer $TOKEN"
}
$response = Invoke-RestMethod -Uri "$BASE_URL/dpc/employee/list" -Method Get -Headers $headers
Write-Host ($response | ConvertTo-Json -Depth 10)
Write-Host ""

# 3. 测试新增员工
Write-Host "[3] 测试新增员工..." -ForegroundColor Yellow
$addBody = @{
    name = "测试员工"
    tellerNo = "TEST001"
    orgNo = "1001"
    idCard = "110101199001011234"
    phone = "13800138000"
    status = "0"
    relatives = @(
        @{
            relativeName = "李四"
            relativeIdCard = "110101199001011235"
            relativePhone = "13800138001"
            relationship = "配偶"
        }
    )
} | ConvertTo-Json -Depth 10

$headers = @{
    "Authorization" = "Bearer $TOKEN"
    "Content-Type" = "application/json; charset=utf-8"
}
$bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($addBody)
$response = Invoke-RestMethod -Uri "$BASE_URL/dpc/employee" -Method Post -Headers $headers -Body $bodyBytes
Write-Host ($response | ConvertTo-Json -Depth 10)
Write-Host ""

# 4. 测试查询员工详情
Write-Host "[4] 测试查询员工详情..." -ForegroundColor Yellow
$headers = @{
    "Authorization" = "Bearer $TOKEN"
}
$response = Invoke-RestMethod -Uri "$BASE_URL/dpc/employee/1" -Method Get -Headers $headers
Write-Host ($response | ConvertTo-Json -Depth 10)
Write-Host ""

# 5. 测试编辑员工
Write-Host "[5] 测试编辑员工..." -ForegroundColor Yellow
$editBody = @{
    employeeId = 1
    name = "测试员工-修改"
    tellerNo = "TEST001"
    orgNo = "1001"
    idCard = "110101199001011234"
    phone = "13800138000"
    status = "0"
    relatives = @(
        @{
            relativeName = "王五"
            relativeIdCard = "110101199001011236"
            relativePhone = "13800138002"
            relationship = "子女"
        }
    )
} | ConvertTo-Json -Depth 10

$headers = @{
    "Authorization" = "Bearer $TOKEN"
    "Content-Type" = "application/json; charset=utf-8"
}
$bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($editBody)
$response = Invoke-RestMethod -Uri "$BASE_URL/dpc/employee" -Method Put -Headers $headers -Body $bodyBytes
Write-Host ($response | ConvertTo-Json -Depth 10)
Write-Host ""

# 6. 测试删除员工
Write-Host "[6] 测试删除员工..." -ForegroundColor Yellow
$headers = @{
    "Authorization" = "Bearer $TOKEN"
}
$response = Invoke-RestMethod -Uri "$BASE_URL/dpc/employee/1" -Method Delete -Headers $headers
Write-Host ($response | ConvertTo-Json -Depth 10)
Write-Host ""

Write-Host "========================================"  -ForegroundColor Cyan
Write-Host "测试完成"  -ForegroundColor Cyan
Write-Host "========================================"  -ForegroundColor Cyan
Read-Host "按回车键退出"
