<template>
  <div class="chart" ref="chart">
    <v-chart
      :option="option"
      autoresize
      :loading="loading"
      style="width: 100%; height: 100%"
      v-if="option.series.some((item) => item.data.length > 0)"
    />
    <el-empty :image="emptyImg" v-else>
      <template v-slot:description>&nbsp;</template>
    </el-empty>
  </div>
</template>

<script lang="ts">
  import { Vue, Component, Prop } from 'vue-property-decorator'
  import { IAssessmentFlaw } from '../api/common'
  import emptyImg from '@/assets/icon/null.png'

  @Component({})
  export default class Chart extends Vue {
    @Prop({ type: Object, default: () => ({ seriesDatas: [], xaxis: [] }) }) flaw!: IAssessmentFlaw
    @Prop({ type: Boolean }) loading!: boolean
    emptyImg = emptyImg
    get option() {
      const { seriesDatas: series = [], xaxis: xAxis = [] } = this.flaw || {}
      const style = (() => {
        try {
          const { color, fontFamily } = window.getComputedStyle(this.$refs.chart as any, null)
          return { color, fontFamily }
        } catch (error) {
          console.log(error)
        }
        return {}
      })()
      return {
        tooltip: { trigger: 'axis' },
        calculable: true,
        xAxis: { type: 'category', data: xAxis },
        yAxis: [{ type: 'value', name: '单位: m' }],
        legend: { show: true, type: 'scroll', right: 0, itemWidth: 14, itemHeight: 14 },
        grid: { top: 45, left: 30, right: 30, bottom: 30, containLabel: true },
        series: series.map(({ name, seriesData: data }) => ({ name, data, type: 'bar' })),
        title: {
          text: '按缺陷类型统计',
          bottom: 0,
          left: 'center',
          textStyle: { fontSize: 14, ...style }
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .chart {
    height: 100%;
    padding-top: $gutter;
    color: $--color-primary;
  }
</style>
