<template>
  <div>
    <el-card class="mb10">
      <mx-condition ref="condition" :query-params="queryParams" :require-fields="requireFields"
                    @query="handleQuery" @invalid="handleInvalidQuery"></mx-condition>
    </el-card>
    <el-container>
      <el-aside width="284px" style="padding-bottom: 104px">
        <div class="aside_header">
          <span>CONTACT</span>
          <span>章节目录</span>
        </div>
        <div class="aside_content">
          <el-tree
            v-show="tabActive == 0"
            :data="treeList"
            :props="defaultProps"
            node-key="$treeNodeId"
            @node-click="clickChapterNode"
            highlight-current
            ref="treeChapter"
            :expand-on-click-node="false"
          >
            <template #default="{node}">
              <span class="width100 text-ellipsis" :title="node.label">{{ node.label }}</span>
            </template>
          </el-tree>
          <el-tree
            v-show="tabActive == 1"
            :data="knowTreeList"
            :props="knowDefaultProps"
            node-key="knowledgeId"
            @node-click="clickKnowNode"
            :expand-on-click-node="false"
            highlight-current
            ref="tree"
          >
            <template #default="{node}">
              <span class="width100 text-ellipsis" :title="node.label">{{ node.label }}</span>
            </template>
          </el-tree>
        </div>
      </el-aside>
      <el-main style="padding: 0; background: #fff">
        <div class="main_tit">
          <div class="tit_left" v-show="tabActive == 0">{{ chapterTitle }}</div>
          <div class="tit_left" v-show="tabActive == 1">{{ konwTitle }}</div>
          <div class="tit_right">
            <div>
              <input type="checkbox"/>
              <span>过滤已做题</span>
            </div>
            <div class="btn" style="cursor: pointer" @click="httpDiagnosticRecords(1)">
              诊断记录
            </div>
          </div>
        </div>
        <!-- 主题内容 -->
        <div class="main_con" v-show="tabActive == 0">
          <template v-if="chapterList.length">
            <div class="con_item" v-for="item in chapterList" :key="item.id">
              <img src="@/assets/images/img_bg_tongbulianxi.png" alt=""/>
              <div class="con_info">
                <div class="tit">
                  {{ item.chapterName }}
                </div>
                <div class="count">共{{ item.num }}题</div>
                <div class="btn" @click="toChapterQue(item)">马上做题</div>
              </div>
            </div>
          </template>
          <template v-else>
            <evaluation-empty class="width100"></evaluation-empty>
          </template>
        </div>
        <!-- 主题内容 -->
        <div class="main_con" v-show="tabActive == 1">
          <template v-if="knowList.length">
            <div class="con_item" v-for="item in knowList" :key="item.knowledgeId">
              <img src="@/assets/images/img_bg_tongbulianxi.png" alt=""/>
              <div class="con_info">
                <p class="tit">{{ item.knowledgeName }}</p>
                <p class="count">共{{ item.num }}题</p>
                <p class="btn" @click="toKnowQue(item)">马上做题</p>
              </div>
            </div>
          </template>
          <template v-else>
            <evaluation-empty class="width100"></evaluation-empty>
          </template>
        </div>
      </el-main>
    </el-container>
    <!-- 诊断的弹窗 -->
    <el-dialog
      title="同步在线诊断记录"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <div class="tables">
        <el-table :data="tableData.rows" stripe style="width: 100%">
          <el-table-column type="index" width="50" label="序号">
          </el-table-column>
          <el-table-column prop="coursename" label="科目"></el-table-column>
          <el-table-column prop="day" label="日期"></el-table-column>
          <el-table-column label="用时">
            <template slot-scope="scope">
              <span>{{ scope.row.seconds + '秒' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="正确率">
            <template slot-scope="scope">
              <span>{{ scope.row.rate + '%' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <span
                style="color: #47c6a2; cursor: pointer"
                @click="toDetails(scope.row)"
              >
                <i class="el-icon-search" style="margin-right: 4px"></i
                >操作</span
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            :page-sizes="[tablePageSize]"
            layout="prev, pager, next"
            :page-count="tableData.pageNum"
            style="backgrond: #47c6a2"
            @current-change="httpDiagnosticRecords"
          >
          </el-pagination>
          <div class="count">
            共{{ tableData.pageNum }}页 {{ tableData.total }}条
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import MxCondition from '@/components/MxCondition/mx-condition'
import {
  diagnosticRecords,
  getQuestionsNumByChapter,
  getQuestionsNumByKnowledge,
  knowledgeTree,
  chapterTree
} from '@/api/webApi/webQue.js'

let flag = 1

export default {
  components: {
    MxCondition
  },
  props: {
    requireFields: {
      type: Array,
      default: []
    },

    queryParams: {
      type: Object,
      default: []
    },
    tabActive: 0
  },
  data() {
    return {
      form: {
        chapterId: 0,
        knowledgeId: 0
      },
      tablePageSize: 5,
      chapterTitle: '',
      konwTitle: '',
      dialogVisible: false,
      tableData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      knowDefaultProps: {
        children: 'children',
        label: 'name'
      },
      treeList: [],
      knowTreeList: [],
      knowList: [],
      chapterList: []
    }
  },
  methods: {
    handleInvalidQuery() {
      // this.getTree()
    },
    handleQuery() {
      this.cleanBeforeQuery()
      if (this.tabActive == 0) {
        this.getTree()
      } else {
        this.getTreeByKnowledge()
      }
    },
    cleanBeforeQuery() {
      this.chapterTitle = ''
      this.konwTitle = ''
      this.knowList = []
      this.chapterList = []
      this.form.chapterId = ''
      this.form.knowledgeId = ''
    },
    httpDiagnosticRecords(pageNum) {
      diagnosticRecords({
        pageNum: pageNum,
        pageSize: this.tablePageSize,
        type: 1
      }).then((res) => {
        this.dialogVisible = true
        this.tableData = res
        this.tableData['pageNum'] = Math.ceil(res.total / this.tablePageSize)
        console.log(res)
      })
    },
    // 获取章节树
    getTree() {
      chapterTree({
        subjectId: this.queryParams.exeSubject,
        editionId: this.queryParams.exeOrder,
        gradeId: this.queryParams.exeGrade,
        chapterId: this.form.chapterId
      }).then((res) => {
        console.log('gettree', res)
        this.treeList = res.data
        this.$nextTick(() => {
          if (res.data.length > 0) {
            this.chapterTitle = res.data[0].name
            this.$refs.treeChapter.setCurrentKey(res.data[0].$treeNodeId)
            this.form.chapterId = res.data[0].id
            this.getQueByChapter()
          }
        })
      })
    },
    // 获取知识点树
    getTreeByKnowledge() {
      knowledgeTree({ pharseId: 3, subjectId: this.queryParams.exeSubject }).then(
        (res) => {
          this.knowTreeList = res.data
          this.$nextTick(() => {
            if (res.data.length > 0) {
              this.konwTitle = res.data[0].name
              this.form.knowledgeId = res.data[0].id
              this.getQueByKnowledge()
            }
          })
        }
      )
    },
    toDetails(item) {
      this.dialogVisible = false
      this.$router.push({
        path: '/question-center/smartExercise/answerDetailas',
        query: {
          batchno: item.batchno,
          info: encodeURIComponent(JSON.stringify(item))
        }
      })
    },
    // 根据章节来获取题目
    getQueByChapter() {
      if (!this.form.chapterId) return
      getQuestionsNumByChapter({
        chapterId: this.form.chapterId,
        subjectId: this.queryParams.exeSubject,
        editionId: this.queryParams.exeOrder,
        gradeId: this.queryParams.exeGrade
      }).then((res) => {
        this.chapterList = res.data
      })
    },
    // 根据知识点来获取题目
    getQueByKnowledge() {
      if (!this.form.knowledgeId) return
      getQuestionsNumByKnowledge({
        knowledgeId: this.form.knowledgeId,
        subjectId: this.form.subjectId
      }).then((res) => {
        this.knowList = res.data
        console.log(res)
      })
    },
    clickChapterNode(item) {
      this.chapterTitle = item.name
      this.form.chapterId = item.id
      this.getQueByChapter()
    },
    clickKnowNode(item) {
      this.form.knowledgeId = item.id
      this.konwTitle = item.name
      this.getQueByKnowledge()
    },
    // 做章节题目
    toChapterQue(item) {
      this.$router.push({
        path: '/question-center/smartExercise/practice',
        query: {
          chapterId: item.chapterId,
          title: item.chapterName,
          subjectId: this.queryParams.exeSubject
        }
      })
    },
    // 做知识点题目
    toKnowQue(item) {
      this.$router.push({
        path: '/question-center/smartExercise/practice',
        query: {
          knowledgeId: item.knowledgeId,
          title: item.knowledgeName,
          subjectId: this.queryParams.exeSubject
        }
      })
    }
  }
}
</script>
<style scoped>


.radio_contianer span {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #232323;
  line-height: 20px;
  height: 20px;
  margin-right: 16px;
}

.radio_contianer .el-radio-button {
  margin-right: 8px;
}

.aside_header {
  background: white;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: 24px 16px 0 16px;
  margin-bottom: 16px;
}

.el-aside {
  background: white;
  padding: 0;
  margin-bottom: 0;
  margin-right: 16px;
}

.aside_header span {
  margin-top: 5px;
  width: 84px;
  height: 22px;
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #343434;
  line-height: 22px;
}

.aside_header span:first-child {
  width: 173px;
  height: 27px;
  background: linear-gradient(180deg, #ffffff 0%, #47c6a2 100%);
  opacity: 0.5;
}

.main_tit {
  display: flex;
  justify-content: space-between;
  padding-top: 52px;
  padding-bottom: 19px;
  border-bottom: 1px solid #eee;
  padding-left: 19px;
  margin-bottom: 23px;
}

.main_tit .tit_right {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.main_tit .tit_right .btn {
  background: #47c6a2;
  border-radius: 16px;
  padding: 6px 19px;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  margin-left: 32px;
  margin-right: 57px;
}

.tit_right input {
  background: #d8d8d8;
  margin: 0;
}

.tit_right span {
  margin-left: 8px;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #47c6a2;
}

.main_con {
  display: flex;
  flex-wrap: wrap;
  padding: 0 21px 0 16px;
}

.main_con .con_item {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  flex: 0 0 33%;
  padding-right: 15px;
  position: relative;
  margin-bottom: 20px;
}

.main_con .con_item img {
  width: 100%;
  height: 100%;
}

.main_con .con_item .con_info {
  position: absolute;
  top: 24px;
  left: 24px;
}

.main_con .con_item .con_info p {
  margin: 0;
}

.main_con .con_item .con_info .tit {
  height: 22px;
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #343434;
  line-height: 22px;
  padding-right: 30px;
}

.main_con .con_item .con_info .count {
  height: 16px;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #717171;
  line-height: 17px;
  margin-top: 40px;
}

.main_con .con_item .con_info .btn {
  cursor: pointer;
  display: inline-block;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  line-height: 17px;
  padding: 4px 12px;
  background: #47c6a2;
  margin-top: 10px;
  text-align: center;
}

.block {
  text-align: center;
  margin-top: 20px;
  position: relative;
  display: flex;
  justify-content: center;
}

.block > .count {
  top: 10px;
  left: 0;
  position: absolute;
}

</style>


