<template>
  <div class="apass_list-container">
    <div class="apass_breadcrumb">
      <slot name="breadcrumb"></slot>
    </div>
    <div class="main-container">
      <div class="header-container" v-if="!hideHeader">
        <div class="header-left apass_button">
          <el-button
            v-if="listFilter.length"
            @click="showListFilter = !showListFilter"
          >
            {{ showListFilter ? "收起" : "筛选" }}
            <i
              class="el-icon--right"
              :class="
                showListFilter ? 'el-icon-caret-bottom' : 'el-icon-caret-top'
              "
            ></i>
          </el-button>
          <slot name="header-left"></slot>
        </div>
        <div class="header-center">
          <slot name="header-center"></slot>
        </div>
        <div class="header-right">
          <el-input
            v-model="searchValue"
            prefix-icon="el-icon-search"
            :placeholder="searchPlaceholder"
            style="width:240px;"
            @input="searchAction"
          ></el-input>
        </div>
      </div>

      <list-filter
        v-if="!hideHeader"
        class="apass_filter"
        :show="showListFilter"
        :filter-list="listFilter"
        @filter-change="filterChange"
      ></list-filter>

      <div class="cross_line" v-if="!hideHeader"></div>

      <div class="list-container" v-if="$slots.list">
        <slot name="list"></slot>
      </div>
      <div class="list-container" v-else>
        <list-table
          :header="listHeader"
          :data="listData"
          :padding-left="35"
        ></list-table>
      </div>

      <list-pagination
        :total="listTotal"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        :current-page="currentPage"
        @size-change="changePageSize"
        @current-change="changeCurrentPage"
        style="margin-top: 20px;"
      ></list-pagination>
    </div>
  </div>
</template>

<script>
import listTable from "@/components/apass-table";
import ListPagination from "@/components/comments-pagination";
import ListFilter from "@/components/table-filter";

export default {
  components: {
    ListFilter,
    listTable,
    ListPagination,
  },
  props: {
    listFilter: {
      type: Array,
      default: () => [],
    },
    listHeader: {
      type: Array,
      default: () => [],
    },
    listData: {
      type: Array,
      default: () => [],
    },
    listPaddingLeft: {
      type: [Number, String],
    },
    listTotal: {
      type: Number,
      default: () => 0,
    },
    hideHeader: {
      type: Boolean,
      default: () => false,
    },
    searchPlaceholder: {
      type: String,
      default: () => "请输入关键字",
    },
  },
  data: () => ({
    showListFilter: false,
    filter: {},
    searchValue: "",
    pageSize: 15,
    currentPage: 1,
    timer: null,
    pageSizes: [15, 50, 100],
  }),
  methods: {
    searchAction(value) {
      this.listAction();
    },
    filterChange(filter) {
      this.filter = filter;
      this.listAction();
    },
    changePageSize(value) {
      this.pageSize = value;
      this.currentPage = 1;
      this.listAction();
    },
    changeCurrentPage(value) {
      this.currentPage = value;
      this.listAction();
    },
    listAction() {
      if (this.timer) {
        clearTimeout(this.timer);
      }

      this.timer = setTimeout(() => {
        this.$emit("list-action", {
          ...this.filter,
          keyword: this.searchValue,
          size: this.pageSize,
          page: this.currentPage,
        });
      }, 200);
    },
  },
  mounted() {
    this.listAction();
  },
};
</script>

<style scoped>
.apass_list-container {
  padding: 0 20px;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;
  overflow: auto;
}
.main-container {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;
  padding: 0 20px 15px;
  background-color: #fff;
  border-radius: 10px;
  margin-bottom: 40px;
}
.header-container {
  padding-top: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.header-container .el-button + .el-button {
  margin-left: 25px;
}
.apass_filter {
  margin-top: 10px;
}
.cross_line {
  border-top: 1px solid #e3e5ef;
  margin: 20px -20px 0;
}
.list-container {
  flex-grow: 1;
}
</style>
