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

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

    <div class="menu">
      <div
        v-for="{ name, path } in menuList"
        :key="path"
        :class="['menu-item', { active: path === curPath }]"
        @click="$router.push(path)"
      >
        {{ name }}
      </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,
      menuList: [
        {
          name: '个人题库',
          path: '/personal_question',
        },
        {
          name: '公共题库',
          path: '/home',
        },
        {
          name: '协作题库',
          path: '/collaborate_question',
        },
      ],
    };
  },
  computed: {
    curPath() {
      return this.$route.path;
    },
  },
  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: $font-light-color;
      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: $font-light-color;
    }
  }
}
</style>
