<template>
  <div class="eContainer-table-block">
    <el-table :data="tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)"
      style="width: 100%"
      class="eContainer-table"
      @selection-change="handleSelectionChange"
      :row-key="getRowKey"
      :header-cell-style="{background: 'eef1f6', color: '#606266'}">
      <el-table-column type="selection" width="55" v-if="showSelection" :reserve-selection="true"></el-table-column>
      <el-table-column
        v-for="(item, key) in tableColumns"
        :key="key"
				:prop="item.prop"
        :label="item.label"
        :width="item.width"
      >
      </el-table-column>
      <slot></slot>
    </el-table>
		<el-pagination
      class="eContainer-pagination"
      layout="prev, pager, next, jumper"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="tableData.length"
			@size-change="sizeChange"
			@current-change="currentChange"
    ></el-pagination>
    <div class="paginationLable">当前显示第 {{(currentPage - 1) * pageSize + 1}}-{{currentPage * pageSize > tableData.length ? tableData.length : currentPage * pageSize}} 条，共 {{tableData.length}} 条</div>
  </div>
</template>

<script>
export default {
  props: {
    columns: {
      type: Array,
      default: () => {
        return [];
      },
    },
    list: {
      type: Array,
      default: () => {
        return [];
      },
    },
    showSelection: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    tableColumns() {
      const arr = [];
      for (let i = 0; i < this.columns.length; i++) {
        const column = this.columns[i];				
				const label = column.split("\"")[1]
				const newStr = column.replace(`\"${label}\"`, "label")
				const vals = newStr.split(" ")
				// "P VERTLINES TRUE"
        if (vals.length <= 3) continue;
        arr.push({
          prop: vals[0],
          label: label,
          // width: vals[3],
          width: "auto",
        });
      }
			return arr;
    },
    tableData() {
      return this.list.map((row) => {
				const res = {}
        const vals = row.split("\t");
				for (let i = 0; i < vals.length; i++) {
					res[`${i}`] = vals[i];
				}
				return res;
      });
    },
  },
  data() {
    return {
			currentPage: 1,
			pageSizes: [5, 10, 20, 30, 40, 50, 100],
			pageSize: 5
		};
  },
  methods: {
		sizeChange(size) {
			this.pageSize = size;
		},
		currentChange(currentPage) {
			this.currentPage = currentPage;
		},
    handleSelectionChange(val) {
      this.$emit("multipleSelect", this.getSelectedRowIndex(val))
    },
    getRowKey(row) {
      return row['1']
    },
    getSelectedRowIndex(val) {
      const indexArr = []
      for (let j = 0; j < val.length; j++) {
        const v = val[j];
        for (let i = 0; i < this.tableData.length; i++) {
          const data = this.tableData[i];
          if (v[1] === data [1]) {
            indexArr.push(i)
          } 
        }
      }
      return indexArr
    }
	}
};
</script>

<style>
.eContainer-table-block .paginationLable{
 font-size: 12px;
 color: #808080;
 height: 26px;
 line-height: 26px;
 float:right;
 margin-top:20px;
}
.eContainer-table-block .el-table__body-wrapper {
  overflow: auto;
}
.el-table .warning-row {
  background: oldlace;
}
.el-table .success-row {
  background: #f0f9eb;
}
</style>