import {
  getTasks,
  addTask,
  updateTask,
  deleteTask,
  activateTask,
  deactivateTask
} from '@/api/device'
import baseMixin from './base'
import Tabs from '../../Tabs'
import TaskDialog from './TaskDialog'

export default {
  components: {
    Tabs,
    TaskDialog
  },
  mixins: [baseMixin],
  data () {
    return {
      isAdd: false,
      show: false,
      active: 'immediate',
      options: null,
      task: {}
    }
  },
  computed: {
    isImmediate () {
      return this.active === 'immediate'
    },
    dialogTitle () {
      return this.isAdd ? `新增${this.type}任务` : `编辑${this.type}任务`
    }
  },
  methods: {
    createSchema (cols, attrs) {
      return {
        list: this.getTasks,
        transform: this.transformTask,
        buttons: [
          { type: 'add', on: this.onAdd }
        ],
        cols: [
          ...(cols || []),
          { prop: 'executeTime', label: '执行时间', attrs },
          { prop: 'type', label: '重复方式' },
          { prop: 'tag', type: 'tag' },
          { type: 'invoke', width: 160, render: [
            { label: '编辑', on: this.onEdit },
            { label: '启用', render: this.canActivate, on: this.onActivate },
            { label: '停用', render: this.canDeactivate, on: this.onDeactivate },
            { label: '删除', on: this.onDel }
          ] }
        ]
      }
    },
    onCloseDialog () {
      this.show = false
    },
    getTasks (params) {
      return getTasks({
        deviceId: this.deviceId,
        ...params
      })
    },
    onAdd () {
      this.isAdd = true
      this.task = this.createTask()
      this.$refs.editDialog.show()
    },
    onEdit (task) {
      this.isAdd = false
      this.task = this.createTask(task)
      this.$task = task
      this.$refs.editDialog.show(task)
    },
    createTask () {
      // implement in component
    },
    onSave ({ value: task, done }) {
      console.log('task', task)
      if (
        !this.isAdd
        && Object.keys(task).every(key => task[key] === this.$task[key])
        && this.isSameTask(this.$task)
      ) {
        done()
        return
      }
      (this.isAdd ? this.addTask : this.updateTask)(task).then(done)
    },
    isSameTask () {
      return true
    },
    addTask (task) {
      return addTask({
        deviceId: this.deviceId,
        functionKey: this.$refs.table.getCondition().functionKey,
        ...task,
        ...this.getTask()
      }).then(() => {
        this.$refs.table.pageTo(1)
      })
    },
    updateTask (task) {
      return updateTask({
        deviceId: this.deviceId,
        taskId: this.$task.taskId,
        functionKey: this.$refs.table.getCondition().functionKey,
        ...task,
        ...this.getTask()
      }).then(() => {
        this.$refs.table.pageTo()
      })
    },
    getTask () {
      // implement in component
    },
    onDel (task) {
      deleteTask(task).then(() => {
        this.$refs.table.decrease(1)
      })
    },
    canActivate ({ status }) {
      return status === 0
    },
    onActivate (task) {
      activateTask(task).then(() => {
        task.status = 1
        task.tag = {
          type: 'success',
          label: '已启用'
        }
      })
    },
    canDeactivate ({ status }) {
      return status === 1
    },
    onDeactivate (task) {
      deactivateTask(task).then(() => {
        task.status = 0
        task.tag = {
          type: 'warning',
          label: '未启用'
        }
      })
    },
    transformTask (task) {
      switch (task.freq) {
        case 0:
          task.type = '每天'
          break
        case 1:
          task.type = `每周${task.dayOfWeek.split(',').map(val => ['一', '二', '三', '四', '五', '六', '日'][val])}`
          break
        default:
          break
      }
      const { status } = task
      task.tag = {
        type: ['warning', 'success'][status],
        label: ['未启用', '已启用'][status]
      }
      return task
    }
  }
}
