import taskMixin from './task'
import { createListOptions } from '@/utils'

export default {
  mixins: [taskMixin],
  data () {
    return {
      tabs: [
        { key: 'immediate', name: '实时控制' },
        { key: 'delay', name: '定时控制' }
      ],
      taskValue: 50
    }
  },
  computed: {
    settings () {
      return `${this.type}设置`
    },
    tip () {
      return `${this.type}值`
    }
  },
  methods: {
    invoke () {
      this.active = 'immediate'
      this.taskValue = 50
      this.options = createListOptions({ functionKey: this.functionKey })
      this.show = true
    },
    onInvoke () {
      if (!this.online) {
        this.$message({
          type: 'warning',
          message: '设备未上线，请稍后再试'
        })
        return
      }
      this.$confirm(
        `将${this.type}设置为${this.taskValue}%？`,
        { type: 'warning' }
      ).then(() => {
        this.sendTopic(this.functionKey, this.createProps(this.taskValue))
      })
    },
    createProps (val) {
      return [{
        name: this.valueKey,
        value: val.toString()
      }]
    },
    createTask (task) {
      if (task && task.inputs) {
        const valueKey = this.valueKey
        const prop = task.inputs.find(item => item.name === valueKey)
        if (prop) {
          this.$taskValue = Number(prop.value)
          return this.$taskValue
        }
      }
      return 50
    },
    isSameTask () {
      return this.$taskValue === this.task
    },
    getTask () {
      return {
        inputs: this.createProps(this.task)
      }
    }
  }
}
