<template>
  <div class="w-548 border-box">
    <div :class="['tags-group', { 'tags-group--active': active }]" @click="checkTagsGroup">
      <!--右上角的下拉菜单-->
      <el-dropdown class="tags-group__dropdown" placement="bottom-end">
        <el-popover width="200" placement="top" v-model="showDelPop" trigger="manual">
          <p class="m-b-20">确认删除输入框及内部所有标签？</p>
          <div class="text-right">
            <el-button @click="showDelPop = false">取消</el-button>
            <el-button type="primary" @click="delTagsGroup">确认</el-button>
          </div>
          <i slot="reference" class="el-icon-more"></i>
        </el-popover>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-if="showAddMenu" @click.native="addTags">添加标签</el-dropdown-item>
          <el-dropdown-item @click.native="showDelPop = true">删除</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <!--展示选中的标签-->
      <div class="tags-group__show">
        <div class="tags-group__tag" v-for="(item, index) in tagsData" :key="item.tagId">
          <span>{{ item.tagName }}</span>
          <span class="tags-group__tag-name">【选择:{{ item.newTagVal }}】</span>
          <i class="el-icon-edit" @click="editTags(index)"></i>
          <i class="el-icon-error" @click="delTags(index)"></i>
        </div>
      </div>
    </div>
    <!--标签组之间的关系-->
    <div class="tags-group__relation" v-show="showTagsRelation">
      <el-tooltip content="点击切换运算符" placement="bottom-start">
        <el-button type="btn btn-default tags-group__btn" @click="changeTagsGroupRelation">
          {{ tagsRelation === 'or' ? '或者' : tagsRelation === 'andNot' ? '剔除' : '' }}
        </el-button>
      </el-tooltip>
    </div>
  </div>
</template>

<script>
/**
 * props
 *  @tagsData 一组标签的数据
 *  @showTagsRelation 是否显示与下一组标签之间的关系
 *  @tagsRelation 与下一组标签之间的关系（or或者，andNot剔除）
 *  @tagsGroupIndex 当前标签组的索引
 *  @showAddMenu 下拉菜单是否显示“添加标签”菜单项
 * methods
 *  @delTagsGroup 删除一个标签组，将会emit(delTagsGroup事件，{tagsGroupIndex: 当前标签组的索引})
 *  @delTags 删除一个标签，将会emit(delTags事件， {tagsGroupIndex: 当前标签组的索引, tagIndex: 当前标签的索引})
 *  @addTags 添加一个标签，将会emit(addTags事件, {tagsGroupIndex: 当前标签组的索引})
 *  @editTags 编辑一个标签，将会emit(editTags事件, {tagsGroupIndex: 当前标签组的索引, tagIndex: 当前标签的索引})
 *  @changeTagsGroupRelation 切换与下一组标签之间的关系，将会emit(changeTagGroupRelation事件)
 * */
export default {
  name: 'tags-group',
  props: {
    // 一组标签（选中的）的数据
    tagsData: Array,
    // 是否显示剔除按钮
    showTagsRelation: Boolean,
    // 与下一组标签之间的关系 or 、andNot
    tagsRelation: String,
    // 当前标签组的索引
    tagsGroupIndex: Number,
    // 下拉菜单是否显示"添加标签"菜单项
    showAddMenu: Boolean,
    // 标签组是否选中
    active: Boolean
  },
  data() {
    return {
      // 确认删除标签组的弹窗
      showDelPop: false
    };
  },
  methods: {
    // 删除一个标签组
    delTagsGroup() {
      this.showDelPop = false;
      this.$emit('delTagsGroup', {
        tagsGroupIndex: this.tagsGroupIndex
      });
    },
    // 添加一个标签
    addTags() {
      this.$emit('addTags', {
        tagsGroupIndex: this.tagsGroupIndex
      });
    },
    // 删除一个标签
    delTags(tagIndex) {
      this.$emit('delTags', {
        tagsGroupIndex: this.tagsGroupIndex,
        tagIndex
      });
    },
    // 编辑一个标签
    editTags(tagIndex) {
      this.$emit('editTags', {
        tagsGroupIndex: this.tagsGroupIndex,
        tagIndex
      });
    },
    // 切换与下一个标签组之间的关系
    changeTagsGroupRelation() {
      this.tagsRelation = this.tagsRelation === 'or' ? 'andNot' : 'or';
      this.$emit('changeTagsGroupRelation', { relationIndex: this.tagsGroupIndex });
    },
    // 选中当前标签组
    checkTagsGroup() {
      this.$emit('checkTagsGroup', {
        tagsGroupIndex: this.tagsGroupIndex
      });
    }
  }
};
</script>

<style lang="less" scoped>
.tags-group {
  position: relative;
  padding: 12px 0;
  border: 1px solid #dcdfe6;
  border-radius: 5px;

  .tags-group__dropdown {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 1;
    padding: 0 8px;
    height: 20px;
  }

  .tags-group__show {
    padding: 12px;
    height: 150px;
    overflow-y: scroll;
  }

  .tags-group__tag {
    display: inline-block;
    vertical-align: top;
    padding: 0 10px;
    margin-right: 8px;
    margin-bottom: 8px;
    background: #f3f6f9;
    border-radius: 2px;
    font-size: 14px;
    color: #606266;
    cursor: pointer;

    &:hover {
      color: #303133;
      background: #e6e8ed;

      i.el-icon-edit {
        color: #1890ff;
      }

      i.el-icon-error {
        color: #f56c6c;
      }
    }

    &:last-child {
      margin-bottom: 0;
    }

    span {
      display: inline-block;
      vertical-align: middle;
    }

    i {
      color: #999;
      cursor: pointer;
      padding-left: 10px;
    }

    .tags-group__tag-name {
      max-width: 200px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}

.tags-group--active {
  border: 1px solid #1890ff;
}

.tags-group__relation {
  position: relative;
  transform: translate(-60px, 0);

  .tags-group__btn {
    padding: 0;
    width: 40px;
    height: 40px;
    text-align: center;
    line-height: 40px;
    border-radius: 40px;
  }

  &::before,
  &::after {
    content: '';
    display: block;
    position: absolute;
    left: 20px;
    z-index: 1;
    width: 40px;
    height: 80px;
    border-left: 1px solid #dcdfe6;
  }

  &::before {
    top: -80px;
    border-top: 1px solid #dcdfe6;
  }

  &::after {
    bottom: -80px;
    border-bottom: 1px solid #dcdfe6;
  }
}
</style>
