<template>
  <div class="card">
    <div class="card-header">
      <div class="card-title">
        <span class="card-title-icon">
          <Icon icon="fa-solid:bolt" />
        </span>
        <span>能耗周趋势</span>
      </div>
      <div class="card-toolbar">
        <el-select
v-model="selectedEnergyTypeId" placeholder="请选择" class="energy-type-select" size="small"
          @change="handleEnergyTypeChange">
          <el-option v-for="item in energyTypes" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </div>
    </div>
    <div class="card-body">
      <div ref="chartRef" class="chart"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import * as echarts from 'echarts'
import { colors, style } from '../utils'
import { EnergyTypeApi, EnergyTypeVO } from '@/api/mes/energytype'
import { EnergyDeviceApi } from '@/api/mes/energydevice'

const route = useRoute()
const orgId = route.query.orgId

const energyTypes = ref<EnergyTypeVO[]>([])
const selectedEnergyTypeId = ref<number | undefined>(undefined)
const chartRef = ref<HTMLElement | null>(null)
let chart: echarts.ECharts | null = null

const getEnergyTypes = async () => {
  try {
    const res = await EnergyTypeApi.getEnergyTypeList({ orgId })
    const list = (res as any).data || (Array.isArray(res) ? res : [])
    energyTypes.value = list
    if (list.length > 0 && !selectedEnergyTypeId.value) {
      selectedEnergyTypeId.value = list[0].id
      await getChartData()
    }
  } catch (e) {
    console.error('Failed to fetch energy types:', e)
  }
}

const getChartData = async () => {
  if (!selectedEnergyTypeId.value || !chart) return
  try {
    const res = await EnergyDeviceApi.getLatestSevenDaysStatistics({
      deviceTypeId: selectedEnergyTypeId.value,
      orgId: orgId
    })
    render(res)
  } catch (e) {
    console.error('Failed to fetch weekly energy data:', e)
  }
}

const handleEnergyTypeChange = () => {
  getChartData()
}

const render = (data: any = []) => {
  if (!chart) return

  const list = (data as any).data || (Array.isArray(data) ? data : [])
  const x: string[] = []
  const actual: number[] = []
  const baseline: number[] = []

  if (list && list.length > 0) {
    list.forEach((item: any) => {
      const label = item.hour || item.day || ''
      x.push(label)
      const v = Number(item.value ?? item.energy ?? item.consumption ?? 0)
      actual.push(Number.isNaN(v) ? 0 : v)
    })
  }

  chart.setOption({
    backgroundColor: 'transparent',
    tooltip: { trigger: 'axis' },
    legend: { top: 0, right: 0, textStyle: style.legendText },
    grid: { top: '20%', left: '6%', right: '6%', bottom: '14%', containLabel: true },
    xAxis: {
      type: 'category',
      data: x,
      axisLine: style.axisLine,
      axisLabel: { ...style.axisLabel, fontSize: 10, rotate: 40 }
    },
    yAxis: { type: 'value', axisLine: { show: false }, axisLabel: style.axisLabel, splitLine: style.splitLine },
    series: [
      {
        name: '实际能耗(kWh)',
        type: 'bar',
        barWidth: 16,
        itemStyle: {
          borderRadius: [4, 4, 0, 0],
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: colors.danger },
            { offset: 1, color: 'rgba(239,68,68,0.10)' }
          ])
        },
        data: actual
      }
    ]
  })
}

const resize = () => {
  chart?.resize()
}

onMounted(async () => {
  if (!chartRef.value) return
  chart = echarts.init(chartRef.value, 'dark', { renderer: 'canvas' })
  render([])
  await getEnergyTypes()
  window.addEventListener('resize', resize)
})

onUnmounted(() => {
  window.removeEventListener('resize', resize)
  chart?.dispose()
})
</script>

<style scoped>
.card {
  position: relative;
  display: flex;
  padding: 10px;
  overflow: hidden;
  background: linear-gradient(135deg, rgb(15 23 42 / 96%), rgb(15 23 42 / 88%));
  border: 1px solid rgb(30 64 175 / 85%);
  border-radius: 10px;
  box-shadow:
    0 18px 45px rgb(15 23 42 / 95%),
    0 0 0 1px rgb(15 23 42 / 100%),
    inset 0 0 0 1px rgb(56 189 248 / 5%);
  flex-direction: column;
}

.card::before,
.card::after {
  position: absolute;
  width: 13px;
  height: 13px;
  pointer-events: none;
  border: 1px solid rgb(56 189 248 / 75%);
  border-radius: 2px;
  content: "";
  opacity: 0.6;
}

.card::before {
  top: -1px;
  left: -1px;
  border-right: none;
  border-bottom: none;
}

.card::after {
  right: -1px;
  bottom: -1px;
  border-top: none;
  border-left: none;
}

.card-header {
  display: flex;
  padding-bottom: 4px;
  margin-bottom: 8px;
  border-bottom: 1px solid rgb(41 54 95 / 90%);
  align-items: center;
  justify-content: space-between;
}

.card-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 700;
  color: #e5f0ff;
}

.card-title-icon {
  color: #22d3ee;
}

.card-toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
}

.card-body {
  display: flex;
  min-height: 0;
  flex: 1;
  flex-direction: column;
  gap: 8px;
}

.tag {
  padding: 2px 6px;
  font-size: 10px;
  color: #94a3b8;
  border: 1px solid rgb(148 163 184 / 40%);
  border-radius: 999px;
}

.chart {
  width: 100%;
  height: 100%;
  min-height: 180px;
}

.energy-type-select {
  width: 140px;
}

:deep(.el-select__wrapper) {
  color: #94a3b8;
  background-color: transparent;
  border: 1px solid rgb(56 189 248 / 55%);
  box-shadow: 0 0 18px rgb(56 189 248 / 35%);
}

:deep(.el-select__placeholder) {
  color: #94a3b8;
}

:deep(.energy-type-select .el-input__wrapper) {
  background: radial-gradient(circle at 0 0, rgb(56 189 248 / 22%), transparent 70%);
  border-color: rgb(56 189 248 / 85%);
  border-radius: 999px;
  box-shadow: 0 0 18px rgb(56 189 248 / 45%);
}

:deep(.energy-type-select .el-input__wrapper.is-focus) {
  background: radial-gradient(circle at 0 0, rgb(59 130 246 / 35%), transparent 70%);
  border-color: rgb(96 165 250 / 95%);
}

:deep(.energy-type-select .el-input__inner) {
  color: #e0f2fe;
}

@media (width <= 1600px) {
  .chart {
    min-height: 160px;
  }
}
</style>
