<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="pieCategory" />
      </div>

       <div class="chartsContainer-content">
        <div class="chartsContainer-content-top">
          <div class="chartsContainer-content-name">查获部位</div>
        </div>
        <div class="chartsContainer-content-content" ref="pieAppPosition" />
      </div>

       <div class="chartsContainer-content">
        <div class="chartsContainer-content-top">
          <div class="chartsContainer-content-name">安检岗位</div>
        </div>
        <div class="chartsContainer-content-content" ref="pieLargeScreenPost" />
      </div>
      
      <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>
  </div>
</template>

<script setup>
import { category, appPosition, largeScreenPost, appTimeSpan } from '@/api/item/items'
import { useTimeOut } from './useTimeOut';
import { useECharts } from '@/hooks/useEcharts.js';
import { ref, computed, inject } from 'vue'
const provideParams = inject('provideParams')
const pieCategory = ref(null)
const pieCategoryData = ref([])
const setChartsOptions = computed(() => {
  return {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      show: false
    },
    series: [
      {
        name: '违禁品类别',
        type: 'pie',
        radius: [ '0%', '50%' ], // 环形图设置
        center: [ '50%', '50%' ],
        data: pieCategoryData.value.map(item => {
          return {
            name: item.name,
            value: item.total
          }
        }),
        label: {
          show: true,
          formatter: function (params) {
            return params.name + '\n' + params.percent + '%'
          },
          color: '#fff',
          fontSize: 14,
        },
        labelLine: {
          length: 15
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
})
useECharts(pieCategory, setChartsOptions);

const pieAppPosition = ref(null)
const pieAppPositionData = ref([])
const setpieAppPositionOptions = computed(() => {
  return {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      show: false
    },
    series: [
      {
        name: '查获部位',
        type: 'pie',
        radius: [ '0%', '50%' ], // 环形图设置
        center: [ '50%', '50%' ],
        data: pieAppPositionData.value.map(item => {
          return {
            name: item.name,
            value: item.total
          }
        }),
        label: {
          show: true,
          formatter: function (params) {
            return params.name + '\n' + params.percent + '%'
          },
          color: '#fff',
          fontSize: 14,
        },
        labelLine: {
          length: 15
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
})
useECharts(pieAppPosition, setpieAppPositionOptions);

const pieLargeScreenPost = ref(null)
const pieLargeScreenPostData = ref([])
const setpieLargeScreenPostOptions = computed(() => {
  return {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      show: false
    },
    series: [
      {
        name: '安检岗位',
        type: 'pie',
        radius: [ '0%', '50%' ], // 环形图设置
        center: [ '50%', '50%' ],
        data: pieLargeScreenPostData.value.map(item => {
          return {
            name: item.name,
            value: item.total
          }
        }),
        label: {
          show: true,
          formatter: function (params) {
            return params.name + '\n' + params.percent + '%'
          },
          color: '#fff',
          fontSize: 14,
        },
        labelLine: {
          length: 15
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
})
useECharts(pieLargeScreenPost, setpieLargeScreenPostOptions);


const lineChart = ref(null)
const lineChartData = ref([])
const setlineChartOptions = computed(() => {
  return {
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: lineChartData.value.map(item => item.hourOfDay),
      axisLabel: {
        color: '#fff',
        rotate: 25,
        fontSize: 12,
      }
    },
    yAxis: {
      type: 'value',
      minInterval: 1,
      max: Math.max(...lineChartData.value.map(item => item.total)) * 2,
      axisLabel: {
        color: '#fff',
      }
    },
    series: {
      data: lineChartData.value.map(item => item.total),
      type: 'line',
      smooth: true,
    },
    grid: {
      left: '5%',
      right: '0%',
      bottom: '10%',
      top: '5%',
      containLabel: true
    }
  };
})
useECharts(lineChart, setlineChartOptions);

useTimeOut(() => {
  provideParams.value.inspectDepartmentId && category({
    startDate: provideParams.value.startDate,
    endDate: provideParams.value.endDate,
    inspectBrigadeId: provideParams.value.inspectDepartmentId === 'ALL' ? '' : provideParams.value.inspectDepartmentId
  }).then(res => {
    pieCategoryData.value = (res.data || [])
  })
  provideParams.value.inspectDepartmentId && appPosition({
    startDate: provideParams.value.startDate,
    endDate: provideParams.value.endDate,
    inspectBrigadeId: provideParams.value.inspectDepartmentId === 'ALL' ? '' : provideParams.value.inspectDepartmentId
  }).then(res => {
    pieAppPositionData.value = (res.data || [])
  })
  provideParams.value.inspectDepartmentId && largeScreenPost({
    startDate: provideParams.value.startDate,
    endDate: provideParams.value.endDate,
    inspectBrigadeId: provideParams.value.inspectDepartmentId === 'ALL' ? '' : provideParams.value.inspectDepartmentId
  }).then(res => {
    pieLargeScreenPostData.value = (res.data || [])
  })
 provideParams.value.inspectDepartmentId &&  appTimeSpan({
    startDate: provideParams.value.startDate,
    endDate: provideParams.value.endDate,
    inspectBrigadeId: provideParams.value.inspectDepartmentId === 'ALL' ? '' : provideParams.value.inspectDepartmentId
  }).then(res => {
    lineChartData.value = Array.isArray(res.data) ? Array.from({length: 24}, (_, index) => {
      const item = res.data.find(item => Number(item.hourOfDay) === index)
      if (item) {
        return {
          total: item.total,
          hourOfDay: `${index}:00`
        }
      } else {
        return {
          total: 0,
          hourOfDay: `${index}:00`
        }
      }
    }) : []
    
  })
}, [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;
    column-gap: 20px;
    row-gap: 20px;
    flex: 1;
    overflow: hidden;
    flex-wrap: wrap;
  }

  .chartsContainer-content {
    display: flex;
    flex-direction: column;
    row-gap: 10px;
    width: calc(50% - 10px);
    height: calc(50% - 10px);
    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%;
      overflow: hidden;
    }
  }
}
</style>