<template>
  <div class="member-group-container">
    <div class="echo-member-group" v-if="echoFlag">
      <div class="subTitle">
        <div class="line"></div>
        <div class="text">
          <div class="space-between">
            已选分组
            <span v-if="!onlyRead"><el-button type="text" class="delBtn" @click="del">删除</el-button><el-button type="text" @click="edit">编辑</el-button></span>
          </div>
        </div>
      </div>
      <div class="echoLine" v-for="item in defaultSelected" :key="item.memberTagGroupId">
        <div class="dot"></div>
        <el-tag size="mini" v-if="item.isRealTime">实时</el-tag>
        <el-tag size="mini" type="warning" v-else>非实时</el-tag>
        <span class="groupName"> {{ item.groupName }}</span>
      </div>
    </div>
    <el-dialog title="选择客户分组" :visible.sync="visiable" width="1000px" :before-close="close">
      <div class="member-group">
        <div class="left">
          <el-tabs v-model="activeName">
            <el-tab-pane label="我的客户分组" name="0" v-if="!onlyFixedType">
              <dm-table ref="table0" name="0" :creatorId="creatorId" :effectiveStatus="effectiveStatus" :readonly="readonly" :key="visiable" :realTimeType="realTimeType" :activeName="activeName" :selected="selectedArray" @handleSelectionChange="handleSelectionChange" @deleteRow="deleteRow" />
            </el-tab-pane>
            <el-tab-pane label="固化分组" name="1">
              <dm-table ref="table1" name="1" :creatorId="creatorId" :effectiveStatus="effectiveStatus" :readonly="readonly" :key="visiable" :realTimeType="realTimeType" :activeName="activeName" :selected="selectedArray" @handleSelectionChange="handleSelectionChange" @deleteRow="deleteRow" />
            </el-tab-pane>
            <el-tab-pane label="金字塔会员分层" name="2" v-if="!onlyFixedType">
              <dm-table ref="table2" name="2" :creatorId="creatorId" :effectiveStatus="effectiveStatus" :readonly="readonly" :key="visiable" :realTimeType="realTimeType" :activeName="activeName" :selected="selectedArray" @handleSelectionChange="handleSelectionChange" @deleteRow="deleteRow" />
            </el-tab-pane>
          </el-tabs>
        </div>
        <div class="right">
          <div class="right-top">
            已选分组
          </div>
          <ul class="right-content">
            <li class="contact-li" v-for="item in selectedArray" :key="item.memberTagGroupId">
              <div class="li-cell cursor-pointer">
                <div>
                  <el-tag size="mini" v-if="item.isRealTime">实时</el-tag>
                  <el-tag size="mini" type="warning" v-else>非实时</el-tag>
                  <span class="groupName"> {{ item.groupName }}</span>
                </div>
                <i v-if="!readonly" class="el-icon-close" @click="deleteRow(item)"></i>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { baseUrl } from './index.js';
