// 员工认证信息（用于 UserInfo 中的嵌套字段）
export interface ICertificationInfo {
  id: number
  real_name: string
  phone: string
  snapshot: string[]
  position?: string
  department_id?: number
  department?: IDepartmentItem
  status: number       // 0 待审核 1 通过 2 拒绝
  approver_id?: number
  comment: string
}

// 用户信息（扩展字段，支持多角色）
export interface IUserInfo {
  id: number
  openid: string
  nickname: string
  real_name: string
  phone: string
  phone_verified: boolean
  avatar_url: string
  face_image_url: string
  system_role_id: number
  status: number
  created_at: string
  updated_at: string
  role?: string
  role_name?: string
  role_codes?: string[]       // 所有角色编码列表
  roles?: IRoleBrief[]        // 所有角色简要信息
  is_formal_employee?: boolean
  department_name?: string
  position?: string
  certification?: ICertificationInfo  // 员工认证信息
}

// 角色简要信息
export interface IRoleBrief {
  id: number
  code: string
  name: string
}

// 员工信息
export interface IEmployee {
  id: number
  user_id: number
  name: string
  phone: string
  department: string
  is_leader: boolean
  employee_type: string  // employee / department_leader / hr_leader / boss
  parent_id: number
  status: number
}

// 访客信息
export interface IVisitor {
  id: number
  user_id: number
  company: string
  name: string
  phone: string
  gender: number
  id_type: string
  id_number: string
  license_plate: string
  face_verified: boolean
  phone_verified: boolean
}

// 预约区域
export interface IAppointmentArea {
  id?: number
  appointment_id?: number
  area: string
}

// 携带物品
export interface IGoods {
  id?: number
  appointment_id?: number
  name: string
  quantity: number
  model: string
  remark: string
}

// 预约信息
export interface IAppointment {
  id: number
  creator_user_id: number
  creator?: IUserInfo
  visit_user_id: number
  visit_type: string
  visit_start_time: string
  visit_end_time: string
  status: number           // 0审核中 1通过 2拒绝 3取消
  remark: string
  visit_purpose?: string
  visitor_company?: string
  qr_code_url: string
  visitor_info: string     // JSON: 所有访客信息
  creator_info: string     // JSON: 创建人信息 {id, name}
  employee_info: string    // JSON: 被访人信息 {id, name, phone, department}
  goods_info: string       // JSON: 物品列表
  areas_info: string       // JSON: 区域列表
  vehicle_info: string     // JSON: 车辆信息 {license_plates: [...]}
  created_at: string
  updated_at: string
}

// 预约详情响应（与列表响应格式不同，JSON 字段已解析为对象/数组）
export interface IAppointmentDetail {
  id: number
  creator_user_id: number
  visit_user_id: number
  visit_type: string
  visit_start_time: string
  visit_end_time: string
  status: number
  remark: string
  comment?: string
  visit_purpose?: string
  visitor_company?: string
  qr_code_url: string
  visitors: {
    name: string
    phone: string
    company: string
    gender: number
    id_type?: string
    id_number?: string
  }[]
  creator_info: { id: number; name: string }
  employee_info: { id: number; name: string; phone: string; department: string }
  goods: { name: string; quantity: number; model: string; remark: string }[]
  areas: string[]
  vehicles: { brand?: string; plate: string; color?: string }[]
  created_at: string
  updated_at: string
}

// 访问记录
export interface IVisitRecord {
  id: number
  appointment_id: number
  appointment?: IAppointment
  guard_id: number
  check_type: number // 1 进场 2 出场
  check_time: string
  is_valid: boolean
  remark: string
}

// 邀请
export interface IInvitation {
  id: number
  employee_id: number
  employee?: IEmployee
  invite_code: string
  visitor_name: string
  visitor_phone: string
  visit_type: string
  visit_area: string
  valid_from: string
  valid_until: string
  max_use_count: number
  used_count: number
  is_active: boolean
}

// 系统角色
export interface ISystemRole {
  id: number
  code: string
  name: string
  description: string
  is_system: boolean
}

// 系统模块
export interface ISystemModule {
  id: number
  code: string
  name: string
  description: string
  status: number
}

