<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-02-08 10:27:21
 * @LastEditors  : 无尘
 * @LastEditTime : 2020-02-15 14:17:17
 -->
<!--
  添加成员选择部门组件：
  <select-department
  :selectData="selectData"
  @checkGroupIds="checkGroupIds">
  </select-department>

  import selectDepartment from '@/components/company/select-department.vue';

  checkGroupIds: function(nodes) {
    const that = this;
    that.conditionObj.groupIds = nodes;
  },
 -->
<template>
  <el-popover placement="bottom" style="min-width: 282px" v-model="departmentVisible">
    <div class="select-search">
      <el-input placeholder="请输入内容" v-model="searchSelect" style="width: 100%;" @keyup.native="value => toInput(value, searchSelect)" @clear="clearSearch"> <i slot="prefix" class="el-input__icon el-icon-search"></i> </el-input>
    </div>
    <div class="select-tree-wrap">
      <el-tree :data="storeData" node-key="groupId" ref="storeTree" show-checkbox default-expand-all :props="defaultProps" :expand-on-click-node="false" @check="checkStore">
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span :class="['font-14 p-r-10 iconfont', node.isStore == 1 ? 'icondianpu-kuai' : 'iconqiye-tianchong', node.bindFlag == 1 ? 'color-2f54eb' : 'color-dedfe6']"></span><span class="font-14 color-606266">{{ node.label }}</span>
        </span>
      </el-tree>
    </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="!storeGroupIds.length">请选择部门</span>
          <div class="el-select__tags" style="max-width: 181px;">
            <span>
              <template v-for="(item, index) in storeGroupIds">
                <span class="el-tag el-tag--info el-tag--small" :key="index">
                  <span class="el-select__tags-text">{{ item.departmentName }}</span>
                  <i class="el-tag__close el-icon-close" @click.stop="delDepart(index, storeGroupIds)"></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: 'store-tree-select',
  props: {
    brandId: {
      type: String,
      default() {
        return '';
      }
    },
    width: {
      type: String,
      default() {
        return '357px';
      }
    },
    selectData: {
      type: [Object, Array],
      default() {
        return [];
      }
    }
  },
  data() {
    return {
      projectName: '', // 当前项目名
      searchSelect: '', // 搜索字段
      departmentVisible: false,
      storeGroupIds: [],
      storeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    };
  },

  methods: {
    /**
     * 输入
     */
    toInput: _debounce(function(e, value) {
      const that = this;
      if (!that.storesCopy.length) {
        return false;
      }
      if (value == '') {
        that.stores = JSON.parse(JSON.stringify(that.storesCopy));
      }
      let filterData = JSON.parse(JSON.stringify(that.storesCopy)).filter(ele => {
        return ele.storeName.includes(value);
      });
      that.stores = filterData;
    }, 500),
    /**
     * 清空
     */
    clearSearch() {
      const that = this;
      that.stores = JSON.parse(JSON.stringify(that.storesCopy));
    },

    /**
     * 删除
     */
    delDepart(index, obj) {
      // const that = this;
      obj.splice(index, 1);
    },

    /**
     * 简单数组-->父子数组对象
     */
    treeData(data) {
      let tree = data.filter(father => {
        //循环所有项
        let branchArr = data.filter(child => {
          return father.groupId == child.parentId; //返回每一项的子级数组
        });
        if (branchArr.length > 0) {
          father.children = branchArr; //如果存在子级，则给父级添加一个children属性，并赋值
        }
        return father.groupId == 0; //返回第一层
      });
      return tree;
    },

    /**
     * 获取门店分组
     */
    getStoreGroup() {
      let that = this;
      let para = {
        parentId: '',
        isStore: '',
        keyword: ''
      };
      getRequest('/haoban-web/department-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            if (!!resData.result && !!resData.result.length) {
              that.treeData(resData.result);
              resData.result.forEach(ele => {
                if (ele.groupId == that.activeGroupId) {
                  that.storeData = [ele];
                }
              });
            }
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    brandId: function(newData, oldData) {
      const that = this;
      if (!!newData) {
        that.activeBrand = newData;
        that.checkStore();
        that.getStoreGroup();
      }
    },
    selectData: function(newData, oldData) {
      let that = this;
      if (!!newData.length) {
        that.storeGroupIds = newData;
      }
    }
  },
  mounted() {
    const that = this;
    if (!!that.brandId) {
      that.activeBrand = that.brandId;
      that.storeGroupIds = that.selectData;
      that.checkStore();
      that.getStoreGroup();
    }
  }
};
</script>
<style lang="less" scoped>
.depart-item-content {
  width: 213px;
  height: 32px;
  overflow: hidden;
  white-space: nowrap;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  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;
  }
}
</style>
