<template>
  <el-popover
    trigger="hover"
    placement="right"
    :close-delay="0"
  >
    <div class="c-event-detail">
      <div class="c-event-detail__time">{{ time }}</div>
      <div
        v-if="desc"
        class="c-event-detail__desc"
      >{{ desc }}</div>
    </div>
    <template #reference>
      <div
        class="c-event u-ellipsis u-pointer"
        :class="{ fill, editable }"
        @click="onClick"
      >
        {{ event.origin.target.name }}
        <i
          class="c-event__icon el-icon-close"
          @click.stop="onRemove"
        />
      </div>
    </template>
  </el-popover>
</template>

<script>
import {
  getEventDateDescription,
  getEventExtraDescription
} from '@/utils/event'

export default {
  name: 'EventItem',
  props: {
    event: {
      type: Object,
      default: null
    },
    fill: {
      type: [Boolean, String],
      default: false
    },
    editable: {
      type: [Boolean, String],
      default: false
    }
  },
  computed: {
    time () {
      return getEventDateDescription(this.event.origin)
    },
    desc () {
      return getEventExtraDescription(this.event.origin)
    }
  },
  methods: {
    onClick () {
      this.$emit('edit', this.event)
    },
    onRemove () {
      this.$emit('remove', this.event)
    }
  }
}
</script>

<style lang="scss" scoped>
.c-event-detail {
  display: inline-block;
  color: $black;
  text-align: center;

  &__time {
    font-size: 14px;
    font-weight: bold;
  }

  &__desc {
    margin-top: 6px;
    color: $black;
    font-size: 12px;
  }
}

.c-event {
  display: inline-block;
  position: relative;
  max-width: 100%;
  padding: 6px 8px;
  color: #fff;
  font-size: 14px;
  line-height: 1;
  border-radius: $radius--mini;
  background-color: $blue;

  &.fill {
    width: 100%;
  }

  &.editable:hover {
    padding-right: 30px;

    i {
      display: inline-flex;
    }
  }

  &__icon {
    display: none;
    position: absolute;
    justify-content: center;
    align-items: center;
    top: 50%;
    right: 0;
    width: 30px;
    height: 100%;
    transform: translateY(-50%);
  }
}
</style>
