<template>
  <el-dialog
    class="topic-select-dialog"
    :visible.sync="modalIsShow"
    title="试题选择"
    top="10vh"
    width="500px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <div class="text-center">
      <el-button
        v-for="(item, index) in topics"
        :key="index"
        :type="curTopic.type === item.type ? 'primary' : ''"
        @click="selectTopic(item)"
        ><i class="el-icon-plus"></i>{{ item.name }}</el-button
      >
    </div>

    <div slot="footer">
      <el-button type="primary" @click="submit">确认</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "TopicSelectDialog",
  props: {
    topics: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    return {
      modalIsShow: false,
      curTopic: {}
    };
  },
  methods: {
    visibleChange() {
      this.curTopic = {};
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    selectTopic(topic) {
      this.curTopic = topic;
    },
    submit() {
      this.$emit("confirm", this.curTopic);
      this.cancel();
    }
  }
};
</script>