export type UserRole = 'sales' | 'dispatch' | 'construction'
export type UserStatus = 'active' | 'inactive'

export interface User {
  id: string
  name: string
  phone: string
  avatar?: string
  role: UserRole
  department?: string
  deptId?: number
  employeeNo?: string
  email?: string
  hireDate?: string
  team?: string
  teamId?: number
  dutyStatus?: string
  skillLevel?: string
  certifications?: string
  tags?: string
  skillTags?: string
  currentTaskId?: number
  postNames?: string
  status: UserStatus
}

/** 项目状态（后台 project_status 字典） */
export type ProjectStatus = 'pending' | 'in_progress' | 'completed' | 'cancelled'
/** 合作类型（后台 cooperation_type 字典） */
export type CooperationType = 'annual' | 'single' | 'quarter' | 'monthly'
/** 设施类型（后台 facility_type 字典） */
export type FacilityType = 'septic_tank' | 'direct_pump' | 'grease_pipe' | 'pvc_pipe'

export interface Contact {
  name: string
  phone: string
  role: string
}

/** 位置联系人（后台 location_contacts JSON 中的单项） */
export interface LocationContact {
  name: string
  phone: string
}

/** 项目服务点（后台 service_points JSON 中的单项） */
export interface ServicePoint {
  id?: number
  name: string
  address?: string
  contact?: string
  phone?: string
  /** 对应服务项（与后台 ProjectForm 对齐） */
  serviceItem?: string
  /** @deprecated 旧字段，请使用 serviceItem */
  serviceType?: string
}

/**
 * 项目（对齐后台 BizProject 实体）
 * 注意：列表/详情返回的主键为 projectId；facilityTypes、servicePoints、locationContacts 后端以 JSON 字符串存储。
 */
export interface Project {
  projectId: number | string
  projectName: string
  projectCode?: string
  cooperationType: CooperationType
  facilityTypes?: string | FacilityType[]
  address?: string
  province?: string
  city?: string
  district?: string
  street?: string
  detailAddress?: string
  latitude?: string
  longitude?: string
  /** 主联系人（通常由 locationContacts 第一个联系人或客户联系人推导） */
  primaryContactName?: string
  primaryContactPhone?: string
  /** 位置联系人 JSON 字符串或对象数组 */
  locationContacts?: string | LocationContact[]
  projectDescription?: string
  contractAmount?: number
  serviceStartDate?: string
  serviceEndDate?: string
  servicePoints?: string | ServicePoint[]
  customerId?: number | string
  customerName?: string
  customerPhone?: string
  contractId?: number | string
  contractNo?: string
  paymentMethod?: PaymentMethod
  contractStatus?: ContractStatus
  status: ProjectStatus
  remark?: string
  createTime?: string
  updateTime?: string
}

/** 后端分页结果（PageResult） */
export interface PageResult<T> {
  rows: T[]
  total: number
  current: number
  size: number
  pages: number
}

/** 客户状态：1 合作中 / 0 已停用 / 2 潜在客户（后端硬编码） */
export type CustomerStatus = 0 | 1 | 2

export interface Customer {
  id: number | string
  customerName: string
  contactName?: string
  phone?: string
  address?: string
  province?: string
  city?: string
  district?: string
  street?: string
  detailAddress?: string
  latitude?: string
  longitude?: string
  status: CustomerStatus
  remark?: string
  createTime?: string
  updateTime?: string
}

export type ContractStatus = 'draft' | 'pending' | 'active' | 'expired' | 'terminated'
export type PaymentMethod = 'lump_sum' | 'installment'

export interface Contract {
  id: number | string
  contractNo: string
  customerId: number | string
  customerName?: string
  projectId?: number | string
  contractAmount: number
  paymentMethod: PaymentMethod
  signDate?: string
  servicePeriod?: string
  status: ContractStatus
  remark?: string
  createTime?: string
  updateTime?: string
}

export type TaskType = 'regular' | 'emergency'
export type TaskStatus =
  // 后台标准状态
  | 'pending'
  | 'auditing'
  | 'approved'
  | 'assigned'
  | 'in_progress'
  | 'completed'
  | 'cancelled'
  | 'rejected'
  // 历史/兼容状态（逐步迁移到后台标准）
  | 'confirmed'
  | 'scheduled'
  | 'departed'
  | 'arrived'
  | 'constructing'
  | 'inspecting'
export type UrgentLevel = 'high' | 'normal' | 'very_urgent' | 'urgent'

export interface TimelineRecord {
  time: string
  action: string
  operator: string
  role: UserRole
}

