<template>
  <div>
    <el-card class="no-bottom">
      <el-form ref="form" :model="form" inline :rules="rules">
        <el-form-item label="设备类型:" prop="deviceType">
          <el-select v-model="form.deviceType" @change="onDeviceTypeChange">
            <el-option
              v-for="(deviceType, index) in deviceTypeList"
              :key="index"
              :label="deviceType.name"
              :value="deviceType.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备实例:" prop="deviceInstance">
          <el-select v-model="form.deviceInstance" placeholder="请选择">
            <el-option
              v-for="(deviceInstance, index) in deviceInstanceList"
              :key="index"
              :label="deviceInstance.name"
              :value="deviceInstance.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="数据模型:" prop="dataMode">
          <el-select v-model="form.dataMode" placeholder="请选择">
            <el-option
              v-for="(dataMode, index) in dataModelList"
              :key="index"
              :label="dataMode.name"
              :value="dataMode.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="时间轴:">
          <div style="margin-right: 20px">
            Timestamp
          </div>
        </el-form-item>
        <el-form-item label="刷新时间">
          <el-input-number
            v-model.number="form.refresh"
            :min="1"
            :max="10"
            label="单位秒"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryData">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="margin-top-20">
      <div slot="header" class="clearfix">
        <span>设备信息</span>
        <el-button style="float: right; padding: 3px 0" type="text">2020-08-12 12:22:21</el-button>
      </div>
      <descriptions
        label-position="right"
        label-width="140px"
      >
        <descriptions-item label="型号" value="AKB48" />
        <descriptions-item label="健康状态" value="健康" />
        <descriptions-item label="运行状态" value="运行中" />
        <descriptions-item label="上次启动时间" value="2020-08-12 12:22:21" />
        <descriptions-item label="运行时长" value="1d 27h 13m 21s" />
        <descriptions-item label="描述" value="非常贵的设备" />
      </descriptions>
    </el-card>
    <el-card class="margin-top-20">
      <div slot="header" class="clearfix">
        <span>模型数据</span>
        <el-button style="float: right; padding: 3px 0" type="text">2020-08-12 12:22:21</el-button>
      </div>
      <descriptions
        label-position="right"
        label-width="140px"
      >
        <descriptions-item label="温度" value="20 摄氏度" />
        <descriptions-item label="湿度" value="70%" />
        <descriptions-item label="压力" value="2500 帕斯卡" />
        <descriptions-item label="转子转速" value="1250 rpm/m" />
      </descriptions>
    </el-card>
    <el-card class="margin-top-20">
      <div v-if="!propertyList.length" slot="header" class="clearfix">
        <span>图表</span>
      </div>
      <el-tabs v-model="activeProperty">
        <el-tab-pane
          v-for="(property, index) in propertyList"
          :key="index"
          :label="property.propertyName"
          :name="property.propertyCode"
        />
      </el-tabs>
      <e-line
        v-if="propertyList.length"
        :id="'line-' + property.propertyCode"
        :title="property.propertyName"
        :line-data="property.data"
        :description="property.propertyUnit"
        format="MM-DD HH:mm:ss"
      />
      <div v-else class="el-table__empty-text" style="text-align: center;margin: 0 auto">暂无数据</div>
    </el-card>
  </div>
</template>

<script>
import { getAllDeviceModel } from '@/api/equipment-management/equipment-instance'
import { getAllInstanceByDeviceTypeId } from '@/api/equipment-management/equipment-model'
import { getDataModelByDeviceTypeId } from '@/api/equipment-management/equipment-model'
import { getModelInstanceChart } from '@/api/device-management/device-monitoring'
import ELine from '@/components/Charts/ELine'

