<!--
* @Author: lb
* @Date: 2022-07-22 13:30:00
* @LastEditors: lb
* @LastEditTime: 2022-07-22 13:30:00
* @Description: 设备状态时序图 - 弹窗
-->
<template>
  <el-dialog :visible.sync="visible" title="选择一个设备" width="40%" @close="visible = false">
    <el-select v-model="eqId">
      <el-option v-for="eq in eqList" :key="eq.id" :label="eq.name" :value="eq.id" />
    </el-select>
    <div slot="footer">
      <el-button @click="visible = false">{{ 'btn.back' | i18nFilter }}</el-button>
      <el-button type="primary" @click="onConfirm">{{ 'btn.confirm' | i18nFilter }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getEquipmentList, getStatus } from '@/api/DataAnalysis/equipmentStatus'

export default {
  name: 'EquipmentStatusDialog',
  props: ['wsId'],
  data() {
    return {
      visible: false,
      eqId: null,
      eqList: [],
      equipment: null,
      // queryCondition: {
      //   productlines: [],
      //   wsId: null,
      //   startTime: null,
      //   endTime: null
      // }
      queryCondition: null
    }
  },
  mounted() {
    this.fetchList('equipment')
  },
  methods: {
    init(data) {
      this.visible = true
      this.queryCondition = data
    },
    fetchList(type) {
      switch (type) {
        case 'equipment': {
          const query = this.wsId ? { workshopSectionId: this.wsId } : {}
          return getEquipmentList(query).then(res => {
            if (res.data) {
              this.eqList = res.data.map(item => ({ id: item.id, name: item.name }))
            } else {
              this.eqList.splice(0)
            }
          })
        }
      }
    },
    onConfirm() {
      if (this.eqId) {
        // 获取 equipment 数据
        getStatus({ ...this.queryCondition, eqId: this.eqId }).then(res => {
          if (res.data && res.data.length > 0) {
            this.equipment = res.data
            // console.log('eq: ', this.equipment)
            // 返回
            this.onClose()
          }
        })
      } else {
        this.$message({
          message: this.$t('module.basicData.visual.hints.selectEqFirst'),
          type: 'error',
          duration: 1500
        })
      }
    },
    onClose() {
      this.visible = false
      this.$emit('add-equipment', this.equipment)
    }
  }
}
</script>
