import {route} from '@/uni_modules/uview-plus';
import {Rules} from "@/common/consts/CommonConst";
import config from "@/common/config";
import defaultImg from "@/static/aiot/default.png";
import dayjs from "dayjs";

const weekdays = [
  '星期日',
  '星期一',
  '星期二',
  '星期三',
  '星期四',
  '星期五',
  '星期六',
]


export function formatWeekday(index) {
  return weekdays[index]
}

export function navigateTo({url = "", param = {}}) {
  route({
    url,
    params: param,
  })
}

export function reLaunch({url = "", param = {}}) {
  route({
    type: 'reLaunch',
    url,
    params: param,
  })
}


export const getStorageObj = (key) => {
  const json = uni.getStorageSync(key);
  if (!json) {
    return null;
  }
  return JSON.parse(json);
};
export const getUserInfo = () => {
  const userInfo = getStorageObj(storageKey.USER_INFO);
  if (!userInfo) {
    return null;
  }
  return userInfo;
};
export const setUserInfo = (userInfo) => {
  setStorageObj(storageKey.USER_INFO, userInfo);
};

export const setStorageObj = (key, value) => {
  const json = JSON.stringify(value);
  uni.setStorageSync(key, json);
};

export const exit = () => {
  uni.clearStorageSync();
};
export const getToken = () => {
  const token = getStorageObj(storageKey.TOKEN);
  if (!token) {
    return null;
  }
  return token;
};

export const setToken = (token) => {
  setStorageObj(storageKey.TOKEN, token);
};
export const authLogin = (callback) => {
  const user = getUserInfo();
  if (!user) {
    uni.navigateTo({
      url: '/pasb/pages/login/index',
    });
    return;
  }
  callback(user);
};
export const storageKey = {
  USER_INFO: 'userInfo',
  TOKEN: 'token',
  regtheme: 'regtheme',
  regmeet: 'regmeet',
};

export const reloadPage = () => {
  let pages = getCurrentPages();  //获取当前页面
  let beforePage = pages[pages.length - 2];  //获取上一个页面的实例
  //1、调用上一个页面onLoad
  if (beforePage) {
    beforePage.onLoad && beforePage.onLoad();
    uni.navigateBack({
      delta: 1,
    });
  }
  reLaunch({
    url: '/pages/workbench/index',
  })
}

export const DateFormat = {
  YYYYMMDD: 'YYYY-MM-DD',
  YYYYMMDDHHMMSS: 'YYYY-MM-DD HH:mm:ss',
  YYYYMMDDHHMM: 'YYYY-MM-DD HH:mm',
}

export const getDevImg = (devImg) => {
  if (!devImg) {
    return defaultImg;
  }
  const parse = JSON.parse(devImg);
  if (parse.length > 0 && parse[0].url) {
    const url = parse[0].url;
    if (!Rules.domainPort.test(url)) {
      return `${config.imgCdn}${url}`;
    }
    return url.replace(Rules.domainPort, config.imgCdn);
  } else {
    return defaultImg;
  }
};
export const getImgs = (items) => {
  return JSON.parse(items).filter(item => !!item.url).map((item) => {
    return getImgUrl(item.url)
  })
}

export const getImgUrl = (img) => {
  if (!img) {
    return defaultImg;
  }
  if (img) {
    return img.replace(Rules.domainPort, config.imgCdn);
  } else {
    return defaultImg;
  }
};

export const formatTxt = (txt) => {
  if (!txt) {
    return "--";
  }
  return txt;
}

export const getStepInfo = (step) => {
  switch (step.type) {
    case 1:
      return {type: 1, name: '创建工单', content: '创建工单'};
    case 2:
      return {type: 2, name: '受理工单', content: '受理工单'};
    case 3:
      return {type: 3, name: '结束工单', content: '结束工单'};
    case 4:
      return {type: 4, name: '报废工单', content: '报废工单'};
    case 5: {
      const param = JSON.parse(step.param)
      if (param.desc) {
        return {type: 5, name: '指派工单', content: `指派此工单给 ${param.user} 原因：${param.desc}`};
      }
      return {type: 5, name: '指派工单', content: `指派此工单给 ${param.user}`};
    }
    case 6:
      return {type: 6, name: '修改工单', content: '修改工单'};
    case 7:
      return {type: 7, name: '处理工单', content: '处理工单'};
    default:
      return {type: 0, name: '未知操作', content: '未知操作'};
  }
}

export const hideKeyboard = () => {
  uni.hideKeyboard()
}

export const timestampToDate = (date, format = DateFormat.YYYYMMDDHHMM) => {
  return dayjs(date).format(format)
}