<template>
  <div class="chartsContainer">
    <div class="chartsContainer-title">巡检计划</div>
    <div class="chartsContainer-list">
      <div class="chartsContainer-content">
        <div class="chartsContainer-content-top">
          <div class="chartsContainer-content-name">计划安排总览</div>
        </div>
        <div class="chartsContainer-content-content" ref="lineChart" />
      </div>

      <div class="chartsContainer-content">
        <div class="chartsContainer-content-top">
          <div class="chartsContainer-content-name">日常任务检查指标累计分布</div>
        </div>
        <div class="chartsContainer-content-content" ref="radar" />
      </div>

      <div class="chartsContainer-content">
        <div class="chartsContainer-content-top">
          <div class="chartsContainer-content-name">任务明细统计</div>
        </div>
        <div class="chartsContainer-content-content">
          <el-table v-auto-scroll :data="tableData" border style="width: 100%" height="100%">
            <el-table-column prop="checkStartTime" label="开始时间" min-width="120" />
            <el-table-column prop="taskName" label="任务名称" />
            <el-table-column prop="checkedLevelDesc" label="检查级别" min-width="80" />
            <el-table-column prop="ruleTypeDesc" label="执行频率" min-width="100">
              <template #default=" scope ">
                {{ scope.row.ruleTypeDesc }}{{ scope.row.ruleTypeNum || 0 }}次
              </template>
            </el-table-column>
            <el-table-column prop="checkEndTime" label="结束时间" min-width="120" />
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { planOverview, planDistribution, planStatistics } from '@/api/item/items'
import { useTimeOut } from './useTimeOut';
import { useECharts } from '@/hooks/useEcharts.js';
import { ref, computed, reactive, inject } from 'vue'
const provideParams = inject('provideParams')
const lineChart = ref(null)
const lineChartData = ref({})
const setChartsOptions = computed(() => {
  return {
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: Object.values(lineChartData.value)[0] ? Object.values(lineChartData.value)[0].map(item => item.date) : [],
      axisLabel: {
        color: '#fff',
        rotate: 25,
        fontSize: 12,
      }
    },
    legend: {
      data: Object.keys(lineChartData.value),
      textStyle: {
        color: '#fff'
      }
    },
    yAxis: {
      type: 'value',
      axisLabel: {
        color: '#fff',
      }
    },
    series: Object.entries(lineChartData.value).map(([ key, vlaue ]) => ({
      data: vlaue.map(item => item.total),
      type: 'line',
      smooth: true,
      name: key
    })),
    grid: {
      left: '5%',
      right: '0%',
      bottom: '10%',
      top: '15%',
      containLabel: true
    }
  };
})
useECharts(lineChart, setChartsOptions);

const radar = ref(null)
const radarData = reactive({
  grounp: [],
  data: [],
  legend: []
})
const setRadarOptions = computed(() => {
  return {
    tooltip: {
      trigger: 'item'
    },
    grid: {
      top: '5%',
      bottom: '5%',
      left: '5%',
      right: '5%'
    },
    radar: {
      indicator: radarData.grounp,
      axisName: {
        color: '#fff'
      },
      radius: '60%',
    },
    series: [
      {
        type: 'radar',
        data: radarData.data,
        color: '#408CFF'
      }
    ]
  }
})
useECharts(radar, setRadarOptions);

const permutationRadarDataHandler = (result = []) => {
  radarData.grounp = result.map(item => ({ text: item.name + ' ' + item.total }))
  radarData.data = [{
    name: '日常任务检查',
    value: result.map(attr => attr.total),
    // // 为每个数据系列设置不同的颜色
    areaStyle: {
      opacity: 0.5
    },
    lineStyle: {
      width: 2
    },
    itemStyle: {
      borderWidth: 2
    }
  }]
}
const tableData = ref([])
useTimeOut(() => {
  provideParams.value.deptId && planOverview({
    startDate: provideParams.value.startDate,
    endDate: provideParams.value.endDate,
    deptId: provideParams.value.deptId === 'ALL' ? '' : provideParams.value.deptId
  }).then(res => {
    lineChartData.value = (res.data || []).reduce((cur, acc) => {
      if (cur[ acc.typeDesc ]) {
        cur[ acc.typeDesc ].push(acc)
      } else {
        cur[ acc.typeDesc ] = [ acc ]
      }
      return cur
    }, {})
  })
  provideParams.value.deptId && planDistribution({
    startDate: provideParams.value.startDate,
    endDate: provideParams.value.endDate,
    deptId: provideParams.value.deptId === 'ALL' ? '' : provideParams.value.deptId
  }).then(res => {
    permutationRadarDataHandler(res.data)
  })
  provideParams.value.deptId && planStatistics({
    startDate: provideParams.value.startDate,
    endDate: provideParams.value.endDate,
    deptId: provideParams.value.deptId === 'ALL' ? '' : provideParams.value.deptId
  }).then(res => {
    tableData.value = res.data
  })
}, [provideParams])



</script>

<style lang="scss" scoped>
.chartsContainer {
  width: 100%;
  height: 100%;
  position: relative;
  background: linear-gradient(to bottom, rgba(8, 97, 117, 0) 0%, #13a2d6 200%);
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  flex-direction: column;

  .chartsContainer-title {
    height: 42px;
    width: 100%;
    background: linear-gradient(to right, #004387 0%, #090B18 100%);
    border-left: 1px solid #1892CE;
    line-height: 42px;
    color: #fff;
    font-weight: 900;
    font-size: 20px;
    text-indent: 1em;
  }

  .chartsContainer-list {
    padding: 20px 15px 10px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    row-gap: 10px;
    flex: 1;
    overflow: hidden;
  }

  .chartsContainer-content {
    display: flex;
    flex-direction: column;
    row-gap: 10px;
    flex: 1;
    overflow: hidden;

    .chartsContainer-content-top {
      display: flex;
      align-items: center;
      color: #78DEF5;

      .chartsContainer-content-name {
        color: #fff;
        font-weight: bold;
        font-size: 16px;
        display: flex;
        align-items: center;

        &::before {
          content: '';
          display: inline-block;
          border-left: 9px solid #1CB6FF;
          border-top: 8px solid transparent;
          border-right: 9px solid transparent;
          border-bottom: 8px solid transparent;
        }
      }
    }

    .chartsContainer-content-describe {
      color: #78DEF5;
      font-size: 12px;
      font-weight: bold;
      display: flex;
      align-items: center;
    }

    .chartsContainer-content-content {
      flex: 1;
      width: 100%;
      --el-bg-color: transparent;
      --el-fill-color-blank: transparent;
      --el-border-color-lighter: #1887A6;
      --el-text-color-regular: #fff;
      --el-fill-color-light: #5e93a1;

      :deep(.el-table) {
        .el-table__header-wrapper th {
          background-color: #1CA7C8 !important;
          color: #fff;
        }
      }
    }
  }
}
</style>