// 权限
export interface IPermission {
  id: number
  role: string
  module_code: string
  resource: string
  action: string
}

// 角色申请
export interface IRoleApplication {
  id: number
  user_id: number
  user?: IUserInfo
  apply_role: number
  apply_role_info?: ISystemRole
  reason: string
  status: number
  approver_id: number
  created_at: string
}

// 登录响应
export interface ILoginRes {
  token: string
  user_info: IUserInfo
}

// 分页参数
export interface IPageParams {
  page: number
  page_size: number
  [key: string]: any
}

// 分页响应
export interface IPageResult<T> {
  list: T[]
  total: number
  page: number
  page_size: number
}

// 今日预约统计
export interface ITodayStats {
  total: number
  checked_in: number
  checked_out: number
}

// Banner 轮播图
export interface IBanner {
  image_url: string
  jump_path: string
  is_jump: boolean
}

// Banner 管理（含 id、sort、is_valid）
export interface IBannerAdmin {
  id: number
  image_url: string
  jump_path: string
  is_jump: boolean
  sort: number
  is_valid: boolean
}

// 系统通知
export interface INotice {
  id: number
  title: string
  content: string
}

// 通知管理（含 sort、is_valid）
export interface INoticeAdmin {
  id: number
  title: string
  content: string
  sort: number
  is_valid: boolean
}

// 用户管理视图
export interface IUserAdmin {
  id: number
  openid: string
  nickname: string
  real_name: string
  phone: string
  avatar_url: string
  status: number
  system_role_id: number
  roles: IRoleBrief[]
  is_super_admin: boolean
  created_at: string
}

// 部门视图
export interface IDepartment {
  name: string
  employees: IEmployeeBrief[]
}

export interface IEmployeeBrief {
  id: number
  user_id: number
  name: string
  phone: string
  is_leader: boolean
}

// ==================== 审批流程 ====================

// 审批模板
export interface IApprovalTemplate {
  id: number
  name: string
  code: string
  description: string
  is_active: boolean
}

// 审批节点
export interface IApprovalNode {
  id: number
  template_id: number
  node_name: string
  node_type: string
  node_order: number
  is_required: boolean
}

// 审批实例
export interface IApprovalInstance {
  id: number
  template_id: number
  template?: IApprovalTemplate
  business_type: string
  business_id: number
  current_node_id: number
  status: number       // 0审批中 1已通过 2已拒绝 3已撤销
  starter_id: number
  finished_at: string
  created_at: string
}

// 审批记录
export interface IApprovalRecord {
  id: number
  instance_id: number
  node_id: number
  approver_id: number
  approver?: IUserInfo
  status: number       // 0待审批 1通过 2拒绝
  comment: string
  approved_at: string
  node_order: number
  node_name: string
  created_at: string
}

// 审批实例详情
export interface IApprovalDetail {
  instance: IApprovalInstance
  records: IApprovalRecord[]
}

// ==================== 员工认证 ====================

// 员工认证申请
export interface IEmployeeCertification {
  id: number
  user_id: number
  user?: IUserInfo
  real_name: string
  phone: string
  role_type?: string
  snapshot: string[]
  position?: string
  department_id?: number
  department?: IDepartmentItem
  status: number       // 0待审核 1通过 2拒绝
  approver_id?: number
  approver?: IUserInfo
  comment: string
  created_at: string
  updated_at?: string
}

// 物品模板
export interface IGoodsTemplate {
  id?: number
  user_id?: number
  name: string
  quantity: number
  model: string
  remark: string
  created_at?: string
}

// 访客历史视图
export interface IVisitorVO {
  id: number
  company: string
  name: string
  phone: string
  gender: number
  id_type: string
  id_number: string
  license_plate: string
}

// 部门（扁平列表）
export interface IDepartmentItem {
  id: number
  name: string
  parent_id?: number
  leader_name: string
  leader_phone: string
  sort: number
  status: number
  created_at: string
}

// 部门树节点
export interface IDepartmentTreeNode {
  id: number
  name: string
  parent_id?: number
  leader_name: string
  leader_phone: string
  sort: number
  status: number
  children: IDepartmentTreeNode[]
}
