<template>
  <div class="contianer">
    <!-- 头部标题 -->
    <van-nav-bar left-arrow @click-left="onClickLeft">
      <div slot="title" class="navtitle" @click="popup(true)">
        {{classes}}
        <img src="../../assets/user/xiabiao.png" />
      </div>
    </van-nav-bar>
    <!-- 顶部导航 -->
    <div class="top-nav">
      <van-tabs
        v-model="activeNav"
        :line-width="20"
        title-active-color=" #FF795C"
        title-inactive-color="#000"
        sticky
      >
        <van-tab title="章节练习" class="textWitdh" v-if="chaper">
          <!-- 章节分类列表 -->
          <div class="exam-box" v-if="showList">
            <!-- 章 -->
            <div class="chaptersBox">
              <div
                class="chaptersList"
                v-for="(item,i) in allData"
                :key="i"
                :class="{activeChapter:activeChapter==i}"
                @click="getSectionsList(i)"
              >
                <!-- <p>{{item.sectionid}}</p> -->
                <p>第{{i+1}}章</p>
                <span>{{item.name}}</span>
              </div>
            </div>
            <!-- 节 -->
            <div class="sectionsBox">
              <!-- 节 -->
              <div class="sectionsList" v-for="(child,n) in childData" :key="n">
                <div
                  class="sectionsList-title"
                  @click="openSort(child.sectionid,categoryid)"
                >{{child.name}}</div>
                <div v-for="(subChild,m) in child.childs" :key="m">
                  <div class="sectionsList-text" @click="openSort(subChild.sectionid,categoryid)">
                    <div>{{subChild.name}}</div>
                    <div>
                      <span>{{subChild.do_num}}</span>
                      /{{subChild.tk_num}}
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!-- 无数据提示 -->
          <Nodata v-if="showNodata" />
        </van-tab>
        <van-tab title="知识点" v-if="knowledge">
          <!-- 无数据提示 -->
          <div class="examRecord-item" v-if="knowledgeDate && knowledgeDate.length">
            <div class="examRecord-box" v-for="(item,i) in knowledgeDate" :key="i">
              <div class="examRecord-box_top">
                <div class="examRecord-box_title">
                  <ul>
                    <li>{{item.title}}</li>
                  </ul>
                </div>
                <!-- <span>{{item.do_num}}/{{item.tk_num}}</span> -->
              </div>
              <div class="examRecord-box_bottom">
                <div class="examRecord-box_type">
                  <div>{{item.do_num}}/{{item.tk_num}}</div>
                </div>
                <div class="examRecord-box_see" @click="openSort2(item.knowledgeid,categoryid)">开始做题</div>
              </div>
            </div>
          </div>
          <Nodata v-else />
        </van-tab>
      </van-tabs>
    </div>

    <!-- 选择考试顺序 -->
    <sort
      v-if="showSort"
      @closeSort="closeSort"
      :type="4"
      name="Exercise"
      :sectionid="sectionid"
      :categoryid="categoryid"
    />
    <sort
      v-if="showSort2"
      @closeSort="closeSort2"
      :type="5"
      name="Exercise"
      :knowledgeid="knowledgeid"
      :categoryid="categoryid"
    />
  </div>
</template>

