<script lang="ts">
import {body} from "@/utils/https";
import {groupByKey} from "@/utils/common";

export default <any>{
  globalData: {
    token: "",
    user: {},
    isLogin: false,
    last: {},
    device: {},
    deviceId: null,
    manualLogout: false
  },
  onLaunch() {
    console.log("onLaunch clean token")
    let device =  uni.getWindowInfo();
    uni.removeStorageSync("token")
    let initGlobalData = {
      token: "",
      user: {},
      isLogin: false,
      last: {},
      device: {
        windowWidth:device.windowWidth,
        windowHeight:device.windowHeight,
        screenTop: device.screenTop,
        windowTop: device.windowTop,
      },
      deviceId: null,
      manualLogout: false
    }
    this.globalData = Object.assign({},  initGlobalData)

    body(`/dict/list`).then((res: any) => {
      if (res) {
        let dicts = res.map((k: any) => {
          let {code, name, value, color} = k;
          return {code, name, value, color}
        })
        let dictMap = groupByKey(dicts, "code");
        uni.setStorage({key: 'dict', data: dictMap})
      }
    })

    // this.globalData.token = fetchToken();
  },
  onShow(options: any) {
    console.log("app onShow", options)
    let device = uni.getWindowInfo();
    let backup = uni.getStorageSync('backup')
    // console.log("backup", backup)
    this.globalData = Object.assign({}, this.globalData, {
      device: {
        windowWidth: device.windowWidth,
        windowHeight: device.windowHeight,
        screenTop: device.screenTop,
        windowTop: device.windowTop,
      }
    }, backup)
    console.log("backup", this.globalData)
  },
  onHide() {
    console.log("app onHide", getApp<any>().globalData)
    //backup
    let backup = {...getApp<any>().globalData}
    uni.setStorageSync('backup', backup)
  },
  onPageNotFound() {
    uni.switchTab({
      url: "/pages/index/index",
    });
  },
};
</script>
<style lang="scss">
@import '@climblee/uv-ui/index.scss';
@import './custom.scss';

</style>
