<template>
  <el-table
    :height="height"
    ref="table"
    class="bg-table"
    :data="rows"
    @selection-change="selectAction"
    @select="selectActionRow"
    @select-all="selectActionAll"
    :stripe="stripe"
    :row-class-name="stripe ? tableRowClassName : ''"
    tooltip-effect="light">
    <template v-slot:empty>
      <div class="empty_container">
        <img src="../assets/imgs/img-no-data.png" alt="" />
        <div class="text">暂无数据</div>
      </div>
    </template>
    <el-table-column v-if="paddingLeft > 10" :width="paddingLeft - 10"></el-table-column>
    <el-table-column type="selection" :selectable="selectable" width="38" v-if="select">
      <!-- checkbox -->
    </el-table-column>
    <el-table-column v-if="isIndex" type="index" :label="indexLabel" width="54" align="left">
      <!-- 序号 -->
    </el-table-column>
    <el-table-column
      v-for="(header, index) in headers"
      :width="header.width"
      :min-width="header.minWidth"
      :align="header.align"
      :key="'col_' + index"
      :fixed="header.fixed"
      show-overflow-tooltip>
      <template v-slot:header>
        <template v-if="$slots[`header-${header.prop}`]">
          <slot :name="`header-${header.prop}`" />
        </template>
        <p class="text-clip" v-else>{{ header.label }}</p>
      </template>
      <template v-slot:default="{ row }">
        <slot v-if="$slots[header.prop]" :name="header.prop" :row="row" />

        <template v-else>
          <!-- {{ row[header.prop] | ellipsis(header.max) }} -->
          {{ row[header.prop] }}
        </template>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import { watch, ref } from "vue";
import { selectTableMixin } from "./hook/mixin-select-table";
let { nowSelectData, allSelectData, selectData, initSelectTableData, runPage, dealSelectData } = selectTableMixin();

const props = defineProps({
  height: {
    type: [Number, String],
    default: "auto",
  },
  headers: {
    type: Array,
    require: true,
  },
  rows: {
    type: Array,
    default: () => [],
  },
  select: {
    type: Boolean,
    default: false,
  },
  // selectable: {
  //   type: Function,
  // },
  isIndex: {
    type: Boolean,
    default: false,
  },
  indexLabel: {
    type: String,
    default: "序号",
  },
  stripe: {
    type: Boolean,
    default: false,
  },
  paddingLeft: {
    type: [Number, String],
    default: () => 12,
  },
  canEdit: {
    type: Boolean,
    default: false,
  }, // 多选框是否禁用
  canEditFlag: {
    typr: Boolean,
    default: "",
  }, // 决定多选框是否禁用的字段
});

const table = ref(null);

const emit = defineEmits(["selectAc", "select"]);
watch(
  () => props.rows,
  (n, o) => {
    if (n.length && props.select) {
      runPage();
      initSelectTableData(n).then((selectData) => {
        if (selectData.length) {
          setTimeout(() => {
            toggleRowArrSelection(selectData);
          });
        }
      });
    }
  }
);

const toggleRowSelection = (row, flag = true) => {
  table.value.toggleRowSelection(row, flag);
};
const selectAction = (selection) => {
  emit("selectAc", { allLength: Object.keys(allSelectData).length + nowSelectData.length, selection });
};
const clearSelection = () => {
  table.value.clearSelection();
  emit("select", { allLength: Object.keys(allSelectData).length + nowSelectData.length, selection: [] });
};
const setSelectedRow = (row) => {
  toggleRowSelection(row);
};
const toggleRowArrSelection = (arr, flag = true) => {
  arr.forEach((e) => {
    toggleRowSelection(e, flag);
  });
};
const selectActionRow = (selection, row) => {
  selectData(selection);
  emit("select", { allLength: Object.keys(allSelectData).length + nowSelectData.length, selection });
};
const selectActionAll = (selection) => {
  selectData(selection);
  emit("select", { allLength: Object.keys(allSelectData).length + nowSelectData.length, selection });
};
const clearTable = () => {
  //清除选中数据,在页面状态更新时使用
  allSelectData = {};
  nowSelectData = [];
  clearSelection();
};
const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 == 0) {
    return "white-row";
  } else {
    return "stripe-row";
  }
};
const selectable = (row, index) => {
  if (props.canEdit) {
    if (row[props.canEditFlag] && row[props.canEditFlag] == 1) {
      return false;
    } else {
      return true;
    }
  } else {
    return true;
  }
};
defineExpose({
  clearTable,
  toggleRowSelection,
});
</script>
