<template>
  <div class="lineBottom" style="height: 180px; width: 100%">
    <operatingLineBarSaleSingle :refName="'totalOperating'" :chartData="chartD" style="height: 99%; width: 100%" />
  </div>
</template>

<script>
import operatingLineBarSaleSingle from './operatingLineBarSaleSingle.vue';
import * as echarts from 'echarts';

export default {
  name: "Container",
  components: { operatingLineBarSaleSingle },
  props: ["detailData"],
  data() {
    return {
    };
  },
  computed: {
    locations() {
      return ['预算', '实际'];
    },
    chartD() {
      // 背景图片路径（若不需要可注释）
      // const bgImageUrl = require('@/assets/img/labelBg.png');
      const rate = this.detailData?.completeRate || 0
      const diff = this.detailData?.diff || 0
      console.log('diff', this.detailData);

      const seriesData = [
        {
          value: this.detailData?.target || 0,
          flag: 1, // 实际项：达标（绿色）
          label: {
            show: true,
            position: 'top',
            offset: [0, 0],
            // 固定label尺寸：68px×20px
            width: 68,
            height: 20,
            // 关键：去掉换行，让文字在一行显示，适配小尺寸
            formatter: function (params) {
              return `{value|完成率}{rate|${rate}%}`;
            },
            // 核心样式：匹配CSS需求
            backgroundColor: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                { offset: 0, color: 'rgba(205, 215, 224, 0.6)' }, // 顶部0px位置：阴影最强
                // { offset: 0.1, color: 'rgba(205, 215, 224, 0.4)' }, // 1px位置：阴影减弱（对应1px）
                // { offset: 0.15, color: 'rgba(205, 215, 224, 0.6)' }, // 3px位置：阴影几乎消失（对应3px扩散）
                { offset: 0.2, color: '#ffffff' }, // 主体白色
                { offset: 1, color: '#ffffff' }
              ]
            },
            // 外阴影：0px 2px 2px 0px rgba(191,203,215,0.5)
            shadowColor: 'rgba(191,203,215,0.5)',
            shadowBlur: 2,
            shadowOffsetX: 0,
            shadowOffsetY: 2,
            // 圆角：4px
            borderRadius: 4,
            // 移除边框
            borderColor: '#BFCBD577',
            borderWidth: 0,
            // 文字垂直居中（针对富文本）
            lineHeight: 20,
            rich: {
              value: {
                // 缩小宽度和内边距，适配68px容器
                width: 'auto', // 自动宽度，替代固定40px
                padding: [5, 0, 5, 10], // 缩小内边距
                align: 'center',
                color: '#464646', // 文字灰色
                fontSize: 11, // 缩小字体，适配小尺寸
                lineHeight: 20 // 垂直居中
              },
              rate: {
                width: 'auto',
                padding: [5, 10, 5, 0],
                align: 'center',
                color: '#0B58FF', // 数字蓝色
                fontSize: 11,
                lineHeight: 20
              }
            }
          },
          itemStyle: {
            color: {
              type: 'linear',
              x: 0, y: 0, x2: 0, y2: 1,
              colorStops: [
                { offset: 0, color: 'rgba(130, 204, 255, 1)' },
                { offset: 1, color: 'rgba(75, 157, 255, 1)' }
              ]
            },
            borderRadius: [4, 4, 0, 0],
            borderWidth: 0
          },
        },
        {
          value: this.detailData?.real || 0,
          flag: this.detailData?.flag, // 实际项：达标（绿色）
          label: {
            show: true,
            position: 'top',
            offset: [0, 0],
            // 固定label尺寸：68px×20px
            width: 68,
            height: 20,
            // 关键：去掉换行，让文字在一行显示，适配小尺寸
            formatter: (params) => {

              // const flags = flags || [];
              const currentDiff = diff || 0;
              const currentFlag = this.detailData?.flag || 0;
              const prefix = currentFlag === 1 ? '+' : '';
              // 根据标志位选择不同的样式类
              if (currentFlag === 1) {
                // 达标 - 使用 rate-achieved 样式
                return `{achieved|${prefix}${currentDiff}}{text|差值}`;
              } else {
                // 未达标 - 使用 rate-unachieved 样式
                return `{unachieved|${prefix}${currentDiff}}{text|差值}`;
              }
            },
            backgroundColor: {
              type: 'linear',
              x: 0, y: 0, x2: 0, y2: 1,
              colorStops: [
                { offset: 0, color: 'rgba(205, 215, 224, 0.6)' },
                { offset: 0.2, color: '#ffffff' },
                { offset: 1, color: '#ffffff' }
              ]
            },
            shadowColor: 'rgba(191,203,215,0.5)',
            shadowBlur: 2,
            shadowOffsetX: 0,
            shadowOffsetY: 2,
            borderRadius: 4,
            borderColor: '#BFCBD577',
            borderWidth: 0,
            lineHeight: 20,
            rich: {
              text: {
                width: 'auto',
                padding: [5, 10, 5, 0],
                align: 'center',
                color: '#464646',
                fontSize: 11,
                lineHeight: 20
              },
              achieved: {
                width: 'auto',
                padding: [5, 0, 5, 10],
                align: 'center',
                color: '#76DABE', // 与达标的 offset: 1 颜色一致
                fontSize: 11,
                lineHeight: 20
              },
              // 未达标样式
              unachieved: {
                width: 'auto',
                padding: [5, 0, 5, 10],
                align: 'center',
                color: '#F9A44A', // 与未达标的 offset: 1 颜色一致
                fontSize: 11,
                lineHeight: 20
              }
            }
          },
          itemStyle: {
            borderRadius: [4, 4, 0, 0],
            borderWidth: 0
          },
        }
      ];

      const data = {
        allPlaceNames: ['预算', '实际'],
        series: [
          {
            type: 'bar',
            barWidth: 24,
            barCategoryGap: '50%',
            data: seriesData,
            itemStyle: {
              color: (params) => {
                const currentFlag = params.data.flag || 0;
                return currentFlag === 1
                  ? {
                    type: 'linear',
                    x: 0, y: 0, x2: 0, y2: 1,
                    colorStops: [
                      { offset: 0, color: 'rgba(174, 239, 224, 1)' },
                      { offset: 1, color: 'rgba(118, 218, 190, 1)' }
                    ]
                  }
                  : {
                    type: 'linear',
                    x: 0, y: 0, x2: 0, y2: 1,
                    colorStops: [
                      { offset: 0, color: 'rgba(253, 209, 129, 1)' },
                      { offset: 1, color: 'rgba(249, 164, 74, 1)' }
                    ]
                  };
              }
            },
          },
        ],
      };
      console.log('data', data);

      return data;
    }
  },
  methods: {},
};
</script>

