<script lang="ts" setup name="BarChartVertical">
/**
 * 垂直条形图，支持渐变，支持增加背景颜色
 */
import * as echarts from 'echarts/core'
import type { ECharts } from 'echarts'
import { init } from 'echarts'
import type { Ref } from 'vue'
import type { ECBasicOption } from 'echarts/types/dist/shared'
import type { barOption, barSeriesOption, lineDataI } from '@/components/Echart/echart-interface'
import tdTheme from '@/components/Echart/theme.json' // 引入默认主题
const props = defineProps({
  /**
   * id
   */
  id: {
    type: String,
    default: 'chart',
  },
  /**
   * 标题
   */
  title: {
    type: String,
    default: '',
  },
  /**
   * 图表宽
   */
  width: {
    type: String,
    default: '100%',
  },
  /**
   * 图表高
   */
  height: {
    type: String,
    default: '100%',
  },
  /**
   * 数据，格式为[{name:'系列名',data:[0,0,0,...0], color:'可选'},...]
   */
  data: {
    type: Array,
    default: () => { return [] },
  },
})

// 图表对象
let chart: ECharts
const chartRef: Ref<HTMLElement | null> = ref(null)

// 监听数据变化
watch(() => props.data, () => {
  refreshChart()
}, {
  immediate: true,
  deep: true,
})
const barWidth = 10 /* 进度条及进度条radius宽度 */
const nameWidth = 60 /* 进度条名称宽度 */
const unit = '人' /* 单位 */
const attaData = [] /* 进度条数据 */
const attaVal = [] /* 进度条数值 */
const topName = [] /* 进度条名称 */
let salvProMax = [] /* 背景条数据 */
const timer = ref()
const refresh = () => {
  props.data.forEach((it, i) => {
  // let itemStyle = {
  //     color: i > 3 ? attackSourcesColor[3] : attackSourcesColor[i],
  // };
    topName[i] = `${it.name}`
    attaVal[i] = it.value
    attaData[i] = {
      value: parseFloat(it.value).toFixed(2),
    // itemStyle: itemStyle,
    }
  })
}
refresh()
/* 该值无具体作用，取进度最大值 * 1.2 */
salvProMax = Array(attaVal.length).fill(Math.max(...attaVal) * 1.2)
const img3
        = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAAXNSR0IArs4c6QAAAkBJREFUOE+dlT1MU2EUhp/TNgVBibVKtEBM+OnCBizGxMRoMMbEgH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ru/xXadPu8mhTDkufei3Obk+bNr6mvSg9eYvxda90qHh4xds3x9c4JefyCelkMe8oYH8vYTr6/DhgDxbTN861CbNzwG69O/oFHBOO4qEaAO0TV/CNdNE+F3b6BfwJwRsoBqR2D5QAAAAASUVORK5CYII='
