<template>
  <div id="centerLeft1">
    <div class="bg-color-black">
      <div class="d-flex pt-2 pl-2">
        <span>
          <icon name="chart-bar" class="text-icon"></icon>
        </span>
        <div class="d-flex">
          <span class="fs-xl text mx-2">设备运行统计</span>
          <dv-decoration-3 class="dv-dec-3" />
        </div>
      </div>
      <div class="d-flex jc-center">
        <CenterLeft1Chart />
      </div>
      <!-- 4个主要的数据 -->
      <div class="bottom-data">
        <div
          class="item-box mt-2"
          v-for="(item, index) in numberData"
          :key="index"
        >
          <div class="d-flex">
            <span class="coin">●</span>
            <dv-digital-flop class="dv-digital-flop" :config="item.number" />
          </div>
          <p class="text" style="text-align: center;">
            {{ item.text }}
            <span class="colorYellow">({{ getUnit(item.type) }})</span>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import CenterLeft1Chart from '@/components/echart/centerLeft/centerLeft1Chart'
import { getCenterInfoStats } from '@/api/data'

export default {
  data() {
    return {
      numberData: [
        {
          number: {
            number: [0],
            toFixed: 0,
            textAlign: 'left',
            content: '{nt}',
            style: {
              fontSize: 24
            }
          },
          text: '今日按摩任务',
          type: 'todayTaskCount'
        },
        {
          number: {
            number: [0],
            toFixed: 0,
            textAlign: 'left',
            content: '{nt}',
            style: {
              fontSize: 24
            }
          },
          text: '累计完成数量',
          type: 'totalTaskCount'
        },
        {
          number: {
            number: [0],
            toFixed: 0,
            textAlign: 'left',
            content: '{nt}',
            style: {
              fontSize: 24
            }
          },
          text: '在线设备数',
          type: 'onlineDeviceCount'
        },
        {
          number: {
            number: [0],
            toFixed: 0,
            textAlign: 'left',
            content: '{nt}',
            style: {
              fontSize: 24
            }
          },
          text: '离线设备数',
          type: 'offlineDeviceCount'
        }
      ]
    }
  },
  components: {
    CenterLeft1Chart
  },
  mounted() {
    this.loadData()
    this.changeTiming()
  },
  methods: {
    getUnit(type) {
      const units = {
        todayTaskCount: '次',
        totalTaskCount: '次',
        onlineDeviceCount: '台',
        offlineDeviceCount: '台'
      }
      return units[type] || '件'
    },
    changeTiming() {
      setInterval(() => {
        this.loadData()
      }, 5000)
    },
    async loadData() {
      try {
        const response = await getCenterInfoStats()
        if (response.code === 200) {
          const data = response.data
          console.log('获取到的数据:', data)
          
          // 直接使用接口返回的对象数据，而不是数组
          if (data) {
            // 更新每个数据项
            this.numberData.forEach((item) => {
              if (data[item.type] !== undefined) {
                item.number.number[0] = data[item.type]
              }
            })
            
            // 触发数字更新
            this.numberData.forEach((item) => {
              item.number = { ...item.number }
            })
          }
        }
      } catch (error) {
        console.error('获取设备数据失败:', error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
$box-width: 300px;
$box-height: 410px;

#centerLeft1 {
  padding: 16px;
  height: $box-height;
  width: $box-width;
  border-radius: 10px;
  .bg-color-black {
    height: $box-height - 30px;
    border-radius: 10px;
  }
  .text {
    color: #c3cbde;
  }
  .dv-dec-3 {
    position: relative;
    width: 100px;
    height: 20px;
    top: -3px;
  }

  .bottom-data {
    .item-box {
      & > div {
        padding-right: 5px;
      }
      font-size: 14px;
      float: right;
      position: relative;
      width: 50%;
      color: #d3d6dd;
      .dv-digital-flop {
        width: 120px;
        height: 30px;
      }
      // 修改为圆点
      .coin {
        position: relative;
        top: 6px;
        font-size: 20px;
        color: #ffc107;
      }
      .colorYellow {
        color: yellowgreen;
      }
      p {
        text-align: center;
      }
    }
  }
}
</style>