<template>
  <div class="l-flex--col o-status o-senser">
    <div class="l-flex--row l-flex__none">
      <i
        class="l-flex__none o-status__icon"
        :class="type"
      />
      <span class="l-flex__fill o-status__title u-ellipsis">{{ title }}</span>
    </div>
    <div class="l-flex--row center l-flex__fill large u-color--black u-bold">
      <div
        class="u-text-center"
        :style="{ color: tipColor }"
      >
        {{ tip }}
        <div
          v-if="sensor"
          class="o-senser__current"
        >
          {{ sensor }}
        </div>
      </div>
    </div>
    <div
      v-for="item in more"
      :key="item.key"
      class="l-flex__none l-flex--row o-senser__more"
    >
      <div class="l-flex__none">{{ item.time }}</div>
      <div class="l-flex__none o-senser__name">传感器{{ item.key }}</div>
      <div class="l-flex__fill o-senser__value">{{ item.value }}</div>
    </div>
  </div>
</template>

<script>
import {
  addListener,
  removeListener
} from '../monitor'

export default {
  props: {
    type: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: ''
    },
    color: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      list: []
    }
  },
  computed: {
    tipColor () {
      return this.list.length ? this.color : ''
    },
    tip () {
      return this.list.length ? this.list[0].value : '未知'
    },
    sensor () {
      return this.list.length
        ? this.list.length === 1
          ? this.list[0].time
          : `${this.list[0].time} 传感器${this.list[0].key}`
        : null
    },
    more () {
      return this.list.length > 1 ? this.list.slice(1, 3) : []
    }
  },
  created () {
    addListener(this.type, this.onUpdate)
  },
  beforeDestroy () {
    removeListener(this.type, this.onUpdate)
  },
  methods: {
    onUpdate (list) {
      this.list = list
    }
  }
}
</script>

<style lang="scss" scoped>
.o-senser {
  padding: 12px 16px 16px;
  line-height: 1;

  &__current {
    margin-top: 6px;
    color: #d5d9e4;
    font-size: 12px;
  }

  &__more {
    padding: 8px 0;
    color: #8e929c;
    font-size: 14px;
    border-bottom: 1px solid #edf0f6;
  }

  &__name {
    margin: 0 16px 0 24px;
  }

  &__value {
    text-align: right;
  }
}
</style>
