<template>
  <transition>
    <div class="dsk-table-header-setting-bar" :class="{'is-outer' : isOuter}">
      <div class="dsk-table-header-setting-bar-inner">

        <!-- 固定表头 -->
        <div class="lock-header-container" v-if="lockColumn.length">
          <div class="lock-header-item" v-for="(item,index) of lockColumn" :key="item.uid">
            <img src="@/assets/images/consultingAgencyManagement/lock.png" alt="">
            <div class="lock-header-item-label">{{item.label}}</div>
          </div>
        </div>

        <!-- 已选字段 -->
        <div class="use-header-container" v-if="useColumn.length">
          <div class="use-header-title">已选字段</div>
          <vuedraggable v-model="useColumn" class="use-header-container-inner" ghostClass="dragClass">
            <transition-group name="fade" tag="div">
              <div class="use-header-item" v-for="(item,index) of useColumn" :key="item.uid">
                <el-checkbox v-model="item.use" class="use-header-item-label"
                  @change="flag => checkedChange(index,flag,item)">{{item.label}}</el-checkbox>
                <img src="@/assets/images/consultingAgencyManagement/use-header-btn.png" alt="">
              </div>
            </transition-group>
          </vuedraggable>
        </div>

        <!-- 未选字段 -->
        <div class="not-use-header-container" v-if="notUseColumn.length">
          <div class="not-use-header-title">未选字段</div>
          <div class="not-use-header-item" v-for="(item,index) of notUseColumn" :key="item.uid">
            <el-checkbox v-model="item.use" class="not-use-header-item-label"
              @change="flag => checkedChange(index,flag,item)">{{item.label}}</el-checkbox>
          </div>
        </div>

      </div>

      <!-- 确定 取消 -->
      <div class="dsk-table-header-setting-footer">
        <el-button @click.stop="cancel">取消</el-button>
        <el-button type="primary" @click.stop="ok">确定</el-button>
      </div>

    </div>
  </transition>
