<template>
  <div
    class="l-flex--col c-schedule-wrapper"
    @click.stop
  >
    <div class="l-flex__none l-flex--row c-schedule-wrapper__header has-bottom-padding">
      <i
        v-if="editable"
        class="l-flex__none c-schedule-wrapper__back el-icon-arrow-left u-bold u-pointer"
        @click="onBack"
      />
      <div class="l-flex__auto l-flex--row c-sibling-item u-bold u-ellipsis">
        {{ name }}
      </div>
      <template v-if="editable">
        <button
          v-if="useAdd"
          class="l-flex__none c-sibling-item o-button"
          @click="onAdd"
        >
          <i class="o-button__icon el-icon-circle-plus-outline" />
          新增
        </button>
        <button
          v-if="dirty"
          class="l-flex__none c-sibling-item o-button"
          @click="onSave"
        >
          <i class="o-button__icon el-icon-download" />
          保存
        </button>
      </template>
    </div>
    <slot />
  </div>
</template>

<script>
export default {
  name: 'ScheduleWrapper',
  props: {
    editable: {
      type: [Boolean, String],
      default: false
    },
    hideHeader: {
      type: [Boolean, String],
      default: false
    },
    name: {
      type: String,
      default: ''
    },
    dirty: {
      type: [Boolean, String],
      default: false
    }
  },
  computed: {
    useAdd () {
      return !!this.$listeners.add
    }
  },
  methods: {
    onAdd () {
      this.$emit('add')
    },
    onSave () {
      if (this.dirty) {
        this.$emit('save')
      } else {
        this.onBack()
      }
    },
    onBack () {
      this.$emit('back')
    }
  }
}
</script>

<style lang="scss" scoped>
.c-schedule-wrapper {
  &__header {
    color: $black;
    font-size: 24px;
    line-height: 1;
    border-bottom: 1px solid $border;
  }

  &__back {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    width: 36px;
    height: 36px;
    margin-right: 6px;
    font-size: 18px;
    border-radius: 50%;
    transition: background-color 0.4s;

    &:hover {
      background-color: $gray--light;
    }
  }
}
</style>
