<template>
  <div class="on-duty-today">
    <div class="on-duty-today__inner">
      <div class="on-duty-today__list">
        <div class="on-duty-today__item">
          <div class="on-duty-today__itemHeader">{{todayBeOnDuty.total || 0}}</div>
          <div class="on-duty-today__itemBody">总单数</div>
        </div>
        <div class="on-duty-today__item">
          <div class="on-duty-today__itemHeader">{{todayBeOnDuty.price || 0}}</div>
          <div class="on-duty-today__itemBody">总费用（元）</div>
        </div>
        <div class="on-duty-today__item">
          <div class="on-duty-today__itemHeader">{{todayBeOnDuty.avg_response | formatHourMinute}}</div>
          <div class="on-duty-today__itemBody">平均接单时间</div>
        </div>
        <div class="on-duty-today__item">
          <div class="on-duty-today__itemHeader">{{todayBeOnDuty.avg_handle | formatHourMinute}}</div>
          <div class="on-duty-today__itemBody">平均解决时间</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { post, timerCommon } from './../http/http'
export default {
  name: 'OnDutyToday',
  inject: ['parentDutyId', 'hosId', 'dutyId'],
  data () {
    return {
      sign: 'lastWeek',
      timer: null,
      todayBeOnDuty: {} // 头部总当班人数
    }
  },
  methods: {
    // 获取数据
    async getData () {
      this.$emit('signEmit', this.sign)
      let startTime
      let endTime

      if (this.sign === 'lastWeek') {
        startTime = this.$moment().subtract(1, 'week').startOf('week').add(1, 'day').format('YYYY-MM-DD HH:mm:ss')
        endTime = this.$moment().subtract(1, 'week').endOf('week').add(1, 'day').format('YYYY-MM-DD HH:mm:ss')
      } else if (this.sign === 'lastMonth') {
        startTime = this.$moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD HH:mm:ss')
        endTime = this.$moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD HH:mm:ss')
      }
      const result = await post(
        '/itsm/report/index',
        {
          parentDutyId: this.parentDutyId,
          hosId: this.hosId,
          dutyId: this.dutyId,
          startDate: startTime,
          endDate: endTime,
          type: 'itsmSummary'
        }
      )
      this.todayBeOnDuty = result.data ? result.data[0] : {}
      this.polling()
    },
    // 轮询请求
    polling () {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        if (this.sign === 'lastWeek') {
          this.sign = 'lastMonth'
        } else if (this.sign === 'lastMonth') {
          this.sign = 'lastWeek'
        }
        this.getData()
      }, timerCommon)
    }
  },
  mounted () {
    this.getData()
  },
  beforeDestroy () {
    clearTimeout(this.timer)
  }
}
</script>

<style lang="less">
.on-duty-today {
  overflow: hidden;
  height: 2.6625rem;
  .on-duty-today__inner {
    height: 100%;
    .on-duty-today__list {
      display: flex;
      justify-content: space-between;
      height: 100%;
      .on-duty-today__item {
        text-align: center;
        display: flex;
        flex-direction: column;
        justify-content: center;
        margin-right: .4rem;
        &:first-child {
          margin-left: .4rem;
        }
        &:last-child {
          margin-right: .4rem;
        }
        .on-duty-today__itemHeader{
          font-size: .65rem;
          color: #00D5EA;
          font-weight: bold;
        }
        .on-duty-today__itemBody{
          font-size: .2rem;
          color: #FFFFFF;
          margin-top: .1rem;
        }
      }
    }
  }
}
</style>
