<template>
  <div id="trash">
    <div class="trash" @click="dialogVisible=true">
      <i class="iconfont icon-shanchu trashIcon"></i>
      <span>一键清除废弃标签值</span>
      <div class="trashState">
        <component :is="stateComponent" :progress="progress"></component>
      </div>
    </div>
    <el-dialog
      title="批处理撕标签"
      :visible.sync="dialogVisible"
      width="600px"
      @open="dialogOpen"
      @closed="dialogClosed">
      <el-alert
        title="每次操作一键清除的时间间隔为1小时。"
        type="info"
        show-icon
        :closable="false"
        style="width: 535px;margin-bottom:15px">
      </el-alert>
      <header>已选择删除的标签值</header>
      <ul class="tagBox">
        <li v-for="item in trashList" :key="item.tagItemId" class="tagItem">
          {{ item.tagItemName }}
          <i class="el-icon-error delIcon" @click="delCheckTag(item.tagItemId)"></i>
        </li>
      </ul>
      <span slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="doClean" :disabled="delTime > 0 || !trashList.length">{{ delTime > 0 ? getDelTime() : '一键清除' }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import CycleWrap from './CycleWrap.vue';
import { getProgress, getTrashList, isCleanTrashList, cleanTrashList, delTrashList } from '@/request/api';
let Success = {
  template: '<i class="el-icon-success" style="color:#52C41A;font-size:20px;"></i>'
}
let Pending = {
  props: ['progress'],
  template: '<div class="pendingClass">{{ progress }}</div>'
}

export default {
  name: 'trash',
  data() {
    return {
      progress: 0,
      trashList: [],
      stateComponent: '',
      dialogVisible: false,
      delTime: 0,
      interval: undefined
    }
  },
  components: { CycleWrap, Success, Pending },
  mounted() {
    this.getProgress();
  },
  methods: {
    getProgress() {
      getProgress().then(res => {
        const { errorCode, message, result } = res;
        if(errorCode != 1) return this.$message.error(message);
        if(result === null) {
          this.getTrashList();
        } else {
          this.progress = result;
          this.stateComponent = 'CycleWrap';
        }
      })
    },
    getTrashList() {
      getTrashList().then(res => {
        const { errorCode, message, result = [] } = res;
        if(errorCode != 1) return this.$message.error(message);
        this.trashList = result;
        this.progress = result.length;
        this.stateComponent = !result.length ? 'Success' : 'Pending';
      })
    },
    dialogOpen() {
      this.getTrashList();
      isCleanTrashList().then(res => {
        const { errorCode, message, result } = res;
        if(errorCode != 1) return this.$message.error(message);
        this.delTime = result;
        result && (this.interval = setInterval(_ => {
          this.delTime -= 1000;
        }, 1000))
      })
    },
    dialogClosed() {
      if(this.interval) clearInterval(this.interval);
      this.getProgress();
    },
    getDelTime() {
      let s = parseInt(this.delTime % (1000 * 60) / 1000);
      let m = parseInt((this.delTime % (1000 * 60 * 60)) / (1000 * 60));
      let h = parseInt(this.delTime % (1000 * 60 * 60 * 24) / (1000 * 60 * 60));
      return `倒计时: ${h}h:${m}min:${s}s`;
    },
    doClean() {
      if(this.delTime) return;
      cleanTrashList().then(res => {
        const { errorCode, message } = res;
        if(errorCode != 1) return this.$message.error(message);
        this.$message.success('已开始执行');
        this.dialogVisible = false;
        this.getProgress();
      })
    },
    delCheckTag(tagItemId) {
      delTrashList({ tagItemId }).then(res => {
        const { errorCode, message } = res;
        if(errorCode != 1) return this.$message.error(message);
        this.getTrashList();
        this.$message.success('移除成功');
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.trash {
  position: fixed;
  right: 29px;
  bottom: 370px;
  height: 44px;
  z-index: 2;
  display: flex;
  flex-wrap: nowrap;
  font-size: 14px;
  padding: 8px 10px 8px 0;
  background-color: #fff;
  color: #1890ff;
  box-shadow: 0 0 7px #7795b152;
  cursor: pointer;
  transition: .4s;
  overflow: hidden;
  width: 46px;
  &:hover {
    width: 220px;
  }
  .trashIcon {
    display: block;
    font-size: 28px;
    margin: 0 8px 0 10px;
  }
  span {
    display: flex;
    align-items: center;
    margin-top: -2px;
    white-space: nowrap;
  }
  .trashState {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 26px;
    height: 26px;
    margin-left: 8px;
  }
  .pendingClass {
    width: 25px;
    height: 25px;
    text-align: center;
    line-height: 25px;
    border-radius: 50%;
    background-color: #F5222D;
    color: #fff;
  }
}
header {
  color: #303133;
  font-weight: 700;
}
.tagBox {
  display: flex;
  flex-wrap: wrap;
  .tagItem {
    position: relative;
    margin-top: 10px;
    margin-right: 13px;
    padding: 5px 10px;
    border: 1px solid #DCDFE6;
    border-radius: 2px;
    background-color: #FAFAFA;
    font-size: 13px;
    color: #606266;
    &:hover .delIcon {
      display: block;
    }
  }
  .delIcon {
    display: none;
    position: absolute;
    top: -6px;
    right: -6px;
    cursor: pointer;
  }
}
</style>