<template>
  <ChartsContainer title="问题整改">
    <div class="content">
      <div :class="type === 'station' ? 'content-left' : 'content-content'">
        <div class="content-left-item" v-for=" item of correctionData " :key="item.title"
          :style="`--color:${item.color}`">
          <div class="content-left-item-title">{{ item.label }}</div>
          <div class="content-left-item-number">{{ item.value }}</div>
        </div>
      </div>
      <div v-if=" type === 'station' " class="content-right" ref="categoryStack">

      </div>
    </div>
  </ChartsContainer>
</template>

<script setup>
import ChartsContainer from '../ChartsContainer.vue';
import { useTimeOut } from './useTimeOut';
import { useECharts } from '@/hooks/useEcharts.js';
import { ref, computed, reactive, inject } from 'vue'
import { correction, correctionDistribution } from '@/api/item/items'
const props = defineProps({
  type: {
    type: String,
    default: 'station'
  }
})
const provideParams = inject('provideParams')
const correctionData = ref([])
const categoryStack = ref(null)
const categoryStackData = reactive({
  yAxis: [],
  onTimeCompletedCount: [],
  overTimeCompletedCount: [],
  onTimeUnfinishedCount: [],
  overTimeUnfinishedCount: []
})
const setCategoryStackOptions = computed(() => {
  return {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      },
      formatter: function (params) {
        return params[ 0 ].name + '<br/>' +
          params[ 0 ].marker + params[ 0 ].seriesName + ': ' + (params[ 0 ].value || 0) + '<br/>' +
          params[ 1 ].marker + params[ 1 ].seriesName + ': ' + (params[ 1 ].value || 0) + '<br/>' +
          params[ 2 ].marker + params[ 2 ].seriesName + ': ' + (params[ 2 ].value || 0) + '<br/>' +
          params[ 3 ].marker + params[ 3 ].seriesName + ': ' + (params[ 3 ].value || 0)
      }
    },
    grid: {
      left: '15%',
      right: '10%',
      bottom: '10%',
      top: '5%',
    },
    xAxis: {
      type: 'value',
      show: true,
      axisLabel: {
        color: '#fff'
      },
      axisLine: {
        show: true,
      },
      minInterval: 1,
    },
    yAxis: {
      type: 'category',
      data: categoryStackData.yAxis,
      axisLabel: {
        color: '#fff'
      }
    },
    series: [
      {
        name: '按期已完成',
        type: 'bar',
        stack: 'total',
        label: {
          show: true
        },
        color: '#408CFF',
        emphasis: {
          focus: 'series'
        },
        data: categoryStackData.onTimeCompletedCount.map(item => item || undefined)
      },
      {
        name: '超期已完成',
        type: 'bar',
        stack: 'total',
        label: {
          show: true
        },
        color: '#58A55C',
        emphasis: {
          focus: 'series'
        },
        data: categoryStackData.overTimeCompletedCount.map(item => item || undefined)
      },
      {
        name: '按期整改中',
        type: 'bar',
        stack: 'total',
        label: {
          show: true
        },
        color: '#FFC061',
        emphasis: {
          focus: 'series'
        },
        data: categoryStackData.onTimeUnfinishedCount.map(item => item || undefined)
      },
      {
        name: '超期整改中',
        type: 'bar',
        stack: 'total',
        label: {
          show: true
        },
        color: '#DD4D43',
        emphasis: {
          focus: 'series'
        },
        data: categoryStackData.overTimeUnfinishedCount.map(item => item || undefined)
      },
    ]
  }
})

props.type === 'station' && useECharts(categoryStack, setCategoryStackOptions)

