<template>
  <view v-if="debugMode">
    <button type="warn" size="mini" @tap='debugCleanCache'>清除缓存</button>
    <view style="border: 3px solid black;">
      <input v-model="userRole" style="border: 1px solid grey; display: inline-block; width: 40%;" />
      <button style="width: 50%;" type="primary" size="mini" @tap='updateUserRole'>更新role ({{ userGroup }})</button>
    </view>
    <textarea maxlength="-1" disabled="true" auto-height="true" style="width: 100%" :value="debugText"></textarea>
  </view>
</template>

<script>
import Taro from '@tarojs/taro'
import { eventCenter, getCurrentInstance } from '@tarojs/taro'

import ENUM from '../utils/const'

export default {
  data() {
    return {
      userRole: -1,
      debugMode: Taro.getApp().globalData.debugMode,
      debugText: "",
    }
  },
  props: {
    params: Object,
    userGroup: String
  },
  mounted() {
    eventCenter.once(getCurrentInstance().router.onShow, () => {
      this.userRole = Taro.getStorageSync("userInfo").role;

      const res = Taro.getStorageInfoSync()
      let storage = {};
      res.keys.forEach(key => storage[key] = Taro.getStorageSync(key))
      this.debugText = JSON.stringify({
        TARO_ENV: process.env.TARO_ENV,
        globalData: Taro.getApp().globalData,
        "启动参数": Taro.getCurrentInstance().router.params,
        params: this.params,
        storage: storage,
        ENUM: ENUM,
        // storageInfo: {
        //   keys: res.keys,
        //   currentSize: res.currentSize,
        //   limitSize: res.limitSize
        // },
      }, null, 8)
    })
  },
  methods: {
    debugCleanCache() {
      Taro.clearStorage({
        success: function () {
          Taro.showToast({
            title: "成功",
            icon: 'success'
          })
          Taro.reLaunch({
            url: '/pages/index/index'
          })
        }
      })
    },
    updateUserRole() {
      let userInfo = Taro.getStorageSync("userInfo");
      userInfo.role = this.userRole;
      Taro.setStorageSync("userInfo", userInfo);
      Taro.reLaunch({
        url: '/pages/index/index'
      })
    }
  }
}
</script>
