<template>
  <div class="apass_list-container">
    <div class="apass_breadcrumb">
      <slot name="breadcrumb"></slot>
    </div>

    <div class="main-container top_container" v-if="$slots.top">
      <slot name="top"></slot>
    </div>

    <div class="main-container">
      <div class="header-container" v-if="!hideHeader">
        <div class="header-left apaas_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>

          <div
            v-for="(item, index) in selectFilter"
            :key="'select_' + index"
            class="select_container"
          >
            <span class="select_title" v-text="item.name"></span>
            <el-select
              v-model="item.value"
              placeholder="请选择"
              @change="listAction"
            >
              <el-option
                v-for="(v, i) in item.options"
                :key="'select_' + index + '_option_' + i"
                :label="v.name"
                :value="v.value"
              >
              </el-option>
            </el-select>
          </div>

          <slot name="header-left"></slot>
        </div>
        <div class="header-center">
          <slot name="header-center"></slot>
        </div>
        <div class="header-right" v-if="!hideSearch">
          <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="main_top_container" v-if="$slots['main-top']">
        <slot name="main-top"></slot>
      </div>

      <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="listPaddingLeft"
        ></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/table-filter";

export default {
  components: {
    ListFilter,
    listTable,
    ListPagination,
  },
  props: {
    listFilter: {
      type: Array,
      default: () => [],
    },
    otherFilter: {
      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,
    },
    hideSearch: {
      type: Boolean,
      default: () => false,
    },
    searchPlaceholder: {
      type: String,
      default: () => "请输入关键字",
    },
  },
  data: () => ({
    showListFilter: false,
    filter: {},
    searchValue: "",
    pageSize: 10,
    currentPage: 1,
    timer: null,
    pageSizes: [10, 20, 50],
    selectFilter: [],
  }),
  watch: {
    otherFilter: {
      handler() {
        this.initOtherFilter();
      },
      deep: true,
    },
  },
  methods: {
    initOtherFilter() {
      if (this.otherFilter && this.otherFilter.length > 0) {
        this.otherFilter.forEach((item) => {
          this.$set(this.filter, item.prop, item.default);

          this.selectFilter.push({
            name: item.name,
            prop: item.prop,
            value: item.default,
            options: item.options,
          });
        });
      }
    },
    searchAction(value) {
      this.currentPage = 1;
      this.listAction();
    },
    filterChange(filter) {
      this.currentPage = 1;
      this.filter = filter;
      this.listAction();
    },
    changePageSize(value) {
      this.pageSize = value;
      this.currentPage = 1;
      this.listAction();
    },
    changeCurrentPage(value) {
      this.currentPage = value;
      this.listAction();
    },
    resetCurrentPage(value = 1) {
      this.currentPage = value;
      this.listAction();
    },
    listAction() {
      if (this.timer) {
        clearTimeout(this.timer);
      }

      this.timer = setTimeout(() => {
        let selectFilter = {};

        this.selectFilter.forEach((item) => {
          selectFilter[item.prop] = item.value;
        });

        this.$emit("list-action", {
          ...this.filter,
          ...selectFilter,
          keyword: this.searchValue,
          size: this.pageSize,
          page: this.currentPage,
        });
      }, 200);
    },
  },
  mounted() {
    this.initOtherFilter();
    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 .select_container {
  display: inline-block;
  vertical-align: middle;
}
.header-container .select_container .select_title {
  font-size: 14px;
  font-weight: 700;
  color: #242c43;
  margin-right: 10px;
}
.header-container .el-button + .el-button,
.header-container .select_container + .select_container {
  margin-left: 25px;
}
.apass_filter {
  margin-top: 10px;
}
.cross_line {
  border-top: 1px solid #e3e5ef;
  margin: 20px -20px 0;
}
.list-container {
  flex-grow: 1;
}
.top_container {
  flex-grow: 0;
  margin-bottom: 20px;
}
</style>