useTimeOut(() => {
  provideParams.value.checkedDepartmentId && correction({
    startDate: provideParams.value.startDate,
    endDate: provideParams.value.endDate,
    checkedBrigadeId: provideParams.value.checkedDepartmentId === 'ALL' ? '' : provideParams.value.checkedDepartmentId
  }).then(res => {
    const data = res.data || {}
    correctionData.value = [
      {
        label: '按期已完成',
        value: data.onTimeCompletedCount || '0',
        color: '#408CFF'
      },
      {
        label: '超期已完成',
        value: data.overTimeCompletedCount || '0',
        color: '#58A55C'
      },
      {
        label: '按期整改中',
        value: data.onTimeUnfinishedCount || '0',
        color: '#FFC061'
      },
      {
        label: '超期整改中',
        value: data.overTimeUnfinishedCount || '0',
        color: '#DD4D43'
      }
    ]
  })
  props.type === 'station' && correctionDistribution({
    startDate: provideParams.value.startDate,
    endDate: provideParams.value.endDate,
    checkedDepartmentId: provideParams.value.checkedDepartmentId === 'ALL' ? '' : provideParams.value.checkedDepartmentId
  }).then(res => {
    const yAxis = [];
    const onTimeCompletedCount = [];
    const overTimeCompletedCount = [];
    const onTimeUnfinishedCount = [];
    const overTimeUnfinishedCount = [];
    (res.data || []).forEach(item => {
      yAxis.push(item.deptName)
      onTimeCompletedCount.push(item.onTimeCompletedCount)
      overTimeCompletedCount.push(item.overTimeCompletedCount)
      onTimeUnfinishedCount.push(item.onTimeUnfinishedCount)
      overTimeUnfinishedCount.push(item.overTimeUnfinishedCount)
    });
    categoryStackData.yAxis = yAxis
    categoryStackData.onTimeCompletedCount = onTimeCompletedCount
    categoryStackData.overTimeCompletedCount = overTimeCompletedCount
    categoryStackData.onTimeUnfinishedCount = onTimeUnfinishedCount
    categoryStackData.overTimeUnfinishedCount = overTimeUnfinishedCount
  })
}, [ provideParams ])

</script>

<style lang="scss" scoped>
.content {
  width: 100%;
  height: 100%;
  overflow: hidden;
  display: flex;

  .content-left {
    display: flex;
    flex-direction: column;
    row-gap: 10px;

    .content-left-item {
      display: flex;
      flex: 1;
      flex-direction: column;
      justify-content: space-evenly;
      color: #fff;
      background: #051E40;
      border-radius: 8px;
      width: 140px;
      background: url('../../../../../assets/images/cardBg.png') no-repeat !important;
      background-size: 100% 105% !important;

      .content-left-item-title {
        font-size: 14px;
        display: flex;
        font-weight: bold;
        align-items: center;
        justify-content: center;

        &::before {
          content: '';
          width: 12px;
          height: 12px;
          display: block;
          border-radius: 50%;
          margin-right: 10px;
          background: var(--color);
        }
      }

      .content-left-item-number {
        font-weight: bold;
        font-size: 24px;
        width: 100%;
        text-align: center;
        color: var(--color);
      }
    }
  }

  .content-content {
    display: flex;
    column-gap: 15px;
    flex-wrap: wrap;
    width: 100%;
    height: 100%;
    overflow: hidden;
    justify-content: center;
    align-items: center;


    .content-left-item {
      display: flex;
      width: calc(35%);
      height: calc(30%);
      flex-direction: column;
      justify-content: space-evenly;
      color: #fff;
      border-radius: 8px;
      padding: 10px 0;
      background: url('../../../../../assets/images/cardBg.png') no-repeat !important;
      background-size: 100% 105% !important;
      overflow: hidden;

      .content-left-item-title {
        font-size: 14px;
        display: flex;
        font-weight: bold;
        align-items: center;
        justify-content: center;

        &::before {
          content: '';
          width: 12px;
          height: 12px;
          display: block;
          border-radius: 50%;
          margin-right: 10px;
          background: var(--color);
        }
      }

      .content-left-item-number {
        font-weight: bold;
        font-size: 24px;
        width: 100%;
        text-align: center;
        color: var(--color);
      }
    }
  }

  .content-right {
    flex: 1;
    height: 100%;
  }
}
</style>