<template>
  <div id="tableList">
    <div class="headerNavTool">
      <div class="leftIndexText">
        {{ navText.title }} <strong>{{ navText.index }}</strong>
        {{ navText.ch }}
      </div>
      <div class="rightBtnBox">
        <el-button size="medium" type="primary" @click="addClick"
          >新增</el-button
        >
        <el-button size="medium" type="warning" @click="backFather"
          >未定义</el-button
        >
        <el-popover
          popper-class="slotPopper"
          placement="bottom-end"
          trigger="click"
        >
          <div class="popoverDis">
            <div class="checkboxHeader">
              <el-checkbox
                :indeterminate="isIndeterminate"
                v-model="checkAll"
                @change="handleCheckAllChange"
                >列展示</el-checkbox
              >
              <div class="initbtns" @click="initVue">重置</div>
            </div>

            <div
              style="height: 1px; width: 100%; background-color: #eaeefb"
            ></div>
            <el-checkbox-group
              class="checkboxGroup"
              v-model="checkedCities"
              @change="handleCheckedCitiesChange"
            >
              <div
                class="checkboxchild"
                v-for="(item, index) in cities"
                :key="index"
              >
                <el-checkbox
                  :label="item"
                  @change="checkboxChange(item, $event)"
                  >{{ item }}</el-checkbox
                >
                <div class="icon-right">
                  <i class="el-icon-upload2" @click="upMove(item, index)"></i>
                  <i
                    class="el-icon-download"
                    @click="downMove(item, index)"
                  ></i>
                </div>
              </div>
            </el-checkbox-group>
          </div>
          <el-button style="margin-left: 10px" size="medium" slot="reference"
            >自定义列</el-button
          >
        </el-popover>
      </div>
    </div>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      @select-all="selectAll"
      @select="select"
      row-key="id"
    >
      <el-table-column align="center" type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" align="center">
      </el-table-column>
      <template v-for="(item, index) in tableSet">
        <el-table-column
          v-if="item.hidden"
          :width="item.width"
          :key="index"
          :label="item.label"
          align="center"
        >
          <template slot-scope="scope">
            <div class="imgboxsq" v-if="item.socpe === 'img'">
              <!-- <img :src="scope.row[item.prop]" alt="" /> -->
              <el-image
                class="el_images"
                style="width: 100%; height: 100%"
                :src="scope.row[item.prop][0]"
                :preview-src-list="scope.row[item.prop]"
              >
              </el-image>
            </div>
            <span v-else>{{ scope.row[item.prop] }}</span>
          </template>
        </el-table-column>
      </template>
      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="scope">
          <slot name="btn" :scope="scope"></slot>
          <!-- <el-button type="text" @click="edit(scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  props: ["tableSets", "tableData", "navText"],
  data() {
    return {
      tableSet: this.tableSets,
      allCheckData: [], //表格多选
      checkAll: true,
      checkedCities: [], //自定义列 选中数组
      cities: [], //自定义列 总数组
      isIndeterminate: false,
    };
  },
  created() {
    this.inittableSet = JSON.stringify(this.tableSet);
    this.initTR();
  },
  methods: {
    initTR() {
      this.cities = [];
      this.checkedCities = [];
      this.checkAll = true;
      this.isIndeterminate = false;
      this.tableSet.forEach((item, index) => {
        this.cities.push(item.label);
        if (item.hidden) {
          this.checkedCities.push(item.label);
        }
      });
      if (this.checkedCities.length === 0) {
        this.isIndeterminate = false;
      } else if (this.checkedCities.length === this.cities.length) {
        this.isIndeterminate = false;
        this.checkAll = true;
      } else {
        this.isIndeterminate = true;
        this.checkAll = false;
      }
    },
    handleCheckAllChange(val) {
      this.checkedCities = val ? this.cities : [];
      this.isIndeterminate = false;
      if (val) {
        this.tableSet.forEach((item, index) => {
          item.hidden = true;
        });
      } else {
        this.tableSet.forEach((item, index) => {
          item.hidden = false;
        });
      }
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.cities.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.cities.length;
    },
    checkboxChange(v, e) {
      this.tableSet.forEach((item) => {
        if (item.label === v) {
          item.hidden = e;
        }
      });
    },
    downMove(option, index) {
      if (index !== this.tableSet.length - 1) {
        this.tableSet[index] = this.tableSet.splice(
          index + 1,
          1,
          this.tableSet[index]
        )[0];
        this.cities[index] = this.cities.splice(
          index + 1,
          1,
          this.cities[index]
        )[0];
      } else {
        this.tableSet.unshift(this.tableSet.splice(index, 1)[0]);
        this.cities.unshift(this.cities.splice(index, 1)[0]);
      }
    },
    upMove(option, index) {
      if (index != 0) {
        this.tableSet[index] = this.tableSet.splice(
          index - 1,
          1,
          this.tableSet[index]
        )[0];
        this.cities[index] = this.cities.splice(
          index - 1,
          1,
          this.cities[index]
        )[0];
      } else {
        this.tableSet.push(this.tableSet.shift());
        this.cities.push(this.cities.shift());
      }
    },
    //新增按钮
    addClick() {
      this.$emit("addClick");
    },
    //将选中值传回调用组件
    backFather() {
      this.$emit("emitData", this.allCheckData);
    },
    edit() {},
    selectAll(value) {
      this.allCheckData = value;
    },
    select(value) {
      this.allCheckData = value;
    },
    //重置
    initVue() {
      this.$emit("initTableset");
      this.tableSet = JSON.parse(this.inittableSet);
      this.initTR();
    },
  },
};
</script>
<style lang="less">
.slotPopper {
  padding: 0px !important;
}
</style>
<style lang="less" scoped>
#tableList {
  padding: 0px 16px 16px;
  border-radius: 8px;
  box-shadow: 0px 0px 9px 1px rgba(28, 41, 90, 0.1);
  background: #ffffff;
  .headerNavTool {
    height: 72px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .rightBtnBox {
      display: flex;
      align-items: center;
    }
  }
}
.popoverDis {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  font-size: 14px;
  .checkboxHeader {
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-left: 13px;
    padding-right: 15px;
    width: 100%;
    .initbtns {
      color: #47a6ff;
      cursor: pointer;
    }
  }
  .checkboxGroup {
    display: flex;
    flex-direction: column;
    width: 100%;
    .checkboxchild {
      width: 100%;
      justify-content: space-between;
      height: 40px;
      display: flex;
      align-items: center;
      padding: 0px 15px 0px 13px;
      transition: all 0.4s;
      &:hover {
        background-color: #ecf5ff;
      }
      &:hover .icon-right {
        display: flex;
      }
      .icon-right {
        display: flex;
        align-items: center;
        width: 30px;
        height: 30px;
        color: #666;
        font-size: 14px;
        display: none;
        i {
          cursor: pointer;
          margin: 0px 3px;
          &:hover {
            color: #47a6ff;
          }
        }
      }
    }
  }
}
/deep/.el-checkbox__input.is-checked + .el-checkbox__label {
  color: #666;
}
.imgboxsq {
  max-height: 100px;
}
</style>

