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

export default {
  mixins: [taskMixin],
  data () {
    return {
      tabs: [
        { key: 'immediate', name: '实时控制' },
        { key: 'delayOpen', name: '定时开机' },
        { key: 'delayClose', name: '定时关机' }
      ],
      delayOpenOptions: null,
      delayCloseOptions: null,
      schema: this.createSchema(null, () => { return { label: this.isDelayClose ? '关机时间' : '开启时间' } })
    }
  },
  computed: {
    currOptions: {
      get () {
        return this[this.isDelayClose ? 'delayCloseOptions' : 'delayOpenOptions']
      },
      set (val) {
        this[this.isDelayClose ? 'delayCloseOptions' : 'delayOpenOptions'] = val
      }
    },
    type () {
      return this.isDelayClose ? '定时关机' : '定时开机'
    },
    isDelayClose () {
      return this.active === 'delayClose'
    }
  },
  methods: {
    invoke () {
      this.active = 'immediate'
      this.delayOpenOptions = createListOptions({ functionKey: this.openFunctionKey })
      this.delayCloseOptions = createListOptions({ functionKey: this.closeFunctionKey })
      this.show = true
    },
    onSwitch (open) {
      if (!this.online) {
        this.$message({
          type: 'warning',
          message: '设备未上线，请稍后再试'
        })
        return
      }
      this.$confirm(
        `立即${open ? '开机' : '关机'}？`,
        { type: 'warning' }
      ).then(() => {
        this.sendTopic(open ? this.openFunctionKey : this.closeFunctionKey)
      })
    }
  }
}
