<template>
  <div class="container">
    <Header />
    <div class="content">
      <div
        id="content-tree"
        :class="[
          fullTree ? 'content-tree-full' : 'content-tree',
          isPhone ? 'content-tree-phone' : '',
          showMenu ? '' : 'content-tree-unfold'
        ]"
      >
        <template v-if="isPhone">
          <div style="text-align: right;color: black;padding: 10px;">
            <i
              @click="toggleMenu"
              :class="[showMenu ? 'el-icon-s-fold' : 'el-icon-s-unfold']"
            ></i>
          </div>
        </template>
        <TreeView
          ref="treeView"
          :book-id="bookId"
          :change-id="changeId"
          :change-tree-data="changeTreeData"
          :current-tree-i-d="chapterId"
        />
      </div>

      <div
        id="data-screen"
        v-loading="loading"
        :class="[
          'inner',
          fullscreen ? 'inner-full' : '',
          isPhone ? 'inner-phone' : ''
        ]"
      >
        <el-image
          :src="pictureUrl"
          fit="scale-down"
          class="img_url"
          v-if="!chapterId && pictureUrl"
        >
          <div slot="placeholder" class="image-slot">
            <img src="../assets/common/icon-imgloading.png" />
          </div>
        </el-image>
        <!-- 显示答案按钮 -->
        <!-- <a v-if="chapterId" :class="['answerShow',isAnswerShow?'answerShowTrue':'']" @click="handleAnswerShow">显示答案</a> -->
        <!-- <a class="edit-btn" @click="handleEdit">编辑</a> -->
        <!-- <a v-if="chapterId" :class="['answerShow']" @click="saveAnswer"
          >保存答案</a
        > -->
        <div v-if="chapterId" class="title-box">
          <img
            v-if="!treeFlag"
            src="../assets/common/icon-view-back.png"
            @click="treeShow"
          />
          <img
            v-if="!treeFlag"
            src="../assets/common/icon-treelist.png"
            @click="chooseCourseware"
          />
          <!-- <h2 class="title">{{ chapterName }}</h2> -->
          <!-- <el-switch
            v-if="!treeFlag"
            v-model="switchvalue"
            active-color="#FF9900"
            active-text
            inactive-text="生词模式"
          /> -->
        </div>
        <Preview
          ref="preview"
          :context="context"
          :que-index="queIndex"
          :current-tree-i-d="chapterId"
          :father-tree-data="FatherTreeData"
          :change-id="changeId"
          :theme-color="themeColor"
          :is-show-title="true"
          :book-answer-content="bookAnswerContent"
          :task-model="TaskModel"
          :is-show-save="false"
          :isAnswerItemShow="isAnswerItemShow"
          @finishTaskMaterial="finishTaskMaterial"
        />
      </div>
      <a
        v-if="chapterId && treeFlag && !isPhone"
        class="screen-full"
        @click="fullScreen()"
      />
    </div>
  </div>
</template>

