<template>
  <div>
    <!-- LOADER -->
    <div class="loader" v-if="showLoader">
      <v-progress-circular indeterminate color="white"></v-progress-circular>
    </div>

    <!-- SNACKBAR -->
    <v-snackbar
      :timeout="timeout"
      :top="y === 'top'"
      :right="x === 'right'"
      :vertical="mode === 'vertical'"
      v-model="snackbar"
      :color="snackbarColor"
    >
      {{ text }}
      <v-spacer></v-spacer>
      <v-btn flat text @click="snackbar = false">X</v-btn>
    </v-snackbar>

    <v-container class="pt-0">
      <v-layout row wrap class="mt-1">
        <v-flex xs8>
          <!-- CHAPTER INFO -->
          <div class="title side-bar-color font-weight-bold">{{chapter.chapters[0].chapterName}}</div>
          <div class="subheading grey--text lighten-1">{{chapter.chapters[0].description}}</div>
          <div
            v-for="(point,index) in chapter.chapters[0].chapterPoints"
            :key="index"
            class="ml-2 mt-2"
          >
            <span class="subheading grey--text lighten-1">{{index +1}}. {{point}}</span>
          </div>
          <!-- ACTIVITIES -->
          <div class="mt-5">
            <v-icon>library_books</v-icon>
            <span class="subheading font-weight-bold">Activities</span>
          </div>
          <!-- OTHER OPTIONS -->
          <div class="mt-5">
            <ul class="subheading">
              <!-- LIVE ONLINE CLASSES -->
              <div>
                <v-btn flat class="px-4">
                  <li
                    @click="$router.push({name: 'Live Online Class', query: {chapterId: $route.query.selectedChapterId, chapterName: chapter.chapters[0].chapterName, courseId: $route.query.courseId, classId: localStorage.getItem('parentClassId') } })"
                  >Live online classes</li>
                </v-btn>
              </div>
              <!-- OPEN PDF IN NEW TAB -->
              <div>
                <a
                  :href="chapter.chapters[0].pdfFileUrl"
                  target="_blank"
                  style="text-decoration: none!important;"
                >
                  <v-btn flat class="px-4">
                    <li>Open PDF</li>
                  </v-btn>
                </a>
              </div>

              <!-- <li>
                <v-btn flat>HSP Interactive content</v-btn>
              </li>
              <li>
                <v-btn flat>Assessment</v-btn>
              </li>-->
            </ul>
          </div>

          <!-- SELECT CHAPTERS -->
          <v-layout row class="mt-5">
            <!-- PREVIOUS CHAPTER -->
            <v-flex
              style="cursor: pointer;"
              class="subheading font-weight-bold text-xs-left"
              v-if="indexSelectedChapter > 0"
              @click="showSelectedChapter('back')"
            >
              <v-icon class="black--text" style="position:relative; top: 4px;">chevron_left</v-icon>
              {{chapterNames[indexSelectedChapter - 1]}}
            </v-flex>
            <v-spacer></v-spacer>
            <!-- NEXT CHAPTER -->
            <v-flex
              style="cursor: pointer;"
              class="subheading font-weight-bold text-xs-right"
              v-if="indexSelectedChapter < chapterNames.length -1"
              @click="showSelectedChapter('forward')"
            >
              {{chapterNames[indexSelectedChapter + 1]}}
              <v-icon class="black--text" style="position:relative; top: 4px;">chevron_right</v-icon>
            </v-flex>
          </v-layout>
          <!-- <v-flex class="text-xs-right mt-5" v-else>
            <span class="subheading font-weight-bold">Return to chapter one</span>
          </v-flex>-->
        </v-flex>

        <v-spacer></v-spacer>

        <!-- COURSES SIDE BAR- positioned to the right of the page -->
        <v-flex md3>
          <v-card class="elevation-0 card-border" height="100%">
            <CoursesSideBar></CoursesSideBar>
          </v-card>
        </v-flex>
      </v-layout>
    </v-container>
  </div>
</template>
<script>
import http from "@/Services/http.js";
import AllApiCalls from "@/Services/AllApiCalls.js";
import CoursesSideBar from "@/pages/Common/CoursesSideBar.vue";
export default {
  mixins: [AllApiCalls],
  components: {
    CoursesSideBar,
  },
  data() {
    return {
      // courseData: [],
      localStorage: localStorage,
      showLoader: false,
      chapter: { chapters: [{}] },
      chapterNames: [],
      chapterIds: [],
      selectedChapterId: "",
      indexSelectedChapter: "",
      pdfLink: "",
    };
  },
  methods: {
    showSelectedChapter(newChapter) {
      if (newChapter == "forward") {
        this.indexSelectedChapter += 1;
        this.selectedChapterId = this.chapterIds[this.indexSelectedChapter];
        this.getParticularChapterDetail();
      }
      if (newChapter == "back") {
        this.indexSelectedChapter -= 1;
        this.selectedChapterId = this.chapterIds[this.indexSelectedChapter];
        this.getParticularChapterDetail();
      }
    },
    getParticularChapterDetail() {
      http()
        .get("/getParticularChapterDetail", {
          params: {
            courseDetailId: this.$route.query.courseDetailId,
            chapterId: this.selectedChapterId,
          },
        })
        .then((response) => {
          this.chapter = response.data.data;
          this.showLoader = false;
        })
        .catch((err) => {
          //   console.log("err in getParticularChapterDetail====>", err);
          this.showLoader = false;
          this.snackbar = true;
          this.color = "error";
          this.text = error.response.data.message;
        });
    },
  },
  // computed:{
  //   nextChapterIndex(){

  //   }
  // }
  async created() {
    // console.log("route query - ", this.$route.query);

    this.selectedChapterId = this.$route.query.selectedChapterId;

    /* set chapterNames array */
    this.chapterNames = Object.keys(this.$route.query);
    this.chapterNames.pop();
    this.chapterNames.pop();
    this.chapterNames.pop();

    /* set chapter Ids */
    this.chapterIds = Object.values(this.$route.query);
    this.chapterIds.pop();
    this.chapterIds.pop();
    this.chapterIds.pop();

    this.indexSelectedChapter = this.chapterIds.findIndex((id) => {
      return id == this.selectedChapterId;
    });

    /* get chapter clicked on using the id */
    await this.getParticularChapterDetail(this.selectedChapterId);
  },
};
</script>
<style scoped>
.side-bar-color {
  color: #827bfa !important;
  /* border-top-right-radius: 74px !important; */
}
.card-border {
  border: 1px #bdbdbd solid;
  border-radius: 3px;
}
</style>
