<template>
  <div id="app" v-cloak v-loading="state.loading" style="min-height: 300px;">
    <template v-if="!state.error && isShow">
      <control-display :companies="companyArr" :hasPlatformDimension="hasPlatformDimension" />
    </template>
    <template v-else-if="!state.loading">
      <unusual-state :hasPermission="isShow" :message="state.message" path="report" />
    </template>
  </div>
</template>
<script>
import asyncRequest from "@/api/index"
import { getParameterByName ,getOpenid, setOpenid, removeOpenid } from "../utils/auth"
import ControlDisplay from "../components/control-display/index.vue";
import UnusualState from "@/components/unusual/index.vue"

export default {
  name:'report',
  components:{ UnusualState , ControlDisplay },
  data() {
    return {
      companyArr:[],
      companies:[],
      hasPlatformDimension: false,
      isShow: false,
      state:{
        error:false,
        loading:false,
        message:'',
      }
    }
  },
  created(){
    document.title = "2.利润看板"
  },
  async mounted(){
    this.requestUserinfo()
  },
  methods:{
    async requestUserinfo(){
      this.state.loading = true;
      const openid = getOpenid()
      const code = getParameterByName('code')
      const result = await asyncRequest.userinfo({
        ...( openid ? { openid } :  { code })
      })

      this.state.loading = false
      switch(Number(result.code)){
        case 0:
          this.companyArr = result.data.companyArr.filter(({info = []}) => info.includes(2) || info.includes('2'))
          this.hasPlatformDimension = (result.data.is_show || []).includes(2) || (result.data.is_show || []).includes('2')
          this.isShow = this.hasPlatformDimension
          setOpenid(result.data.openid)
          break
        default:
          this.state.error = true
          this.state.message = result.message
          openid && removeOpenid()
          break
      }
    }
  }
};
</script>
