<template>
  <div class="float-left">
    <div class="float-left mb-15">
      <div class="float-left d-flex">
        <div class="d-flex">
          <el-input
            class="w-150px"
            v-model="pagination.keywords"
            placeholder="关键字"
          ></el-input>
        </div>
        <div class="ml-10">
          <el-button @click="paginationReset"> 清空 </el-button>
          <el-button @click="firstPageLoad" type="primary">筛选</el-button>
        </div>
      </div>
    </div>
    <el-table
      ref="multipleTable"
      :header-cell-style="{ background: '#f1f2f9' }"
      :data="courses"
      @row-click="handleRowClick"
      @selection-change="handleSelectionChange"
      class="float-left mb-15"
      v-loading="loading"
    >
      <el-table-column
        type="selection"
        width="55"
        :selectable="checkSelectable"
      ></el-table-column>
      <el-table-column prop="id" label="课程ID" width="120"> </el-table-column>
      <el-table-column label="课程">
        <template slot-scope="scope">
          <div class="d-flex">
            <div>
              <img :src="scope.row.thumb" width="80" height="60" />
            </div>
            <div class="ml-15">{{ scope.row.title }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="价格" width="120">
        <template slot-scope="scope"> ￥{{ scope.row.charge }} </template>
      </el-table-column>
    </el-table>

    <div class="float-left mt-15 text-center">
      <el-pagination
        @size-change="paginationSizeChange"
        @current-change="paginationPageChange"
        :current-page="pagination.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  props: ["selected"],
  data() {
    return {
      pagination: {
        page: 1,
        size: 10,
        sort: "created_at",
        order: "desc",
        keywords: null,
      },
      spids: {
        ids: [],
      },
      loading: false,
      total: 0,
      courses: [],
    };
  },
  mounted() {
    this.getCourse();
  },
  methods: {
    checkSelectable(row) {
      let id = row.id;
      if (!this.selected) {
        return true;
      }
      return this.selected.indexOf(id) <= -1;
    },
    paginationReset() {
      this.pagination.page = 1;
      this.pagination.keywords = null;
      this.getCourse();
    },
    paginationSizeChange(size) {
      this.pagination.page = 1;
      this.pagination.size = size;
      this.getCourse();
    },
    paginationPageChange(page) {
      this.pagination.page = page;
      this.getCourse();
    },
    firstPageLoad() {
      this.pagination.page = 1;
      this.getCourse();
    },
    handleSelectionChange(val) {
      var newbox = [];
      for (var i = 0; i < val.length; i++) {
        let item = {
          type: "live",
          id: val[i].id,
          title: val[i].title,
          thumb: val[i].thumb,
          charge: val[i].charge,
        };
        newbox.push(item);
      }
      this.spids.ids = newbox;
      this.$emit("change", this.spids.ids);
    },
    handleRowClick(row) {
      let id = row.id;
      if (this.checkSelectable(row)) {
        this.$refs.multipleTable.toggleRowSelection(row);
        // 获取当前选中的数据
        const _selectData = this.$refs.multipleTable.selection;
        this.handleSelectionChange(_selectData);
      }
    },
    getCourse() {
      if (this.loading) {
        return;
      }
      this.loading = true;
      this.$api.Course.Live.Course.List(this.pagination).then((res) => {
        this.loading = false;
        this.courses = res.data.data.data;
        this.total = res.data.data.total;
      });
    },
  },
};
</script>
