<template>
  <!-- 具体标签 -->
  <div class="tag-some-list">
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="标签名称" prop="tagName" min-width="100" 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="90" prop="tagLevelGroupName">
        <template slot-scope="{ row }">
          <span>{{ row.tagLevelGroupName || '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="覆盖人数" prop="memberCount" min-width="80" 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="90">
        <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">
        <template slot-scope="{ row }">
          <el-button type="text" @click="editActiveTag(row)">编辑</el-button>
          <el-button type="text" @click="editActiveTagValue(row)">详情</el-button>
          <el-button type="text" @click="deleteActiveTag(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <delete-tag-dialog :visible.sync="deleteTagDialog.visible" :data="deleteTagDialog.deleteData" @submit="doDelete" />
  </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';
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: {}
      },
    };
  },
  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) {
      this.deleteTagDialog.visible = true;
      this.deleteTagDialog.deleteData = list;
    },
    doDelete(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('deleteActiveTag', this.groupId);
          }
        })
    },
  },
  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;
}
.icon-tag-name {
  display: inline-block;
  vertical-align: bottom;
  font-size: 16px;
  cursor: pointer;
  color: #303133;
  transition: all .3s;
  &:hover {
    transform: scale(1.25);
  }
}
.refresh {
  color: #909399;
  &:hover {
    color: #2f54eb;
  }
}
</style>