<style scoped lang="scss">
.coreBar {
  display: flex;
  flex-direction: column;
  width: 100%;
  padding: 12px;

  .barTop {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 16px;
    width: 100%;

    .title {
      height: 18px;
      font-family: PingFangSC, PingFangSC;
      font-weight: 400;
      font-size: 18px;
      color: #000000;
      line-height: 18px;
      letter-spacing: 1px;
      text-align: left;
      font-style: normal;
      white-space: nowrap;
    }

    .right-container {
      display: flex;
      align-items: center;
      gap: 24px;
      margin-right: 46px;
    }

    .legend {
      display: flex;
      gap: 16px;
      align-items: center;
      margin: 0;
    }

    .legend-item {
      display: flex;
      align-items: center;
      gap: 8px;
      font-family: PingFangSC;
      font-weight: 400;
      font-size: 14px;
      color: rgba(0, 0, 0, 0.8);
      text-align: left;
      font-style: normal;
      white-space: nowrap;
    }

    .legend-icon {
      display: inline-block;
    }

    .legend-icon.line {
      width: 12px;
      height: 2px;
      position: relative;

      &::before {
        position: absolute;
        content: "";
        top: -2px;
        left: 3px;
        width: 6px;
        border-radius: 50%;
        height: 6px;
        background-color: rgba(40, 138, 255, 1);
      }
    }

    .legend-icon.square {
      width: 8px;
      height: 8px;
    }

    .yield {
      background: rgba(40, 138, 255, 1);
    }

    .target {
      background: #2889FF;
    }

    .achieved {
      background: rgba(40, 203, 151, 1);
    }

    .unachieved {
      background: rgba(255, 132, 0, 1);
    }

    .button-group {
      display: flex;
      position: relative;
      gap: 2px;
      width: 283px;
      align-items: center;
      height: 24px;
      background: #ecf4fe;
      border-radius: 12px;
      margin: 0;

      .item-button {
        cursor: pointer;
        width: 142px;
        height: 24px;
        font-family: PingFangSC;
        font-weight: 400;
        font-size: 12px;
        color: #0b58ff;
        line-height: 24px;
        text-align: center;
        font-style: normal;
        letter-spacing: 8px;
        padding-left: 8px;
      }

      .item-button.active {
        width: 142px;
        height: 24px;
        background: #3071ff;
        border-radius: 12px;
        color: #ffffff;
        font-weight: 500;
      }
    }
  }
}
</style>
