<template>
  <div shadow="never" class="homeBoxCard" v-loading="loading">
    <!-- <div>{{aa}}</div> -->
    <div :style="'height:' + Height" ref="sumeChartRef" />
  </div>
</template>
<script>
import { computed, defineComponent, onMounted, ref, watch } from 'vue'
import { useStore } from 'vuex'
import * as echarts from 'echarts'
import json from './json.js'
// import { ChartDataType } from "../../data";

// const dataAll = [
//   {
//     name: 'IA(A)',
//     type: 'line',
//     smooth: false,
//     data: [0, 0.1, 0, 0, 0, 0, 0, 0, 0, 0.7, 0, 0],
//     symbolSize: 6,
//     markPoint: {
//       data: [
//         { type: 'max', name: 'Max' },
//         { type: 'min', name: 'Min' },
//       ],
//     },
//   },
//   {
//     name: 'IB(A)',
//     type: 'line',
//     smooth: false,
//     data: [0, 0, 0.2, 0, 0, 0, 0, 0.8, 0, 0, 0, 0],
//     symbolSize: 6,
//     markPoint: {
//       data: [
//         { type: 'max', name: 'Max' },
//         { type: 'min', name: 'Min' },
//       ],
//     },
//   },
//   {
//     name: 'IC(A)',
//     type: 'line',
//     smooth: false,
//     data: [0, 0.1, 0, 0, 0, 0.9, 0, 0, 0, 0, 0, 0],
//     symbolSize: 6,
//     markPoint: {
//       data: [
//         { type: 'max', name: 'Max' },
//         { type: 'min', name: 'Min' },
//       ],
//     },
//   },
// ]

const yName = ''

export default defineComponent({
  name: 'CurveCom',
  props: {
    ecahrtsData: Object,
    typeSrarch: Number,
  },

  setup(props) {
    props
    const store = useStore()
    const sumeChartRef = ref(null)
    const newData = ref([])

    // const listData = ref([])

    // 总数
    // num
    const num = computed(() => store.state.Home.worksChartData.num)
    const newDataName = ref('')
    const aa = ref('')
    const dataTime = ref([])

    // 读取数据 func
    const loading = ref(true)
    const getData = async () => {
      loading.value = false
      // console.log('props.ecahrtsData')
      // console.log(props.ecahrtsData)
      aa.value = props.ecahrtsData[0] ? props.ecahrtsData[0].name : ''
      var jsona = JSON.stringify(props.ecahrtsData)
      var jsonb = jsona.replace(/"list"/g, '"data"')
      newData.value = JSON.parse(jsonb)

      newData.value.forEach((value) => {
        value.type = 'line'
        value.smooth = false
        value.symbolSize = 6
        value.markPoint = {
          data: [
            { type: 'max', name: 'Max' },
            { type: 'min', name: 'Min' },
          ],
        }
      })
      console.log(newData.value)

      var arr1 = props.ecahrtsData[0].listDate

      if (props.typeSrarch == 1) {
        dataTime.value = arr1.map(function (item) {
          return item.toString().substring(5, 10)
        })
        console.log('dataTime.value')
        console.log(dataTime.value)
      } else if (props.typeSrarch == 2) {
        dataTime.value = arr1.map(function (item) {
          return item.toString().substring(5, 7) + '月'
        })
      } else {
        dataTime.value = arr1.map(function (item) {
          return item
        })
      }
    }


    function ecahrts() {
      console.log('json.color')
      console.log(json.color)
      let myChart = echarts.init(sumeChartRef.value)
      myChart.setOption(
        {
          color: json.color,
          title: {
            text: '同比分析',
            left: 'center',
            textStyle: {
              color: '#9d9d9d',
              fontSize: 14,
              fontWeight: 600,
            },
          },
          tooltip: {
            trigger: 'axis',
          },
          // 图列组件
          legend: {
             type:'scroll',//就这一个属性 设置legend滚动轴的出现
            itemHeight: 10, //改变圆圈大小
            itemWidth: 26, //改变圆圈大小
            itemGap: 30,
            width: '80%',
            textStyle: {
              color: 'black',
            },

            left: 'center',
            bottom: '0',
          },
          toolbox: {
            show: true,
            // feature: {
            //   saveAsImage: { show: true, color: 'black' },
            // },
            feature: {
              // dataView: { show: true, readOnly: false },
              // magicType: { show: true, type: ["line", "bar"] },
              // restore: { show: true },
              saveAsImage: {
                show: true,
                color: 'black',
                title: '',
                name: '图表下载',
              },
            },
            iconStyle: {
              borderColor: '#666',
            },
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '5%',
            containLabel: true,
          },
          xAxis: {
            type: 'category',
            boundaryGap: true,
            data: props.ecahrtsData[0] ? dataTime.value : '',
            axisTick: {
              show: false, //去除刻度线
            },
            axisLabel: {
              color: 'black', // 文本颜色
            },
            axisLine: {
              show: false, // 去除轴线
            },
          },
          yAxis: {
            name: yName,
            nameTextStyle: {
              color: 'black',
              fontSize: 10,
            },
            type: 'value',
            axisTick: {
              show: true, //去除刻度线
            },
            axisLabel: {
              color: 'black', // 文本颜色
            },
            axisLine: {
              show: true, // 去除轴线
              lineStyle: {
                color: 'black',
              },
            },
            splitNumber: 5,
            splitLine: {
              show: true,
              lineStyle: {
                color: '#9d9d9d',
              },
            },
          },
          series: newData.value,
        },
        true
      )
      window.onresize = function () {
        // 自适应大小
        myChart.resize()
      }
    }

    const writeValue = (val) => {
      val

      getData()
      ecahrts()
    }
    const writeValue2 = (val) => {
      val

      getData()
    }

    //监听变化
    watch(
      () => props.ecahrtsData,
      (newVal, oldVal, clear) => {
        // 执行异步任务，并得到关闭异步任务的 id
        // console.log(newVal)
        let id = writeValue(newVal, oldVal)
        // 如果 watch 监听被重复执行了，则会先清除上次未完成的异步任务
        clear(() => clearTimeout(id))
      },
      { lazy: true }
    )

    //监听变化
    watch(
      () => props.typeSrarch,
      (newVal, oldVal, clear) => {
        // alert(props.typeSrarch)
        // 执行异步任务，并得到关闭异步任务的 id
        // console.log(newVal)
        let id = writeValue2(newVal, oldVal)
        // 如果 watch 监听被重复执行了，则会先清除上次未完成的异步任务
        clear(() => clearTimeout(id))
      },
      { lazy: true }
    )

    onMounted(() => {
      getData()
      ecahrts()
    })

    const Height = ref(0)
    Height.value = window.innerHeight - 210 + 'px'
    window.addEventListener('resize', () => {
      Height.value = window.innerHeight - 210 + 'px'
    })
    return {
      Height,
      sumeChartRef,
      loading,
      aa,
      num,
      newDataName,
      newData,
      ecahrts,
      dataTime,
    }
  },
})
</script>
<style lang="scss" scoped>
.homeBoxCard {
  // margin-bottom: 24px;
  ::v-deep(.el-card__header) {
    padding-left: 12px;
    padding-right: 12px;
  }
  ::v-deep(.el-card__body) {
    padding: 12px;
    font-size: 14px;
    line-height: 1.5715;
  }
  ::v-deep(.el-divider) {
    margin: 8px 0;
  }
  .num {
    font-size: 30px;
    color: #515a6e;
  }
  .height300 {
    height: 43rem;
  }
}
</style>