<template>
  <wrapper
    v-loading="loading"
    class="c-detail"
    fill
  >
    <div class="l-flex__none c-detail__header">
      <template v-if="device">
        <div class="l-flex--row has-padding">
          <span
            class="c-sibling-item o-status"
            :class="statusType"
          >
            {{ statusTip }}
          </span>
          <auto-text
            class="c-sibling-item o-name"
            :text="deviceName"
          />
        </div>
        <el-tabs
          v-model="active"
          class="c-tabs"
        >
          <el-tab-pane
            label="设备信息"
            name="info"
          />
          <el-tab-pane
            label="运行状态"
            name="status"
          />
          <el-tab-pane
            label="设备告警"
            name="alarm"
          />
        </el-tabs>
      </template>
      <el-result
        v-if="!loading && !device"
        icon="warning"
      >
        <template #extra>
          <el-link
            class="u-pointer"
            type="warning"
            @click="getDevice"
          >
            出错了，点击重试
          </el-link>
        </template>
      </el-result>
    </div>
    <div
      v-if="device"
      class="l-flex--col l-flex__fill has-padding"
    >
      <keep-alive>
        <component
          :is="activeComponent"
          class="l-flex__fill c-detail__wrapper has-padding u-overflow-y--auto"
          :device="device"
        />
      </keep-alive>
    </div>
  </wrapper>
</template>

<script>
import { getDevice } from '@/api/device'
import {
  start,
  stop,
  addListener
} from './monitor'
import DeviceInfo from './components/DeviceInfo'
import DeviceStatus from './components/DeviceStatus'
import DeviceAlarm from './components/DeviceAlarm'

export default {
  name: 'DeviceDetail',
  components: {
    DeviceInfo,
    DeviceStatus,
    DeviceAlarm
  },
  data () {
    return {
      loading: true,
      device: null,
      isActivated: false,
      isOnline: false,
      active: 'info'
    }
  },
  computed: {
    deviceId () {
      return this.$route.params.id
    },
    statusType () {
      return this.isActivated
        ? this.isOnline
          ? 'success'
          : 'error'
        : this.device.activate
          ? 'primary'
          : 'warning'
    },
    statusTip () {
      return this.isActivated
        ? this.isOnline
          ? '在线'
          : '离线'
        : this.device.activate
          ? '已激活'
          : '未激活'
    },
    deviceName () {
      return this.device?.name
    },
    activeComponent () {
      switch (this.active) {
        case 'info':
          return 'DeviceInfo'
        case 'status':
          return 'DeviceStatus'
        case 'alarm':
          return 'DeviceAlarm'
        default:
          return ''
      }
    }
  },
  watch: {
    deviceId () {
      console.log(this.deviceId)
      stop()
      this.active = 'info'
      this.device = null
      this.getDevice()
    }
  },
  created () {
    this.getDevice()
  },
  beforeDestroy () {
    stop()
  },
  methods: {
    getDevice () {
      this.loading = true
      const id = this.deviceId
      getDevice(id).then(({ data }) => {
        if (this.deviceId === id) {
          this.device = data
          this.isActivated = data.activate === 2
          this.isOnline = data.onlineStatus === 1
          start(this.device)
          addListener('online', this.onUpdate)
        }
      }).finally(() => {
        if (this.deviceId === id) {
          this.loading = false
        }
      })
    },
    onUpdate (online) {
      this.isActivated = true
      this.isOnline = online
    }
  }
}
</script>

<style lang="scss" scoped>
.c-detail {
  &__header {
    background-color: #fff;
  }

  &__wrapper {
    border-radius: $radius;
    background-color: #fff;
  }
}

.o-name {
  color: $black;
  font-size: 20px;
  font-weight: bold;
}

.o-status {
  display: inline-block;
  padding: 4px 8px;
  color: #fff;
  font-size: 16px;
  line-height: 1;
  border-radius: 4px;
  background-color: $success--dark;

  &.primary {
    background-color: $primary;
  }

  &.error {
    background-color: $error;
  }

  &.warning {
    background: $warning;
  }
}
</style>
