<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 }) })
    // const result ={
    //   "code": 0,
    //   "message": "获取成功",
    //   "data": {
		//       "id": "1",
		//       "openid": "oOpc26KiZFBKIm7SB8knFGvov1qg",
		//       "mobile": "",
		//       "gender": "0",
		//       "nickname": "雪寒",
		//       "avatar": "",
		//       "subscribe_time": "2022-12-21 15:52:14",
		//       "addr": "\/\/",
		//       "status": "1",
		//       "is_show": ["1", "2", "4"],
		//       "companyArr": [{
		//       	"companyNo": "GS2302231125079621",
		//       	"companyName": "北京百辰荣达国际科贸有限公司",
		//       	"info": [1,2]
		//       }, {
		//       	"companyNo": "GS2302231323386950",
		//       	"companyName": "北京泓源广诚国际商贸有限公司",
		//       	"info": [1,2]
		//       }, {
		//       	"companyNo": "GS2304031312553746",
		//       	"companyName": "北京锦兴弘昌科技有限公司",
		//       	"info": [1, 2]
		//       }, {
		//       	"companyNo": "GS2302231124114965",
		//       	"companyName": "北京普润心堂商贸有限公司",
		//       	"info": [1, 2]
		//       }, {
		//       	"companyNo": "GS2203161855277894",
		//       	"companyName": "北京万宇恒通国际科贸有限公司",
		//       	"info": [1, 2]
		//       }],
		//       "addtime": "2023-04-10 18:11:07",
		//       "updatetime": "2023-05-09 16:22:33"
    //   	}
    //   }


      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>