</template>
<script>
import vuedraggable from "vuedraggable";
import { v4 } from 'uuid';
export default {
  name: "dskTableHeaderSettingBar",
  components: {
    vuedraggable
  },
  props: {
    settingList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      lockColumn: [],
      useColumn: [],
      notUseColumn: [],
      isOuter: false
    };
  },
  //可访问data属性
  created() {
    this.containerPositionInit();
    this.settingHeaderClassify();
  },
  //计算集
  computed: {

  },
  //方法集
  methods: {
    async containerPositionInit() {
      try {
        await this.$nextTick();
        const container = document.querySelector(".dsk-table-header-setting");
        // 获取定位元素距离 视口右边缘状况
        const right = container.getBoundingClientRect().right;
        // 获取屏幕视口大小进行对比
        const viewPortWidth = document.documentElement.clientWidth;
        // 获取当前容器超出点击区域宽度
        const settingBar = document.querySelector(".dsk-table-header-setting-bar").offsetWidth - container.offsetWidth;
        // 是否超出视口
        const outer = right + settingBar > viewPortWidth ? true : false;
        this.isOuter = outer;
      } catch (error) {

      }
    },
    // 表头当前状态分类
    settingHeaderClassify() {
      if (this.settingList?.length) {
        const temp = JSON.parse(JSON.stringify(this.settingList));
        const len = temp.length;
        for (let index = 0; index < len; index++) {
          temp[index].uid = v4();
          if (temp[index].lock) {
            this.lockColumn.push({
              ...temp[index],
              use: true,
            });
            continue;
          }
          // 设置了false值
          if (temp[index].hasOwnProperty("use") && temp[index].use === false) {
            this.notUseColumn.push({
              ...temp[index],
              use: false
            });
            continue;
          }
          // 默认为true 展示
          this.useColumn.push({
            ...temp[index],
            use: true,
          });
        }
        // console.log(this.lockColumn);
        // console.log(this.useColumn);
        // console.log(this.notUseColumn);
      }
    },
    checkedChange(index, flag, item) {
      // 选中 往use push
      if (flag) {
        this.notUseColumn.splice(index, 1);
        this.useColumn.push(item);
        return;
      }
      this.useColumn.splice(index, 1);
      this.notUseColumn.push(item);
    },
    cancel() {
      this.$emit("settingClose");
    },
    ok() {
      const _lockColumn = JSON.parse(JSON.stringify(this.lockColumn));
      const _useColumn = JSON.parse(JSON.stringify(this.useColumn));
      const _notUseColumn = JSON.parse(JSON.stringify(this.notUseColumn));
      this.$emit("settingChange", _lockColumn.concat(_useColumn, _notUseColumn));
      this.cancel();
    }
  },
}
</script>
<style lang="scss" scoped>
.dsk-table-header-setting-bar {
  position: absolute;
  top: 28px;
  left: 0px;
  width: 236px;
  height: 456px;
  background: #fff;
  border: 1px solid #eeeeee;
  border-radius: 4px;
  box-sizing: border-box;
  z-index: 3000;
  box-shadow: 0px 4px 8px 2px rgba(0, 0, 0, 0.04),
    0px 2px 6px 0px rgba(0, 0, 0, 0.06), 0px 0px 4px 0px rgba(0, 0, 0, 0.08);

  &.is-outer {
    left: unset;
    right: 0px;
  }

  .dsk-table-header-setting-bar-inner {
    width: 100%;
    height: calc(100% - 56px);
    padding: 12px;
    box-sizing: border-box;
    overflow-y: auto;

    &::-webkit-scrollbar {
      width: 16px;
    }

    &::-webkit-scrollbar-track {
      background: #f3f4f5;
    }

    &::-webkit-scrollbar-thumb {
      border-radius: 4px;
      border: 4px solid #f3f4f5;
      background: rgba(98, 110, 126, 0.2);
    }

    .lock-header-container {
      padding-bottom: 12px;
      border-bottom: 1px solid #eeeeee;
      box-sizing: border-box;

      .lock-header-item {
        display: flex;
        align-items: center;
        line-height: 24px;
        font-size: 14px;
        color: #232323;

        & > img {
          width: 14px;
          height: 14px;
          margin-right: 8px;
        }
      }
    }

    ::v-deep .use-header-container {
      padding: 12px 0px;
      box-sizing: border-box;

      .dragClass {
        background: rgba(0, 129, 255, 0.3);
      }

      .use-header-title {
        color: rgba(35, 35, 35, 0.4);
        font-size: 14px;
        font-weight: 400;
      }

      .use-header-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 8px;
        cursor: pointer;

        .el-checkbox__input.is-checked .el-checkbox__inner {
          background-color: #0081ff;
          border-color: #0081ff;
        }

        .el-checkbox__inner:hover {
          border-color: #0081ff;
        }

        .el-checkbox__label {
          color: rgba(35, 35, 35, 0.8);
          font-size: 14px;
          padding-left: 8px;
        }
        & > img {
          width: 14px;
          height: 14px;
        }
      }
    }

    ::v-deep .not-use-header-container {
      border-top: 1px solid #eeeeee;
      padding: 12px 0px;
      box-sizing: border-box;

      .not-use-header-title {
        color: rgba(35, 35, 35, 0.4);
        font-size: 14px;
        font-weight: 400;
      }

      .not-use-header-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 8px;

        .el-checkbox__input.is-checked .el-checkbox__inner {
          background-color: #0081ff;
          border-color: #0081ff;
        }

        .el-checkbox__inner:hover {
          border-color: #0081ff;
        }

        .el-checkbox__label {
          color: rgba(35, 35, 35, 0.8);
          font-size: 14px;
          padding-left: 8px;
        }
      }
    }
  }

  .dsk-table-header-setting-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    height: 56px;
    padding: 0px 12px;
    border-top: 1px solid #eeeeee;
    box-sizing: border-box;

    .el-button {
      height: 32px;
      padding: 0 16px;
      box-sizing: border-box;
    }
  }
}
</style>
