<style>

</style>
<template>
  <q-dialog style="min-width:800px;max-width:1000px;min-height:100px;" v-model="persistent" persistent
    content-class="bg-grey-1" transition-show="scale" transition-hide="scale">
    <q-card style="min-width:800px;max-width:1000px;min-height:100px;">
      <q-card-section>
        <div class="text-h6">{{importType==1?'模板导入(EXCEL)':'智能导入(WORD)'}}
          <el-upload class="upload-demo" action="" style="display:inline-block;margin-left:80px;" :accept="accept"
            :show-file-list="false" :http-request="UploadAttachment">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
          <a style="color:blue;font-size:14px;cursor:pointer;float:right;margin-right:5px;text-decoration:none;"
            :href="downLoadUrl" :download="downLoadName">下载模板文件</a>
        </div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <table>
          <tr>
            <td>
              <template v-for="(item,index) in questionData">
                <div style="margin-bottom:20px;">
                  <span>{{index+1}}. [{{item.QuestionTypeName}}]
                    <template v-if="IsEdit&&ClickIndex==index">
                      <input type="text" v-model="item.Title" />
                    </template>
                    <template v-else>
                      <span v-html="item.Title"></span>
                    </template>
                    &nbsp;<a style="cursor:pointer;color:red;" @click="DeleteQuestion(index)">删除</a>
                  </span><br />
                  <!--选择题-->
                  <template
                    v-if="item.QuestionTypeKey=='single'||item.QuestionTypeKey=='multiple'||item.QuestionTypeKey=='single-number'"
                    v-for="subItem in item.QuestionContentObj">
                    <span>{{subItem.Name}}.{{subItem.Content}} </span><br />
                  </template>
                  <!--判断题-->
                  <template v-if="item.QuestionTypeKey=='judge'" v-for="subItem in item.QuestionContentObj">
                    <span>{{subItem.Name}}.{{subItem.Content}} </span><br />
                  </template>
                  <!--填空题-->
                  <template v-if="item.QuestionTypeKey=='fill-in'" v-for="subItem in item.QuestionContentObj">
                    <span>{{subItem.Content}} </span><br />
                  </template>
                  <!--阅读理解-->
                  <template v-if="item.QuestionTypeKey=='reading-comprehensio'">
                    <template v-for="(subItem,subIndex) in item.QuestionContentObj">
                      <br />
                      <span>
                        <template>
                          <span>第{{subIndex+1}}小题 <label v-html="subItem.SubTitle"></label></span><br />
                        </template>
                        <template
                          v-if=" subItem.QuestionKey=='single'||subItem.QuestionKey=='multiple'|| subItem.QuestionKey=='single-number'"
                          v-for="childItem in subItem.SubAnwser">
                          <template v-if="childItem.Content.indexOf('<img')>0">
                            <span>{{childItem.Name}}.<b v-html="childItem.Content"></b></span>
                          </template>
                          <template v-else>
                            <span> {{childItem.Name}}. {{childItem.Content}}</span>
                          </template>
                          <br />
                        </template>
                      </span>
                      <template
                        v-if="subItem.QuestionKey=='single'||subItem.QuestionKey=='multiple'|| subItem.QuestionKey=='single-number'">
                        <template v-for="childItem in subItem.SubAnwser">
                          <span v-if="childItem.IsAnswer">答案：{{childItem.Name}} </span>
                          <br v-if="childItem.IsAnswer" />
                        </template>
                      </template>
                    </template>
                  </template>
                  <template v-if="item.QuestionTypeKey!='reading-comprehensio'">
                    <span>答案：{{item.Answer}} </span><br />
                  </template>
                  <template v-if="item.AnswerParse">
                    <span>答案解析：{{item.AnswerParse}} </span><br />
                  </template>
                  <template v-if="item.DifficultyTypeStr">
                    <span v-if="item.DifficultyTypeStr">难易程度：{{item.DifficultyTypeStr}} </span><br />
                  </template>
                </div>
              </template>
            </td>
          </tr>
        </table>
      </q-card-section>
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeCourseForm" />
        <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" :loading="saveCourseLoading"
          @click="setQuestion" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    saveQuestionBeatch,
  } from '../../api/question/question'

  import {
    UploadLocalSystem
  } from '../../api/common/common'
  import UeEditor from '../editor/UeEditor'
  //知识点列表
  import questionpoint from './questionpoint'

  import single from '../questiontype/single'
  import multiple from '../questiontype/multiple'
  import fillIn from '../questiontype/fill-in'
  import judge from '../questiontype/judge'
  import shortAnswer from '../questiontype/short-answer'
  import matching from '../questiontype/matching'
  import sortingProblem from '../questiontype/sorting-problem'
  import cloze from '../questiontype/cloze'
  import readingComprehensio from '../questiontype/reading-comprehensio'
  import sharingChoose from '../questiontype/sharing-choose'
  export default {
    components: {
      UeEditor,
      questionpoint, //知识点
      single, //单选题
      multiple, //多选题
      fillIn, //填空题
      judge, //判断题
      shortAnswer, //简答题
      matching, //连线题
      sortingProblem, //排序题
      cloze, //完型填空
      readingComprehensio, //阅读理解
      sharingChoose, //共用选择题
    },
    props: {
      importType: {
        type: Number,
        default: 0 //1-模板导入(Excel)，2-智能导入(Word)
      },
      CourseId: {
        type: String, //课程编号
        default: "0",
      },
      BankId: {
        type: String, //题库编号
        default: "0",
      }
    },
    data() {
      return {
        config: {
          initialFrameWidth: null,
          initialFrameHeight: 80,
        },
        persistent: true,
        saveCourseLoading: false,
        IsEdit: false,
        ClickIndex: 0,
        questionData: [], //导入问题列表
        chooseItem: {}, //当前选择的项
        ExcelUrl: "http://imgfile.oytour.com/static/EduExcelTemplate.xls",
        WordUrl: "http://imgfile.oytour.com/static/EduWordTemplate.doc",
      }
    },
    computed: {
      accept: {
        get() {
          if (this.importType == 1) {
            return '.xls,.xlsx';
          }
          if (this.importType == 2) {
            return '.doc,.docx';
          }
          return '*/*';
        },
      },
      downLoadUrl: {
        get() {
          if (this.importType == 1) {
            return this.ExcelUrl;
          }
          if (this.importType == 2) {
            return this.WordUrl;
          }
          return '';
        }
      },
      downLoadName: {
        get() {
          if (this.importType == 1) {
            return "EduExcelTemplate.xls";
          }
          if (this.importType == 2) {
            return "EduWordTemplate.doc";
          }
          return '';
        }
      },
    },
    created() {
      if (!this.CourseId) {
        this.CourseId = 0;
      }
    },
    mounted() {

    },
    methods: {
      //修改问题
      EditQuestion(item, index) {
        this.chooseItem = item;
        this.IsEdit = true;
        this.ClickIndex = index;
      },
      //删除问题
      DeleteQuestion(index) {
        this.$q.dialog({
          title: '提示信息',
          message: '是否要删除此问题？',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          this.questionData.splice(index, 1);
        }).onCancel(() => {

        });
      },
      //上传文件
      UploadAttachment(files) {
        this.questionData = [];
        var fileParams = {
          Analysis: 1,
          CourseId: this.CourseId,
          BankId:this.BankId,
          Uid: this.getLocalStorage().Id
        }
        if (this.importType == 2) {
          fileParams.Word = 1;
        } else {
          fileParams.Excel = 1;
        }
        UploadLocalSystem(JSON.stringify(fileParams), files.file, res => {
          if (res.Code == 1) {
            this.questionData = res.Data;
          }
        })
      },
      //关闭弹窗
      closeCourseForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存问题
      setQuestion() {
        if (this.questionData && this.questionData.length > 0) {
          this.questionData.forEach(item => {
            item.CourseId = this.CourseId;
            item.BankId=this.BankId;
          })
          this.saveCourseLoading = true;
          saveQuestionBeatch(this.questionData).then(res => {
            this.saveCourseLoading = false
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '数据保存成功！',
              position: 'top'
            })
            this.$emit("success")
            this.closeCourseForm()
          })
        } else {
          this.$q.notify({
            type: 'negative',
            position: "top",
            message: `请先上传文件!`
          })
        }
      },
    }
  }

</script>