<script>
import Header from "@/components/Header.vue";
import TreeView from "@/components/TreeView";
import { getContent, TextbookAPI } from "@/api/ajax";
import Cookies from "js-cookie";
import Preview from "@/components/Adult/Preview.vue";
import { getToken } from "../utils/auth";
export default {
  name: "CourseView",
  components: {
    Header,
    TreeView,
    Preview
  },
  props: [],
  data() {
    return {
      bookId: "",
      chapterId: "",
      chapterName: "",
      fullscreen: false, // 控制全屏
      context: null,
      question: null, // 选择的模板题型
      queIndex: "",
      treeFlag: true, // tree是否显示
      switchvalue: true, // 生词模式
      isAnswerShow: false, // 是否显示答案
      answer: [
        // 假数据
        {
          table_list: [
            [
              {
                data: {
                  Bookanswer: []
                }
              }
            ]
          ]
        }
      ],
      bookAnswerContent: "",
      TaskModel: "", // TEST 考试模式； PRACTICE 练习模式； ANSWER 查看答案模式； 空 预览模式
      category: "",
      FatherTreeData: null,
      themeColor: "",
      loading: false,
      fullTree: false, // 全屏模式下树是否显示
      isAnswerItemShow: false,
      previewType: "previewCheck",
      pictureUrl: "",
      isPhone: false, // 是否是移动端打开

      showMenu: true //是否展开菜单
    };
  },
  created() {
    const _this = this;
    const regExp = /Android|webOS|iPhone|BlackBerry|IEMobile|Opera Mini/i;
    this.isPhone = regExp.test(navigator.userAgent);
    _this.bookId = this.$route.query.bookId;
    // if (localStorage.getItem("Bookanswer")) {
    //   this.TaskModel = "ANSWER";
    // }
    let userInfor = JSON.parse(getToken());
    if (userInfor && userInfor.user_type == "TEACHER") {
      _this.isAnswerItemShow = true;
    } else {
      _this.isAnswerItemShow = false;
    }
  },
  mounted() {},
  methods: {
    toggleMenu() {
      this.showMenu = !this.showMenu;
    },
    getdetail() {
      let MethodName = "book-book_manager-GetBookChapterStruct";
      let data = {
        book_id: this.bookId
      };
      getContent(MethodName, data).then(res => {
        let nodes = res.nodes;
        for (let i = 0; i < nodes.length; i++) {
          if (nodes[i].is_courseware == "true") {
            this.changeId(nodes[i].id, "");
            return false;
          } else {
            if (nodes[i].children) {
              let nodesC = nodes[i].children;
              for (let j = 0; j < nodesC.length; j++) {
                if (nodesC[j].is_courseware == "true") {
                  this.changeId(nodesC[j].id, "");
                  return false;
                } else {
                  if (nodesC[j].children) {
                    let nodesCs = nodesC[j].children;
                    for (let l = 0; j < nodesCs.length; l++) {
                      if (nodesCs[l].is_courseware == "true") {
                        this.changeId(nodesCs[l].id, "");
                        return false;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      });
    },
    changeTreeData(val) {
      this.FatherTreeData = JSON.parse(JSON.stringify(val));
      let nodes = this.FatherTreeData;
      for (let i = 0; i < nodes.length; i++) {
        if (nodes[i].is_courseware == "true") {
          this.changeId(nodes[i].id, "");
          return false;
        } else {
          if (nodes[i].children) {
            let nodesC = nodes[i].children;
            for (let j = 0; j < nodesC.length; j++) {
              if (nodesC[j].is_courseware == "true") {
                this.changeId(nodesC[j].id, "");
                return false;
              } else {
                if (nodesC[j].children) {
                  let nodesCs = nodesC[j].children;
                  for (let l = 0; j < nodesCs.length; l++) {
                    if (nodesCs[l].is_courseware == "true") {
                      this.changeId(nodesCs[l].id, "");
                      return false;
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    changeId(id, name) {
      const _this = this;

      _this.chapterId = id;
      _this.chapterName = name;
      _this.isAnswerShow = false;

      _this.onGetData();
      if (!_this.treeFlag) {
        _this.fullTree = false;
        document.getElementById("content-tree").style.display = "none";
      }
    },
    // 点击全屏展示 隐藏tree
    fullScreen() {
      this.treeFlag = false;
      this.fullscreen = true;
      document.getElementById("content-tree").style.display = "none";
    },
    treeShow() {
      this.treeFlag = true;
      this.fullTree = false;
      this.fullscreen = false;
      document.getElementById("content-tree").style.display = "block";
    },
    // 获取预览数据
    onGetData() {
      const _this = this;
      _this.loading = true;
      _this.context = null;
      const MethodName = "book-courseware_manager-GetCoursewareContent_View";
      const data = {
        id: _this.chapterId
      };
      getContent(MethodName, data)
        .then(res => {
          _this.loading = false;
          this.category = res.category;
          this.themeColor = res.book_theme_color;
          if (res.content) {
            const _this = this;
            if (!this.category || this.category == "OC") {
              _this.context = {
                id: _this.chapterId,
                ui_type: JSON.parse(res.content).question
                  ? JSON.parse(res.content).question.ui_type
                  : "",
                sort_number: 1,
                content: JSON.parse(res.content)
              };
            } else if (this.category == "NPC") {
              _this.context = JSON.parse(res.content);
              console.log(_this.context);
            }
          } else {
            _this.context = null;
          }
        })
        .catch(() => {
          _this.loading = false;
        });
    },
    getAnswer() {
      // this.answer;
    },
    // 跳转编辑页面
    handleEdit() {
      const index = this.$refs.treeView.handleParentIndex().split("###");
      if (index.length > 1) {
        Cookies.set("bookIndex", index[0]);
        Cookies.set("bookLevel", index[1]);
        Cookies.set("bookNodename", index[2]);
      }
      this.$router.push("/input?bookId=" + this.bookId);
    },
    // 显示或隐藏答案
    handleAnswerShow() {
      this.isAnswerShow = !this.isAnswerShow;
      this.$refs.previewAnswer.bookAnswerShow(this.isAnswerShow);
    },
    // 悬浮树隐藏显示
    chooseCourseware() {
      this.fullTree = !this.fullTree;
      if (this.fullTree) {
        document.getElementById("content-tree").style.display = "block";
      } else {
        document.getElementById("content-tree").style.display = "none";
      }
    },
    finishTaskMaterial(data, time, rightNumber, errorNumber) {
      console.log("用户提交的答案：" + data);
      console.log("答题时间" + time);
      console.log("答对个数" + rightNumber);
      console.log("答错个数" + errorNumber);
    }
  }
};
</script>
<style lang="scss" scoped>
.container {
  width: 100%;
  min-height: 100vh;
  height: auto;
  .content {
    width: 100%;
    display: flex;
    justify-content: flex-start;
    align-items: flex-start;
    &-tree {
      width: 340px;
      height: 100%;
      overflow: auto;
      -webkit-overflow-scrolling: touch;
      padding: 20px 0px;
      border-right: 1px solid #d9d9d9;
      position: fixed;
      top: 0px;
      left: 0;
      transform: translate(0, 0);
      padding-top: 64px;
      z-index: 2;
      background: #fff;
      &-phone {
        width: 50%;
      }
      &-unfold {
        width: 40px;
        height: 90px;
        border: none;
        overflow: hidden;
      }
    }
    .content-tree-full {
      position: fixed;
      top: 100px;
      left: 152px;
      max-height: 588px;
      overflow: auto;
      z-index: 999;
      background: #fff;
      box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.25);
      border-radius: 4px;
    }
    .inner {
      // border-left: 1px solid #d9d9d9;
      width: 1000px;
      flex: 1;
      margin: 0 auto;
      margin-left: 340px;
      box-sizing: border-box;
      padding: 20px;
      position: relative;
      background: #fff;
      &.inner-full {
        margin-left: 0;
      }
      &-phone {
        margin-left: 0;
      }
      .title-box {
        display: flex;
        align-items: center;
        margin-bottom: 12px;
        padding-right: 160px;
        position: relative;
        > img {
          width: 40px;
          margin-right: 16px;
          cursor: pointer;
        }
      }
      .title {
        font-size: 24px;
        margin-bottom: 20px;
        line-height: 40px;
        margin: 0;
      }
      .has-module {
        display: flex;
        justify-content: flex-start;
        align-items: flex-start;
        &-tree {
          width: 340px;
          height: 100%;
          overflow: auto;
          padding: 20px 0px;
          border-right: 1px solid #d9d9d9;
        }
        .inner {
          // border-left: 1px solid #d9d9d9;
          width: 1000px;
          flex: 1;
          margin: 0 auto;
          box-sizing: border-box;
          padding: 20px;
          position: relative;
          background: #fff;
          .title-box {
            display: flex;
            align-items: center;
            margin-bottom: 12px;
            padding-right: 160px;
            position: relative;
            > img {
              width: 40px;
              margin-right: 16px;
              cursor: pointer;
            }
          }
          .title {
            font-size: 24px;
            margin-bottom: 20px;
            line-height: 40px;
            margin: 0;
          }
          .has-module {
            display: flex;
            justify-content: flex-start;
            align-items: flex-start;
          }
          .edit-btn {
            display: inline-block;
            font-size: 14px;
            background: #ff9900;
            float: right;
            line-height: 36px;
            color: #fff;
            width: 60px;
            text-align: center;
            border-radius: 4px;
          }
        }
      }
    }

    .nav-list {
      width: 200px;
      height: 40px;
      background: rgba(49, 212, 134, 0.2);
      border-radius: 240px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      padding: 0;
      margin: 0;
      margin-bottom: 10px;
      list-style: none;
      > li {
        height: 40px;
        width: 100px;
        text-align: center;
        font-style: normal;
        font-weight: bold;
        font-size: 14px;
        line-height: 40px;
        color: #19b068;
        cursor: pointer;
        &.active {
          background: #19b068;
          border-radius: 240px;
          color: #ffffff;
        }
      }
    }
    .screen-full {
      position: fixed;
      right: 30px;
      bottom: 30px;
      width: 48px;
      height: 48px;
      background: rgba(0, 0, 0, 0.4) url("../assets/common/icon-screenFull.png")
        center no-repeat;
      background-size: 32px;
    }
  }
  .answerShow {
    position: absolute;
    right: 20px;
    top: 20px;
    width: 112px;
    height: 40px;
    background: #ffffff url("../assets/common/icon-eye-close.png") 16px center
      no-repeat;
    background-size: 16px;
    border: 1px solid rgba(44, 44, 44, 0.15);
    box-sizing: border-box;
    border-radius: 4px;
    font-size: 14px;
    line-height: 150%;
    color: #000000;
    padding: 9px 16px 9px 36px;
    cursor: pointer;
    z-index: 2;
    &.answerShowTrue {
      background: #f5f5f5 url("../assets/common/icon-eye-open.png") 16px center
        no-repeat;
      background-size: 16px;
    }
  }
}
.img_url {
  width: 1000px;
  height: 700px;
  margin: 0 auto;
}
</style>
<style lang="scss">
.title-box {
  .el-switch {
    position: absolute;
    top: 0;
    right: 0px;
    border: 1px solid rgba(44, 44, 44, 0.15);
    border-radius: 40px;
    height: 40px;
    padding: 6px 6px 6px 16px;
  }
  .el-switch__label.is-active {
    color: #000000;
    font-size: 16px;
  }
}
</style>
