<template>
  <!-- 近12月钢瓶数增长统计 -->
  <div class="border" :style="{height: boxHeight}">
    <div class="card_box">
      <div class="title_trapezoid center_in">
        <span class="center_in">{{title}}</span>
      </div>
    </div>
    <div class="card_collect">
      <div id="statistics"></div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'wavyBlock',
    props: {
      title: {
        type: String,
        default: () => '文字',
      },
      boxHeight: {
        type: String,
        default: () => '95%',
      },
      dataList: {
        type: Array,
        default: () => [],
      },
    },
    watch: {
      dataList: {
        deep: true, // 立即触发监听函数
        immediate: true,
        handler(newVal) {
          if (newVal) {
            this.growthList = newVal;
          }
          this.$nextTick(() => {
            this.getEnterpriseCylinder()
          })
        },
      },
    },
    data() {
      return {
        growthList: [],
      }
    },
    mounted() {},
    methods: {
      getEnterpriseCylinder() {
        var chartDom = document.getElementById('statistics');
        var myChart = this.$echarts.init(chartDom);
        var option;
        // 近12个月
        var nameArr = [];
        // var data = new Date();
        // var year = data.getFullYear();
        // data.setMonth(data.getMonth() + 1, 1) //获取到当前月份,设置月份
        // for (var i = 0; i < 12; i++) {
        //   data.setMonth(data.getMonth() - 1); //每次循环一次 月份值减1
        //   var m = data.getMonth() + 1;
        //   m = m < 10 ? "0" + m : m;
        //   nameArr.push(data.getFullYear() + "-" + (m));
        // }
        // const valueArr = [150, 60, 80, 180, 120, 160, 80, 40, 20, 30, 22, 13]
        const valueArr = []
        if (this.growthList.length > 0) {
          this.growthList.forEach((item, index) => {
            nameArr.push(item.month)
            valueArr.push(item.count)
          })
        }
        let showFlag = true
        if (valueArr.length > 0) {
          showFlag = false
        } else if (valueArr.length == 0) {
          showFlag = true
        }
        option = {
          title: {
            show: showFlag,
            textStyle: {
              color: '#ffffff',
              fontSize: 18,
              fontWeight: 'normal',
            },
            text: '暂无数据',
            left: 'center',
            top: 'center',
          },
          grid: {
            top: '10%',
            left: '15%',
            right: '5%',
            bottom: '12%'
          },
          tooltip: {
            trigger: 'axis',
            backgroundColor: '#3A4667',
            borderColor: '#3A4667',
            textStyle: {
              color: '#fff'
            },
            formatter: '{b} : {c}瓶',
            axisPointer: {
              type: 'cross',
              crossStyle: {
                color: '#999'
              }
            }
          },
          legend: {
            icon: 'rect',
            top: 10,
            right: 5,
            itemWidth: 10,
            itemHeight: 10,
            textStyle: {
              fontSize: 12, // 字体大小
              color: '#B3CFFF' // 字体颜色
            }
          },
          xAxis: {
            type: 'category',
            axisPointer: {
              type: 'shadow'
            },
            axisLine: {
              lineStyle: {
                color: 'rgba(112, 138, 198, 1)',
              },
            },
            axisLabel: {
              textStyle: {
                color: '#B3CFFF', // x轴文本颜色
                fontSize: 12
              },
              // rotate:30,
            },
            axisTick: {
              show: false
            },
            data: nameArr
          },
          yAxis: {
            type: 'value',
            splitLine: {
              show: true,
              lineStyle: {
                color: '#162647',
                type: 'solid'
              }
            },
            axisLabel: {
              formatter: '{value}瓶',
              textStyle: {
                color: '#B3CFFF', // x轴文本颜色
                fontSize: 12
              },
            },
            name: '',
            nameTextStyle: {
              color: '#B3CFFF',
              fontSize: 12,
            }
          },
          series: [{
              type: 'bar',
              name: '',
              barWidth: 16,
              emphasis: {
                itemStyle: {
                  color: '#7fb7e9'
                }
              },
              itemStyle: {
                normal: {
                  color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                      offset: 1,
                      color: 'rgba(147, 157, 223, 1)',
                      opacity: 0.6
                    },
                    {
                      offset: 0,
                      color: 'rgba(147, 157, 223, 0)',
                      opacity: 1
                    }
                  ]),
                }
              },
              data: valueArr
            },
            {
              name: '',
              type: 'pictorialBar',
              itemStyle: {
                normal: {
                  color: 'rgba(147, 157, 223, 1)'
                }
              },
              symbolRotate: 0,
              symbolSize: ['16', '3'],
              symbolPosition: 'end',
              data: valueArr,
              z: 3
            },
          ]
        }
        myChart.setOption(option);
      }
    }
  }
</script>

<style lang="scss" scoped>
  .border {
    border: 1px solid #3077ca;
    margin: 0 10px 0 auto;
    border-radius: 4px;
    box-shadow: inset 0 0 40px rgba(48, 119, 202, 0.3);
  }


  .card_box {
    position: absolute;
    top: 0;
    left: calc(50% - 100px);
    right: 0;
  }

  .title_trapezoid {
    position: relative;
    color: #fff !important;
    width: 200px;
    height: 30px;
    text-align: center;
    z-index: 2;
  }

  .title_trapezoid span {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 200px;
    top: -1px;
    z-index: 2;
    font-size: 15px;
    line-height: 25px;
  }

  .title_trapezoid::before {
    border-bottom: 2px solid #2CD5FF;
    content: '';
    z-index: 1;
    width: 100%;
    height: 30px;
    background-image: linear-gradient(to top, #3077ca99, #3077ca7f, transparent);
    display: inline-block;
    position: absolute;
    left: 0;
    top: -1px;
    transform: perspective(100px) rotateX(150deg);
  }

  .card_collect {
    display: flex;
    flex-wrap: wrap;
    flex-direction: row;
    margin-top: 30px;
    height: calc(100% - 30px);
  }

  #statistics {
    width: 100%;
    height: 100%;
  }
</style>
