class Login_Request {
  constructor() {
    this.password = ''
    this.username = ''
  }
  toJson() {
    return JSON.stringify(this)
  }
}

class Login_Response {
  constructor(data) {
    data = data || {}
    this.success = data.success !== undefined ? data.success : false
    this.message = data.message !== undefined ? data.message : ''
    this.code = data.code !== undefined ? data.code : 0
    this.mark = data.mark !== undefined ? data.mark : null
    this.result = data.result ? new LoginResult(data.result) : null
    this.timestamp = data.timestamp !== undefined ? data.timestamp : 0
  }

  static fromJson(json) {
    try {
      const data = typeof json === 'string' ? JSON.parse(json) : json
      return new Login_Response(data)
    } catch (error) {
      console.error('JSON解析错误:', error)
      return new Login_Response({})
    }
  }

  toJson() {
    return JSON.stringify(this)
  }

  // 判断登录是否成功
  isSuccess() {
    return this.success && this.code === 200
  }

  // 获取token
  getToken() {
    return this.result && this.result.token ? this.result.token : ''
  }

  // 获取用户信息
  getUserInfo() {
    return this.result && this.result.userInfo ? this.result.userInfo : null
  }

  // 获取部门列表
  getDepartments() {
    return this.result && this.result.departs ? this.result.departs : []
  }

  // 获取当前租户ID
  getCurrentTenantId() {
    return (this.result && this.result.userInfo && this.result.userInfo.currentTenantId) ? this.result.userInfo.currentTenantId : ''
  }
}

class LoginResult {
  constructor(data) {
    data = data || {}
    this.multi_depart = data.multi_depart !== undefined ? data.multi_depart : 0
    this.userInfo = data.userInfo ? new UserInfo(data.userInfo) : null
    this.sysAllDictItems = data.sysAllDictItems !== undefined ? data.sysAllDictItems : {}
    this.departs = data.departs ? data.departs.map(dept => new Department(dept)) : []
    this.token = data.token !== undefined ? data.token : ''
  }
}

class UserInfo {
  constructor(data) {
    data = data || {}
    this.id = data.id !== undefined ? data.id : ''
    this.username = data.username !== undefined ? data.username : ''
    this.realname = data.realname !== undefined ? data.realname : ''
    this.avatar = data.avatar !== undefined ? data.avatar : null
    this.birthday = data.birthday !== undefined ? data.birthday : null
    this.sex = data.sex !== undefined ? data.sex : null
    this.email = data.email !== undefined ? data.email : null
    this.phone = data.phone !== undefined ? data.phone : ''
    this.orgCode = data.orgCode !== undefined ? data.orgCode : ''
    this.orgCodeTxt = data.orgCodeTxt !== undefined ? data.orgCodeTxt : null
    this.status = data.status !== undefined ? data.status : 0
    this.delFlag = data.delFlag !== undefined ? data.delFlag : 0
    this.workNo = data.workNo !== undefined ? data.workNo : ''
    this.post = data.post !== undefined ? data.post : null
    this.telephone = data.telephone !== undefined ? data.telephone : null
    this.createBy = data.createBy !== undefined ? data.createBy : ''
    this.createTime = data.createTime !== undefined ? data.createTime : ''
    this.updateBy = data.updateBy !== undefined ? data.updateBy : ''
    this.updateTime = data.updateTime !== undefined ? data.updateTime : ''
    this.activitiSync = data.activitiSync !== undefined ? data.activitiSync : 0
    this.userIdentity = data.userIdentity !== undefined ? data.userIdentity : 0
    this.departIds = data.departIds !== undefined ? data.departIds : ''
    this.relTenantIds = data.relTenantIds !== undefined ? data.relTenantIds : ''
    this.currentTenantId = data.currentTenantId !== undefined ? data.currentTenantId : ''
    this.clientId = data.clientId !== undefined ? data.clientId : ''
    this.company = data.company !== undefined ? data.company : null
    this.tenantId = data.tenantId !== undefined ? data.tenantId : null
    this.openId = data.openId !== undefined ? data.openId : ''
    this.userType = data.userType !== undefined ? data.userType : ''
    this.roles = data.roles !== undefined ? data.roles : null
    this.wxOpenId = data.wxOpenId !== undefined ? data.wxOpenId : ''
  }

  // 获取部门ID数组
  getDepartIdArray() {
    return this.departIds ? this.departIds.split(',') : []
  }

  // 获取租户ID数组
  getTenantIdArray() {
    return this.relTenantIds ? this.relTenantIds.split(',') : []
  }

  // 判断用户是否启用
  isEnabled() {
    return this.status === 1 && this.delFlag === 0
  }
}

class Department {
  constructor(data) {
    data = data || {}
    this.id = data.id !== undefined ? data.id : ''
    this.parentId = data.parentId !== undefined ? data.parentId : ''
    this.departName = data.departName !== undefined ? data.departName : ''
    this.departNameEn = data.departNameEn !== undefined ? data.departNameEn : null
    this.departNameAbbr = data.departNameAbbr !== undefined ? data.departNameAbbr : null
    this.departOrder = data.departOrder !== undefined ? data.departOrder : 0
    this.description = data.description !== undefined ? data.description : null
    this.orgCategory = data.orgCategory !== undefined ? data.orgCategory : ''
    this.orgType = data.orgType !== undefined ? data.orgType : ''
    this.orgCode = data.orgCode !== undefined ? data.orgCode : ''
    this.mobile = data.mobile !== undefined ? data.mobile : null
    this.fax = data.fax !== undefined ? data.fax : null
    this.address = data.address !== undefined ? data.address : null
    this.memo = data.memo !== undefined ? data.memo : null
    this.status = data.status !== undefined ? data.status : null
    this.delFlag = data.delFlag !== undefined ? data.delFlag : '0'
    this.qywxIdentifier = data.qywxIdentifier !== undefined ? data.qywxIdentifier : null
    this.createBy = data.createBy !== undefined ? data.createBy : ''
    this.createTime = data.createTime !== undefined ? data.createTime : ''
    this.updateBy = data.updateBy !== undefined ? data.updateBy : null
    this.updateTime = data.updateTime !== undefined ? data.updateTime : null
    this.tenantId = data.tenantId !== undefined ? data.tenantId : ''
    this.timemodel = data.timemodel !== undefined ? data.timemodel : null
    this.insideCode = data.insideCode !== undefined ? data.insideCode : ''
    this.depusernum = data.depusernum !== undefined ? data.depusernum : null
  }

  // 判断是否为根部门
  isRootDepartment() {
    return !this.parentId
  }

  // 获取格式化时间模型
  getTimeModel() {
    return this.timemodel || '无时间模型'
  }
}

export {
  Login_Request,
  Login_Response
}
