<template>
  <div class="scard-layout" :style="'max-width:' + size + 'px;' + minWidth">
    <div class="scard-left">
      <div class="title">{{title}}</div>
      <div :style='"height: " + (scalePixel * 2.5) + "px;"'></div>
      <div
        class="total-value"
        :style='"zoom: " + scaleText + ";"'
        :title="value">{{value || "0"}}</div>
      <div class="unit-text">{{unit}}</div>
    </div>
    <div class="scard-right">
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "StatisticCard",
  props: {
    title: {
      type: String,
      default: ""
    },
    value: {
      type: String|Number,
      default: 0
    },
    size: {
      type: String | Number,
      default: ""
    },
    unit: String
  },
  data() {
    return {
      minWidth: "",
      scaleText: 1,
      scalePixel: 0
    };
  },
  watch: {
    value: {
      deep: true,
      handler(val) {
        this.getValueTextSize()
      }
    },
    size: {
      handler(val) {
        this.getMinWidth();
      }
    }
  },
  mounted() {
    this.getMinWidth();
    this.getValueTextSize();
  },
  methods: {
    getMinWidth() {
      this.minWidth = "min-width:" + (this.size - 20) + "px;"
    },
    getValueTextSize() {
      this.scaleText = 1;
      if (this.value) {
        const length = ("" + this.value).length;
        if (length > 6) {
          this.scalePixel = (length - 6);
          this.scaleText -= this.scalePixel * 0.13;
        }
        if (this.scaleText < 0.35) {
          this.scaleText = 0.35
        }
      }
    }
  }
}
</script>

<style scoped="scoped" lang="scss">
  @import '../../../styles/element-ui.scss';
  .scard-layout {
    flex: 1;
    display: flex;
    min-width: 30vw;
    padding: 10px 20px;
    border-radius: 6px;
    background-color: white;
  }
  .scard-left {
    flex: 2;
    display: flex;
    overflow: hidden;
    min-height: 90px;
    align-items: center;
    flex-direction: column;
    padding: 5px 15px 5px 0;
    border-right: 1px solid #333;
  }
  .title {
    color: #333;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
    position: relative;
  }
  .total-value {
    color: $--color-accent;
    cursor: pointer;
    font-size: 42px;
    max-width: 92%;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    padding: 15px 0 5px;
    transition: all .3s;
    text-overflow: ellipsis;
  }
  .total-value:hover {
    transform: scale(1.1);
    text-shadow: 0px 1px 2px rgba($--color-accent, .2);
  }
  .unit-text {
    color: #333;
    font-size: 14px;
  }
  .scard-right {
    flex: 3;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    padding: 2px 0px 2px 10px;
  }
  .scard-item-row {
    display: flex;
    align-items: center;
  }
  .scard-item-title {
    flex: 1;
    color: #333;
    font-size: 14px;
    padding-right: 5px;
  }
  .scard-item-value {
    color: $--color-accent;
    font-size: 14px;
  }
  @media screen and (max-width: 1500px) {
    .total-value {
      font-size: 40px;
    }
  }
  @media screen and (max-width: 900px) {
    .total-value {
      font-size: 35px;
    }
  }
  @media screen and (max-width: 600px) {
    .total-value {
      font-size: 35px;
    }
  }
  @media screen and (max-width: 400px) {
    .scard-layout {
      min-width: 100%;
      overflow: hidden;
    }
    .total-value {
      font-size: 32px;
    }
  }
</style>