<template>
  <div class="fpy-statistics">
    <!-- 顶部栏 -->
    <div class="top-bar">
      <div class="query-group">
        <span class="query-label">统计日期</span>
        <el-date-picker v-model="queryDate" type="date" size="small" :clearable="false"
          value-format="yyyy-MM-dd" format="yyyy-MM-dd" placeholder="请选择日期" style="width: 160px" />
        <!-- 工位Tab额外筛选 -->
        <template v-if="activeTab === 'station'">
          <span class="query-label" style="margin-left: 12px">开始工位</span>
          <el-select v-model="startStation" placeholder="请选择" filterable clearable size="small" style="width: 240px">
            <el-option v-for="item in allStationOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
          <span class="query-label" style="margin-left: 8px">至</span>
          <el-select v-model="endStation" placeholder="请选择" filterable clearable size="small" style="width: 240px">
            <el-option v-for="item in allStationOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </template>
        <el-button type="primary" size="small" icon="el-icon-search" :loading="loading" style="margin-left: 12px"
          @click="handleQuery">查询</el-button>
      </div>
      <div class="tab-group">
        <div :class="['tab-item', activeTab === 'line' ? 'active' : '']" @click="switchTab('line')">整线一次合格</div>
        <div :class="['tab-item', activeTab === 'station' ? 'active' : '']" @click="switchTab('station')">工位一次合格</div>
      </div>
    </div>

    <!-- Tab1: 整线一次合格率 -->
    <div v-show="activeTab === 'line'" class="lines-grid">
      <div class="line-section" v-for="line in lineConfigs" :key="line.key">
        <!-- 段头 -->
        <div class="section-header">
          <div class="section-title-wrap">
            <div :class="['section-accent', line.key === 'module' ? 'accent-blue' : 'accent-green']"></div>
            <div>
              <div class="section-title">{{ line.title }}</div>
              <div class="section-desc">一次合格率统计</div>
            </div>
          </div>
          <div class="section-tag">{{ queryDate }}</div>
        </div>

        <!-- 概览卡片 -->
        <div class="summary-grid">
          <div class="gauge-card">
            <div :ref="'gauge_' + line.key" class="gauge-canvas"></div>
          </div>
          <div class="summary-card">
            <div class="summary-name">上线总数</div>
            <div class="summary-value">{{ lineDataMap[line.key].overview.上线总数 || 0 }}</div>
          </div>
          <div class="summary-card card-ng">
            <div class="summary-name">不合格数</div>
            <div class="summary-value value-danger">{{ lineDataMap[line.key].overview.不合格数 || 0 }}</div>
          </div>
        </div>

        <!-- 产品列表（放在柱状图上方） -->
        <div class="panel table-panel">
          <div class="panel-title-row">
            <div class="panel-title">今日产品列表</div>
            <div class="panel-actions">
              <el-button :loading="exportLoading" :disabled="exportLoading" type="warning" plain size="mini" icon="el-icon-download" @click="exportLineProductList(line)">导出</el-button>
              <el-checkbox v-model="lineDataMap[line.key].onlyNG" size="mini" @change="$forceUpdate()">只看不合格</el-checkbox>
            </div>
          </div>
          <div class="panel-subtitle">
            合格 <span class="count-ok">{{ getPassCount(line.key) }}</span> 件
            &nbsp;/&nbsp;
            不合格 <span class="count-ng">{{ getFailCount(line.key) }}</span> 件
          </div>
          <el-table
            :data="getFilteredProductList(line.key)"
            :header-cell-style="{ background: '#f6f8fb', color: '#314257' }"
            border size="mini" max-height="300"
            :row-class-name="productRowClass"
            @expand-change="(row, expanded) => handleExpandChange(row, expanded)">
            <el-table-column type="expand">
              <template slot-scope="props">
                <div v-loading="ngDetailLoading[props.row.发动机号]" class="expand-content">
                  <el-table :data="ngDetailMap[props.row.发动机号] || []" size="mini" border
                    :header-cell-style="{ background: '#fef0f0', color: '#F56C6C' }">
                    <el-table-column prop="来源" label="来源" width="100" align="center" />
                    <el-table-column prop="工位号" label="工位号" width="120" align="center" />
                    <el-table-column prop="时间" label="时间" width="180" align="center" />
                    <el-table-column prop="描述" label="描述" min-width="150" show-overflow-tooltip />
                  </el-table>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="#" width="50" align="center">
              <template slot-scope="scope">{{ scope.$index + 1 }}</template>
            </el-table-column>
            <el-table-column prop="发动机号" label="产品编号" min-width="200" show-overflow-tooltip />
            <el-table-column prop="产品型号" label="型号" width="70" align="center" />
            <el-table-column prop="当前工位" label="当前工位" width="90" align="center" />
            <el-table-column label="创建时间" width="150" align="center">
              <template slot-scope="scope">{{ formatTime(scope.row.创建时间) }}</template>
            </el-table-column>
            <el-table-column label="状态" width="80" align="center">
              <template slot-scope="scope">
                <el-tag :type="scope.row.是否一次合格 === 1 ? 'success' : 'danger'" size="mini" effect="dark">
                  {{ scope.row.是否一次合格 === 1 ? '合格' : '不合格' }}
                </el-tag>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!-- 工位合格率柱状图 -->
        <div class="panel chart-panel" v-loading="lineDataMap[line.key].chartLoading">
          <div class="panel-title">各工位合格率</div>
          <div class="panel-subtitle">当日各工位一次合格率对比</div>
          <div :ref="'bar_' + line.key" class="bar-canvas"></div>
        </div>
      </div>
    </div>

    <!-- Tab2: 工位一次合格率 -->
    <div v-show="activeTab === 'station'" class="station-section">
      <div class="line-section" style="max-width: 100%;">
        <div class="section-header">
          <div class="section-title-wrap">
            <div class="section-accent accent-purple"></div>
            <div>
              <div class="section-title">工位一次合格率</div>
              <div class="section-desc">{{ startStation && endStation ? startStation + ' ~ ' + endStation : '请选择工位范围' }}</div>
            </div>
          </div>
          <div class="section-tag">{{ queryDate }}</div>
        </div>

        <!-- 概览 -->
        <div class="summary-grid">
          <div class="gauge-card">
            <div ref="gauge_station" class="gauge-canvas"></div>
          </div>
          <div class="summary-card">
            <div class="summary-name">过站总数</div>
            <div class="summary-value">{{ stationData.total }}</div>
          </div>
          <div class="summary-card">
            <div class="summary-name">一次合格</div>
            <div class="summary-value" style="color: #67c23a">{{ stationData.pass }}</div>
          </div>
          <div class="summary-card card-ng">
            <div class="summary-name">不合格</div>
            <div class="summary-value value-danger">{{ stationData.fail }}</div>
          </div>
        </div>

        <!-- 产品列表 -->
        <div class="panel table-panel">
          <div class="panel-title-row">
            <div class="panel-title">产品明细</div>
            <div class="panel-actions">
              <el-button :loading="exportLoading" :disabled="exportLoading" type="warning" plain size="mini" icon="el-icon-download" @click="exportStationProductList">导出</el-button>
              <el-checkbox v-model="stationOnlyNG" size="mini">只看不合格</el-checkbox>
            </div>
          </div>
          <el-table
            :data="filteredStationProductList"
            :header-cell-style="{ background: '#f6f8fb', color: '#314257' }"
            border size="mini" max-height="500"
            :row-class-name="productRowClass"
            @expand-change="(row, expanded) => handleExpandChange(row, expanded)">
            <el-table-column type="expand">
              <template slot-scope="props">
                <div v-loading="ngDetailLoading[props.row.发动机号]" class="expand-content">
                  <el-table :data="ngDetailMap[props.row.发动机号] || []" size="mini" border
                    :header-cell-style="{ background: '#fef0f0', color: '#F56C6C' }">
                    <el-table-column prop="来源" label="来源" width="100" align="center" />
                    <el-table-column prop="工位号" label="工位号" width="120" align="center" />
                    <el-table-column prop="时间" label="时间" width="180" align="center" />
                    <el-table-column prop="描述" label="描述" min-width="150" show-overflow-tooltip />
                  </el-table>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="#" width="50" align="center">
              <template slot-scope="scope">{{ scope.$index + 1 }}</template>
            </el-table-column>
            <el-table-column prop="发动机号" label="产品编号" min-width="300" show-overflow-tooltip />
            <el-table-column label="状态" width="100" align="center">
              <template slot-scope="scope">
                <el-tag :type="scope.row.是否一次合格 === 1 ? 'success' : 'danger'" size="mini" effect="dark">
                  {{ scope.row.是否一次合格 === 1 ? '合格' : '不合格' }}
                </el-tag>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'FirstPassYieldStatistics',
  data() {
    return {
      activeTab: 'line',
      queryDate: '',
      loading: false,
      exportLoading: false,
      pendingCount: 0,
      // 整线Tab
      lineConfigs: [
        { key: 'module', title: '模组段', searchType: 1 },
        { key: 'pack', title: 'PACK段', searchType: 2 }
      ],
      lineDataMap: {
        module: {
          overview: { 上线总数: 0, 一次合格数: 0, 不合格数: 0, 合格率: 0 },
          stationChart: [],
          productList: [],
          chartLoading: false,
          onlyNG: false
        },
        pack: {
          overview: { 上线总数: 0, 一次合格数: 0, 不合格数: 0, 合格率: 0 },
          stationChart: [],
          productList: [],
          chartLoading: false,
          onlyNG: false
        }
      },
      // 工位Tab
      allStationOptions: [],
      startStation: '',
      endStation: '',
      stationData: {
        total: 0,
        pass: 0,
        fail: 0,
        rate: 0,
        productList: []
      },
      stationOnlyNG: false,
      // 不合格详情
      ngDetailMap: {},
      ngDetailLoading: {},
      // ECharts
      chartInstances: {}
    }
  },
  created() {
    this.queryDate = this.getToday()
    this.loadStationOptions()
    this.handleQuery()
  },
  mounted() {
    window.addEventListener('resize', this.handleResize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize)
    Object.keys(this.chartInstances).forEach(key => {
      if (this.chartInstances[key]) this.chartInstances[key].dispose()
    })
    this.chartInstances = {}
  },
  methods: {
    getToday() {
      const now = new Date()
      return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`
    },
    switchTab(tab) {
      if (this.activeTab === tab) return
      this.activeTab = tab
      // v-show切换后需要resize已有图表
      this.$nextTick(() => {
        this.handleResize()
        this.handleQuery()
      })
    },
    loadStationOptions() {
      const param = []
      const Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.allStationOptions = (response.data || []).map(item => ({
          label: `【${item.工位号}】${item.工位名称 || ''}`,
          value: item.工位号
        }))
        // 自然排序：OP450-1, OP450-2, ..., OP450-9, OP450-10, OP450-11
        this.allStationOptions.sort((a, b) => a.value.localeCompare(b.value, undefined, { numeric: true }))
      })
    },
    handleQuery() {
      if (this.activeTab === 'line') {
        this.queryLineFPY()
      } else {
        this.queryStationFPY()
      }
    },
    finishRequest() {
      this.pendingCount--
      if (this.pendingCount <= 0) {
        this.loading = false
        this.pendingCount = 0
      }
    },

    // ========== 整线Tab ==========
    queryLineFPY() {
      this.loading = true
      this.ngDetailMap = {}
      this.ngDetailLoading = {}
      this.pendingCount = this.lineConfigs.length * 3
      this.lineConfigs.forEach(line => {
        this.getLineOverview(line)
        this.getStationChart(line)
        this.getProductList(line)
      })
    },
    getLineOverview(line) {
      const param = [['查询日期', this.queryDate], ['SearchType', line.searchType]]
      const Data = this.CreateData('11', '一次合格率_整线概览', param)
      this.ExecDatabase(Data).then(response => {
        const d = response.data && response.data[0] ? response.data[0] : {}
        this.lineDataMap[line.key].overview = {
          上线总数: Number(d.上线总数 || 0),
          一次合格数: Number(d.一次合格数 || 0),
          不合格数: Number(d.不合格数 || 0),
          合格率: Number(d.合格率 || 0)
        }
        this.$nextTick(() => this.drawGauge('gauge_' + line.key, this.lineDataMap[line.key].overview.合格率))
      }).catch(() => {
        this.lineDataMap[line.key].overview = { 上线总数: 0, 一次合格数: 0, 不合格数: 0, 合格率: 0 }
      }).finally(() => this.finishRequest())
    },
    getStationChart(line) {
      this.lineDataMap[line.key].chartLoading = true
      const param = [['查询日期', this.queryDate], ['SearchType', line.searchType]]
      const Data = this.CreateData('11', '一次合格率_工位柱状图', param)
      this.ExecDatabase(Data).then(response => {
        this.lineDataMap[line.key].stationChart = response.data || []
        this.$nextTick(() => this.drawBarChart(line))
      }).catch(() => {
        this.lineDataMap[line.key].stationChart = []
      }).finally(() => {
        this.lineDataMap[line.key].chartLoading = false
        this.finishRequest()
      })
    },
    getProductList(line) {
      const param = [['查询日期', this.queryDate], ['SearchType', line.searchType]]
      const Data = this.CreateData('11', '一次合格率_产品列表', param)
      this.ExecDatabase(Data).then(response => {
        this.lineDataMap[line.key].productList = response.data || []
      }).catch(() => {
        this.lineDataMap[line.key].productList = []
      }).finally(() => this.finishRequest())
    },
    getPassCount(key) {
      return (this.lineDataMap[key].productList || []).filter(p => p.是否一次合格 === 1).length
    },
    getFailCount(key) {
      return (this.lineDataMap[key].productList || []).filter(p => p.是否一次合格 !== 1).length
    },
    getFilteredProductList(key) {
      const list = this.lineDataMap[key].productList || []
      if (this.lineDataMap[key].onlyNG) {
        return list.filter(p => p.是否一次合格 !== 1)
      }
      return list
    },
    exportLineProductList(line) {
      if (this.getFilteredProductList(line.key).length === 0) {
        this.$message.warning('暂无数据')
        return
      }
      const param = [['查询日期', this.queryDate], ['SearchType', line.searchType]]
      const exportData = JSON.parse(this.CreateData('2005', '一次合格率_产品列表', param))
      exportData.exportFileName = `${line.title}一次合格率产品列表_`
      exportData.exportColumns = '发动机号=产品编号,产品型号=型号,上线时间,下线时间'
      const Data = JSON.stringify(exportData)
      this.runExcelExport(Data, 'exportLoading')
    },
    formatTime(val) {
      if (!val) return ''
      const d = new Date(val)
      return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')} ${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}:${String(d.getSeconds()).padStart(2, '0')}`
    },

    // ========== 工位Tab ==========
    // 根据排序后的下拉列表，取开始~结束之间所有工位号拼成逗号分隔字符串
    buildStationList() {
      const values = this.allStationOptions.map(o => o.value)
      const startIdx = values.indexOf(this.startStation)
      const endIdx = values.indexOf(this.endStation)
      if (startIdx === -1 || endIdx === -1) return ''
      const from = Math.min(startIdx, endIdx)
      const to = Math.max(startIdx, endIdx)
      return values.slice(from, to + 1).join(',')
    },
    queryStationFPY() {
      if (!this.startStation || !this.endStation) {
        this.$message.warning('请选择开始工位和结束工位')
        return
      }
      const stationList = this.buildStationList()
      if (!stationList) {
        this.$message.warning('工位范围无效')
        return
      }
      this.loading = true
      this.ngDetailMap = {}
      this.ngDetailLoading = {}
      const param = [['查询日期', this.queryDate], ['工位号列表', stationList]]
      const Data = this.CreateData('11', '一次合格率_工位合格率', param)
      this.ExecDatabase(Data).then(response => {
        const list = response.data || []
        const total = list.length
        const pass = list.filter(p => p.是否一次合格 === 1).length
        const fail = total - pass
        const rate = total > 0 ? Math.round(pass * 1000 / total) / 10 : 0
        this.stationData = { total, pass, fail, rate, productList: list }
        this.$nextTick(() => this.drawGauge('gauge_station', rate))
      }).catch(() => {
        this.stationData = { total: 0, pass: 0, fail: 0, rate: 0, productList: [] }
      }).finally(() => { this.loading = false })
    },
    exportStationProductList() {
      const stationList = this.buildStationList()
      if (!this.startStation || !this.endStation || !stationList) {
        this.$message.warning('请选择有效的工位范围')
        return
      }
      if (this.filteredStationProductList.length === 0) {
        this.$message.warning('暂无数据')
        return
      }
      const param = [['查询日期', this.queryDate], ['工位号列表', stationList]]
      const exportData = JSON.parse(this.CreateData('2005', '一次合格率_工位合格率', param))
      exportData.exportFileName = '工位一次合格率产品明细_'
      exportData.exportColumns = '发动机号=产品编号,产品型号=型号,上线时间,下线时间'
      const Data = JSON.stringify(exportData)
      this.runExcelExport(Data, 'exportLoading')
    },

    // ========== 不合格详情 ==========
    handleExpandChange(row, expandedRows) {
      if (row.是否一次合格 !== 1 && !this.ngDetailMap[row.发动机号]) {
        this.loadNGDetail(row.发动机号)
      }
    },
    loadNGDetail(engineNo) {
      this.$set(this.ngDetailLoading, engineNo, true)
      const param = [['发动机号', engineNo]]
      const Data = this.CreateData('11', '一次合格率_不合格详情', param)
      this.ExecDatabase(Data).then(response => {
        this.$set(this.ngDetailMap, engineNo, response.data || [])
      }).catch(() => {
        this.$set(this.ngDetailMap, engineNo, [])
      }).finally(() => {
        this.$set(this.ngDetailLoading, engineNo, false)
      })
    },

    // ========== ECharts ==========
    drawGauge(refName, rate) {
      const dom = this.$refs[refName]
      if (!dom) return
      const el = Array.isArray(dom) ? dom[0] : dom
      if (!el) return
      if (!this.chartInstances[refName]) {
        this.chartInstances[refName] = echarts.init(el)
      }
      const colorVal = rate >= 95 ? '#67c23a' : rate >= 80 ? '#e6a23c' : '#f56c6c'
      const option = {
        series: [{
          type: 'gauge',
          startAngle: 220,
          endAngle: -40,
          min: 0,
          max: 100,
          progress: { show: true, width: 14, itemStyle: { color: colorVal } },
          axisLine: {
            lineStyle: {
              width: 14,
              color: [[0.8, '#f56c6c'], [0.95, '#e6a23c'], [1, '#67c23a']]
            }
          },
          axisTick: { show: false },
          splitLine: { show: false },
          axisLabel: { show: false },
          pointer: { show: false },
          anchor: { show: false },
          title: { show: true, offsetCenter: [0, '65%'], fontSize: 13, color: '#667788' },
          detail: {
            valueAnimation: true,
            fontSize: 28,
            fontWeight: 'bold',
            formatter: '{value}%',
            color: '#1d2b3a',
            offsetCenter: [0, '10%']
          },
          data: [{ value: rate, name: '一次合格率' }]
        }]
      }
      this.chartInstances[refName].setOption(option, true)
    },
    drawBarChart(line) {
      const refName = 'bar_' + line.key
      const dom = this.$refs[refName]
      if (!dom) return
      const el = Array.isArray(dom) ? dom[0] : dom
      if (!el) return
      if (!this.chartInstances[refName]) {
        this.chartInstances[refName] = echarts.init(el)
      }
      const data = this.lineDataMap[line.key].stationChart
      if (data.length === 0) {
        this.chartInstances[refName].clear()
        this.chartInstances[refName].setOption({
          title: { text: '当日暂无过站数据', left: 'center', top: 'middle', textStyle: { color: '#8b97a6', fontSize: 14, fontWeight: 400 } },
          xAxis: { show: false }, yAxis: { show: false }, series: []
        })
        return
      }
      const stations = data.map(d => d.工位号)
      const rates = data.map(d => Number(d.合格率))
      const ngCounts = data.map(d => Number(d.不合格数))
      const barHeight = Math.max(data.length * 28 + 80, 250)
      el.style.height = barHeight + 'px'
      this.chartInstances[refName].resize()

      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'shadow' },
          formatter: params => {
            const idx = params[0].dataIndex
            const d = data[idx]
            return `<b>${d.工位号}</b> ${d.工位名称 || ''}<br/>合格率: ${d.合格率}%<br/>过站: ${d.过站总数} / 不合格: ${d.不合格数}`
          }
        },
        grid: { left: 10, right: 60, top: 10, bottom: 10, containLabel: true },
        xAxis: {
          type: 'value', min: 0, max: 100,
          axisLabel: { formatter: '{value}%', color: '#5b6b7c' },
          splitLine: { lineStyle: { color: '#e8edf3' } }
        },
        yAxis: {
          type: 'category', data: stations, inverse: true,
          axisLabel: { color: '#5b6b7c', fontSize: 11 },
          axisLine: { lineStyle: { color: '#b8c4d3' } }
        },
        series: [{
          type: 'bar',
          data: rates.map((v, i) => ({
            value: v,
            itemStyle: {
              color: v >= 95 ? '#67c23a' : v >= 80 ? '#e6a23c' : '#f56c6c',
              borderRadius: [0, 4, 4, 0]
            }
          })),
          barMaxWidth: 16,
          label: {
            show: true, position: 'right',
            formatter: p => p.value + '%',
            fontSize: 11, color: '#314257'
          }
        }]
      }
      this.chartInstances[refName].clear()
      this.chartInstances[refName].setOption(option)
    },
    handleResize() {
      Object.keys(this.chartInstances).forEach(key => {
        if (this.chartInstances[key]) this.chartInstances[key].resize()
      })
    },

    // ========== 辅助 ==========
    productRowClass({ row }) {
      return row.是否一次合格 !== 1 ? 'row-ng' : ''
    }
  },
  computed: {
    filteredStationProductList() {
      if (this.stationOnlyNG) {
        return (this.stationData.productList || []).filter(p => p.是否一次合格 !== 1)
      }
      return this.stationData.productList || []
    }
  }
}
</script>

