<template>
  <!-- 具体标签 -->
  <div class="tag-some-list">
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="标签名称" prop="tagName" min-width="170" fixed="left">
        <template slot-scope="scope">
          <span class="tag-name">{{ scope.row.tagName }}</span>
          <el-tooltip class="item" effect="dark" content="添加标签" placement="bottom">
            <i class="iconfont icon-tag-name icon-PlusOutlined" v-if="getCodeAuth" :limit-code="getCode" v-track:common.click="handleTrackParam('addTag', scope.row)" @click="addTag(scope.row)"></i>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="上级分类名称" min-width="110" prop="tagLevelGroupName">
        <template slot-scope="{ row }">
          <span>{{ row.tagLevelGroupName || '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="覆盖人数" prop="memberCount" min-width="100" show-overflow-tooltip>
        <template slot="header">
          覆盖人数
          <el-popover width="260" trigger="hover" placement="top">
            <div style="color: #303133;">
              标签下不同标签值可能覆盖相同的会员，因此此处“标签”覆盖人数去重显示（如“喜欢的颜色-白、红、黑、蓝”，某会员身上可能同时具备“白、黑”二种标签值）
            </div>
            <i slot="reference" class="iconfont icon-QuestionCircleOutlined refresh"></i>
          </el-popover>
        </template>

        <template slot-scope="scope">
          {{ scope.row.memberCount ? (scope.row.memberCount | formatNum) + '人' : '--' }}
        </template>
      </el-table-column>
      <el-table-column label="标签描述" prop="tagDescribe" min-width="150">
        <template slot-scope="scope">
          <div>{{ scope.row.tagDescribe ? scope.row.tagDescribe : '--' }}</div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" min-width="100">
        <template slot-scope="{ row }">
          <template v-if="row.createTime">
            <p>{{ dateformat(new Date(row.createTime), 'yyyy-MM-dd') }}</p>
            <p>{{ dateformat(new Date(row.createTime), 'hh:mm:ss') }}</p>
          </template>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" min-width="200">
        <template slot-scope="scope">
          <div class="font-0">
            <el-button class="m-r-12" type="text" @click="editActiveTag(scope.row)">编辑</el-button>
            <el-button class="m-r-12" type="text" @click="editActiveTagValue(scope.row)">详情</el-button>
            <el-button class="m-r-12" type="text" @click="deleteActiveTag(scope.row, scope.$index, $event)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <delete-tag-dialog :visible.sync="deleteTagDialog.visible" :data="deleteTagDialog.deleteData" @submit="doDelete" />
    <i id="activeAniIcon" class="iconfont icon-biaoqian activeAniIcon" :style="{ right: `${animationPos.x}px`, bottom: `${animationPos.y}px` }"></i>
  </div>
</template>

<script>
import Emitter from '@/mixins/emitter';
import tagDetails from '@/components/tagDetail/mixin/index';
import { dateformat } from '@/utils/formatTime';
import { delThirdHandTag } from '@/request/api';
import DeleteTagDialog from '../manualTag/delete-tag-dialog.vue';
import errMsg from '@/common/js/error';
import { getRequest } from '@/api/api';
export default {
  name: 'tag-container',

  mixins: [tagDetails, Emitter],
  components: { DeleteTagDialog },
  props: {
    data: {
      type: Array,
      default() {
        return [];
      }
    },
    refersh: Object
  },

  data() {
    return {
      tableData: [],
      tagData: {},
      dateformat,
      deleteTagDialog: {
        visible: false,
        deleteData: {}
      },
      animationPos: {}
    };
  },
  methods: {
    addTag(list) {
      this.$emit('addTag', list);
    },
    handleTrackParam(type, data) {
      let fn = {
        addTag: () => {
          return {
            data: { id: data.tagId, name: data.tagName }
          };
        }
      }[type];
      return { fn: () => {}, data: fn ? fn() : {} };
    },
    editActiveTag(list) {
      this.$emit('ediActiveTag', list);
    },
    editActiveTagValue(list) {
      this.$emit('setGroupId');
      localStorage.setItem('jumpTag', '');
      localStorage.setItem('jumpThirdTag', '');
      this.$router.push({
        path: '/manualTagValueEdit',
        query: { tagId: list.tagId, type: 'active' }
      });
    },
    deleteActiveTag(list, index, e) {
      this.deleteTagDialog.visible = true;
      this.deleteTagDialog.deleteData = {
        ...list,
        currentIndex: index,
        e
      };
    },
    doDelete(list) {
      const { currentIndex: index, e, memberCount } = list;
      // memberCount为0时不加入垃圾桶,直接删除
      if (memberCount == 0) {
        this.directDelTag(list);
      } else {
        this.intoTrash(list, index, e);
      }
    },
    directDelTag(list) {
      const param = {
        tagId: list.tagId,
        requestProject: 'gic-member-tag-web'
      };
      delThirdHandTag(param)
        .then(res => {
          if (res.errorCode == 1) {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1000
            });
            this.$emit('deleteHandTag', this.groupId);
          }
        })
    },
    // 加入到垃圾桶
    intoTrash(item, index, e) {
      const para = {
        itemId: item.tagId,
        type: 1
      };
      getRequest('/delTagItem/add', para).then(res => {
        const { errorCode } = res.data;
        if (errorCode === 1) {
          item.delStatus = 0;
          this.startAnimation(e)
          this.$store.commit('changeDelTrashNum', this.$store.state.delTrashNum + 1);
          this.$message.success('添加成功，请点击右侧「垃圾桶」图标进行删除');
          return;
        }
        errMsg.errorMsg(res.data);
      }).catch(error => {
        this.$message.error({
          duration: 1000,
          message: error.message
        });
      });
    },
    startAnimation(e) {
      this.animationPos = {
        x: document.body.offsetWidth - e.pageX,
        y: document.body.offsetHeight - e.pageY
      }
      document.getElementById('activeAniIcon').style.display = 'block';
      setTimeout(() => {
        document.getElementById('activeAniIcon').style.display = 'none';
      }, 1800)
    },
  },
  watch: {
    data: {
      immediate: true,
      handler(newval) {
        this.tableData = newval;
      }
    },
    refersh: {
      immediate: true,
      handler(newval) {
        // 刷新
        if (newval.changeRefersh) {
          let index = this.tableData.findIndex(el => newval.tagId === el.tagId);
          this.tableData[index].refersh = true;
        }
      }
    }
  },
  computed: {
    getCodeAuth() {
      let code = 'memberTagCreateGroup';
      return this.$getButtonLimit(this.$buttonCode[code]);
    },
    getCode() {
      let code = 'memberTagCreateGroup';
      return this.$buttonCode[code];
    }
  }
};
</script>

<style lang="scss" scoped>
.tag-some-list {
  margin: 0 20px;
  overflow-y: auto;
}
.tag-name {
  display: inline-block;
  word-break: break-all;
  max-width: 120px;
}
.icon-tag-name {
  display: inline-block;
  vertical-align: top;
  font-size: 16px;
  cursor: pointer;
  color: #303133;
  transition: all .3s;
  color: #2f54eb;
  &:hover {
    transform: scale(1.25);
  }
}
.refresh {
  color: #606266;
}
#activeAniIcon {
  display: none;
  position: fixed;
  z-index: 99999;
  color: #2F54EB;
  font-size: 26px;
  animation: toTrash 2s;
  opacity: 0;
}
@keyframes toTrash {
  0% {
    opacity: 1;
  }
  40% {
    opacity: 1;
    right: 38px;
    bottom: 345px;
    transform: scale(0.5);
  }
  50% {
    opacity: 0.5;
    right: 37px;
    bottom: 345px;
    transform: scale(1.2);
  }
  100% {
    opacity: 0.5;
    right: 37px;
    bottom: 345px;
    transform: scale(0);
  }
}
</style>
