AddQuestion.vue 8.84 KB
Newer Older
纪泽龙's avatar
纪泽龙 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
<template>
  <div ref="myBody" class="add-question flex">
    <div class="text flex">
      <div class="left">
        目前录入题目是第<span>{{ questionNextNum }}</span
        >道题
      </div>
      <div class="right">{{ courseName }}</div>
    </div>

    <el-form class="form flex" ref="form" :model="form" label-width="auto">
      <!-- <div class="top flex"> -->
      <div>
        <el-form-item
          label="题目内容"
          prop="topicTitle"
          :rules="{
            required: true,
            message: '必须输入题目内容',
            trigger: 'blur',
          }"
        >
          <el-input
            type="textarea"
            placeholder="多行输入"
            resize="none"
            rows="4"
            v-model="form.topicTitle"
          >
          </el-input>
        </el-form-item>
      </div>

      <div class="bottom">
        <!-- <el-form-item label="选项1" prop="title">
          <el-input v-model="form.title" placeholder="请输入"></el-input>
        </el-form-item> -->
        <el-form-item
          v-for="(question, index) in form.questions"
          :label="'选项' + (index + 1)"
          :key="question.key"
          :prop="'questions.' + index + '.value'"
          :rules="
            index === 0
              ? {
                  required: true,
                  message: '第一项不能为空不能为空',
                  trigger: 'blur',
                }
              : {}
          "
        >
          <div class="add-select flex">
            <el-input
              type="textarea"
              placeholder="多行输入"
              style="flex: 1; margin-right: 10px"
              rows="2"
              v-model="question.value"
            ></el-input>
            <div class="flex algin-items">
              <div
                @click="rightAnswerClick(index)"
                class="right"
                :class="{ active: answerNum === index }"
              >
                设为正确答案
              </div>
              <el-button
                size="mini"
                type="danger"
                v-if="index > 0"
                @click.prevent="removeDomain(question)"
                >删除</el-button
              >
            </div>
          </div>
        </el-form-item>

        <el-form-item
          class="noAttr"
          :label="`选项${form.questions.length + 1}`"
          prop=""
        >
          <div class="add-select flex">
            <el-input
              type="textarea"
              placeholder="多行输入"
              resize="none"
              rows="2"
              v-model="addValue"
              style="flex: 1; margin-right: 10px"
            >
            </el-input>
            <div class="flex algin-items">
              <div
                @click="rightAnswerClick(form.questions.length)"
                class="right"
                :class="{ active: answerNum === form.questions.length }"
              >
                设为正确答案
              </div>

              <el-button
                size="mini"
                type="primary"
                @click.prevent="add(addValue)"
                >新增</el-button
              >
            </div>
          </div>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
119 120 121 122 123 124 125
// import {
//   addQuestion,
//   checkQuestion,
//   changeQuestion,
//   getQuestion,
//   getLessonById,
// } from "@/api/educationPlanExam/lessonsProgram.js";
126
import { addSubject ,getSubject,listSubject,updateSubject} from "@/api/educationPlanExam/subject";
纪泽龙's avatar
纪泽龙 committed
127 128 129 130 131 132 133 134