<style scoped>
.fpy-statistics {
  width: 100%;
  min-height: 100%;
  padding: 12px;
  background: linear-gradient(180deg, #eef3f8 0%, #e7edf4 100%);
  box-sizing: border-box;
}

/* 顶部栏 */
.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid #d8e0ea;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(38, 58, 89, 0.06);
  backdrop-filter: blur(8px);
  flex-wrap: wrap;
  gap: 10px;
}
.query-group {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: #f7f9fc;
  border: 1px solid #dde5ef;
  border-radius: 12px;
  flex-wrap: wrap;
}
.query-label {
  font-size: 13px;
  color: #516274;
  font-weight: 600;
  white-space: nowrap;
}
.tab-group {
  display: flex;
  background: #f0f3f7;
  border-radius: 10px;
  padding: 3px;
}
.tab-item {
  padding: 8px 20px;
  font-size: 13px;
  font-weight: 600;
  color: #667788;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}
.tab-item:hover { color: #2f67d8; }
.tab-item.active {
  background: #fff;
  color: #2f67d8;
  box-shadow: 0 2px 8px rgba(47, 103, 216, 0.12);
}

/* 左右两栏 */
.lines-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-top: 14px;
}
.station-section {
  margin-top: 14px;
}
.line-section {
  background: rgba(255, 255, 255, 0.94);
  border: 1px solid #d8e0ea;
  border-radius: 18px;
  box-shadow: 0 10px 28px rgba(38, 58, 89, 0.06);
  overflow: hidden;
}

/* 段头 */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 18px;
  background: linear-gradient(180deg, #fbfcfe 0%, #f3f6fa 100%);
  border-bottom: 1px solid #e1e7ef;
}
.section-title-wrap {
  display: flex;
  align-items: center;
  gap: 12px;
}
.section-accent {
  width: 4px;
  height: 34px;
  border-radius: 999px;
}
.accent-blue { background: linear-gradient(180deg, #2f67d8 0%, #6aa1ff 100%); }
.accent-green { background: linear-gradient(180deg, #1d9b84 0%, #56d4b8 100%); }
.accent-purple { background: linear-gradient(180deg, #7c3aed 0%, #a78bfa 100%); }
.section-title { font-size: 16px; font-weight: 700; color: #233445; }
.section-desc { margin-top: 4px; font-size: 12px; color: #768698; }
.section-tag {
  padding: 5px 10px;
  background: #eef3f8;
  border: 1px solid #d8e2ec;
  border-radius: 999px;
  font-size: 12px;
  color: #556575;
}

/* 概览卡片 */
.summary-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 12px;
  padding: 16px 18px;
}
.summary-card {
  padding: 16px;
  border: 1px solid #e1e7ef;
  border-radius: 14px;
  background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8);
}
.card-ng { border-color: #fde2e2; background: linear-gradient(180deg, #fff5f5 0%, #fef0f0 100%); }
.gauge-card {
  padding: 8px;
  border: 1px solid #e1e7ef;
  border-radius: 14px;
  background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
}
.gauge-canvas { width: 100%; height: 160px; }
.summary-name { font-size: 13px; color: #667788; }
.summary-value { margin-top: 8px; font-size: 32px; font-weight: 700; color: #223244; letter-spacing: 1px; }
.value-danger { color: #f56c6c; }

/* 面板 */
.panel {
  display: flex;
  flex-direction: column;
  margin: 0 18px 18px 18px;
  padding: 14px;
  border: 1px solid #e1e7ef;
  border-radius: 14px;
  background: #ffffff;
}
.panel-title { font-size: 15px; font-weight: 700; color: #233445; }
.panel-title-row { display: flex; justify-content: space-between; align-items: center; }
.panel-actions { display: flex; align-items: center; gap: 10px; }
.panel-subtitle { margin-top: 4px; margin-bottom: 10px; font-size: 12px; color: #6f7f8f; }
.bar-canvas { width: 100%; min-height: 250px; }

/* 统计标签 */
.count-ok { color: #67c23a; font-weight: 700; }
.count-ng { color: #f56c6c; font-weight: 700; }

/* 展开行 */
.expand-content { padding: 8px 16px; }

/* 不合格行高亮 */
::v-deep .row-ng { background: #fef0f0 !important; }
::v-deep .row-ng:hover > td { background: #fde2e2 !important; }

@media (max-width: 1280px) {
  .lines-grid { grid-template-columns: 1fr; }
}
</style>
