<template>
  <div class="notice-item">
    <div class="mode-text">
      <div class="mode">
        {{ type }}
        <sup v-if="!noticeInfo.read" class="unread-dot"></sup>
      </div>
      <div class="text">
        {{ noticeInfo.title }}
      </div>
    </div>
    <div class="time-avatar">
      <div class="time">
        {{ timestampToTime(noticeInfo.createdAt) }}
      </div>
      <div class="avatar">
        {{ noticeInfo.sender.name.substring(0, 1) }}
      </div>

      <div v-if="isCreator" class="teacher-buttons">
        <el-button class="update-button" type="text" @click.stop="$emit('update')">更改</el-button>
        <el-button class="delete-button" type="text" @click.stop="$emit('delete')">删除</el-button>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: "NoticeItem",
  props: {
    noticeVO: null,
  },
  data() {
    return {
      noticeInfo: null,
    };
  },
  computed: {
    type() {
      let res = "";
      switch (this.noticeInfo.type) {
        case "ANNOUNCEMENT":
          res = "公告";
          break;
        case "HOMEWORK":
          res = "作业";
          break;
        case "EXAM":
          res = "考试";
          break;
      }
      return res;
    },
    color() {
      let res = "";
      switch (this.noticeInfo.type) {
        case "ANNOUNCEMENT":
          res = "#67C23A";
          break;
        case "HOMEWORK":
          res = "#E38800";
          break;
        case "EXAM":
          res = "#F56C6C";
          break;
      }
      return res;
    },
    isTeacher() {
      return this.$store.getters.isTeacher;
    },
    isCreator() {
      return this.noticeInfo.sender.id === Number(this.$store.state.user.id);
    },
  },
  created() {
    this.noticeInfo = this.noticeVO;
  },
  methods: {
    timestampToTime(timestamp) {
      const date = new Date(timestamp);
      const Y = date.getFullYear() + "-";
      const M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
      const D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
      return Y + M + D;
    },
    setNoticeInfo(item) {
      this.noticeInfo = item;
      this.$forceUpdate();
    },
  },
};

</script>

<style scoped>
.notice-item {
  padding: 0 20px 0 20px;
  border-radius: 6px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 45px;
  cursor: pointer;
  transition: all 0.2s ease-in-out;
}

.notice-item:hover {
  background-color: #F0F0F0;
}

.mode-text, .time-avatar {
  display: flex;
  align-items: center;
  color: #666666;
  font-size: 16px;
}

.mode {
  position: relative;
  border-radius: 18px;
  /*border: v-bind(color) 2px solid;*/
  box-sizing: border-box;
  padding: 2px 4px;
  color: v-bind(color);
  margin-right: 16px;
  font-size: 18px;
}

.avatar {
  border-radius: 50%;
  background-color: #619dc9;
  height: 28px;
  width: 28px;
  line-height: 28px;
  text-align: center;
  color: #FFFFFF;
  margin-left: 16px;
}

.text, .time {
  font-size: 14px;
}

.teacher-buttons {
  margin-left: 16px;
}

.update-button {
  color: rgb(238, 190, 119);
}

.update-button:hover {
  color: #E38800
}

.delete-button {
  color: rgb(250, 182, 182);
}

.delete-button:hover {
  color: #F56C6C;
}

.unread-dot{
  background-color: #f56c6c;
  color: #fff;
  display: inline-block;
  font-size: 12px;
  line-height: 18px;
  text-align: center;
  white-space: nowrap;
  border: 1px solid #fff;

  position: absolute;
  top: 0;
  transform: translateY(-50%) translateX(100%);

  height: 8px;
  width: 8px;
  padding: 0;
  right: 0;
  border-radius: 50%;
}
</style>
