<template>
  <div id="app" v-cloak v-loading="state.loading" style="min-height: 300px;">
    <template v-if="!state.error && isShow">
      <div style="color:red;position:absolute;top:30px;left:10px;font-weight: 700;display:flex">
        <div>
          <i class="el-icon-warning-outline"></i> 以下报表一小时更新一次
        </div>

      </div>

      
      <control-display :companyArr="companyArr" :hasPlatformDimension="hasPlatformDimension"  />
    </template>
    <template v-else-if="!state.loading">
      <unusual-state :hasPermission="isShow" :message="state.message" path="results" />
    </template>
  </div>
</template>
<script>
import asyncRequest from "@/api/index"
import { ControlDisplay } from "@/ccomponents/reports"
import UnusualState from "@/components/unusual/index.vue"
import { getParameterByName , setOpenid, removeOpenid, getOpenid } from "../utils/auth"

export default {
  name:'report',
  components:{ UnusualState, ControlDisplay},
  data() {
    return {
      companyArr:[],
      hasPlatformDimension: false,
      isShow : false,
      state:{
        error:false,
        loading:false,
        message:'',
      }
    }
  },
  async mounted(){
    document.title = "1.业绩报表"
    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(1) || info.includes('1'))
          this.hasPlatformDimension = (result.data.is_show || []).includes(1) || (result.data.is_show || []).includes('1')
          this.isShow =  this.hasPlatformDimension
          setOpenid(result.data.openid)
          break
        default:
          this.state.error = true
          this.state.message = result.message
          openid && removeOpenid()
          break
      }
    }
  }
};
</script>
