<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-02-08 10:27:21
 * @LastEditors: 无尘
 * @LastEditTime: 2020-11-20 16:37:59
 -->
<!--
  选择门店分组组件：
  <gic-select-tag
  :selectData="selectData"
  @returnTags="returnTags">
  </gic-select-tag>

  import gicSelectTag from '@/components/app/card/gic-select-tag.vue';

  returnTags: function(nodes) {
    const that = this;
    that.conditionObj.tags = nodes;
  },
 -->
<template>
  <el-popover placement="bottom" title="" width="350" trigger="click" v-model="storeVisible">
    <div class="daily-store-select">
      <div class="el-scrollbar define-search-select">
        <div class="el-select-dropdown__wrap el-scrollbar__wrap" style="margin-bottom: -5px; margin-right: -5px;">
          <el-checkbox-group v-model="dailyRuleForm.tags" @change="handleStoresChange">
            <ul class="el-scrollbar__view el-select-dropdown__list">
              <li class="el-select-dropdown__item" v-for="item in tags" :key="item.storeTagId">
                <el-checkbox :label="item.storeTagId">{{ item.storeTagName }}</el-checkbox>
              </li>
              <li v-if="!tags.length" class="text-center el-select-dropdown__item"><span>暂无门店标签</span></li>
            </ul>
          </el-checkbox-group>
        </div>
        <div class="el-scrollbar__bar is-horizontal"><div class="el-scrollbar__thumb" style="transform: translateX(0%);"></div></div>
        <div class="el-scrollbar__bar is-vertical"><div class="el-scrollbar__thumb" style="transform: translateY(0%);"></div></div>
      </div>
    </div>
    <div class="flex-column item-cell-select inline-block " slot="reference">
      <div class="depart-item-wrap">
        <div :style="{ width: width }" class="el-select el-select--large depart-item-content">
          <span class="font-14 color-c0c4cc p-l-10" style="display: inline-block;line-height: 32px;color: #c0c4cc;" v-if="!dailyRuleForm.tags.length">请选择门店标签</span>
          <div class="el-select__tags" style="max-width: 348px;">
            <span style="display:block">
              <template v-for="(item, index) in selectTags">
                <span class="el-tag el-tag--info el-tag--small" :key="index + 'tag'">
                  <span class="el-select__tags-text">{{ item.storeTagName }}</span>
                  <i class="el-tag__close el-icon-close" @click.stop="delDepart(index, selectTags)"></i>
                </span>
              </template>
            </span>
          </div>
        </div>
      </div>
    </div>
  </el-popover>
</template>
<script>
import fetch from '@/api/target-manage-app.js';
const { getTagList } = fetch;
// import errMsg from '@/common/js/error';
// import { _debounce } from '@/common/js/public';
export default {
  name: 'GicSelectTag',
  props: {
    width: {
      type: String,
      default() {
        return '357px';
      }
    },
    selectData: {
      type: [ Object, Array ],
      default() {
        return [];
      }
    },
    brandId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      projectName: '', // 当前项目名
      enterpriseId: this.brandId,
      storeVisible: false,
      tags: [],
      dailyRuleForm: {
        tags: []
      },
      selectTags: []
    };
  },
  mounted() {
    const that = this;
    if (!!that.brandId) {
      that.tags = [];
      that.getTagsData();
    }
    if (!!that.selectData.length) {
      that.selectTags = JSON.parse(JSON.stringify(that.selectData));
      that.dailyRuleForm.tags = that.selectData.map(ele => ele.storeTagId);
    } else {
      that.dailyRuleForm.tags = [];
      that.selectTags = [];
    }
  },

  methods: {
    /**
     * @description: 选中改变
     * @param {Array} value
     * @author: 无尘
     */
    handleStoresChange(value) {
      const that = this;
      let arr = [];
      that.tags.forEach(ele => {
        if (value.includes(ele.storeTagId)) {
          arr.push(ele);
        }
      });
      that.selectTags = JSON.parse(JSON.stringify(arr));
      that.$emit('returnTags', that.selectTags);
    },

    /**
     * @description: 删除
     * @param {Number} index
     * @param {Array} arr
     * @author: 无尘
     */
    delDepart(index, arr) {
      const that = this;
      arr.splice(index, 1);
      that.$emit('returnTags', that.selectTags);
    },

    /**
     * @description: 获取门店标签
     * @author: 无尘
     */
    getTagsData() {
      const that = this;
      let para = {
        enterpriseId: that.brandId
      };
      getTagList(para)
        .then(res => {
          if (res.code == '0000') {
            that.tags = res.result || [];
            return;
          }
        })
        .catch(function(error) {

        });
    }
  },
  watch: {
    brandId(val) {
      const that = this;
      if (val) {
        that.dailyRuleForm.tags = [];
        that.selectTags = [];
        that.tags = [];
        that.getTagsData();
      }
    },
    selectData: function(newData, oldData) {
      const that = this;
      if (!!newData.length) {
        that.selectTags = JSON.parse(JSON.stringify(newData));
        that.dailyRuleForm.tags = newData.map(ele => ele.storeTagId);
      } else {
        that.dailyRuleForm.tags = [];
        that.selectTags = [];
      }
    }
  }
};
</script>
<style lang="less" scoped>
.depart-item-content {
  width: 213px;
  height: 32px;
  overflow: hidden;
  white-space: nowrap;
  border-radius: 2px;
  border: 1px solid #c4c6cf;
  cursor: pointer;
  box-sizing: border-box;
}

.select-tree-wrap {
  max-height: 300px;
  overflow-y: auto;
}

.item-cell-select {
  /deep/ .el-select__tags {
    white-space: nowrap;
    overflow: hidden;
  }
  .el-select {
    .el-tag {
      border-radius: 2px;
      border: 1px solid rgba(220, 223, 230, 1);
      background-color: #fff;
      .el-tag__close {
        top: 1px;
        color: #909399;
        &:hover {
          background-color: transparent;
        }
      }
    }
  }
}
.w-350 {
  width: 350px;
}
.show-select-num {
  position: relative;
  display: inline-block;
  font-size: inherit;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  -webkit-appearance: none;
  background-color: #fff;
  background-image: none;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  color: #606266;
  .el-select__caret {
    color: #c0c4cc;
    font-size: 14px;
    transition: transform 0.3s;
    transform: rotate(180deg);
    cursor: pointer;
    &.is-reverse {
      transform: rotate(0deg);
    }
  }
}
.daily-store-select {
  position: relative;
}
</style>
