<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 padding"
        >
          <el-tab-pane
            v-for="tab in tabs"
            :key="tab.key"
            :label="tab.label"
            :name="tab.key"
          />
        </el-tabs>
      </template>
      <warning
        v-if="!loading && !device"
        @click="getDevice"
      />
    </div>
    <div
      v-if="device"
      class="l-flex--col l-flex__fill has-padding"
    >
      <keep-alive exclude="DeviceCamera,LinkState">
        <component
          :is="active"
          :key="active"
          class="l-flex__fill c-detail__wrapper has-padding u-overflow-y--auto"
          :device="device"
          :online="isOnline"
        />
      </keep-alive>
    </div>
  </wrapper>
</template>

<script>
import { getDevice } from '@/api/device'
import { Access } from '@/constant'
import { ScreenshotCache } from '@/utils/cache'
import {
  start,
  stop,
  addListener
} from './monitor'
import DeviceInfo from './components/DeviceInfo'
import DeviceRuntime from './components/DeviceRuntime'
import DeviceAlarm from './components/DeviceAlarm'
import DeviceInvoke from './components/DeviceInvoke'
import LinkState from './components/LinkState'
import Sensors from './components/external/Sensors'
import Transmitter from './components/external/Transmitter'
import ReceivingCard from './components/external/ReceivingCard'
import Camera from './components/external/Camera'

export default {
  name: 'DeviceDetail',
  components: {
    DeviceInfo,
    DeviceRuntime,
    DeviceAlarm,
    DeviceInvoke,
    LinkState,
    Sensors,
    Transmitter,
    ReceivingCard,
    Camera
  },
  data () {
    return {
      loading: true,
      device: null,
      isActivated: false,
      isOnline: false,
      active: 'DeviceInfo',
      tabs: [
        { key: 'DeviceInfo', label: '设备信息' },
        { key: 'DeviceRuntime', label: '运行状态' },
        { key: 'DeviceAlarm', label: '设备告警' },
        this.accessSet.has(Access.MANAGE_DEVICE) ? { key: 'DeviceInvoke', label: '设备操控' } : null,
        this.accessSet.has(Access.VIEW_SENSORS) ? { key: 'Sensors', label: '传感器' } : null,
        __STAGING__ ? { key: 'Transmitter', label: '发送控制设备' } : null,
        __STAGING__ ? { key: 'ReceivingCard', label: '接收卡' } : null,
        __STAGING__ ? { key: 'LinkState', label: '全链路监测状态' } : null,
        __STAGING__ ? { key: 'Camera', label: '摄像头' } : null
      ].filter(val => val)
    }
  },
  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
    }
  },
  watch: {
    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) {
          if (data) {
            this.device = data
            this.isActivated = data.activate === 2
            this.isOnline = this.isActivated && data.onlineStatus === 1
            if (!this.isOnline) {
              ScreenshotCache.remove(id)
            }
            start(this.device)
            addListener('online', this.onUpdate)
          } else {
            this.$message({
              type: 'warning',
              message: '设备不存在'
            })
            this.$router.replace({
              name: 'device-list'
            })
          }
        }
      }).finally(() => {
        if (this.deviceId === id) {
          this.loading = false
        }
      })
    },
    onUpdate (online) {
      this.isActivated = true
      this.isOnline = online
      if (!this.isOnline) {
        ScreenshotCache.remove(this.deviceId)
      }
    }
  }
}
</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: $radius--mini;
  background-color: $success--dark;

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

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

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

<style lang="scss">
.o-device-grid-item {
  height: 180px;
  padding: 12px 16px 16px;
  color: $info;
  font-size: 14px;
  line-height: 1;

  &__large {
    font-size: 18px;
  }

  &__largest {
    font-size: 32px;
  }

  &__icon {
    margin-right: 8px;
  }
}
</style>