export interface Task {
  /** 后台 taskId（优先使用） */
  taskId?: string | number
  /** 兼容字段，等同于 taskId */
  id?: string
  taskNo?: string
  taskName?: string
  projectId: string
  projectName: string
  coopType?: string
  /** 后台 taskType */
  taskType?: string
  /** 兼容字段，等同于 taskType */
  type?: TaskType
  status: TaskStatus
  /** 后台 urgencyLevel */
  urgencyLevel?: string
  /** 兼容字段 */
  urgent?: UrgentLevel
  serviceType?: string
  facilityType?: string
  serviceMode?: string
  servicePointId?: number
  servicePointName?: string
  faultLocation?: string
  faultType?: string
  faultDesc?: string
  customerWords?: string
  address?: string
  geoLocation?: string
  vehicle?: string
  vehicleNo?: string
  team?: string
  /** 调度班组名称（后台 teamName） */
  teamName?: string
  /** 调度车辆车牌号（后台 plateNo） */
  plateNo?: string
  assignee?: string
  staffName?: string
  applicant?: string
  applicantPhone?: string
  applyTime?: string
  rejectReason?: string
  acceptType?: string
  acceptRequirement?: string
  /** 预计作业量，对应后台 estimatedWorkload */
  estimatedQuantity?: string
  estimatedWorkload?: string
  /** 预计工时，对应后台 estimatedDuration */
  estimatedHours?: string
  estimatedDuration?: string
  /** 施工助力信息，对应后台 assistantInfo */
  constructionHelp?: string
  assistantInfo?: string
  /** 验收要求，对应后台 acceptanceRequirement */
  acceptanceReq?: string
  waterSource?: string
  septicTank?: string
  previousConstruction?: string
  customerName?: string
  customerPhone?: string
  primaryContactName?: string
  primaryContactPhone?: string
  partyAContacts?: { name: string; phone: string }[]
  contacts?: { name: string; phone: string; role?: string }[]
  scheduledDate?: string
  scheduleDate?: string
  scheduleTime?: string
  serviceDate?: string
  serviceTime?: string
  planDate?: string
  planEndDate?: string
  planTimeRange?: string
  /** SOP 当前步骤 0-8 */
  currentStep?: number
  /** SOP 状态描述 */
  sopStatus?: string
  departureTime?: string
  arrivalTime?: string
  completionTime?: string
  /** 兼容字段，分别对应 departureTime / arrivalTime / completionTime */
  departTime?: string
  arriveTime?: string
  constructTime?: string
  completeTime?: string
  cleanTime?: string
  inspectTime?: string
  createTime: string
  updateTime?: string
  timeline?: TimelineRecord[]
  /** 后台操作日志 */
  logList?: any[]
  location?: { lat: number; lng: number }
  mediaList?: { type: 'image' | 'video'; url: string }[]
  /** 施工设施 */
  constructionFacility?: string
  /** 服务方式 */
  serviceMethod?: string
  /** 取水距离 */
  waterDistance?: string
  /** 化粪池排口是否正常 1-是 0-否 */
  outletNormal?: number
  /** 是否有前期施工关联 1-是 0-否 */
  hasPreviousWork?: number
  /** 验收方式 */
  acceptanceMethod?: string
  /** 建议出车，对应后台 toolRequirement */
  suggestedVehicles?: string[]
  toolRequirement?: string
  /** 结算类型 contract-合同内 extra-额外 */
  settlementType?: 'contract' | 'extra'
  /** 结算方式 */
  settlementMethod?: string
  /** 结算金额 */
  settlementAmount?: number
  /** 扩展字段 JSON 字符串 */
  extraData?: string
  remark?: string
}

export type ScheduleStatus = 'scheduled' | 'completed' | 'cancelled'

export interface Schedule {
  id: string
  date: string
  team: string
  taskId: string
  taskName: string
  status: ScheduleStatus
}

export type VehicleStatus = 'available' | 'in_use' | 'maintenance'

export interface Vehicle {
  id: string
  plateNumber: string
  type: string
  status: VehicleStatus
  driver?: string
  driverPhone?: string
  size?: string
  loadCapacity?: string
  capacity?: string
  purchaseDate?: string
  mileage?: number
}

export type PersonnelStatus = 'idle' | 'working' | 'rest' | 'standby'

export interface Personnel {
  id: string
  name: string
  phone: string
  team: string
  status: PersonnelStatus
  position?: string
  skillLevel?: string
  qualifications?: string[]
  workYears?: number
}

export type NotificationType = 'system' | 'task' | 'audit'

export interface Notification {
  id: string
  title: string
  content: string
  type: NotificationType
  read: boolean
  createTime: string
}

export interface Knowledge {
  id: string
  title: string
  summary: string
  content: string
  category: string
  createTime: string
}

export interface FAQ {
  id: string
  question: string
  answer: string
  category: string
}

export interface NavItem {
  path: string
  icon: string
  activeIcon?: string
  text?: string
  name?: string
}

export interface ToastOptions {
  title: string
  icon?: 'success' | 'loading' | 'none' | 'error'
  duration?: number
  mask?: boolean
}

export interface ModalOptions {
  title?: string
  content: string
  showCancel?: boolean
  cancelText?: string
  confirmText?: string
  success?: (res: { confirm: boolean; cancel: boolean }) => void
}
