const TokenKey = 'App-Token'
const TenantIdKey = 'TENANT_ID'
const userInfo = 'userInfo'
const InviteIdKey = 'INVITE_ID'
const CoachIdKey = 'COACH_ID'
const StaffTypeKey = 'STAFF_TYPE'

export function getToken() {
  return uni.getStorageSync(TokenKey)
}

export function setToken(token) {
  return uni.setStorageSync(TokenKey, token)
}

export function removeToken() {
  return uni.removeStorageSync(TokenKey)
}

export function setTenantId(TenantId) {
  return uni.setStorageSync(TenantIdKey, TenantId);
}

export function getTenantId(){
  return uni.getStorageSync(TenantIdKey)
}

export function removeTenantId(){
  return  uni.removeStorageSync(TenantIdKey)
}

export function setLocalUserInfo(userinfo){
  return uni.setStorageSync(userInfo,userinfo)
}

export function getLocalUserInfo() {
  return uni.getStorageSync(userInfo)
}

export function setInviteId(InviteId) {
  return uni.setStorageSync(InviteIdKey, InviteId);
}
export function getInviteId(){
  return uni.getStorageSync(InviteIdKey)
}
export function removeInviteId(){
  return  uni.removeStorageSync(InviteIdKey)
}

export function setCoachId(CoachId) {
  return uni.setStorageSync(CoachIdKey, CoachId);
}
export function getCoachId(){
  return uni.getStorageSync(CoachIdKey)
}
export function removeCoachId(){
  return  uni.removeStorageSync(CoachIdKey)
}
// 员工为01，教练为02
export function setStaffType(StaffType) {
  return uni.setStorageSync(StaffTypeKey, StaffType);
}
export function getStaffType(){
  return uni.getStorageSync(StaffTypeKey)
}
export function removeStaffType(){
  return  uni.removeStorageSync(StaffTypeKey)
}

// 设置本地存储，并设置一个过期时间（单位为秒）
export function setStorageWithExpiry(key, value, ttl) {
  const now = new Date();
  // 计算过期时间
  const item = {
    value: value,
    expiry: now.getTime() + ttl * 1000,
  };
  // 将数据对象转换为字符串存储
  uni.setStorageSync(key, JSON.stringify(item));
}

// 获取本地存储的数据，检查是否过期
export function getStorageWithExpiry(key) {
  // 从本地存储获取数据
  const itemStr = uni.getStorageSync(key);
  if (!itemStr) {
    return null; // 未找到数据
  }
  const item = JSON.parse(itemStr);
  const now = new Date();
  // 检查项目是否过期
  if (now.getTime() > item.expiry) {
    // 如果过期，从存储中移除该项
    uni.removeStorageSync(key);
    return null;
  }
  return item.value; // 数据未过期，返回数据
}