export default {
  name: "AnswerLesson",
  props: {
    // visible: {
    //   type: Boolean,
    //   default: false,
    // },
135
    bankId: {
纪泽龙's avatar
纪泽龙 committed
136 137
      type: Number,
    },
138
    subjectId: {
纪泽龙's avatar
纪泽龙 committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      type: Number,
    },
  },
  components: {},
  data() {
    return {
      form: {
        topicTitle: "",
        questions: [{ value: "" }, { value: "" }, { value: "" }],
      },
      answerNum: null,
      addValue: "",
      // 录入的是第几道题
      questionNextNum: 1,
      courseName: "",
    };
  },

  created() {
    // 如果存在就是修改
159 160
    if (this.subjectId) {
      getSubject(this.subjectId).then((res) => {
161
        console.log('?',res.data);
纪泽龙's avatar
纪泽龙 committed
162 163 164 165 166 167 168 169 170 171 172 173
        const data = res.data;
        this.form = {
          topicTitle: data.topicTitle,
          questions: JSON.parse(data.topicOption),
        };
        this.answerNum = data.answer;
      });
    }

    // 查询是第几道题
    this.getQuestion();
    // 获取课程标题
174
    // this.getLessonById(this.bankId);
纪泽龙's avatar
纪泽龙 committed
175 176 177
  },
  methods: {
    getQuestion() {
178
      listSubject({ bankId: this.bankId }).then((res) => {
179 180
        console.log(res)
         // 如果是修改 就是原来的值,如果不是,就是总数+1
181
        if (this.subjectId) {
182
          res.rows.forEach((item, index) => {
183
            if (item.subjectId == this.subjectId) {
184 185 186 187 188 189
              this.questionNextNum = index+1;
            }
          });
        } else {
          this.questionNextNum = res.total + 1;
        }
纪泽龙's avatar
纪泽龙 committed
190 191
      });
    },
192 193 194 195 196 197
    // getLessonById(bankId) {
    //   getLessonById(bankId).then((res) => {
    //     console.log(res);
    //     this.courseName = res.data.courseName;
    //   });
    // },
纪泽龙's avatar
纪泽龙 committed
198 199
    addQuestion(data) {
      // 如果是修改,就用修改的方法,如果是新增,就用新增的方法
200 201
      if (this.subjectId) {
        return updateSubject({ subjectId: this.subjectId, ...data });
纪泽龙's avatar
纪泽龙 committed
202
      } else {
203
        return addSubject({ bankId: this.bankId, ...data });
纪泽龙's avatar
纪泽龙 committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
      }
    },
    rightAnswerClick(index) {
      this.answerNum = index;
      console.log(index);
    },
    // 删除选项
    removeDomain(question) {
      const index = this.form.questions.indexOf(question);
      // 如果是正确答案,就让正确答案清空
      if (this.answerNum === index) {
        this.answerNum = null;
      }
      if (index >= 0) {
        this.form.questions.splice(index, 1);
      }
    },
    // 新增选项
    add(addValue) {
      this.form.questions.push({ value: addValue });
      console.log();
    },
    save(num = 2) {
      return new Promise((resove) => {
        if (!this.answerNum && this.answerNum !== 0) {
          this.$message({
            message: "警告,请设置一个正确答案",
            type: "warning",
          });
          return resove(false);
        }
        this.$refs.form.validate((valid) => {
          if (valid) {
            const data = {};
            data.topicTitle = this.form.topicTitle;
            data.topicOption = JSON.stringify(this.form.questions);
            data.answer = this.answerNum;
            this.addQuestion(data).then((res) => {
              if (res.code == 200) {
                // 把修改的这个归位,变成正常添加
244
                this.$emit("update:subjectId", null);
纪泽龙's avatar
纪泽龙 committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
                this.$message({
                  message: "添加题目成功",
                  type: "success",
                });
                this.$parent.$parent.componentsNumChange(num);
                this.$parent.$parent.$parent.getList();
                resove(true);
              }
            });
          }
        });
      });
    },

    saveAndNext() {
      this.save(3).then((res) => {
        if (res) {
          this.reset();
263 264
          // this.questionNextNum++;
          this.getQuestion();
纪泽龙's avatar
纪泽龙 committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
        }
      });
    },
    reset() {
      this.form = {
        topicTitle: "",
        questions: [{ value: "" }, { value: "" }, { value: "" }],
      };
      this.answerNum = null;
      this.addValue = "";
    },
  },
};
</script>
<style lang="scss" scoped>
.add-question {
  width: 100%;
  height: 550px;
  // overflow: hidden;
  flex-direction: column;
  padding-bottom: 7px;
  margin-bottom: 20px;
  border-bottom: 1px solid #bbbbbb;
  .form {
    flex: 1;
    flex-direction: column;
    height: 100%;
    .bottom {
      overflow-y: auto;
      height: 330px;
      box-sizing: border-box;

      .algin-items {
        align-items: center;
        width: 200px;
      }
      .right {
        display: inline-block;
        width: 133px;
        margin-right: 10px;
        line-height: initial;
        padding: 4px 0;
        border: 1px solid #bbbbbb;
        color: #101010;
        font-size: 12px;
        text-align: center;
        border-radius: 4px;
        box-sizing: border-box;
        cursor: pointer;
        &.active {
          background-color: #0bab0c;
          color: #ffffff;
        }
        &:hover {
          background-color: rgba(11, 171, 12, 0.5);
          color: #ffffff;
        }
      }
    }
  }
  .text {
    margin-top: 13px;
    margin-bottom: 34px;
    justify-content: space-between;
    height: 28px;
    .left {
      line-height: 28px;
      color: #101010;
      font-size: 14px;
    }
    .right {
      width: 411px;
      line-height: 28px;
      background: #1d84ff;
      padding-right: 5px;
      color: #fff;
      text-align: right;
    }
  }
}
</style>