<template>
  <div>
    <el-card>
      <mx-condition :local-data="extraData" :query-params="queryParams" :require-fields="requireFields"
                    @query="handleQuery" use-alias-mapping></mx-condition>
    </el-card>
    <el-container>
      <left-tree-side>
        <span slot="title">{{ !isKnowledgeBranch ? '章节目录' : '知识点目录' }}</span>
        <el-tree ref="branchTree" highlight-current :expand-on-click-node="false" :data="branchTree"
                 :props="treeProps" node-key="id" @node-click="handleTreeNodeClick"></el-tree>
      </left-tree-side>
      <!-- 手动组卷 -->
      <el-main style="background: #fff">
        <!-- 头部 -->
        <div class="main_header">
          <div class="left">
            <div class="main_tit">{{ title }}</div>
            <!-- 暂不支持 -->
            <div class="filter" style="display: flex; align-items: center" v-if="false">
              <input type="checkbox"/><span>过滤已做题</span>
            </div>
          </div>
          <div class="search_box">
            <input v-model="searchTerm" placeholder="请输入内容"/>
            <img src="@/assets/images/icon_search2.png" alt="" @click="resetQuestionQuery"/>
          </div>
        </div>
        <!-- 主题内容 -->
        <div class="main_con">
          <!-- 题目 -->
          <div class="paper_questions" :id="mathId">
            <div class="que_item" v-for="(item, index) in queList" :key="item.id">
              <div class="que_content">
                <div class="que-content-title">
                  <div class="index">{{ (pageSize * (pageNum - 1) + index + 1) }}.</div>
                  <div v-html="item.title"></div>
                </div>
                <div class="que-option">
                  <div v-if="item.optionA">
                    <span>A</span>
                    <span v-html="item.optionA"></span>
                  </div>
                  <div v-if="item.optionB">
                    <span>B</span>
                    <span v-html="item.optionB"></span>
                  </div>
                  <div v-if="item.optionC">
                    <span>C</span>
                    <span v-html="item.optionC"></span>
                  </div>
                  <div v-if="item.optionD">
                    <span>D</span>
                    <span v-html="item.optionD"></span>
                  </div>
                </div>
              </div>
              <div class="que_footer pd20 fx-row ai-center jc-between">
                <div class="spans">
                  <span class="id">ID: {{ item.id }}</span>
                  <span>题型： {{ item.qtpye }}</span>
                  <span>难度： 一般</span>
                </div>
                <div class="operation">
                  <div class="shoucan">
                    <div v-show="item.collect" @click="toCancelCollectQue(item)"
                         style="display: flex; align-items: center">
                      <img src="@/assets/images/icon_shoucang_s.png" alt="" style="margin-right: 8px"/>
                      <span>已收藏</span>
                    </div>
                    <div v-show="!item.collect" @click="toCollectQue(item)" style="display: flex; align-items: center">
                      <img src="@/assets/images/icon_shoucang_n.png" style="margin-right: 8px" alt=""/>
                      <span>收藏</span>
                    </div>
                  </div>
                  <div class="jiucuo" @click="$refs.correct.open(item.id)">
                    <img src="@/assets/images/icon_jiucuo.png" alt=""/>
                    <span>纠错</span>
                  </div>
                  <div class="detail" @click="viewDetail(item)">
                    <img src="@/assets/images/icon_chakan.png" alt=""/>
                    <span>查看详情>></span>
                  </div>
                  <div class="addQue">
                    <el-button @click="addQueCard(item)" icon="el-icon-shopping-cart-2" type="success" plain
                               :disabled="queCardForm.queList.some((q) => q.id == item.id)">加入选题
                    </el-button>
                  </div>
                </div>
              </div>
              <div class="pd20" v-show="item.expand" v-html="'【解答】'+item.answer2"></div>
              <div class="pd20" v-show="item.expand" v-html="'【解析】'+item.parse"></div>
            </div>
            <div v-if="queList.length == 0" class="empty-text">
              暂时没有内容
            </div>
          </div>
        </div>
        <pagination v-if="total > 0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                    @pagination="queryQuestions"/>
      </el-main>
    </el-container>
    <div v-if="enableBox" class="queBoxer">
      <div class="left">
        <div class="tit">
          <i class="el-icon-shopping-cart-2" style="margin-bottom: 10px"></i>
          <div style="width: 17px; margin-bottom: 10px">试题篮</div>
          <span class="count" style="background: #ff4e00; padding: 2px 5px; margin-bottom: 15px">{{
              queCardForm.queList.length
            }}</span>
        </div>
        <div style="cursor: pointer" @click="show = !show">
          <i :class="show ? 'el-icon-arrow-right' : 'el-icon-arrow-left'"></i>
        </div>
      </div>
      <div class="main">
        <el-collapse-transition>
          <div v-show="show">
            <div style="margin-bottom: 30px">
              <div style="margin-bottom: 10px">
                共（{{ queCardForm.queList.length }}）道题
              </div>
              <div v-for="(value, key) in groupedCardQueList" :key="key + value.length"
                   style="margin-top: 3px; color: #666666">
                <span>{{ key }}</span><span> {{ value.length }} </span>道
              </div>
            </div>
            <div>
              <p style="
                  color: #ff4e00;
                  text-align: right;
                  border-bottom: 1px solid #dedede;
                ">
                <span @click="clearQueCard" style="cursor: pointer"> 清空</span>
              </p>
            </div>
            <div class="btn" @click="createdPaper">生成试卷</div>
          </div>
        </el-collapse-transition>
      </div>
    </div>
    <correct-question-dialog ref="correct"></correct-question-dialog>
  </div>
