<template>
  <div class="card bg-white flex text-center w-full overflow-hidden">
    <div class="w-1/4 mt-10" v-for="(item, index) in list" :key="index">
      <div class="text-text-color">{{ item.name }}</div>
      <div
        ref="father"
        class="
          mt-4
          text-title-color text-xl
          font-bold
          overflow-hidden overflow-ellipsis
          whitespace-nowrap
        "
      >
        <span v-if="index < 2" ref="a">
          {{ filterNum(item.value / 1e8) }}
        </span>
        <span v-else-if="index == 2" ref="b">
          {{ filterNum(Number(item.value / 1e8).toFixed(2)) }}
          {{ $t("lang.price.dollar") }}
        </span>
        <span v-else ref="c">
          {{ item.value | filterNum }}
        </span>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { filterNum } from "@/utils/utils";
import Vue, { PropType } from "vue";

export default Vue.extend({
  props: {
    list: Array as PropType<{ name: string; value: string | number }[]>,
  },
  mounted() {
    this.setFontSize();
  },
  methods: {
    filterNum,
    setFontSize() {
      const father = this.$refs.father as HTMLElement[];
      const a = this.$refs.a as HTMLElement[];
      const b = this.$refs.b as HTMLElement[];
      const c = this.$refs.c as HTMLElement[];
      let beSmall = false;
      [...a, ...b, ...c].some((el: HTMLElement) => {
        if (el.offsetWidth >= father[0].offsetWidth) {
          beSmall = true;
        }
      });
      beSmall &&
        [...a, ...b, ...c].forEach((el: HTMLElement) => {
          el.style.fontSize = "14px";
        });
    },
  },
  watch: {
    list(newV) {
      this.setFontSize();
    },
  },
});
</script>

<style scoped>
.card {
  height: 140px;
  border-radius: 4px;
}
</style>
