<template>
  <a-card :bordered="false" title="必选题设置">
    <template slot="extra">
      <a-button size="small" @click="goback">返回</a-button>
      <a-button size="small" @click="save">确定</a-button>
    </template>

    <div>
      <s-table ref="table" :columns="columns" :data="loadData" :rowKey="(record) => record.id" :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }">
      </s-table>
    </div>
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { requiredList, addOrUpdate, requiredQuestionId } from '@/api/course/question/requiredQuestion'
export default {
  components: {
    STable,
  },
  data() {
    return {
      queryParam: { courseId: this.$route.query.courseId, coursewareId: this.$route.query.coursewareId }, //获取必选题列表请求参数
      saveParam: { selectedRowKeys: this.selectedRowKeys, coursewareId: this.$route.query.coursewareId }, //
      selectedRowKeys: [],
      selectedRows: [],
      form: {
        ids:'',
        coursewareId:''
      },
      loadData: (parameter) => {
        return requiredList(Object.assign(parameter, this.queryParam)).then((res) => {
          return res
        })
      },
      columns: [
        { title: '题目内容', dataIndex: 'questionName', key: 'questionName' },
        { title: '题目类型', dataIndex: 'questionTypeName', key: 'questionTypeName' },
      ],
    }
  },
  created() {
    //获取已选中的必选题
    this.getRequiredQuestionId()
  },
  methods: {
    //返回到课件列表
    goback() {
      this.$router.push({ path: '/course/CoursewareList', query: { id: this.$route.query.courseId } })
    },

    onSelectChange(selectedRowKeys, selectedRows) {
      console.log('selectedRowKeys', selectedRowKeys) //这一行的题目ID
      console.log('selectedRows', selectedRows) //这一行的对象
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },

    //确定
    save() {
      this.form.ids = this.selectedRowKeys.toString();
      this.form.coursewareId = this.$route.query.coursewareId;
      console.log('from:{}', this.form)
      
      addOrUpdate(this.form).then((res) => {
        if (res.code == 200) {
          this.$message.info('保存成功')
          //新增完成后直接返回课程列表
          this.goback();
        }
      })
    },

    //获取必选题题目ID
    getRequiredQuestionId() {
      // 查询所有题目id列表
      requiredQuestionId({ coursewareId: this.$route.query.coursewareId }).then((res) => {
        console.log('+++++++++++++', res)
        this.selectedRowKeys = res.data;
      })
    },
  },
}
</script>