<template>
  <schema-table
    ref="table"
    :schema="schema"
    default-expand-all
    @row-click="onToggle"
  >
    <program-dialog ref="programDialog" />
    <schedule-dialog ref="scheduleDialog" />
  </schema-table>
</template>

<script>
import {
  State,
  EventPriority,
  EventTarget,
  PublishType
} from '@/constant'
import { getEventDescription } from '@/utils/event'
import mixin from './mixin'

export default {
  name: 'WorkflowReviewPublish',
  mixins: [mixin],
  data () {
    return {
      type: 'calendar'
    }
  },
  computed: {
    schema () {
      return {
        condition: { pageSize: this.list.length },
        list: this.getList,
        cols: [
          { prop: 'expand', type: 'expand', render (data, h) {
            return h('div', {
              staticClass: 'o-info'
            }, [
              h('div', null, data.desc),
              h('div', null, `设备：${data.device}`)
            ])
          } },
          { prop: 'type', label: '类型' },
          { prop: 'name', label: '名称' },
          { prop: 'resolutionRatio', label: '分辨率' },
          ...this.reviewCol
        ]
      }
    }
  },
  created () {
    this.list = this.transform([this.workflow.calendarReleaseScheduling])
    if (!this.list.some(({ pass }) => !pass)) {
      this.$emit('next', 2)
    }
  },
  methods: {
    transform (arr) {
      return arr.map(this.transformItem)
    },
    transformItem (item) {
      const same = this.getSame(item)
      const diff = this.getDiff(item)
      return { ...same, ...diff }
    },
    getSame ({ id, status, programCalendarName, resolutionRatio, createBy, createByUsername, createTime, calendarReleaseDeviceList }) {
      return {
        pass: status !== State.SUBMITTED,
        id,
        name: programCalendarName,
        resolutionRatio,
        createBy: createByUsername || createBy,
        createTime,
        device: calendarReleaseDeviceList?.map(item => item.deviceName).join('，')
      }
    },
    getDiff (item) {
      const target = JSON.parse(item.target)
      let type = ''
      switch (target.type) {
        case PublishType.CALENDAR:
          type = '排期'
          break
        case PublishType.EVENT:
          type = ['', '默认播放', '单播', '插播'][target.detail.priority]
          break
        default:
          break
      }
      return {
        type,
        target,
        desc: this.getDesc(target)
      }
    },
    getDesc (target) {
      if (target.type === PublishType.EVENT && target.detail.priority === EventPriority.INSERTED) {
        return getEventDescription(target.detail)
      }
      return ''
    },
    onToggle (row) {
      this.$refs.table.getInst().toggleRowExpansion(row)
    },
    onView ({ target: { type, detail } }) {
      switch (type) {
        case PublishType.CALENDAR:
          this.viewSchedule(detail)
          break
        case PublishType.EVENT:
          if (detail.target.type === EventTarget.RECUR) {
            this.viewSchedule(detail.target.id)
          } else {
            this.$refs.programDialog.show(detail.target.id)
          }
          break
        default:
          break
      }
    },
    viewSchedule (id) {
      this.$refs.scheduleDialog.show(id)
    }
  }
}
</script>
