<template>
  <div class="dymic-table scrollbar">
    <el-table
      :height="height"
      class="no-scrollbar"
      :summary-method="getSummaries"
      style="width: 100%"
      :data="data"
      :show-header="settings.showHeader"
      size="small"
      lazy
      v-loading="loading"
      :row-class-name="setRowClassName"
      :show-summary="!!(settings.summaryCol && settings.summaryCol.length)"
      @selection-change="selectionChange"
      :empty-text="emptyText"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
        v-if="settings.showCheckbox"
        :selectable="selectable"
      ></el-table-column>
      <el-table-column
        label="No."
        type="index"
        align="center"
        v-if="settings.showIndex"
      ></el-table-column>
      <el-table-column
        v-for="(item, index) in cols"
        :key="index"
        :width="item.width"
        :min-width="item.minWidth"
        :label="item.label"
        :prop="item.prop"
        align="center"
      >
        <template v-if="item.children" :label="item.label">
          <el-table-column
            v-for="(item1, index1) in item.children"
            :prop="item1.prop"
            :key="index1"
            :width="item.width"
            :min-width="item.minWidth"
            :label="item1.label"
            align="center"
          >
            <template slot-scope="scope">
              <!-- 插槽 -->
              <slotItem v-if="item1.slot" :row="scope.row" :index="scope.$index" :slots="$scopedSlots[item1.slot]"></slotItem>
              <!-- 过滤方法 -->
              <p v-else-if="item1.format" v-tip
              :data-txt="item1.format(scope.row[item1.prop],scope.row)"
              @click="item1.click?item1.click():null">
                {{
                  item1.format(scope.row[item1.prop],scope.row)||
                  (item1.format(scope.row[item1.prop],scope.row)===0?0:item1.empty?'':'-')
                }}
              </p>
              <!-- 默认显示 -->
              <p v-else-if="item1.fixToolTip" v-tip.same :data-txt="scope.row[item1.prop]" @click="item1.click?item1.click():null">
                {{scope.row[item1.prop]||(scope.row[item1.prop]===0?0:item1.empty?'':'-')}}
              </p>
              <p v-else v-tip :data-txt="scope.row[item1.prop]" @click="item1.click?item1.click():null">
                {{scope.row[item1.prop]||(scope.row[item1.prop]===0?0:item1.empty?'':'-')}}
              </p>
            </template>
            <template v-if="item1.children" :label="item1.label">
              <el-table-column
              v-for="(item2,index2) in item1.children" :prop="item2.prop"
              :key="index2"
              :width="item1.width"
              :min-width="item1.minWidth"
              :label="item2.label"
              align="center">
                <template slot-scope="scope">
                  <p v-if="item2.format" v-tip
                  :data-txt="item2.format(scope.row[item2.prop],scope.row)"
                  @click="item2.click?item2.click():null">
                    {{
                      item2.format(scope.row[item2.prop],scope.row)||
                      (item2.format(scope.row[item2.prop],scope.row)===0?0:item2.empty?'':'-')
                    }}
                  </p>
                </template>
              </el-table-column>
            </template>
          </el-table-column>
        </template>
        <template slot-scope="scope">
          <!-- 插槽 -->
          <slotItem v-if="item.slot" :row="scope.row" :index="scope.$index" :slots="$scopedSlots[item.slot]"></slotItem>
          <!-- 过滤方法 -->
          <p v-else-if="item.format" v-tip
          :data-txt="item.format(scope.row[item.prop],scope.row)"
          @click="item.click?item.click():null">
            {{
              item.format(scope.row[item.prop],scope.row)||
              (item.format(scope.row[item.prop],scope.row)===0?0:item.empty?'':'-')
            }}
          </p>
          <!-- 默认显示 -->
          <p v-else-if="item.fixToolTip" v-tip.same :data-txt="scope.row[item.prop]" @click="item.click?item.click():null">
            {{scope.row[item.prop]||(scope.row[item.prop]===0?0:item.empty?'':'-')}}
          </p>
          <p v-tip v-else :data-txt="scope.row[item.prop]" @click="item.click?item.click():null">
            {{scope.row[item.prop]||(scope.row[item.prop]===0?0:item.empty?'':'-')}}
          </p>
        </template>
      </el-table-column>
    </el-table>
    <div class="foot">
      <!-- 手动全选及操作 -->
      <el-pagination
        class="pagination"
        background
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="currentPage"
        :page-sizes="pageset.pageSizes || [15, 30, 60, 120]"
        :page-size="pageset.pageSize || 15"
        :total="pageset.total || 0"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "dymic-table",
  props: {
    /** 列数据*/
    cols: {
      type: Array,
      default() {
        return [];
      },
    },
    /**table数据*/
    data: {
      type: Array,
      default() {
        return [];
      },
    },
    /**table表格设置 */
    settings: {
      type: Object,
      default() {
        return {
          // 是否显示表头
          showHeader: false,
          // 是否显示序号
          showIndex: false,
          // 是否显示多选框
          showCheckbox: false,
          hideAllCheckbox: false,
          // 是否显示单选框
          showRadio: false,
          // 需要统计列的prop集合，从0开始
          summaryCol: [],
        };
      },
    },
    /**分页设置*/
    pageset: {
      type: Object,
      default() {
        return {
          total: 0,
          pageSize: 10,
          pageNum: 1,
          pageSizes: [15, 30, 60, 120],
        };
      },
    },
    loading: {
      type: Boolean,
      default() {
        return false;
      }
    },
    height: 0,
    /**数据为空的显示 */
    emptyText: {
      type: String,
      default() {
        return "暂无相关结果";
      },
    },
    /**判断多选框是否可以勾选*/
    selectable: {
      type: Function,
      default() {
        return (row, index) => {
          return true;
        };
      },
    },
  },
  watch: {
    radioSelect(n) {
      this.$emit("radio-change", this.data[n]);
    },
    pageset: {
      deep: true,
      handler(n) {
        this.currentPage = n.pageNum;
      },
    },
  },
  components:{
    slotItem:{
      props:['slots','row','index'],
      render(h){
        let vnode = this.slots({
          row:this.row,
          index:this.index
        })
        return h('div',vnode)
      }
    }
  },
  data() {
    return {
      currentPage: 1,
    };
  },
  methods: {
    // 多选派发事件selection-change
    selectionChange(val) {
      this.isSelectAll = val.length == this.data.length;
      this.$emit("selection-change", val);
    },
    currentChange(page) {
      this.currentPage = page;
      this.$emit("page-change", {
        page: page,
      });
    },
    sizeChange(pageSize) {
      this.$emit("page-change", {
        pageSize: pageSize,
      });
    },
    currentChange(page) {
      this.currentPage = page;
      this.$emit("page-change", {
        page: page,
      });
    },
    // 合计
    getSummaries(params) {
      let { data } = params;
      if ("{}" != JSON.stringify(this.summaryData)) {
        data = [this.summaryData];
      }
      let sums = [];
      sums[0] = "合计";
      let cols = JSON.parse(JSON.stringify(this.settings.summaryCol));
      let i = cols.length;
      while (i--) {
        // 计算当前列的prop字段
        let index = cols[i];
        if (this.settings.showIndex) {
          index -= 1;
        }
        if (this.settings.showCheckbox) {
          index -= 1;
        }
        if (this.settings.showRadio) {
          index -= 1;
        }
        let prop = this.cols[index].prop;
        const values = data.map((item) => Number(item[prop]));
        if (!values.every((value) => isNaN(value))) {
          sums[cols[i]] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[cols[i]];
        }
      }

      return sums;
    },
    checkAllChange(val) {
      if (val) {
        this.$refs.table.toggleAllSelection();
      } else {
        this.$refs.table.clearSelection();
      }
    },
    //自定义表头斑马线
    setRowClassName({ row, rowIndex }) {
      let index = rowIndex;
      if (index % 2 == 0) {
        return "warning-row";
      }
    },
  },
  created() {
  },
};
</script>

<style lang="scss" scoped>
/deep/.dymic-table .el-table .el-table__body td p{
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
/deep/.dymic-table .el-table--enable-row-transition .el-table__body td{
  border:none;
}
.dymic-table .el-table {
  border-radius: 6px;
}
/deep/ .el-table{
  .el-table__header-wrapper thead tr th:nth-child(-n+2){
        .cell{
           width: max-content;
        }
        
    }
}
</style>
