<template>
  <div class="container-md">
    <div class="tip-box">
      选择用于本节直播课程的题库试题，试题可用于直播间内发送
      <el-link
        type="primary"
        style="margin-left: 5px;"
        @click="handleToQuestionBank"
      >配置题库试题 >></el-link>
    </div>

    <el-button type="primary" icon="el-icon-plus" style="margin: 20px 0;" @click="handleAddQuestion">添加试题</el-button>
    <!-- 试题列表表格 -->
    <el-table
      :data="questionLiveList"
      style="width: 100%"
      v-loading="loading"
    >
      <!-- 题干列：显示试题的主要内容 -->
      <el-table-column
        prop="title"
        label="题干"
        show-overflow-tooltip
      ></el-table-column>

      <!-- 题型列：显示是单选还是多选 -->
      <el-table-column
        prop="type"
        label="题型"
      >
        <template slot-scope="scope">
          {{ scope.row.type === 1 ? '单选题' : '多选题' }}
        </template>
      </el-table-column>

      <!-- 创建时间列：显示试题创建的时间 -->
      <el-table-column
        prop="createTime"
        label="创建时间"
      ></el-table-column>

      <!-- 操作列：包含编辑和删除按钮 -->
      <el-table-column
        label="操作"
        width="180"
        fixed="right"
      >
        <template slot-scope="scope">
          <!-- 删除按钮：用于移除试题 -->
          <el-button
            type="text"
            size="small"
            style="color: #F56C6C;"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件：用于分页展示试题列表 -->
    <pagination
      v-show="questionTotal > 0"
      :total="questionTotal"
      :page.sync="questionParams.pageNum"
      :limit.sync="questionParams.pageSize"
      @pagination="getLiveQuestionLiveList"
      style="margin-top: 20px;"
    />

    <!-- 添加试题弹窗 -->
    <el-dialog
      title="添加试题"
      :visible.sync="questionDialogVisible"
      width="800px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="dialog-content">
        <div style="text-align: right; margin-bottom: 20px;">
          <el-input
            v-model="searchTitle"
            placeholder="请输入搜索内容"
            style="width: 300px;"
            @input="handleQuestionSearch"
          ></el-input>
        </div>

        <el-table
          ref="questionTable"
          :data="questionList"
          style="width: 100%"
          v-loading="questionLoading"
          @selection-change="handleSelectionChange"
          @row-click="handleRowClick"
          row-key="id"
        >
          <!-- 复选框列：用于多选试题 -->
          <el-table-column
            type="selection"
            width="55"
          >
          </el-table-column>
          <!-- 题干列：显示试题的主要内容 -->
          <el-table-column
            prop="title"
            label="题干"
            class-name="clickable-column"
          ></el-table-column>
          <!-- 题型列：显示单选或多选 -->
          <el-table-column
            prop="type"
            label="题型"
            class-name="clickable-column"
          >
            <template slot-scope="scope">
              {{ scope.row.type === 1 ? '单选题' : '多选题' }}
            </template>
          </el-table-column>
          <!-- 创建人列 -->
          <el-table-column
            prop="createBy"
            label="创建人"
            class-name="clickable-column"
          ></el-table-column>
          <!-- 创建时间列 -->
          <el-table-column
            prop="createTime"
            label="创建时间"
            width="150"
            class-name="clickable-column"
          ></el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getQuestionList"
          style="margin-top: 20px;"
        />
      </div>
      <div slot="footer" class="dialog-footer">
        <div style="display: flex; justify-content: space-between; align-items: center;">
          <span class="selected-count">当前已选择 <span style="color: #00BFFF; font-style: italic;">{{ selectedQuestions.length }}</span> 题</span>
          <div>
            <el-button @click="questionDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirmAddQuestion">确 定</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {addLiveQuestionLive, listLiveQuestionLive, listLiveQuestionOptionList} from "@/api/live/liveQuestionLive";

export default {
  data() {
    return {
      questionDialogVisible: false,
      questionLoading: false,
      questionTotal: 0,
      selectedQuestions: [],
      questionLiveList: [],
      questionList: [],
      loading: true,
      searchTitle: '',
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        liveId: null
      },
      questionParams: {
        pageNum: 1,
        pageSize: 10,
        liveId: null
      },
    };
  },
  created() {
    this.liveId = this.$route.params.liveId
    this.queryParams.liveId = this.liveId
    this.questionParams.liveId = this.liveId
    this.getLiveQuestionLiveList()
  },
  methods: {
    handleQuestionSearch() {
      this.queryParams.pageNum = 1
      this.queryParams.title = this.searchTitle
      this.getQuestionList()
    },
    getLiveQuestionLiveList() {
      this.loading = true
      listLiveQuestionLive(this.questionParams).then(response => {
        this.questionLiveList = response.rows
        this.questionTotal = response.total
        this.loading = false
      })
    },
    handleToQuestionBank() {
      this.$router.push('/live/liveQuestionBank')
    },
    handleAddQuestion() {
      this.questionDialogVisible = true
      this.getQuestionList()
    },
    getQuestionList() {
      this.questionLoading = true
      listLiveQuestionOptionList(this.queryParams).then(response => {
        this.questionList = response.rows
        this.total = response.total
        this.questionLoading = false
      })
    },
    handleSelectionChange(selection) {
      this.selectedQuestions = selection
    },
    confirmAddQuestion() {
      if (this.selectedQuestions.length === 0) {
        this.$message({
          message: '请选择要添加的试题',
          type: 'warning'
        })
        return
      }
      // 调用添加直播间试题接口
      addLiveQuestionLive({
        liveId: this.liveId,
        questionIds: this.selectedQuestions.map(item => item.id).join(',')
      }).then(response => {
        this.questionDialogVisible = false
        this.getLiveQuestionLiveList()
      })
    },
    handleRowClick(row, column) {
      // 如果点击的是复选框列，不进行处理
      if (column.type === 'selection') {
        return
      }

      // 获取表格实例
      const table = this.$refs.questionTable[0]
      if (!table) {
        return
      }

      // 判断当前行是否已经被选中
      const isSelected = this.selectedQuestions.some(item => item.id === row.id)

      // 切换选中状态
      table.toggleRowSelection(row, !isSelected)
    },
  }
};
</script>
