<template>
  <div class="go-top-alarms">
    <div class="header">
      <div class="title">
        <svg-icon icon-class="rocket" class="title-icon"></svg-icon>
        <span :style="{ color: option.titleColor, fontSize: option.titleSize + 'px' }">{{ option.title }}</span>
      </div>
      <n-dropdown trigger="hover" :options="dropdownOptions" @select="handleSelect">
        <n-button type="primary" size="small">
          {{ selectedOption }}
          <template #icon>
            <n-icon>
              <svg-icon icon-class="arrow-down" />
            </n-icon>
          </template>
        </n-button>
      </n-dropdown>
    </div>
    <div class="content">
      <ul>
        <li v-for="(item, index) in option.dataset" :key="index" class="alarm-item">
          <div class="item-info">
            <span class="rank" :style="{ color: option.rankColor }">
              <svg-icon icon-class="circle" class="rank-icon"></svg-icon>
              {{ item.rank }}
            </span>
            <span class="name" :style="{ color: option.nameColor }">{{ item.name }}</span>
          </div>
          <div class="item-value">
            <span class="value" :style="{ color: option.valueColor }">{{ item.value }}</span>
          </div>
          <div class="progress-bar">
            <div class="progress" :style="{ width: calculateWidth(item.value), backgroundColor: option.barColor }"></div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, PropType } from 'vue'
import { option as configOption } from './config'

const props = defineProps({
  chartConfig: {
    type: Object as PropType<{ option: typeof configOption }>,
    required: true
  }
})

const option = computed(() => props.chartConfig.option)

const selectedOption = ref(props.chartConfig.option.dropdownDefault)

const dropdownOptions = computed(() => {
  return props.chartConfig.option.dropdownOptions.map(opt => ({
    label: opt,
    key: opt
  }))
})

const maxValue = computed(() => {
  return Math.max(...props.chartConfig.option.dataset.map(item => item.value))
})

const calculateWidth = (value: number) => {
  if (maxValue.value === 0) return '0%'
  return `${(value / maxValue.value) * 100}%`
}

const handleSelect = (key: string) => {
  selectedOption.value = key
}
</script>

<style lang="scss" scoped>
.go-top-alarms {
  width: 100%;
  height: 100%;
  background-color: #0F1C3D;
  padding: 15px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    .title {
      display: flex;
      align-items: center;
      .title-icon {
        font-size: 24px;
        margin-right: 10px;
        color: #00E5FF;
      }
    }
  }

  .content {
    flex: 1;
    ul {
      list-style: none;
      padding: 0;
      margin: 0;
    }
  }

  .alarm-item {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    grid-template-areas:
      "info value"
      "bar bar";
    gap: 5px 15px;
    margin-bottom: 15px;

    .item-info {
      grid-area: info;
      display: flex;
      align-items: center;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;

      .rank {
        font-weight: bold;
        margin-right: 10px;
        .rank-icon {
          font-size: 12px;
          margin-right: 5px;
        }
      }
    }

    .item-value {
      grid-area: value;
      text-align: right;
    }

    .progress-bar {
      grid-area: bar;
      width: 100%;
      height: 6px;
      background-color: #2A3A5B;
      border-radius: 3px;
      overflow: hidden;

      .progress {
        height: 100%;
        border-radius: 3px;
      }
    }
  }
}
</style>
