<template>
  <box
    componentkey="Sensor"
    :title="title"
    :bus="{title,color,type}"
  >
    <div
      class="l-flex--col center jcenter"
      style="height:100%"
      :class="{full}"
    >
      <div
        class="tip"
        :style="{ color: tipColor }"
      >
        {{ tip }}
      </div>
      <div class="sensor">{{ sensor }}</div>
      <div
        v-if="sorted.length > 1"
        class="lines"
      >
        <vue-seamless-scroll
          :data="sorted"
          class="warp"
          :class-option="classOption"
        >
          <div
            v-for="(item,index) in sorted"
            :key="index"
            class="line"
          >
            <div class="mini"> {{ item.time }}</div> <div class="mini"> {{ item.name }}</div>
            <div
              v-if="full"
              :style="{ color: tipColor }"
              class="mini"
            >{{ item.value||'-' }}</div>
          </div>
        </vue-seamless-scroll>
      </div>
    </div>

  </box>
</template>

<script>
import Box from './Box'
import vueSeamlessScroll from './vue-seamless-scroll.min.js'
import {
  Type,
  addListener,
  removeListener
} from '@/views/device/detail/monitor'
export default {
  components: {
    Box,
    vueSeamlessScroll
  },
  props: {
    full: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: ''
    },
    color: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      list: [],
      classOption: {
        step: 0.3,
        hoverStop: false
      }
    }
  },
  computed: {
    sensor () {
      return this.list.length
        ? this.list.length === 1
          ? this.sorted[0].time
          : `${this.sorted[0].time} 传感器${this.sorted[0].key}`
        : null
    },
    tipColor () {
      return this.list.length ? this.color : ''
    },
    tip () {
      return this.list.length ? this.sorted[0].value : '未知'
    },
    sorted () {
      return this.list.slice().sort((a, b) => b.sensorValue - a.sensorValue)
    }
  },
  created () {
    addListener(this.type, this.onUpdate, { type: Type.CACHE, value: [] })
  },
  beforeDestroy () {
    removeListener(this.type, this.onUpdate)
  },
  methods: {
    onUpdate (list) {
      list = list.map(item => { return { ...item, time: item.time.substring(0, 16) } })
      this.list = list.concat(list.slice()).concat(list.slice())
    },
    getSensors () {
      return Promise.resolve({ data: this.list })
    }
  }
}
</script>

<style lang="scss" scoped>
.tip {
  font-size: 18px;
  font-weight: 600;
  line-height: 25px;
  height: 25px;
  margin: 10px 0
}
.sensor {
  color: #9ea9cd;
  font-size: 12px;
  margin-bottom: 10px ;
}
.lines{
  color: #FFFFFF;
  font-size: 12px;
  .line{
    line-height: 20px;
    border-bottom: 1px solid #9ea9cd;
    .mini{
       display: inline-block;
    }
  //   display: flex;
  //   .mini{
  //     flex: 1;
  //   }
  // }
  }
}
.full{
  .tip{
    height: 90px;
font-size: 64px;font-weight: 600;
margin-bottom: 40px ;
line-height: 90px;

  }
  .sensor {
  color: #9ea9cd;
  font-size: 16px;
  margin-bottom: 40px ;
  line-height: 22px;
}
  .lines{
  color: #FFFFFF;
  font-size: 20px;
  width: 100%;
  .line{
    line-height: 38px;
    border-bottom: 1px solid #9ea9cd;
    margin-bottom: 30px;
    display: flex;
    justify-content: space-around;
    .mini{
      flex: 1;
      text-align: center;
    }
  }
}
}
.warp{
  height: 70px;
  overflow: hidden;
}
.full{
  .warp{
    height: 280px;
  }
}
// .mini{
//   display:inline-block;
//   transform: scale(.5);
// }
</style>