</template>

<script>
import consts from '@/common/mx-const'
import MxCondition from '@/components/MxCondition/mx-condition'
import {
  addToQuestionCard,
  chapterTree,
  deleteQuestionCard,
  getQuestionCardList,
  knowledgeTree,
  listByChapter,
  listByKnowledge,
  queCancelCollect,
  queCollect
} from '@/api/webApi/webQue'
import CorrectQuestionDialog from '@/components/MxPaper/plus/correct-question-dialog'
import LeftTreeSide from '@/views/questioncenter/components/generate-tabs/plugins/left-tree-side'
import PaperWorkIdentifierMixin from './paper-work-identifier-mixin'
import EventBus from '@/components/EventBus'

export default {
  mixins: [PaperWorkIdentifierMixin],
  name: 'paper-by-hand',
  components: { LeftTreeSide, CorrectQuestionDialog, MxCondition },
  data() {
    return {
      queryParams: null,
      queryOutput: null,
      requireFields: [],
      chapterTree: [],
      knowledgeTree: [],
      treeProps: {
        label: 'name',
        children: 'children'
      },
      treeCache: {},
      treeCacheKey: '',
      currentNode: null,
      title: '',
      searchTerm: '',
      queList: [],
      queCardForm: {
        queList: []
      },
      total: 0,
      pageNum: 1,
      pageSize: 10,
      mathId: 'question_by_hand',
      show: false,
      enableBox: true,
      ignoreQType: false, // 智能组卷会乎略题型
      uploading: false
    }
  },
  computed: {
    isKnowledgeBranch() {
      return this.queryParams?.exeBranch == consts.enum.questionBranches.knowledge.value
    },
    branchTree() {
      return this.isKnowledgeBranch ? this.knowledgeTree : this.chapterTree
    },
    groupedCardQueList() {
      let groupedCard = {}
      this.queCardForm.queList.forEach((que) => {
        if (!groupedCard[que.qtpye]) groupedCard[que.qtpye] = []
        let groupedQueByType = groupedCard[que.qtpye]
        groupedQueByType.push(que)
      })
      return groupedCard
    }
  },
  watch: {
    'queryParams.exeBranch': async function(newVal, oldVal) {
      if (newVal && oldVal) {
        // ignored first mx-condition model assign
        await this.$nextTick()
        this.resetQueryParams(true)
      }
    }
  },
  mounted() {
    this.resetQueryParams()
    this.loadQuestionCard()
    EventBus.instance.$on(consts.keys.keyGenerationCartChanged, () => this.loadQuestionCard())
  },
  beforeDestroy() {
    EventBus.instance.$off(consts.keys.keyGenerationCartChanged)
  },
  methods: {
    loadQuestionCard() {
      getQuestionCardList(this.extraData).then((res) => {
        this.queCardForm.queList = res.data
      })
    },
    resetQueryParams(force = false) {
      if (!this.queryParams || force) {
        const model = {}
        model.exeBranch = this.queryParams?.exeBranch || consts.enum.questionBranches.chapter.value
        switch (model.exeBranch) {
          case consts.enum.questionBranches.chapter.value:
            model.exeSubject = '' // 科目
            model.exeOrder = '' // 版本
            model.exeGrade = '' // 学册
            if (!this.ignoreQType) model.exeQTypeChapter = '' // 题型
            this.requireFields = ['exeBranch', 'exeSubject', 'exeOrder', 'exeGrade']
            if (this.isPaperWork && !this.ignoreQType) this.requireFields.push('exeQTypeChapter')
            break
          case consts.enum.questionBranches.knowledge.value:
            model.exeSubject = '' // 科目
            if (!this.ignoreQType) model.exeQTypeKnowledge = '' // 题型
            this.requireFields = ['exeBranch', 'exeSubject']
            if (this.isPaperWork && !this.ignoreQType) this.requireFields.push('exeQTypeKnowledge')
            break
          default:
            console.log('unexpected type for branch', model.exeBranch)
            return
        }
        this.queryParams = model
      }
    },
    async handleQuery(model) {
      // temporary save & parameter check
      this.queryOutput = model
      delete this.queryOutput.exeBranch // 本页API调用并不需要这个参数
      const isKnowledgeToChapter = !this.isKnowledgeBranch && !this.queryOutput.gradeId
      const isChapterToKnowledge = this.isKnowledgeBranch && !!this.queryOutput.gradeId
      // console.log('中间状态？', isKnowledgeToChapter, isChapterToKnowledge)
      if (isKnowledgeToChapter || isChapterToKnowledge) return // 这是exeBranch条件切换时的中间状态，不要触发后续查询

      this.cleanBeforeQuery()
      // refresh tree by need
      // TODO: 这里最好是由mx-condition解析依赖关系，但现在mx-condition的渲染逻辑不太方便切入
      const treeCacheObj = { ...this.queryOutput }
      delete treeCacheObj.qtpye // 树和题型无关
      const newTreeCacheKey = `${model.exeBranch}_${JSON.stringify(treeCacheObj)}`
      if (newTreeCacheKey != this.treeCacheKey) {
        this.treeCacheKey = newTreeCacheKey
        const treeFunc = this.isKnowledgeBranch ? knowledgeTree : chapterTree
        let treeRes = this.treeCache[this.treeCacheKey]
        if (!treeRes) {
          treeRes = await treeFunc(this.queryOutput)
          this.treeCache[this.treeCacheKey] = treeRes
        }
        this.branchTree.length = 0 // clear
        this.branchTree.push(...treeRes.data)
        // current & query question
        if (!this.branchTree.length) return
        const firstNode = this.branchTree[0]
        this.setCurrentNodeAndTitle(firstNode)
        await this.$nextTick()
        this.$refs.branchTree.setCurrentNode(firstNode)
      }

      // query question
      console.log('call resetQuestionQuery by mx-condition')
      this.resetQuestionQuery()
    },
    cleanBeforeQuery() {
      this.queList = []
      this.currentNode = null
      this.title = ''
    },
    setCurrentNodeAndTitle(nodeData) {
      const branchKey = this.queryParams.exeBranch + 'Id'
      const branchParam = {}
      branchParam[branchKey] = nodeData.id
      this.currentNode = branchParam
      this.title = nodeData.name
    },
    handleTreeNodeClick(data) {
      this.setCurrentNodeAndTitle(data)
      console.log('call resetQuestionQuery by tree node click')
      this.resetQuestionQuery()
    },
    resetQuestionQuery() {
      this.pageNum = 1
      this.queryQuestions()
    },
    async queryQuestions() {
      if (!this.currentNode) return
      const commit = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        ...this.queryOutput,
        ...this.currentNode,
        searchTerm: this.searchTerm
      }
      const queryFunc = this.isKnowledgeBranch ? listByKnowledge : listByChapter
      const res = await queryFunc(commit)
      this.total = res.total
      this.queList = res.rows.map(r => {
        r.expand = false // add reactive field
        return r
      })

      await this.$nextTick()
      this.mxGlobal.MathQueue(this.mathId)
    },
    /* methods of question box */
    addQueCard(item) {
      if (this.uploading) return
      if (this.queCardForm.queList.includes(item.id)) return
      if (this.queCardForm.queList.length == 0) {
        // 为0直接添加
        this.uploading = true
        addToQuestionCard({
          questionId: item.id,
          ...this.extraData
        }).then(() => {
          this.queCardForm.queList.push(item)
        }).finally(() => this.uploading = false)
      } else {
        // 判断是否是同一个科目下的题目
        if (item.subjectid == this.queCardForm.queList[0].subjectid) {
          // 是，则添加
          this.uploading = true
          addToQuestionCard({
            questionId: item.id,
            ...this.extraData
          }).then((res) => {
            this.queCardForm.queList.push(item)
          }).finally(() => this.uploading = false)
        } else {
          // 不是，弹出警告
          this.$message.error('科目不一致，请清空试题篮或选择同一科目')
        }
      }
    },
    clearQueCard() {
      if (this.queCardForm.queList.length > 0) {
        deleteQuestionCard(this.extraData).then((res) => {
          console.log(res)
          this.queCardForm.queList = []
          this.msgSuccess('清空成功')
        })
      }
    },
    toCollectQue(item) {
      queCollect({
        questionId: item.id
      }).then((res) => {
        item.collect = !item.collect
        this.msgSuccess('收藏成功')
        console.log(res)
      })
    },
    toCancelCollectQue(item) {
      queCancelCollect({
        questionId: item.id
      }).then((res) => {
        item.collect = !item.collect
        this.msgSuccess('取消收藏成功')
        console.log(res)
      })
    },
    viewDetail(item) {
      item.expand = !item.expand
    },
    createdPaper() {
      if (this.queCardForm?.queList?.length > 0) {
        localStorage.setItem(
          'paperData',
          JSON.stringify({
            paperTitle: null,
            subjectId: this.queryOutput.subjectId
          })
        )
        localStorage.setItem(
          'questionList',
          JSON.stringify(this.queCardForm.queList)
        )
        this.$router.push({
          path: '/question-center/generatingPaperCenter/paper',
          query: this.extraData
        })
      }
    }
  }
}
</script>

<style scoped>
</style>
