<template>
  <div>
    <head-bar @back="back">
      <template #title>
        <div class="navTitle">课程详情</div>
      </template>
    </head-bar>
    <div class="courseBoxMes">
      <course-message
        :cDetail="courseDetail"
        :cInfo="chapInfo"
        :rwLength="rw"
        :cType="courseType"
        :cJson="courseTypeJson"
        :tName="Tname"
      ></course-message>
      <course-chap :chInfo="chapInfo" :brief="courseDetail.brief" @goCourse="goToStudy"></course-chap>
    </div>
    <div class="studyCss">
      <div style="stuButton" @click="goToStudy">点击学习</div>
    </div>
  </div>
</template>

<script>
import { getCourseDetail } from '@/api/courseDetail'
import headBar from '@/components/headBar.vue'
import courseMessage from './components/courseMessage.vue'
import courseChap from './components/courseChap.vue'
export default {
  components: {
    headBar,
    courseMessage,
    courseChap
  },
  data() {
    return {
      courseid: this.$route.query.courseid,
      courseDetail: {},
      courseTypeJson: {},
      chapInfo: [],
      courseType: [],
      Tname: [],
      rw: 0
    }
  },
  methods: {
    back() {
      this.$router.push({ path: '/home' })
    },
    goToStudy() {
      // eslint-disable-next-line object-curly-spacing
      this.$router.push({ path: '/course', query: { courseid: this.courseid } })
    },
    getCourse() {
      const params = {
        courseId: this.courseid
      }
      getCourseDetail(params)
        .then(res => {
          this.courseDetail = res[0][0]
          var a = res[0]
          var b = res[1]
          var c = res[2]
          for (var i = 0; i < b.length; i++) {
            for (var j = 0; j < a.length; j++) {
              if (b[i].id === a[j].pid) {
                if (!this.courseTypeJson[b[i].name]) {
                  this.courseType.push(b[i].name)
                  this.courseTypeJson[b[i].name] = []
                }
                this.courseTypeJson[b[i].name].push(a[j].name)
              }
            }
          }
          this.Tname = []
          for (var k = 0; k < c.length; k++) {
            this.Tname.push(c[k].name)
          }
          this.chapInfo = JSON.parse(this.courseDetail.chapters)
          for (var z = 0; z < this.chapInfo.length; z++) {
            this.rw += this.chapInfo[z].chapterInfo[0].taskJson.length
          }
        })
        .catch(err => {
          console.error(err)
        })
    }
  },
  mounted() {
    this.getCourse()
  }
}
</script>

<style lang="scss" scoped>
.courseBoxMes {
  height: 100vh;
  overflow: auto;
  padding: 0 0 60px;
  box-sizing: border-box;
}
.navTitle {
  font-size: 16px;
  color: #fff;
}
.studyCss {
  height: 60px;
  border-top: 1px solid #dddddd;
  position: fixed;
  width: 100%;
  bottom: 0;
  background: #fff;
  > div {
    width: 90%;
    height: 40px;
    margin: 10px auto;
    text-align: center;
    color: #fff;
    background-image: linear-gradient(90deg, #477cd7, #65b9fc);
    line-height: 40px;
    border-radius: 10px;
  }
}
</style>
