<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-02-08 10:27:21
 * @LastEditors: 无尘
 * @LastEditTime: 2020-08-14 17:05:22
 -->
<!--
  选择门店分组组件：
  <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', item.select ? 'selected hover' : '']" v-for="item in tags" :key="item.tagId">
                <el-checkbox :label="item.tagId">{{ item.tagName }}</el-checkbox>
              </li>
              <li v-if="!tags.length" class="text-center"><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.tagName }}</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 { getRequest } from '@/api/api';
import errMsg from '@/common/js/error';
// import { _debounce } from '@/common/js/public';
export default {
  name: 'gic-select-store',
  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: []
    };
  },

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

    /**
     * 删除
     */
    delDepart(index, arr) {
      arr.splice(index, 1);
    },

    /**
     * 获取门店标签
     */
    getTagsData() {
      const that = this;
      let para = {
        gicEnterpriseId: that.brandId
      };
      getRequest('/haoban-app-customer-web/cardCoupon/find-store-tag-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.tags = resData.result || [];
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  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.tagId);
      } else {
        that.dailyRuleForm.tags = [];
        that.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.tagId);
    } 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>
