<template>
  <div class="collaborate">
    <HomeCommon ref="common" :data="exercise_list" :total="total_count" @getList="pageQueryExerciseList">
      <template #default>
        <el-table-column prop="index" label="序号" width="70">
          <template slot-scope="{ $index }">{{ $index + 1 }}</template>
        </el-table-column>
        <el-table-column prop="name" label="练习名称" width="280" />
        <el-table-column prop="tag" label="标签" width="180">
          <template slot-scope="{ row }">
            <span v-for="(item, i) in row.label_list" :key="i" class="tag">{{ item }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="creator_name" label="创建者" width="160" />
        <el-table-column prop="create_time" label="创建时间" width="180" />
        <el-table-column prop="last_modifier_name" label="最近编辑" width="140" />
        <el-table-column prop="last_modify_time" label="最近编辑时间" width="180" />
        <el-table-column prop="intro" label="简介" />

        <el-table-column prop="operation" label="操作" fixed="right" width="300">
          <template slot-scope="{ row }">
            <span
              class="link"
              @click="$router.push({ path: '/exercise', query: { id: row.id, back_url: '/collaborate_question' } })"
            >
              编辑
            </span>
            <span class="link danger" @click="deleteMyShareEditExercise(row.id)">删除</span>
          </template>
        </el-table-column>
      </template>

      <template #operation> </template>

      <template #search>
        <span class="search-name">搜索</span>
        <el-input
          v-model="searchData.search_content"
          placeholder="全部"
          suffix-icon="el-icon-search"
          @keyup.enter.native="getPageList"
        />

        <span class="search-name">创建者</span>
        <el-input v-model="searchData.creator_name" placeholder="请输入内容" @keyup.enter.native="getPageList" />
      </template>
    </HomeCommon>
  </div>
</template>

<script>
import { PageQueryExerciseList, DeleteMyShareEditExercise } from '@/api/exercise';
import HomeCommon from '../common.vue';

export default {
  name: 'CollaborateQuestion',
  components: {
    HomeCommon,
  },
  data() {
    return {
      // 搜索数据
      searchData: {
        search_content: '',
        creator_name: '',
      },
      total_count: 0, // 总条数
      exercise_list: [],
    };
  },
  methods: {
    getPageList() {
      this.$refs.common.getList();
    },
    pageQueryExerciseList(data) {
      PageQueryExerciseList({ ...data, store_type: 10, ...this.searchData }).then(({ total_count, exercise_list }) => {
        this.total_count = total_count;
        this.exercise_list = exercise_list;
      });
    },
    deleteMyShareEditExercise(exercise_id) {
      this.$confirm('是否删除当前练习题', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          DeleteMyShareEditExercise({ exercise_id })
            .then(() => {
              this.$message.success('删除成功');
              this.getPageList();
            })
            .catch(() => {
              this.$message.error('删除失败');
            });
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.collaborate {
  height: 100%;
}
</style>
