<template>
  <div class="courseware-list">
    <div class="toolbar">
      <button
        v-for="item in statusList"
        :key="item.key"
        :class="['filter-btn', { active: curStatus === item.key }]"
        type="button"
        @click="changeStatus(item.key)"
      >
        {{ item.label }}
      </button>

      <div class="search-wrap">
        <el-input
          v-model.trim="searchText"
          class="search-input"
          placeholder="搜素关键词..."
          @keydown.enter.native="getList({ cur_page: 1 })"
        >
          <el-button slot="prepend" icon="el-icon-search" @click="getList({ cur_page: 1 })" />
          <el-button slot="append" icon="el-icon-close" @click="clearSearch" />
        </el-input>
      </div>
    </div>

    <div class="card-grid">
      <slot name="card"></slot>
    </div>

    <div class="pagination">
      <div class="pagination-lite">共 {{ total }} 本教材</div>
      <PaginationPage
        :is-init="false"
        layout="prev, pager, next, jumper"
        :total="total"
        :page-size="9"
        @getList="getList"
      />
    </div>
  </div>
</template>

<script>
import PaginationPage from '@/components/PaginationPage.vue';

export default {
  name: 'CoursewareList',
  components: {
    PaginationPage,
  },
  props: {
    statusList: {
      type: Array,
      required: true,
    },
    currentStatus: {
      type: String,
      default: 'all',
    },
    total: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      curStatus: this.currentStatus,
      searchText: '',
    };
  },
  computed: {
    totalPages() {
      return Math.max(1, Math.ceil(this.total / this.pageSize));
    },
  },
  methods: {
    /**
     * 切换状态
     * @param {string} status - 状态值
     */
    changeStatus(status) {
      this.curStatus = status;
      this.$emit('update:currentStatus', status);
      this.$emit('getList', { cur_page: 1, name: this.searchText });
    },
    /**
     * 获取列表数据
     * @param {Object} data - 额外的查询参数
     */
    getList(data) {
      this.$emit('getList', { ...data, name: this.searchText });
    },
    clearSearch() {
      this.searchText = '';
      this.getList({ cur_page: 1 });
    },
  },
};
</script>

<style lang="scss" scoped>
.courseware-list {
  display: flex;
  flex-direction: column;
  width: 1280px;
  padding: 0 0 12px;
  margin: 0 auto;
  overflow: hidden;
  background: #f7f9fe;

  .toolbar {
    display: flex;
    gap: 10px;
    align-items: center;
    margin: 80px 0 22px;

    .filter-btn {
      padding: 6px 18px;
      font-size: 13px;
      color: #64748b;
      cursor: pointer;
      background: #fff;
      border: 1px solid #e2e8f0;
      border-radius: 20px;
      transition: all 0.25s ease;

      &:hover {
        color: #2d3e8f;
        border-color: #2d3e8f;
      }

      &.active {
        color: #fff;
        background: #2d3e8f;
        border-color: #2d3e8f;
      }
    }

    .search-icon-btn {
      width: 32px;
      height: 32px;
      padding: 0;
      color: #64748b;
      cursor: pointer;
      background: #fff;
      border: 1px solid #e2e8f0;
      border-radius: 50%;
    }

    .search-input {
      width: 210px;
      height: 32px;
      padding: 0 12px;
      color: #1f2937;
      background: #fff;
      border: 1px solid #fcfcfc;
      border-radius: 16px;
      outline: none;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 15%);

      :deep(.el-input__inner) {
        background-color: #fff;
        border: none;
      }

      .el-button {
        background-color: #fff;
      }

      :deep(.el-input-group__prepend),
      :deep(.el-input-group__append) {
        color: #000;
        background-color: #fff;
        border: none;

        .el-button {
          padding: 8px 0;
        }
      }

      :deep(.el-input-group__prepend) {
        padding-right: 0;
      }

      :deep(.el-input-group__append) {
        padding-right: 0;
      }
    }
  }

  .card-grid {
    display: grid;
    flex: 1;
    grid-template-rows: repeat(2, minmax(0, 1fr));
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 20px;
    min-height: 0;
    overflow: hidden;

    .prod-card {
      display: flex;
      height: 191px;
      overflow: hidden;
      cursor: pointer;
      background: #fff;
      border: 1px solid rgba(226, 232, 240, 40%);
      border-radius: 12px;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 3%);
      transition: all 0.25s ease;

      &:hover {
        box-shadow: 0 6px 20px rgba(45, 62, 143, 7%);
        transform: translateY(-2px);
      }

      .card-cover {
        width: 130px;
        min-height: 185px;
        color: #fff;
        background: linear-gradient(180deg, #4a90d9, #2d6cbf);

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }

      .card-right {
        display: flex;
        flex: 1;
        flex-direction: column;
        padding: 16px 18px 14px;
      }

      .card-name {
        margin-bottom: 12px;
        font-size: 15px;
        font-weight: 600;
        color: #1f2937;
      }

      .card-meta {
        display: flex;
        flex: 1;
        flex-direction: column;
        gap: 16px;
      }

      .meta-row {
        display: flex;
        gap: 8px;
        align-items: center;
        font-size: 12px;
        color: #64748b;
      }

      .meta-label {
        min-width: 60px;
      }

      .avatar-group {
        display: flex;
      }

      .mini-avatar {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 22px;
        height: 22px;
        margin-left: -4px;
        font-size: 10px;
        color: #fff;
        background: #2d3e8f;
        border: 2px solid #fff;
        border-radius: 50%;

        &:first-child {
          margin-left: 0;
        }
      }

      .card-time {
        font-size: 11px;
        color: #94a3b8;
      }

      .card-bottom {
        display: flex;
        gap: 10px;
        align-items: center;
        padding-top: 12px;
      }

      .progress-bar {
        flex: 1;
        height: 6px;
        overflow: hidden;
        background: #e5e7eb;
        border-radius: 3px;

        .fill {
          height: 100%;
          background: #4a7dd4;
          border-radius: 3px;
        }
      }

      .progress-text {
        font-size: 11px;
        color: #94a3b8;
      }

      .status-btn {
        padding: 4px 12px;
        font-size: 12px;
        border: none;
        border-radius: 16px;

        &.building {
          color: #f59e0b;
          background: #fef3c7;
        }

        &.reviewing {
          color: #8b5cf6;
          background: #ede9fe;
        }

        &.done {
          color: #3b82f6;
          background: #dbeafe;
        }

        &.published {
          color: #10b981;
          background: #d1fae5;
        }
      }
    }
  }

  .pagination {
    display: flex;
    column-gap: 20px;
    align-items: center;
    justify-content: center;
    margin-top: 36px;

    .pagination-lite {
      font-size: 13px;
      color: #94a3b8;
      text-align: center;
    }
  }
}
</style>
