import {
  saveScheduleEvents,
  submitSchedule
} from '@/api/calendar'

export default {
  props: {
    detail: {
      type: Object,
      default: null
    },
    hideHeader: {
      type: [Boolean, String],
      default: false
    },
    editable: {
      type: [Boolean, String],
      default: false
    }
  },
  data () {
    return {
      scheduleOptions: null
    }
  },
  computed: {
    events () {
      return this.scheduleOptions?.events || []
    },
    isEmpty () {
      return this.events.length === 0
    },
    name () {
      return this.scheduleOptions?.name
    },
    ratio () {
      return this.scheduleOptions?.resolutionRatio
    }
  },
  created () {
    this.scheduleOptions = {
      ...this.detail,
      events: this.transformEvents(this.detail.events)
    }
  },
  methods: {
    transformEvents (events) {
      return events
    },
    getEvents () {
      return this.events
    },
    save () {
      return saveScheduleEvents(this.scheduleOptions, this.getEvents())
    },
    submit () {
      submitSchedule(this.scheduleOptions, this.getEvents()).then(() => {
        this.$emit('submit')
      })
    }
  }
}
