<template>
  <div class="header-task">
    <el-popover placement="bottom" width="400" trigger="hover">
      <el-badge slot="reference" :hidden="tasks.length === 0" :value="tasks.length" :max="10" class="item">
        <svg-icon class-name="task-icon" icon-class="flag-fill" />
      </el-badge>
      <div class="task-header">
        您有
        <span class="task-num">{{ tasks.length }}</span>项待办任务
      </div>
      <el-scrollbar class="task-item-list" style="height:400px;">
        <div v-show="!tasks.length" class="no-tasks">您目前没有待办任务</div>
        <div v-for="task in tasks" :key="task.id" class="task-item">
          <div class="task-item-title">
            <i class="el-icon-tickets" />
            {{ task.applicationName }} - {{ task.type }}
          </div>
          <div class="task-item-content">{{ task.content }}</div>
          <div class="bottom clearfix">
            <span class="time">{{ task.creationTime | datetime }}</span>
            <el-button type="text" class="button" style="float:right;" @click="markAsDone(task)">完成</el-button>
            <el-button type="text" class="button" style="float:right;margin-right:10px" @click="handleTask(task)">进入处理</el-button>
          </div>
        </div>
      </el-scrollbar>
      <div>
        <el-button type="text" @click="handleMore">查看更多</el-button>
      </div>
    </el-popover>
  </div>
</template>

<script>
export default {
  name: 'HeaderTask',
  props: {
    items: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      tasks: this.items
    }
  },
  watch: {
    items(val) {
      this.tasks = val
    }
  },
  methods: {
    handleTask(row) {
      window.open(row.completePageUrl)
    },
    markAsDone(task) {
      this.tasks = this.tasks.filter(t => t.id !== task.id)
      this.$emit('markAsComplete', task)
    },
    handleMore() {
      this.$router.push({ name: 'Todo' })
    }
  }
}
</script>

<style lang="scss" scoped>
.header-task {
  font-size: 0 !important;

  ::v-deep .el-badge__content {
    height: 14px;
    line-height: 14px;
    font-size: 10px;
  }

  .item {
    line-height: 20px;
    ::v-deep .el-badge__content {
      padding: 0px 2px;
    }
  }

  .task-icon {
    cursor: pointer;
    font-size: 21px;
    vertical-align: middle;
  }
}
</style>

<style lang="scss">
.el-popover {
  .task-item-list {
    .el-scrollbar__wrap {
      overflow-x: auto;
    }

    .no-tasks {
      height: 400px;
      width: 100%;
      vertical-align: middle;
      line-height: 400px;
      text-align: center;
    }
  }

  .task-header {
    font-weight: bold;
    padding-bottom: 8px;
    border-bottom: 1px solid #ddd;

    .task-num {
      color: #fb6767;
    }
  }

  .task-item {
    padding-top: 8px;
    border-bottom: 1px solid #ddd;

    .task-item-title {
      font-weight: bold;
    }

    .task-item-content {
      margin-top: 5px;
    }

    .time {
      display: inline-block;
      margin-top: 10px;
    }
  }
}
</style>
