/**
 * 通用类型定义
 */

/** API 响应结构 */
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
  timestamp?: number
}

/** 分页参数 */
export interface PaginationParams {
  page?: number
  page_size?: number
  sort_by?: string
  sort_order?: 'asc' | 'desc'
}

/** 分页响应 */
export interface PaginationResponse<T> {
  total: number
  page: number
  page_size: number
  total_pages: number
  items: T[]
}

/** 用户信息 */
export interface UserInfo {
  id: number
  username: string
  realName: string
  email?: string
  phone?: string
  avatar?: string
  status: 'active' | 'disabled' | 'locked'
  isAdmin: boolean
  roles?: Role[]
  permissions?: string[]
  lastLoginAt?: string
  createdAt: string
}

/** 角色 */
export interface Role {
  id: number
  roleCode: string
  roleName: string
  description?: string
  status: string
  sortOrder: number
  userCount?: number
  permissions?: Permission[]
}

/** 权限 */
export interface Permission {
  id: number
  moduleName?: string
  permissionName: string
  permissionCode: string
  children?: Permission[]
}

/** 资产状态 */
export type AssetStatus =
  | 'pending'
  | 'in_stock'
  | 'in_use'
  | 'transferring'
  | 'maintenance'
  | 'pending_scrap'
  | 'scrapped'
  | 'lost'

/** 资产实体 */
export interface Asset {
  id: number
  assetCode: string
  assetName: string
  deviceTypeId: number
  deviceType: {
    id: number
    typeName: string
    category?: string
  }
  brandId?: number
  brand?: {
    id: number
    brandName: string
  }
  model?: string
  serialNumber?: string
  supplierId?: number
  supplier?: {
    id: number
    supplierName: string
  }
  organizationId: number
  organization: {
    id: number
    orgName: string
  }
  location?: string
  status: AssetStatus
  statusName?: string
  purchaseDate?: string
  purchasePrice?: number
  warrantyExpireDate?: string
  qrCodeUrl?: string
  dynamicAttributes: Record<string, any>
  statusHistory?: AssetStatusHistory[]
  createdAt: string
  updatedAt: string
  createdBy?: string
}

/** 资产状态历史 */
export interface AssetStatusHistory {
  id: number
  assetId: number
  oldStatus?: string
  newStatus: string
  operationType: string
  operatorName?: string
  remark?: string
  createdAt: string
}

/** 设备类型 */
export interface DeviceType {
  id: number
  typeCode: string
  typeName: string
  category: string
  description?: string
  icon?: string
  status: string
  sortOrder: number
  fieldCount?: number
}

/** 动态字段 */
export interface DynamicField {
  id: number
  fieldCode: string
  fieldName: string
  fieldType: 'text' | 'textarea' | 'number' | 'date' | 'select' | 'checkbox' | 'url' | 'email' | 'phone'
  isRequired: boolean
  placeholder?: string
  options?: Array<{ label: string; value: any }>
  validationRules?: Record<string, any>
  defaultValue?: any
  sortOrder: number
}

/** 机构网点 */
export interface Organization {
  id: number
  orgCode: string
  orgName: string
  orgType: 'province' | 'city' | 'outlet'
  parentId?: number
  treeLevel: number
  address?: string
  contactPerson?: string
  contactPhone?: string
  children?: Organization[]
}

/** 分配单 */
export interface AllocationOrder {
  id: number
  orderCode: string
  orderType: 'allocation' | 'transfer' | 'recovery' | 'maintenance' | 'scrap'
  orderTypeName: string
  title: string
  targetOrganizationId?: number
  targetOrganization?: Organization
  applicantId: number
  applicant?: UserInfo
  approvalStatus: 'pending' | 'approved' | 'rejected' | 'cancelled'
  approvalStatusName: string
  executeStatus: 'pending' | 'executing' | 'completed' | 'failed'
  assetCount: number
  remark?: string
  approvalRemark?: string
  createdAt: string
}

/** 统计概览 */
export interface StatisticsOverview {
  totalAssets: number
  totalValue: number
  assetsInStock: number
  assetsInUse: number
  assetsMaintenance: number
  assetsScrapped: number
  organizationDistribution: Array<{
    orgName: string
    count: number
    value: number
  }>
  deviceTypeDistribution: Array<{
    typeName: string
    count: number
  }>
}

/** 维修记录 */
export interface MaintenanceRecord {
  id: number
  assetId: number
  asset?: Asset
  faultDescription: string
  faultType: 'hardware' | 'software' | 'other'
  priority: 'low' | 'medium' | 'high'
  maintenanceType: 'self_repair' | 'vendor_repair'
  vendorId?: number
  status: 'pending' | 'in_progress' | 'completed' | 'closed'
  cost?: number
  completedAt?: string
  createdAt: string
}

/** 表单项配置 */
export interface FormItemConfig {
  prop: string
  label: string
  type: 'input' | 'select' | 'date' | 'number' | 'textarea'
  placeholder?: string
  options?: Array<{ label: string; value: any }>
  required?: boolean
  rules?: any[]
  span?: number
}

/** 菜单项 */
export interface MenuItem {
  path: string
  name: string
  icon?: string
  title: string
  redirect?: string
  children?: MenuItem[]
  hidden?: boolean
  meta?: {
    title: string
    icon?: string
    permissions?: string[]
  }
}