export default {
  name: 'DeviceDetail',
  components: {
    ELine
  },
  data() {
    return {
      form: {
        deviceType: null,
        deviceInstance: null,
        dataMode: null,
        refresh: 1
      },
      property: {
        propertyCode: null,
        propertyName: null,
        propertyUnit: null,
        data: []
      },
      rules: {
        deviceType: [
          { required: true, message: '', trigger: 'blur' }
        ],
        deviceInstance: [
          { required: true, message: '', trigger: 'blur' }
        ],
        dataMode: [
          { required: true, message: '', trigger: 'blur' }
        ]
      },
      deviceInstanceList: [],
      dataModelList: [],
      propertyList: [],
      activeProperty: null,
      deviceTypeList: [],
      tableData: [],
      timer: 0
    }
  },
  watch: {
    activeProperty(newVal) {
      if (newVal !== null) {
        const property = this.propertyList.find(v => v.propertyCode === newVal)
        if (property) {
          const { propertyCode, propertyName, propertyUnit } = property
          this.property = {
            propertyCode,
            propertyName,
            propertyUnit,
            data: [...this.getChartData(this.activeProperty)]
          }
        }
      } else {
        this.propertyList = []
      }
    },
    form: {
      deep: true,
      handler: function() {
        this.$refs.form.clearValidate()
      }
    }
  },
  created() {
    const { deviceTypeId, deviceInstanceId } = this.$route.query
    this.timer && clearInterval(this.timer)
    this.init(deviceTypeId, deviceInstanceId)
  },
  beforeDestroy() {
    this.timer && clearInterval(this.timer)
  },
  methods: {
    async init(deviceTypeId, deviceInstanceId) {
      // 获取所有设备类型
      const deviceTypeList = await getAllDeviceModel()
      this.deviceTypeList = deviceTypeList.data
      this.form.deviceType = deviceTypeId || (this.deviceTypeList.length ? this.deviceTypeList[0].id : null)
      // 获取该设备类型下的设备实例
      const deviceInstanceList = await getAllInstanceByDeviceTypeId({ deviceTypeId: this.form.deviceType })
      this.deviceInstanceList = deviceInstanceList.data
      this.form.deviceInstance = deviceInstanceId || (this.deviceInstanceList.length ? this.deviceInstanceList[0].id : null)
      // 根据设备类型 ID 获取关联数据模型
      const dataModelList = await getDataModelByDeviceTypeId({ id: this.form.deviceType })
      this.dataModelList = dataModelList.data
      if (this.dataModelList.length) {
        this.form.dataMode = this.dataModelList[0].id
        this.propertyList = this.dataModelList[0].propertyList
        this.activeProperty = this.propertyList.length ? this.propertyList[0].propertyCode : null
      }
      // // 查询图表数据
      await this.queryData()
      return {}
    },
    queryData() {
      this.$refs.form.validate()
        .then(_ => {
          const index = this.dataModelList.findIndex(i => i.id === this.form.dataMode)
          this.propertyList = [...this.dataModelList[index].propertyList]
          if (this.propertyList.length) {
            this.activeProperty = this.propertyList[0].propertyCode
          }
          return [{
            modelId: this.form.dataMode,
            deviceId: this.form.deviceInstance,
            propertyCodeList: this.propertyList.map(v => v.propertyCode)
          }]
        })
        .then(params => getModelInstanceChart(params))
        .then(res => {
          this.tableData = res.data[0].dataList
          this.property.data = [...this.getChartData(this.activeProperty)]
          return [{
            modelId: this.form.dataMode,
            deviceId: this.form.deviceInstance,
            propertyCodeList: this.propertyList.map(v => v.propertyCode)
          }]
        })
        .then(params => {
          this.timer && clearInterval(this.timer)
          this.timer = setInterval(async() => {
            const chartData = await getModelInstanceChart(params)
            this.tableData = chartData.data[0].dataList
            this.property.data = [...this.getChartData(this.activeProperty)]
          }, this.form.refresh * 1000)
        })
        .catch(_ => {})
    },
    onDeviceTypeChange(val) {
      this.form.deviceInstance = null
      this.form.dataMode = null
      this.getAllInstanceByDeviceTypeId(val)
      this.getDataModelByDeviceTypeId(val)
    },
    getChartData(propertyCode) {
      return this.tableData.map(v => {
        return {
          time: v.time,
          value: v[propertyCode]
        }
      })
    },
    getAllInstanceByDeviceTypeId(val) {
      getAllInstanceByDeviceTypeId({ deviceTypeId: val })
        .then(res => {
          this.deviceInstanceList = res.data
          if (this.deviceInstanceList.length) {
            this.form.deviceInstance = this.deviceInstanceList[0].id
          }
        })
        .catch(_ => {})
    },
    getDataModelByDeviceTypeId(id) {
      getDataModelByDeviceTypeId({ id })
        .then(res => {
          this.dataModelList = res.data
          if (this.dataModelList.length) {
            this.form.dataMode = this.dataModelList[0].id
          }
        })
        .catch(_ => {})
    }
  }
}
</script>

<style scoped>

</style>
