<template>
  <div class="flex between v-center mb-10">
    <span class="bar-title">{{ title }}</span>
    <span class="bar-sub-title">{{ subTitle }}</span>
  </div>
  <div>
    <div
        class="flex v-center bar-item wrap between"
        v-for="item in data"
        :size="size"
        :style="{ '--label-width': `${labelWidth}px` }"
        :position="labelPosition"
    >
            <span class="bar-item-label mr-8">
                {{ item.label }}
            </span>
      <div class="bar-item_content " :long="!item.denominator">
        <div
            class="bar-item_content-bar"
            :style="{
                        width: `${(item.value / max) * 100}%`,
                        background: getColor((item.value / max) * 100),
                    }"
        ></div>
      </div>
      <span :class="spanClass">{{ item.value + props.unit }}</span>
      <span
          class="bar-item_remark text-right ml-8"
          v-if="item.denominator"
          style="min-width: 50px; "
          :style="{ fontSize: props.remarkFontSize }"
      >
                <span class="text-success">{{ item.numerator }}</span>
                <span>/</span>
                <span>{{ item.denominator }}</span>
      </span>
    </div>
  </div>
</template>
<script setup>
import {onMounted} from "vue";

const props = defineProps({
  title: {
    type: String,
    default: "",
  },
  subTitle: {
    type: String,
    default: "",
  },
  data: {
    type: Array,
    default: [],
  },
  size: {
    type: String,
    default: "",
  },
  unit: {
    type: String,
    default: "",
  },
  color: {
    type: Object,
    default: "linear-gradient(270deg, #63e700 0%, #19674c 100%)",
  },
  labelWidth: {
    type: Number,
    default: 80,
  },
  labelPosition: {
    type: String,
    default: "left",
  },
  remarkFontSize: {type: String, default: "10px",},
  spanClass: { type: String, default: "", },
});

const max = ref(100);
watch(
    () => props.data,
    () => {
      getMax();
    }
);


function getMax() {
  if (props.unit !== "%") {
    max.value = Math.max(...props.data.map((item) => item.value));
  }
}

onMounted(() => {
  getMax();
});

function getColor(val) {
  if (props.color instanceof String) {
    return props.color;
  }
  if (props.color instanceof Array) {
    const colors = [...props.color];
    colors.sort((a, b) => b.percentage - a.percentage);
    for (let i = 0; i < colors.length; i++) {
      if (val > colors[i].percentage) {
        return colors[i].color;
      }
    }
  }
  return "linear-gradient(270deg, #63e700 0%, #19674c 100%)";

}
</script>
<style lang="scss" scoped>
.bar-title {
  font-size: 19px;
}

.bar-sub-title {
  color: #597ae9;
  font-size: 14px;
}

.bar-item {
  font-size: 17px;

  &[size="large"] {
    .bar-item_content {
      .bar-item_content-bar {
        height: 13px;
      }
    }
  }

  &[size="small"] {
    font-size: 12px;

    .bar-item_content {
      width: calc(100% - 180px);
    }

  }

  .bar-item-label {
    text-align: right;
    width: var(--label-width);
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  &[position="left"] {
    height: 32px;
    line-height: 32px;
  }

  &[position="top"] {
    margin-bottom: 4px;

    .bar-item-label {
      width: 100%;
      text-align: left;
    }

    .bar-item_content {
      width: calc(100% - 80px);
    }
  }

  .bar-item_content {
    width: calc(100% - var(--label-width) - 16px - 30px);

    &[long=false] {
      width: calc(100% - var(--label-width) - 36px - 110px);
    }

    .bar-item_content-bar {
      width: 0;
      height: 9px;
      background: linear-gradient(270deg, #63e700 0%, #19674c 100%);
      transition: width 0.3s;
    }
  }

  .bar-item_remark {
    font-size: 14px;

    .text-success {
      color: #09c700;
    }
  }
}

.right-aligned {
  text-align: right;
  width: 60px;
}

</style>
