import { getItem, setItem } from "@/utils/storage"
import { TOKEN_TIMEOUT_VSLUE, TIME_STAMP } from "@/constant"

/*
 * 1.设置时间戳
 */
export function setTimeStamp() {
  setItem(TIME_STAMP, Date.now())
}

/*
 * 2.获取时间戳
 */
export function getTimeStamp() {
  return getItem(TIME_STAMP)
}

/*
 * 1.是否超时  对比当前时间 和登录时间
 */
export function isCheckTimeout() {
  const currentTime = Date.now()
  const timeStamp = getTimeStamp()
  return currentTime - timeStamp > TOKEN_TIMEOUT_VSLUE
}
//格式化时间
export function dateFormat(date, fmt = "YY-MM-DD hh:mm:ss") {
  let ret
  const opt = {
    "Y+": date.getFullYear().toString(), // 年
    "M+": (date.getMonth() + 1).toString(), // 月
    "D+": date.getDate().toString(), // 日
    "h+": date.getHours().toString(), // 时
    "m+": date.getMinutes().toString(), // 分
    "s+": date.getSeconds().toString() // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  }
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt)
    if (ret) {
      fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0"))
    }
  }
  return fmt
}
//输入框自动保留两位小数
export function JEblurFn(value) {
  return (value = Number(value).toFixed(2))
}
// 字符串拼数组
export function spliceArray(targetArr) {
  let currentArr = []
  if (targetArr.length > 0) {
    targetArr.forEach((item, index) => {
      if (index % 2 == 0 && index != targetArr.length) {
        currentArr.push([targetArr[index], targetArr[index + 1]])
      }
    })
  }
  return currentArr
}

// 数组转字符串
export function spliceString(targetArr) {
  let tempArr = []
  let currentStr = ""
  if (targetArr.length > 0) {
    targetArr.forEach(item => {
      tempArr.push(...item)
    })
    currentStr = tempArr.toString()
  }
  return currentStr
}

// 获取前后几年得时间
export function filterYear(num, add) {
  let year = new Date().getFullYear()
  let yearArr = []
  for (let i = 0; i < num; i++) {
    yearArr.push({
      value: year - i,
      label: year - i + "年"
    })
  }
  return yearArr
}
//上半年、下半年
export function filterbn(num, add) {
  let year = new Date().getFullYear()
  let yearArr = []
  for (let i = 0; i < num; i++) {
    yearArr.push(
      {
        value: `${year - i}、1`,
        label: year - i + "年-上半年"
      },
      {
        value: `${year - i}、2`,
        label: year - i + "年-下半年"
      },
    )
  }
  return yearArr
}
//季度
export function filterjd(num, add) {
  let year = new Date().getFullYear()
  let yearArr = []
  for (let i = 0; i < num; i++) {
    yearArr.push(
      {
        value: `${year - i}、1`,
        label: year - i + "年第一季度"
      },
      {
        value: `${year - i}、2`,
        label: year - i + "年第二季度"
      },
      {
        value: `${year - i}、3`,
        label: year - i + "年第三季度"
      },
      {
        value: `${year - i}、4`,
        label: year - i + "年第四季度"
      },
    )
  }
  return yearArr
}
//判断是否是安全部门
export function filterqx(type) {
  let dept = getItem("deptId")
  let orgLevel = dept.orgLevel ? dept.orgLevel[0] : ''
  let Isyes = ""
  if (type == "isaqbm") {
    //判断是否是安全部门 安全科
    if (dept.orgBizType == "04" || dept.orgBizType == "52") {
      Isyes = "1"
    }
  } else if (type == "isSgs") {
    //判断是否是省公司
    if (orgLevel == "1") {
      Isyes = "1"
    }
  } else if (type == "isgs") {
    //判断是否是市公司
    if (orgLevel == "2") {
      Isyes = "1"
    }
  } else if (type == "isxgs") {
    //判断是否是县公司
    if (orgLevel == "3" || orgLevel == "4") {
      Isyes = "1"
    }
  } else if (type == "saqbm") {
    //判断是否是省公司并且是安全部门
    if (orgLevel == "1" && (dept.orgBizType == "04" || dept.orgBizType == "52")) {
      Isyes = "1"
    }
  } else if (type == "isldbz") {
    // 是否是领导班子
    if (dept.orgBizType == "02") {
      Isyes = "1"
    }
  } else {
    Isyes = "0"
  }
  return Isyes
}
