<style>

</style>
<template>
  <q-dialog v-model="persistent" persistent content-class="bg-grey-1" transition-show="scale" transition-hide="scale">
    <q-card style="width: 1400px;max-width:900px;min-height:100px;">
      <q-card-section>
        <div class="text-h6">{{importType==1?'模板导入(EXCEL)':'智能导入(WORD)'}}
          <a style="color:blue;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>
          <thead>
            <tr>
              <td>
                <el-upload class="upload-demo" action="" :accept="accept" :show-file-list="false"
                  :http-request="UploadAttachment">
                  <el-button size="small" type="primary">点击上传</el-button>
                </el-upload>
              </td>
            </tr>
          </thead>
          <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">
                      <div>
                        <template>
                          <span>第{{subIndex+1}}小题 {{subItem.SubTitle}}</span><br />
                        </template>
                        <template v-if="subItem.QuestionKey=='single'||subItem.QuestionKey=='multiple'"
                          v-for="childItem in subItem.SubAnwser">
                          <span>{{childItem.Name}}.{{childItem.Content}} </span><br />
                        </template>
                      </div>
                      <template v-if="subItem.QuestionKey=='single'||subItem.QuestionKey=='multiple'">
                        <template v-for="childItem in subItem.SubAnwser">
                          <span v-if="childItem.IsAnswer">答案：{{childItem.Name}} </span><br />
                        </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 entryProblem from '../questiontype/entry-problem'
  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, //简答题
      entryProblem, //分录题
      matching, //连线题
      sortingProblem, //排序题
      cloze, //完型填空
      readingComprehensio, //阅读理解
      sharingChoose, //共用选择题
    },
    props: {
      importType: {
        type: Number,
        default: 0 //1-模板导入(Excel)，2-智能导入(Word)
      },
      CourseId: {
        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() {

    },
    mounted() {

    },
    methods: {
      //修改问题
      EditQuestion(item, index) {
        this.chooseItem = item;
        this.IsEdit = true;
        this.ClickIndex = index;
      },
      //删除问题
      DeleteQuestion(index) {

      },
      //上传文件
      UploadAttachment(files) {
        this.questionData = [];
        var fileParams = {
          Analysis: 1,
          CourseId: this.CourseId,
          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
          })
        }
        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()
        })
      }
    },
  }

</script>
