<template>
  <div class="page">
    <van-nav-bar
      :title="title"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    ></van-nav-bar>
    <div class="courseContent">
      <div class="previewImg">
        <img :src="courseDetail.thumb" />
      </div>
      <div class="title">
        <p>{{ courseDetail.name }}</p>
      </div>
      <div class="tabs">
        <scroller ref="scroller">
          <van-tabs v-model="activeTab" swipeable>
            <van-tab title="课程目录">
              <div class="catelog">
                <div class="sup">
                  <span>共{{ courseDetail.sections.length }}节课</span>
                </div>
                <div class="logConetent">
                  <div
                    class="logLi"
                    v-for="(item, index) in courseDetail.sections"
                    :key="index"
                    @click="toSectionVidio(item, index)"
                  >
                    <p>{{ index + 1 }}、{{ item.name }}</p>
                  </div>
                </div>
              </div>
            </van-tab>
            <van-tab title="课程介绍">
              <div class="images">
                <div
                  class="descImage"
                  v-for="(item, index) in courseDetail.images"
                  :key="index"
                >
                  <img :src="item" />
                </div>
              </div>
            </van-tab>
          </van-tabs>
        </scroller>
      </div>
      <div
        class="getCourse"
        v-if="!courseDetail.buy && codeDetail && codeDetail.enable"
        @click="useCode"
      >
        <van-button icon="fire-o" square class="getBtn"
          >点击领取课程名额</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import {
  learnerCourseDetail,
  getCourseCodeDetail,
  getCourseCode,
  getVideoSrc
} from "../api";
export default {
  name: "",
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      finished: false,
      activeTab: 0,
      title: "课程详情",
      courseDetail: {
        thumb: "",
        name: "",
        images: [],
        sections: [],
        buy: false
      },
      giveCode: false,
      codeDetail: {
        enable: true
      }
    };
  },
  created() {
    this.init();
  },
  methods: {
    useCode() {
      this.$dialog
        .confirm({
          title: "提示",
          message: "确定领取当前课程吗"
        })
        .then(() => {
          let data = {
            salesCode: this.codeDetail.salesCode,
            userId: this.codeDetail.userId,
            subjectId: this.codeDetail.subjectId
          };
          getCourseCode(data).then(res => {
            this.$toast.success("领取成功！");
            this.giveCode = true;
            this.getDetail();
          });
        });
    },
    getCodeDetail() {
      getCourseCodeDetail(this.$route.query.code).then(res => {
        this.codeDetail = res;
        if (res && !res.enable) {
          this.codeDetail.enable = false;
          this.$toast.fail("该分享码已失效");
        }
      });
    },
    getVideoLink(link){
      let data = {
        subjectId:this.$route.params.id,
        link:link
      }
      getVideoSrc(data).then(res=>{
        window.location.href = res
      })
    },
    toSectionVidio(item, index) {
      if (this.courseDetail.buy) {
        // this.getVideoLink(item.link)
        let data = {
          index: index,
          link: item.link
        };
        sessionStorage.setItem('vd_info',JSON.stringify(data))
        this.$router.push({
          path: `/course/video/${this.$route.params.id}`
        });
      } else {
        this.$toast.fail("请先领取课程后再观看");
      }
    },
    onClickLeft() {
      this.$router.push('/courseHome');
    },
    init() {
      this.courseId = this.$route.params.id;
      this.getDetail();
      if (this.$route.query && this.$route.query.code) {
        this.getCodeDetail();
      }
    },
    getDetail() {
      learnerCourseDetail(this.courseId).then(res => {
        this.courseDetail = res;
        if (
          this.courseDetail.buy &&
          this.$route.query &&
          this.$route.query.code &&
          !this.giveCode
        ) {
          this.$toast.fail("您已经购买课程，无需再次购买");
        }
      });
    }
  },
  computed: {}
};
</script>
<style scoped lang="scss">
* {
  margin: 0;
  padding: 0;
}
img {
  display: block;
}
.page {
  background-color: #fff;
  box-sizing: border-box;
  .courseContent {
    padding: 0 0.2rem;
    margin-top: 0.3rem;
    .previewImg {
      border-radius: 0.1rem;
      overflow: hidden;
      img {
        width: 100%;
        height: 4rem;
      }
    }
    .title {
      p {
        font-size: 0.32rem;
        font-weight: 600;
        color: #000;
        line-height: 1rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        word-break: break-all;
      }
    }
    .tabs {
      height: calc(100vh - 6.25rem);
      position: relative;
      .images {
        margin-top: 0.1rem;
        img {
          width: 100%;
        }
      }
      .catelog {
        margin-top: 1px;
        border-top: 1px solid #ccc;
        .sup {
          display: flex;
          justify-content: space-between;
          align-items: center;
          span {
            font-size: 0.24rem;
            color: #666;
            line-height: 3;
          }
        }
        .logConetent {
          .logLi {
            border: 1px solid #ccc;
            border-radius: 0.1rem;
            padding: 0 0.2rem;
            margin-bottom: 0.15rem;
            p {
              font-size: 0.3rem;
              color: #000;
              line-height: 3;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
              word-break: break-all;
            }
          }
        }
      }
    }
  }
  .getCourse {
    position: absolute;
    bottom: 0.1rem;
    left: 0.1rem;
    width: calc(100% - 0.2rem);
    .getBtn {
      width: 100%;
      background-color: #26a2ff;
      color: #fff;
      // animation: getFire 3s infinite linear;
      border-radius: 0.1rem;
    }
  }
}
@keyframes getFire {
  0% {
    transform: scale(0.95);
  }
  100% {
    transform: scale(1.05);
  }
}
</style>
