<template>
  <div class="go-energy-box">
    <div class="grid-container">
      <div v-for="(item, index) in configOption.gridItems" :key="index" class="grid-item">
        <span class="item">
          <img class="icon" :src="item.icon" :alt="item.label">
        </span>
        <div class="item-container item">
          <span class="label">{{ item.label }}</span>
          <div class="value-container">
            <div class="value" :class="item.valueClass">
              {{ getDataValue(item.field).toFixed(2) }}
            </div>
            <span class="unit">{{ item.unit }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { PropType, shallowReactive, watch } from 'vue'
import { CreateComponentType } from '@/packages/index.d'
import { useChartDataFetch } from '@/hooks'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { option as configOption } from './config'

const props = defineProps({
  chartConfig: {
    type: Object as PropType<CreateComponentType>,
    required: true
  }
})

const option = shallowReactive({
  dataset: configOption.dataset
})


// 获取数据值的方法
const getDataValue = (field: string) => {
  return (option.dataset as Record<string, number>)[field] || 0
}

watch(
  () => props.chartConfig.option.dataset,
  (newData: any) => {
    option.dataset = newData
  },
  {
    immediate: true,
    deep: false
  }
)

useChartDataFetch(props.chartConfig, useChartEditStore, (newData: any) => {
  option.dataset = newData
})
</script>

<style lang="scss" scoped>
@include go('energy-box') {
  width: 100%;
  height: 100%;

  .grid-container {
    height: 100%;
    width: 100%;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-gap: 10px;

    .grid-item {
      // background-color: #043951;
      padding: 10px 20px 5px 20px;
      font-size: 30px;
      display: flex;
      align-items: center;
      gap: 0px;


      .item-container {
        display: flex;
        flex-direction: column;
        margin-left: 20px;
        margin-right: 20px;
        width: 100%;
        height: auto;
        font-family: 'CustomFont';


        .value-container {
          display: flex;
          align-items: flex-end;
          overflow: hidden;
          width: 100%;
          height: 100%;

          .value {
            font-size: 35px;
            letter-spacing: 3px;
            // flex-grow: 1;
            // flex-shrink: 0;
            align-self: flex-end;
            white-space: nowrap;
            overflow: hidden;
            margin-right: 20px;
            // -webkit-background-clip: text;
            // background-clip: text;
            // color: transparent;
            // -webkit-text-fill-color: transparent;
          }

          .unit {
            font-size: 18px;
            flex-shrink: 0;
            color: #eee;
            margin-bottom: 10px;
          }
        }

        .label {
          color: #07A1C8;
          font-size: 22px;
          align-self: flex-start;

          // 使用渐变背景色
          background: linear-gradient(to right, rgba(255, 255, 255, 0.624) 0%, rgba(255, 255, 255, 1) 100%);
          -webkit-background-clip: text;
          background-clip: text;
          -webkit-text-fill-color: transparent;
          color: transparent; // 兼容性处理
        }



        // 为每个value定义不同的颜色
        .value-cost {
          color: #479FFE;
        }

        .value-electricity {
          color: #90FFAD;
        }

        .value-water {
          color: #0DBBF4;
        }

        .value-gas {
          color: #E5B55B;
        }



      }

      .item {}



      .icon {
        width: 60px;
        height: 60px;
        flex-shrink: 0;
        object-fit: contain;
      }
    }
  }
}
</style>