import test from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'

const source = fs.readFileSync(new URL('../src/views/subSystem/waterSupply/components/WaterDevicePage.vue', import.meta.url), 'utf8')

test('device dispatch uses a department tree and paged single-assignee table', () => {
  assert.match(source, /<el-tree[\s\S]*@node-click="handleDeptClick"/)
  assert.match(source, /搜索用户名、昵称或手机号/)
  assert.match(source, /<el-pagination[\s\S]*assigneeTotal/)
  assert.match(source, /selectSingleAssignee\(\[row\]\)/)
  assert.match(source, /:disabled="!assigneeDraft"/)
})

test('assignee loading failure remains in place with a retry action', () => {
  assert.match(source, /userLoadError/)
  assert.match(source, /责任人列表加载失败，请点击重试/)
  assert.match(source, /@click="retryAssigneeLoading"/)
})

test('assignee loading failure preserves existing rows and total', () => {
  const loadFn = source.match(/async function loadAssigneeUsers\(\)[\s\S]*?\nfunction searchAssigneeUsers/)?.[0] || ''
  assert.doesNotMatch(loadFn, /users\.value = \[\]/)
  assert.doesNotMatch(loadFn, /assigneeTotal\.value = 0/)
  assert.match(loadFn, /userLoadError\.value = '责任人列表加载失败，请点击重试'/)
})

test('retry path force reloads the department tree before users', () => {
  assert.match(source, /async function loadDeptTree\(force = false\)/)
  assert.match(source, /async function retryAssigneeLoading\(\) \{ const deptLoaded = await loadDeptTree\(true\); if \(!deptLoaded\) return; await loadAssigneeUsers\(\) \}/)
})

test('assignee keyword search resets to the first result page', () => {
  assert.match(source, /@keyup\.enter="searchAssigneeUsers"/)
  assert.match(source, /@click="searchAssigneeUsers"/)
  assert.match(source, /function searchAssigneeUsers\(\) \{ assigneePageNum\.value = 1; loadAssigneeUsers\(\) \}/)
})

test('assignee pagination is server-backed', () => {
  assert.match(source, /buildAssigneeQuery\(\{ deptId: assigneeDeptId\.value, keyword: assigneeKeyword\.value, pageNum: assigneePageNum\.value, pageSize: assigneePageSize\.value \}\)/)
  assert.match(source, /@current-change="loadAssigneeUsers"/)
  assert.match(source, /@size-change="handleAssigneeSizeChange"/)
})

test('uses draft assignee state until confirmation in the split dispatch dialog', () => {
  assert.match(source, /const assigneeDraft = ref\(null\)/)
  assert.match(source, /function chooseAssignee\(row\) \{ assigneeDraft\.value = selectSingleAssignee\(\[row\]\) \}/)
  assert.match(source, /function confirmAssignee\(\) \{ selectedAssignee\.value = assigneeDraft\.value; dispatchForm\.value\.receiveUserId = assigneeDraft\.value\?\.userId \}/)
  assert.match(source, /<div class="dispatch-layout">/)
  assert.match(source, /<el-form class="dispatch-form"/)
  assert.match(source, /:current-row-key="assigneeDraft\?\.userId"/)
})

test('opens the split dispatch dialog with a fresh assignee query', () => {
  assert.match(source, /async function openOrder\(row\)/)
  assert.match(source, /assigneeDeptId\.value = undefined/)
  assert.match(source, /assigneeKeyword\.value = ''/)
  assert.match(source, /dispatchVisible\.value = true/)
  assert.match(source, /await retryAssigneeLoading\(\)/)
})

test('dispatch form exposes fixed fault type and editable priority', () => {
  assert.match(source, /<el-form-item label="工单类型"><span>1-故障维修<\/span><\/el-form-item>/)
  assert.match(source, /<el-form-item label="优先级" required><el-select v-model="dispatchForm\.orderLevel"/)
  assert.match(source, /<el-option label="紧急" :value="1" \/>/)
})

test('dispatch requires a valid server order summary and preserves failure state', () => {
  assert.match(source, /const summary = normalizeDispatchOrderResponse\(response\)/)
  assert.match(source, /服务端未返回有效的待接单工单/)
  assert.match(source, /responseData\?\.msg \|\| responseData\?\.message \|\| error\?\.message/)
})

test('does not expose filters unsupported by the device GIS endpoint', () => {
  assert.match(source, /<el-form-item v-if="view !== 'gis'" label="设备名称"/)
  assert.match(source, /<el-form-item v-if="view !== 'gis'" label="设备编码"/)
  assert.match(source, /<el-form-item v-if="view === 'ledger'" label="报警状态"/)
})
