<template>
  <div class="l-flex--col o-device-grid-item o-sensor has-border radius">
    <div class="l-flex--row l-flex__none">
      <i
        class="l-flex__none o-device-grid-item__icon o-icon has-bg"
        :class="type"
      />
      <span class="l-flex__fill u-color--info u-ellipsis">{{ title }}</span>
      <i
        v-if="enough"
        class="l-flex__none o-device-grid-item__large u-color--blue el-icon-s-operation u-pointer"
        @click="show = true"
      />
    </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-sensor__current"
        >
          {{ sensor }}
        </div>
      </div>
    </div>
    <div
      v-for="item in more"
      :key="item.key"
      class="l-flex__none l-flex--row o-sensor__more"
    >
      <div class="l-flex__none">{{ item.time }}</div>
      <div class="l-flex__none o-sensor__name">{{ item.name }}</div>
      <div class="l-flex__fill o-sensor__value">{{ item.value }}</div>
    </div>
    <el-dialog
      :title="listTitle"
      :visible.sync="show"
      custom-class="c-dialog"
    >
      <schema-table
        ref="table"
        :schema="schema"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  Type,
  addListener,
  removeListener
} from '@/views/device/detail/monitor'

export default {
  name: 'Sensor',
  props: {
    type: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: ''
    },
    color: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      list: [],
      show: false,
      schema: {
        singlePage: true,
        list: this.getSensors,
        cols: [
          { prop: 'time', label: '采集时间' },
          { prop: 'name', label: '名称' },
          { prop: 'value', label: this.title }
        ]
      }
    }
  },
  computed: {
    listTitle () {
      return `${this.title}传感器详情`
    },
    sorted () {
      return this.list.slice().sort((a, b) => b.sensorValue - a.sensorValue)
    },
    tipColor () {
      return this.list.length ? this.color : ''
    },
    tip () {
      return this.list.length ? this.sorted[0].value : '未知'
    },
    sensor () {
      return this.list.length
        ? this.list.length === 1
          ? this.sorted[0].time
          : `${this.sorted[0].time} 传感器${this.sorted[0].key}`
        : null
    },
    more () {
      return this.list.length > 1 ? this.sorted.slice(1, 3) : []
    },
    enough () {
      return this.list.length > 3
    }
  },
  created () {
    addListener(this.type, this.onUpdate, { type: Type.CACHE, value: [] })
  },
  beforeDestroy () {
    removeListener(this.type, this.onUpdate)
  },
  methods: {
    onUpdate (list) {
      this.list = list
      this.$refs.table?.pageTo()
    },
    getSensors () {
      return Promise.resolve({ data: this.list })
    }
  }
}
</script>

<style lang="scss" scoped>
.o-device-grid-item__icon {
  &.temperature {
    background-image: url("~@/assets/icon_temperature.png");
  }

  &.smoke {
    background-image: url("~@/assets/icon_smoke.png");
  }

  &.flooding {
    background-image: url("~@/assets/icon_flooding.png");
  }

  &.light {
    background-image: url("~@/assets/icon_light.png");
  }
}

.o-sensor {
  &__current {
    margin-top: 6px;
    color: $info;
    font-size: 12px;
  }

  &__more {
    padding: 8px 0;
    color: $info--dark;
    font-size: 14px;
    border-bottom: 1px solid $border;
  }

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

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