import { get, post, put, del } from './request'

export interface TaskParams {
  taskNo: string
  taskName?: string
  projectId: number | string
  taskType: string
  planDate: string
  planEndDate?: string
  address: string
  status?: string
  serviceContent?: string
  faultLocation?: string
  constructionType?: string
  constructionFacility?: string
  serviceMethod?: string
  waterDistance?: string
  assistantInfo?: string
  estimatedWorkload?: string
  estimatedDuration?: string
  acceptanceRequirement?: string
  acceptanceMethod?: string
  remark?: string
  extraData?: string
  // 项目与服务点信息
  servicePointId?: number
  servicePointName?: string
  contacts?: { name: string; phone: string }[]
  // 结算信息
  settlementType?: string
  settlementAmount?: number
  settlementMethod?: string
  // 扩展字段
  expectedArrivalTime?: string
  outletNormal?: number
  hasPreviousWork?: number
  toolRequirement?: string
  // 兼容/历史字段（发布新任务时不再使用）
  urgencyLevel?: string
  faultType?: string
  customerWords?: string
  firstOccurrenceTime?: string
  recurrenceStatus?: string
  impactScope?: string
  latestDeadline?: string
  acceptanceStandard?: string
  blockageCount?: number
  dredgingVolume?: number
  constructionInstruction?: string
  riskTip?: string
  dispatchResult?: string
  attentionPoint?: string
  workOrderNo?: string
  partyAContacts?: { name: string; phone: string; position?: string }[]
  // 前端表单别名，提交时映射到对应字段
  estimatedQuantity?: string
  estimatedHours?: string
  acceptanceReq?: string
  [key: string]: any
}

export function getTaskList(pageNum = 1, pageSize = 10) {
  return get('/task/list', { pageNum, pageSize })
}

export function getMyTasks(planDate?: string) {
  return get<any[]>('/task/my', { planDate })
}

export function getTaskDetail(taskId: number) {
  return get(`/task/${taskId}`)
}

export function getTasksByProjectId(projectId: number | string) {
  return get<any[]>(`/task/project/${projectId}`)
}

export function createTask(data: TaskParams) {
  return post('/task', data)
}

export function updateTask(taskId: number, data: Partial<TaskParams>) {
  return put('/task', { taskId, ...data })
}

export function deleteTask(taskId: number) {
  return del(`/task/${taskId}`)
}

export interface ExecuteSopStepData {
  taskId: number
  stepCode: string
  action?: string
  remark?: string
  operatorId?: number
  operatorName?: string
  photos?: string[]
  /** 步骤业务数据 JSON 字符串（与 admin 前端字段对齐） */
  data?: string
}

export function executeSopStep(data: ExecuteSopStepData) {
  return post('/sop/execute-step', data)
}

export function getSopSteps(taskId: number) {
  return get(`/sop/steps/${taskId}`)
}

export function urgeTask(taskId: number, urgeReason: string, operatorId: number, operatorName: string) {
  return post(`/task/${taskId}/urge`, { taskId, urgeReason, operatorId, operatorName })
}

export function cancelTask(taskId: number, cancelReason: string, operatorId: number, operatorName: string) {
  return put(`/task/${taskId}/cancel`, { taskId, cancelReason, operatorId, operatorName })
}

export function completeTask(taskId: number, data: { taskId: number; remark?: string; operatorId?: number; operatorName?: string }) {
  return post(`/task/${taskId}/complete`, data)
}

export function getTaskLogs(taskId: number) {
  return get(`/task/${taskId}/logs`)
}

export interface TaskLogData {
  operation?: string
  operationType?: string
  remark?: string
  operatorId?: number
  operatorName?: string
}

export function addTaskLog(taskId: number, data: TaskLogData) {
  return post(`/task/${taskId}/log`, data)
}

export interface AuditParams {
  remark?: string
  auditUserId?: number | string
  auditUserName?: string
}

export function approveTask(taskId: number, data: AuditParams) {
  return post(`/task/audit/${taskId}/approve`, data)
}

export function rejectTaskAudit(taskId: number, data: AuditParams) {
  return post(`/task/audit/${taskId}/reject`, data)
}

export function dispatchTask(taskId: number, data: AuditParams & { teamId: number; vehicleId: number }) {
  return post(`/task/audit/${taskId}/dispatch`, data)
}