// 构建option
function buildOption() {
  const option = {
    grid: [
      {
        left: 0,
        top: 40,
        right: 10,
        bottom: 40,
        containLabel: true,
      },
    ],
    xAxis: [
      {
        gridIndex: 0,
        type: 'value',
        axisLabel: { show: false },
        axisLine: { show: false },
        splitLine: { show: false },
        axisTick: { show: false },
      },
      {
        gridIndex: 0,
        type: 'value',
        max: 100,
        axisLabel: { show: false },
        axisLine: { show: false },
        splitLine: { show: false },
        axisTick: { show: false },
      },
    ],
    yAxis: [
      {
        gridIndex: 0,
        type: 'category',
        inverse: true,
        position: 'left',
        data: [],
        axisTick: { show: false },
        axisLine: { show: false },
        splitLine: { show: false },
        axisLabel: {
          width: 80,
          padding: [0, 0, 40, -20],
          align: 'left',
          formatter: (name, index) => {
            const id = index + 1
            if (id < 4) {
              return `{icon${id}|}`
            }
            else {
              return `{count|${id}}`
            }
          },
          rich: {
            icon1: {
              width: 20,
              height: 20,
              align: 'center',
              borderRadius: 50,
              backgroundColor: {
                image: img1,
              },

            },
            icon2: {
              width: 20,
              height: 20,
              align: 'center',
              borderRadius: 50,
              backgroundColor: {
                image: img2,
              },
            },
            icon3: {
              width: 20,
              height: 20,
              shadowColor: '#3374ba',
              borderColor: '#3374ba',
              borderWidth: 1,
              borderRadius: 50,
              align: 'center',
              backgroundColor: {
                image: img3,
              },
            },
            count: {
              padding: [2, 0, 0, 0],
              width: 20,
              height: 18,
              color: '#000',
              align: 'center',
              fontSize: 12,
              fontFamily: 'DIN',
              fontWeight: 500,
              shadowColor: '#008AFF',
              borderColor: '#008AFF',
              borderRadius: 50,
              borderWidth: 1,
              backgroundColor: {
                image: img4,
              },
            },
            name: {
              width: 80,
              fontSize: 12,
              align: 'left',
              color: '#000',
              fontFamily: 'Source Han Sans CN',
              fontWeight: 500,
            },
          },
        },
      },
      {
        gridIndex: 0,
        type: 'category',
        inverse: true,
        data: [],
        axisTick: { show: false },
        axisLine: { show: false },
        splitLine: { show: false },
        position: 'left',
        axisLabel: {
          width: 80,
          padding: [0, 0, 40, 40],
          align: 'left',
          formatter: (name, index) => {
            return `{value|${name}}`
          },
          rich: {
            value: {
              color: '#000',
              fontSize: 12,
              fontWeight: 500,
            },
          },
        },
      },
      {
        gridIndex: 0,
        type: 'category',
        position: 'right',
        inverse: true,
        margin: 20,
        data: [],
        axisTick: { show: false },
        axisLine: { show: false },
        splitLine: { show: false },
        axisLabel: {
          align: 'right',
          padding: [0, 0, 50, -40],
          formatter: (_, index) => {
            return `{value|${props.data[index].value}}`
          },
          rich: {
            value: {
              color: '#000',
              fontSize: 12,
              fontWeight: 500,
            },
          },
        },
      },
    ],
    series: [] as any[],
  }
  // 数据
  if (props.data && props.data.length > 0) {
    option.yAxis[0].data = props.data.map(item => item.name)
    option.yAxis[1].data = props.data.map(item => item.name)
    option.yAxis[2].data = props.data.map(item => item.name)
    option.series = [
      {
        z: 1,
        type: 'bar',
        xAxisIndex: 0,
        yAxisIndex: 0,
        barWidth: 10,
        data: props.data.map(item => item.value),
        silent: true,
        itemStyle: {
          emphasis: {
            barBorderRadius: [0, 20, 20, 0],
          },
          normal: {
            // barBorderRadius: [0, 20, 20, 0],
            barBorderRadius: [30, 30, 30, 30],
            color: new echarts.graphic.LinearGradient(
              0,
              0,
              1,
              0,
              [
                {
                  offset: 0,
                  color: '#01A6EB', // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: '#005CD8', // 100% 处的颜色
                },
              ],
              false,
            ),
          },
        },
      },
      {
        z: 3,
        name: '背景',
        type: 'bar',
        barWidth: 10,
        barGap: '-100%',
        data: salvProMax,
        itemStyle: {
          normal: {
            color: 'rgba(131, 132, 132, 0.1)',
            barBorderRadius: [30, 30, 30, 30],
            borderColor: '#004FBB',
          },
        },
      },
    ]
  }
  return option
}
// 初始化图表
function initChart() {
  chart = init(chartRef.value as HTMLElement, tdTheme)
  chart.setOption({})
}

// 刷新图表
function refreshChart() {
  if (chart) {
    clearInterval(timer.value)
    const option = buildOption()
    // timer.value = setInterval(() => {
    //   if (option.dataZoom[0].endValue == props.data.length - 1) {
    //     option.dataZoom[0].endValue = 5
    //     option.dataZoom[0].startValue = 0
    //   }
    //   else {
    //     option.dataZoom[0].endValue = option.dataZoom[0].endValue + 1
    //     option.dataZoom[0].startValue = option.dataZoom[0].startValue + 1
    //   }
    // }, 2000)

    chart.setOption(option as unknown as ECBasicOption, true)
  }
}

window.addEventListener('resize', () => {
  chart.resize()
})

onMounted(() => {
  initChart()
})
</script>

<template>
  <div :id="id" ref="chartRef" class="chart" :style="{ height, width }" />
</template>

<style lang="scss" scoped>
.chart {
  width: 100%;
  height: 100%;
}
</style>
