<template>
  <div class="business-stat-list" :class="{ 'is-compact': compact, 'is-dense': rows.length > 10 }">
    <div v-if="!rows.length" class="business-stat-empty">暂无业务统计数据</div>
    <div
      v-for="(item, index) in rows"
      :key="item.key || item.name"
      class="business-stat-row"
      :data-chart-tooltip="rowTooltip(item)"
    >
      <div class="business-stat-rank">{{ index + 1 }}</div>
      <div class="business-stat-main">
        <div class="business-stat-name">{{ item.name }}</div>
        <div class="business-stat-bar">
          <span :style="{ width: `${barWidth(item)}%` }"></span>
        </div>
      </div>
      <div class="business-stat-value">
        <strong>{{ item.value }}</strong>
        <em>{{ item.unit }}</em>
        <span v-if="item.extra">{{ item.extra }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from "vue"

const props = defineProps({
  rows: {
    type: Array,
    default: () => [],
  },
  compact: {
    type: Boolean,
    default: false,
  },
})

const maxValue = computed(() => {
  const values = props.rows.map((item) => Number(item.value)).filter((value) => !Number.isNaN(value))
  return Math.max(...values, 1)
})

function barWidth(item) {
  const value = Number(item.value)
  if (Number.isNaN(value)) return 36
  return Math.max(8, Math.round((value / maxValue.value) * 100))
}

function rowTooltip(item) {
  const value = item?.value === undefined || item?.value === null || item?.value === "" ? "--" : item.value
  const unit = item?.unit || ""
  const lines = [`数值：${value}${unit}`]
  if (item?.extra) lines.push(String(item.extra))
  return [item?.name || "数据", ...lines].join("\n")
}
</script>

<style lang="scss">
.business-stat-list {
  height: 100%;
  padding: 14px 16px;
  box-sizing: border-box;
  overflow-y: auto;

  &::-webkit-scrollbar {
    width: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(48, 220, 255, 0.28);
  }

  &.is-compact {
    padding: 6px 14px;

    .business-stat-row {
      height: 30px;
      gap: 8px;
      grid-template-columns: 22px minmax(0, 1fr) 104px;
    }

    .business-stat-rank {
      width: 19px;
      height: 19px;
      font-size: 11px;
      line-height: 17px;
    }

    .business-stat-name {
      font-size: 12px;
    }

    .business-stat-bar {
      height: 4px;
      margin-top: 5px;
    }

    .business-stat-value {
      strong {
        font-size: 15px;
      }

      em {
        font-size: 10px;
      }

      span {
        font-size: 10px;
      }
    }
  }

  &.is-compact.is-dense {
    padding: 4px 12px 6px;

    .business-stat-row {
      height: 20px;
      gap: 6px;
      grid-template-columns: 19px minmax(0, 1fr) 92px;
    }

    .business-stat-rank {
      width: 16px;
      height: 16px;
      font-size: 10px;
      line-height: 14px;
    }

    .business-stat-name {
      font-size: 11px;
      line-height: 13px;
    }

    .business-stat-bar {
      height: 3px;
      margin-top: 2px;
    }

    .business-stat-value {
      strong {
        font-size: 13px;
        line-height: 15px;
      }

      em {
        font-size: 10px;
      }
    }
  }
}

.business-stat-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: rgba(224, 250, 255, 0.82);
  font-size: 14px;
}

.business-stat-row {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr) 96px;
  align-items: center;
  gap: 10px;
  height: 38px;
  border-bottom: 1px solid rgba(48, 220, 255, 0.18);
}

.business-stat-rank {
  width: 22px;
  height: 22px;
  border: 1px solid rgba(48, 220, 255, 0.58);
  background: rgba(48, 220, 255, 0.18);
  color: #7ff7ff;
  font-family: D-DIN, Arial, sans-serif;
  font-size: 13px;
  line-height: 20px;
  text-align: center;
  box-shadow: 0 0 4px rgba(48, 220, 255, 0.18);
}

.business-stat-main {
  min-width: 0;
}

.business-stat-name {
  color: rgba(236, 252, 255, 0.94);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.business-stat-bar {
  height: 5px;
  margin-top: 7px;
  background: rgba(48, 220, 255, 0.14);
  overflow: hidden;
  box-shadow: inset 0 0 0 1px rgba(48, 220, 255, 0.06);

  span {
    display: block;
    height: 100%;
    background: linear-gradient(90deg, #14f1b2, #30dcff);
    box-shadow: 0 0 6px rgba(48, 220, 255, 0.36);
  }
}

.business-stat-value {
  min-width: 0;
  text-align: right;

  strong {
    color: #38f8ff;
    font-family: D-DIN, Arial, sans-serif;
    font-size: 18px;
    font-weight: 700;
    text-shadow: 0 0 4px rgba(48, 220, 255, 0.32);
  }

  em {
    margin-left: 3px;
    color: rgba(238, 253, 255, 0.72);
    font-size: 11px;
    font-style: normal;
  }

  span {
    display: block;
    margin-top: 2px;
    color: rgba(238, 253, 255, 0.68);
    font-size: 10px;
    line-height: 12px;
    white-space: normal;
  }
}

.business-stat-list,
.business-stat-list span,
.business-stat-list strong,
.business-stat-list em,
.business-stat-name,
.business-stat-rank,
.business-stat-empty {
  color: #ffffff !important;
  text-shadow: none !important;
}
</style>
