<template>
  <div class="common">
    <div class="operation-container">
      <span class="title">练习列表</span>

      <slot name="operation"></slot>
    </div>

    <div class="menu">
      <div :class="['menu-item', isPublic ? 'active' : '']" @click="$router.push('/')">公共题库</div>
      <div :class="['menu-item', isPublic ? '' : 'active']" @click="$router.push('personal_question')">个人题库</div>
    </div>

    <div class="search">
      <slot name="search"></slot>
    </div>

    <el-table :data="data" height="100%">
      <slot></slot>
    </el-table>

    <el-pagination
      background
      :current-page="cur_page"
      :page-sizes="[10, 20, 30, 40, 50]"
      :page-size="page_capacity"
      layout="total, prev, pager, next, sizes, jumper"
      :total="total"
      @prev-click="changePage"
      @next-click="changePage"
      @current-change="changePage"
      @size-change="changePageSize"
    />
  </div>
</template>

<script>
export default {
  name: 'HomeCommon',
  props: {
    pageSize: {
      type: Number,
      default: 10
    },
    total: {
      type: Number,
      default: 0
    },
    data: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      cur_page: 1,
      page_capacity: this.pageSize
    };
  },
  computed: {
    isPublic() {
      return this.$route.path === '/home';
    }
  },
  created() {
    this.getList();
  },
  methods: {
    changePage(number) {
      this.cur_page = number;
      this.getList();
    },
    changePageSize(size) {
      this.page_capacity = size;
      this.getList();
    },
    getList() {
      this.$emit('getList', { cur_page: this.cur_page, page_capacity: this.page_capacity });
    }
  }
};
</script>

<style lang="scss" scoped>
.common {
  display: flex;
  flex-direction: column;
  row-gap: 16px;
  height: calc(100% - 16px);
  padding: 24px;
  margin: 0 24px 16px;
  background: #fff;
  border-radius: 4px;

  .operation-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 32px;

    .title {
      font-size: 20px;
      font-weight: bold;
    }
  }

  .menu {
    display: flex;

    &-item {
      padding: 5px 16px;
      font-size: 14px;
      color: #4e5969;
      cursor: pointer;

      &.active {
        font-weight: bold;
        color: $main-color;
        background-color: $fill-color;
        border-radius: 18px;
      }
    }
  }

  .search {
    display: grid;
    grid-template: 30px 32px / repeat(auto-fill, 210px);
    grid-auto-flow: column;
    column-gap: 24px;

    &-name {
      font-size: 14px;
      color: #4e5969;
    }
  }
}
</style>
