<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>控制台 <filter-search v-if="$route.query.tabrouter === 'teamSchedule'" searchKey="ids" @reload="reloadItem"/>
    </h2>
    <div class="m-core">
      <tab-router @change="changeKey">
        <el-tab-pane v-permission="'newIndex'" lazy label="基本信息" name="baseinfo">
          <baseinfo v-if="activeKey === 'baseinfo'"/>
        </el-tab-pane>
        <el-tab-pane v-permission="'getIndexErrData'" lazy name="abnormal">
          <el-badge slot="label" class="badge" is-dot :hidden="!status.indexErrData">异常处理</el-badge>
          <abnormal v-if="activeKey === 'abnormal'"/>
        </el-tab-pane>
        <el-tab-pane v-permission="'getRemindMatterData'" name="reminders">
          <el-badge slot="label" class="badge" is-dot :hidden="!status.remindMatterData">事项提醒</el-badge>
          <reminders v-if="activeKey === 'reminders'"/>
        </el-tab-pane>
        <el-tab-pane v-permission="'inspectionItemPlan/queryPage'" lazy label="【乐团主管】日程安排" name="teamSchedule">
          <teamSchedule v-if="activeKey === 'teamSchedule'" ref="teamSchedule" />
        </el-tab-pane>
        <el-tab-pane v-permission="'inspection/queryPage'" lazy label="【分部经理】日程安排" name="ScheduleBranch">
          <ScheduleBranch v-if="activeKey === 'ScheduleBranch'"/>
        </el-tab-pane>
      </tab-router>
    </div>
  </div>
</template>
<script>
import baseinfo from './baseinfo'
import abnormal from './abnormal'
import teamSchedule from './teamSchedule'
import ScheduleBranch from './schedule-branch'
import reminders from './reminders'
import { hasIndexErrData } from './api'
export default {
  name: 'Main',
  components: {
    baseinfo,
    abnormal,
    teamSchedule,
    ScheduleBranch,
    reminders,
  },
  data() {
    return {
      activeKey: '',
      status: {
        indexErrData: false,
        remindMatterData: false,
      }
    }
  },
  computed: {
    hasError() {
      const { indexErrData, remindMatterData } = this.status
      return indexErrData || remindMatterData
    }
  },
  watch: {
    '$route.query'(newValue, oldValue) {
      if (newValue.tabrouter !== oldValue.tabrouter) {
        this.$forceUpdate()
        this.FetchDot()
      }
    }
  },
  mounted() {
    this.FetchDot()
  },
  methods: {
    changeKey(val) {
      this.activeKey = val
    },
    async FetchDot() {
      try {
        const res = await hasIndexErrData()
        this.status = res.data
        this.toggleDot()
      } catch (error) {}
    },
    toggleDot() {
      const el = document.querySelector('.menu-wrapper a[href="#/main/main"] span')
      if (el) {
        if (this.hasError) {
          el.classList.add('main-dot')
        } else {
          el.classList.remove('main-dot')
        }
      }
    },
    __init () {
      getIndex().then(res => {
        if (res.code == 200) {
          // this.dataInfo = res.data
          let md = res.data.musicDatas || {},
            vd = res.data.vipDatas || {},
            dd = res.data.demoDatas || {}
          let tempDate = {
            mdTotal: (md.progress_num_ ? md.progress_num_ : 0) + (md.prepare_num_ ? md.prepare_num_ : 0) + (md.pause_num_ ? md.pause_num_ : 0),
            progress_num_: md.progress_num_ >= 0 ? md.progress_num_ : '--',
            prepare_num_: md.prepare_num_ >= 0 ? md.prepare_num_ : '--',
            pause_num_: md.pause_num_ >= 0 ? md.pause_num_ : '--',
            vdTotal: (vd.finished_num_ ? vd.finished_num_ : 0) + (vd.applying_num_ ? vd.applying_num_ : 0),
            finished_num_: vd.finished_num_ >= 0 ? vd.finished_num_ : '--',
            applying_num_: vd.applying_num_ >= 0 ? vd.applying_num_ : '--',
            ddTotal: (dd.booked_num_ ? dd.booked_num_ : 0) + (dd.unbooked_num_ ? dd.unbooked_num_ : 0),
            booked_num_: dd.booked_num_ >= 0 ? dd.booked_num_ : '--',
            unbooked_num_: dd.unbooked_num_ >= 0 ? dd.unbooked_num_ : '--'
          }
          this.dataInfo = Object.assign(res.data, tempDate)
        }
      })
    },
    reloadItem() {
      this.$refs.teamSchedule.getList()
    }
  }
}
</script>
<style lang="scss" scope>
.itemWrap {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
  .item {
    width: 333px;
    height: 137px;
    padding: 22px 28px 20px;
    box-shadow: 0px 8px 20px 0px rgba(0, 0, 0, 0.1);
    border-radius: 6px;
    margin: 0 15px 3rem;
    h4 {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      margin-bottom: 20px;
      color: #323c47;
      font-size: 14px;
    }
    .infos {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      .sub {
        font-size: 14px;
        line-height: 20px;
        color: #aaa;
        margin-bottom: 8px;
      }
      .msg {
        font-size: 24px;
        font-weight: 500;
        color: rgba(68, 68, 68, 1);
        line-height: 28px;
        text-align: center;
      }
    }
  }
}
.badge{
  .el-badge__content.is-fixed.is-dot{
    top: 5px;
  }
}
</style>
<style lang="less">
.main-dot {
  position: relative;
  &::after{
    content: '';
    display: block;
    position: absolute;
    height: 8px;
    width: 8px;
    background-color: #F56C6C;
    border-radius: 50%;
    right: -10px;
    top: 15px;
  }
}
</style>