<script>
import {
  GetPaperSection,
  GetAllPaperCategory,
  GetPaperKnowledge,
  GetSubjectStatistics
} from "@/api/paper";
export default {
  data() {
    return {
      showList: true,
      showNodata: false,
      // showNodata2: false,
      showSort: false,
      showSort2: false,
      onshow: false,
      allClass: [],
      active: null,
      classes: "未选择", //课程名称
      activeNav: 0, //当前选中导航
      activeChapter: 0, //当前选中章
      childData: [],
      allData: [],
      knowledgeDate: [],
      sectionid: 0,
      knowledgeid: 0,
      categoryid: this.$route.query.categoryid || 0,
      chaper: false,
      knowledge: false
    };
  },
  methods: {
    openSort(sectionid, categoryid) {
      this.showSort = true;
      this.sectionid = sectionid;
      this.categoryid = categoryid;
    },
    openSort2(knowledgeid, categoryid) {
      this.showSort2 = true;
      this.knowledgeid = knowledgeid;
      this.categoryid = categoryid;
    },
    closeSort() {
      this.showSort = false;
    },
    closeSort2() {
      this.showSort2 = false;
    },
    //获取节列表
    getSectionsList(index) {
      if (this.allData && this.allData.length) {
        this.childData = this.allData[index].childs;
        this.activeChapter = index;
      }
    },
    //返回上一页
    onClickLeft() {
      this.$router.go(-1);
    },
    
    popup() {
      this.onshow = true;
    },
    newClass(classes) {
      this.classes = classes;
    },

    getKnowledgeDate() {
      this.onshow = false;
      GetPaperKnowledge({
        categoryid: this.$route.query.categoryid
      }).then(res => {
        if (res.result == true) {
          this.knowledgeDate = res.data;
        } else {
          this.knowledgeDate = [];
        }
      });
    },
    getPaperSection() {
      this.onshow = false;
      GetPaperSection({
        sectionid:0,
        categoryid: this.$route.query.categoryid
      }).then(res => {
        if (res.result == true) {
          this.showList = true;
          this.showNodata = false;
          this.allData = res.data;
          if (this.allData && this.allData.length) {
            this.childData = this.allData[0].childs;
            this.activeChapter = 0;
          }
        } else {
          this.showList = false;
          this.showNodata = true;
        }
      });
    },
    /*获取模块数据 **/
    getSubjectStatistics() {
      GetSubjectStatistics({
        categoryid: this.$route.query.categoryid
      }).then(res => {
        for (var i in res.module) {
          if (res.module[i].module_id == "0") {
            this.chaper = res.module[i].isopen_chaper;
            this.knowledge = res.module[i].isopen_knowledge;
            if (!this.knowledge && !this.chaper) {
              this.$notify({ type: "danger", message: "未开启章节知识点" });
              this.$router.push("/user/myexam");
            }
            if (this.knowledge) {
              this.getKnowledgeDate();
            }
            if (this.chaper) {
              this.getSectionsList(0);
              this.getPaperSection();
            }
          }
        }
      });
    }
  },
  activated() {
    document.title = "练习中心";
    this.getSubjectStatistics();

    // 获取用户课程记录
    if (localStorage.classes) {
      this.classes = localStorage.classes;
    } else {
      this.showPopup = true;
    }
    this.active = localStorage.id; //显示当前样式
  }
};
</script>

<style scoped>
.van-tab,
.van-tag--large {
  font-size: 16px;
}
.van-icon-arrow-left {
  color: #4d4c4c !important;
}
.contianer {
  width: 100%;
  min-height: 100vh;
  /* background: #f5f4f9; */
}
/* 顶部导航 */
.top-nav {
  font-weight: bold;
}
.navtitle img {
  width: 10px;
  height: 10px;
  margin-left: 5px;
}
/* 章节分类列表 */
.exam-box {
  width: 100%;
  min-height: 89vh;
}
/* 章 */
.chaptersBox {
  width: 33%;
  min-height: 89vh;
  float: left;
}
.chaptersList {
  width: 100%;
  height: 70px;
  text-align: center;
  border-bottom: 1px solid #ebedef;
}
.chaptersList p {
  color: #333;
  font-size: 11px;
  font-family: PingFang-SC-Bold;
  font-weight: bold;
  padding-top: 12px;
}
.chaptersList span {
  display: inline-block;
  margin-top: 10px;
  color: #999;
  /* font-size: 10px;
  font-family: PingFang-SC-Regular; */
  font-weight: 400;
}
.activeChapter {
  background: #fff;
}
/* 节 */
.sectionsBox {
  width: 67%;
  min-height: 89vh;
  background: #fff;
  float: right;
}
.sectionsList {
  width: 90%;
  padding: 0 5%;
  float: right;
  background: #fff;
}
.sectionsList-title {
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ebedef;
  /* color: #333; */
  font-size: 14px;
  /* font-family: PingFang-SC-Medium; */
  font-weight: 500;
}
.sectionsList-text {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ebedef;
  color: #999;
  font-size: 11px;
  font-family: PingFangSC-Regular;
  font-weight: 400;
  display: flex;
  justify-content: space-between;
}
.sectionsList-text span {
  color: #ff795c;
}
.examRecord-item {
  width: 90%;
  margin: 0 auto;
  padding: 13px 5% 0 5%;
  background: #fff;
}
.examRecord-box {
  margin-bottom: 13px;
  border-bottom: 1px solid #ebedef;
}
.examRecord-box_top {
  display: flex;
  justify-content: space-between;
  margin-top: 3px;
}
.examRecord-box_top span {
  font-size: 14px;
  color: #ff795c;
}
.examRecord-box_title {
  width: 75%;
  font-size: 14px;
  color: #333;
  float: left;
}
.examRecord-box_title ul li {
  list-style: disc inside none;
}
.examRecord-box_top span {
  width: 25%;
  float: right;
  text-align: right;
}
.examRecord-box_bottom {
  margin-top: 6px;
  width: 100%;
  height: 32px;
  display: flex;
  justify-content: space-between;
}
.examRecord-box_type {
  width: 75%;
  font-size: 10px;
  font-family: PingFangSC-Light;
  font-weight: 300;
  color: #999;
  display: flex;
  justify-content: space-between;
}
.examRecord-box_see {
  font-size: 12px;
  color: #fff;
  width: 65px;
  height: 20px;
  border-radius: 10px;
  background: #ff795c;
  text-align: center;
  line-height: 20px;
}
</style>