import table from './table';
export default {
  name: 'gic-new-member-group',
  props: {
    selectedIds: {
      type: String,
      default() {
        return [];
      }
    },
    visiable: {
      type: Boolean,
      default: false
    },
    height: {
      type: Number,
      default: 543
    },
    projectName: {
      type: String,
      default: 'memberTag'
    },
    creatorId: {
      type: String,
      default: ''
    },
    effectiveStatus: {
      type: [String, Number],
      default: 1
    },
    maxLimit: {
      type: Number,
      default: 5
    },
    onlyFixedType: {
      // 仅展示固化分组
      type: Boolean,
      default: false
    },
    readonly: {
      // 只读
      type: Boolean,
      default: false
    },
    realTimeType: {
      // 实时与非实时分组的可选
      // 0：可选非实时分组 1：可选实时分组
      type: Array,
      default: () => [0, 1]
    },
    onlyRead: {
      type: Boolean,
      default: false
    }
  },
  components: {
    'dm-table': table
  },
  data() {
    return {
      activeName: '0', //0 我的客户分组 1固化分组 2金字塔会员分层
      baseUrl,
      echoFlag: false,
      selected: {}
    };
  },
  mounted() {
    document.addEventListener('visibilitychange', () => {
      if (!document.hidden) {
        this.$refs[`table${this.activeName}`] && this.$refs[`table${this.activeName}`].getGroupList();
      }
    });
    if (this.onlyFixedType) {
      this.activeName = '1';
    }
  },
  computed: {
    selectedArray() {
      let arr = [];
      Object.keys(this.selected).forEach(item => {
        arr.push(this.selected[item]);
      });
      return arr;
    }
  },
  watch: {
    selectedIds: {
      immediate: true,
      handler(val) {
        if (val.length) {
          this.echoFlag = true;
          this.getDefaultSelected(val);
        }
      }
    },
    visiable(val) {
      if (val && this.selectedIds) {
        this.getDefaultSelected(this.selectedIds);
      }
    }
  },
  methods: {
    getDefaultSelected(val) {
      this.axios.get(`${this.baseUrl}/gic-member-tag-web/member-tag-group/queryGroupByIds?requestProject=${this.projectName}&memberTagGroupIds=${val}`).then(res => {
        if (res.data.errorCode == 1) {
          this.defaultSelected = res.data.result;
          this.handleSelectionChange(this.defaultSelected);
        }
      });
    },
    confirm() {
      if (!Object.keys(this.selected).length) {
        this.$message.warning('请选择分组');
        return;
      }
      let arr = [];
      Object.keys(this.selected).forEach(key => {
        arr.push(this.selected[key]);
      });
      const str = arr.map(item => item.memberTagGroupId).join(',');
      if (arr.length) {
        this.echoFlag = true;
      } else {
        this.echoFlag = false;
      }
      this.initPage();
      this.$emit('change', arr, str); // 被选中数组，数组id的字符串
    },
    close() {
      Object.keys(this.selected).forEach(key => {
        this.deleteRow(this.selected[key]);
      });
      this.initPage();
    },
    initPage() {
      this.activeName = '0';
      this.$emit('update:visiable', false);
    },
    del() {
      this.selected = {};
      this.$emit('change', [], '');
      this.echoFlag = false;
    },
    edit() {
      this.$emit('update:visiable', true);
    },
    handleSelectionChange(val) {
      const obj = JSON.parse(JSON.stringify(this.selected));
      if (val.length) {
        val.forEach(item => {
          obj[item.memberTagGroupId] = item;
        });
        if (Object.keys(obj).length > this.maxLimit) {
          this.deleteRow(val[val.length - 1]);
          this.$message.error(`最多支持选择${this.maxLimit}个分组`);
          return false;
        }
      }
      this.selected = obj;
    },
    deleteRow(row) {
      delete this.selected[row.memberTagGroupId];
      this.selected = Object.assign({}, this.selected);
      this.$refs[`table${this.activeName}`].$refs.table.toggleRowSelection(row, false);
    }
  }
};
</script>

<style lang="scss" scoped>
.delBtn {
  color: #f5222d;
}
.space-between {
  display: flex;
  justify-content: space-between;
}
.subTitle {
  width: 100%;
  height: 40px;
  display: flex;
  box-sizing: border-box;
  align-items: center;
  .line {
    width: 2px;
    height: 14px;
    background: #2f54eb;
    margin-right: 8px;
  }
  .text {
    font-size: 14px;
    color: #303133;
    width: 100%;
  }
}
.member-group-container {
  width: 400px;
}
.echo-member-group {
  width: 100%;
  background: #f7f8fa;
  border-radius: 4px;
  padding: 12px 15px;
  box-sizing: border-box;
  .echoLine {
    margin-bottom: 8px;
    line-height: 20px;
    .dot {
      display: inline-block;
      width: 6px;
      height: 6px;
      background: #2f54eb;
      border-radius: 3px;
      margin-right: 6px;
    }
    .groupName {
      font-weight: 400;
      margin-left: 5px;
    }
    &:first-child {
      margin-top: 20px;
    }
  }
}
.member-group {
  background: #ffffff;
  display: flex;
  .left {
    width: 666px;
    height: 582px;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    padding: 5px 17px;
  }

  .right {
    width: 274px;
    // height: 582px;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    margin-left: 12px;

    .right-top {
      padding: 20px 15px;
      display: flex;
      justify-content: space-between;
      color: #303133;
      box-sizing: border-box;
      line-height: 20px;
      background: #fff;
    }
    .right-content {
      color: #303133;
      overflow-y: auto;
      height: 89%;
    }
    .contact-li {
      padding: 0 12px 0 16px;
      margin-top: 4px;
      &:hover {
        background: #f7f8fa;
      }
      &:first-child {
        margin-top: 0;
      }
      .li-cell {
        border-radius: 2px;
        line-height: 32px;
        display: inline-flex;
        width: 100%;
        justify-content: space-between;
        padding: 0 4px;
        align-items: center;
        box-sizing: border-box;

        i {
          width: 20px;
          height: 20px;
          border-radius: 10px;
          cursor: pointer;
          font-size: 12px;
          display: inline-flex;
          justify-content: center;
          align-items: center;
          &:hover {
            background: #e5e6eb;
          }
        }
      }
    }
    .contact-li + .contact-li {
      margin-top: 4px;
    }
  }
}
</style>
